-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
281 lines (220 loc) · 10.4 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Train the CNN.
Authors: Joe Comer, Roger Filmyer
April 2018
"""
# Tensorflow Imports
import tensorflow as tf
from sklearn.model_selection import train_test_split
tf.logging.set_verbosity(tf.logging.INFO)
# Sound file stuff
import numpy as np
# Manage our files
import voxforge
import os
import tempfile
import lingua_franca_config
# Verbosity flag
import argparse
# # # MODEL SETTINGS
MODEL_DIR = os.path.join(tempfile.gettempdir(), "lingua-franca-model")
parser = argparse.ArgumentParser(description="Tensorflow CNN model for language detection")
parser.add_argument("-v", action="store_true", help="Turn on verbose logging")
parser.add_argument("--modeldir", default=MODEL_DIR, help="Directory in which to find the model info")
# # # SOUND FILES
MFCC_FILE_NAME = "mfccs.npz"
NUM_LANGUAGES = 3 # This is a default that should get reset
# Input Layer
def cnn_model_fn(features, labels, mode) -> tf.estimator.EstimatorSpec:
tf.logging.debug("Feature Shape: %s", features["mfccs"].shape)
input_layer = tf.reshape(features["mfccs"], [-1,
lingua_franca_config.num_frames,
lingua_franca_config.num_cepstra,
1])
tf.logging.debug("Input Layer Shape: %s", input_layer.shape)
#ROUND1#####################################################################
# Convolutional/Pooling layer 1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[7, 7],
padding="same",
activation=tf.nn.relu
)
tf.logging.debug("Conv 1 Layer Shape: %s", conv1.shape)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
tf.logging.debug("Pool 1 Layer Shape: %s", pool1.shape)
# #############################################################################
# ROUND2#######################################################################
# Convolutional and Pooling Layer 2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu
)
tf.logging.debug("Conv 2 Shape: %s", conv2.shape)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
tf.logging.debug("Pool 2 Shape: %s", pool2.shape)
# #############################################################################
# ROUND3#####################################################################
# Convolutional/Pooling layer 3
conv3 = tf.layers.conv2d(
inputs=pool2,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu
)
tf.logging.debug("Conv 3 Layer Shape: %s", conv3.shape)
pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=1)
# ###########################################################################
# ROUND4#####################################################################
# Convolutional/Pooling layer 4
conv4 = tf.layers.conv2d(
inputs=pool3,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu
)
tf.logging.debug("Conv 4 Layer Shape: %s", conv4.shape)
pool4 = tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=1)
tf.logging.debug("Pool 4 Shape: %s", pool4.shape)
pool4_flat = tf.reshape(pool4, [-1, np.product(pool4.shape[1:])])
tf.logging.debug("Pool 4 Flat Shape: %s", pool4_flat.shape)
dense = tf.layers.dense(inputs=pool4_flat, units=1024, activation=tf.nn.relu)
tf.logging.debug("Dense Shape: %s", dense.shape)
dropout = tf.layers.dropout(inputs=dense, training=mode == tf.estimator.ModeKeys.TRAIN)
tf.logging.debug("Dropout Shape: %s", dropout.shape)
# Logits Layer
logits = tf.layers.dense(inputs=dropout, units=NUM_LANGUAGES)
predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
tf.logging.debug("Making Predictions...")
tf.logging.debug("Probabilities: %s", predictions["probabilities"])
export_outputs = {'predict_output': tf.estimator.export.PredictOutput({"classes": predictions["classes"],
'probabilities': predictions["probabilities"]})}
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)
# Loss Function
tf.logging.debug("Labels Shape: %s, Logits Shape: %s", labels.shape, logits.shape)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
tf.logging.debug("Loss Shape: %s", loss.shape)
accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])
tf.summary.scalar("accuracy", accuracy[1])
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
elif mode == tf.estimator.ModeKeys.EVAL:
eval_metric_ops = {"accuracy": accuracy}
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
tf.logging.warn("Current mode is %s, expected %s", mode, (tf.estimator.ModeKeys.PREDICT,
tf.estimator.ModeKeys.TRAIN,
tf.estimator.ModeKeys.EVAL))
def serving_input_receiver_fn():
"""Build the serving inputs."""
# The outer dimension (None) allows us to batch up inputs for
# efficiency. However, it also means that if we want a prediction
# for a single instance, we'll need to wrap it in an outer list.
tf.logging.debug("building input receiver")
inputs = {"mfccs": tf.placeholder(shape=[None, lingua_franca_config.num_frames, lingua_franca_config.num_cepstra],
dtype=tf.float32)}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
def regenerate_images() -> tuple:
"""
Create MFCCs from Voxforge WAV files.
Returns a tuple (data, raw_labels), with 2 ndarrays
"""
tf.logging.info("Creating new images...")
images = []
raw_labels = []
for filename, language in voxforge.get_files():
try:
image = voxforge.create_mfcc(filename)
except ValueError:
tf.logging.warn("An audio file is messed up: %s", filename)
break
if voxforge.image_is_big_enough(image):
cropped = voxforge.randomCrop(image)
images.append(cropped)
else:
padded = np.zeros((lingua_franca_config.num_frames, lingua_franca_config.num_cepstra))
frames, cepstra = image.shape
padded[0:frames, 0:cepstra] = image
images.append(padded)
raw_labels.append(language)
tf.logging.info("Created %d MFCCs from %d images.", len(images), len(voxforge.get_files()))
data = np.array(images).astype(np.float32)
raw_labels = np.array(raw_labels)
np.savez_compressed(MFCC_FILE_NAME, data=data, raw_labels=raw_labels)
return data, raw_labels
def main(unused_argv):
# Have we computed MFCCs before?
if os.path.exists(MFCC_FILE_NAME):
tf.logging.info("Loading saved images...")
loaded_data = np.load(MFCC_FILE_NAME)
if isinstance(loaded_data["data"], np.ndarray) and \
len(loaded_data["data"]) > 0 and \
loaded_data["data"][0].shape == (lingua_franca_config.num_frames, lingua_franca_config.num_cepstra):
data = loaded_data["data"]
raw_labels = loaded_data["raw_labels"]
else:
tf.logging.warn("Precomputed MFCCs are of wrong size, expected %s, got %s. Will have to recompute.",
(lingua_franca_config.num_frames, lingua_franca_config.num_cepstra),
loaded_data["data"].shape)
data, raw_labels = regenerate_images()
else:
data, raw_labels = regenerate_images()
tf.logging.debug("Data Shape: %s", data.shape)
language_list = np.sort(np.unique(raw_labels))
tf.logging.debug("Languages detected: %s", language_list)
global NUM_LANGUAGES
NUM_LANGUAGES = len(language_list)
labels = np.array([np.where(language_list == language) for language in raw_labels]).flatten()
tf.logging.debug("Labels Shape: %s", labels.shape)
train_data, eval_data, train_labels, eval_labels = train_test_split(data, labels, test_size=0.10, random_state=42)
tf.logging.debug("Split Training/Testing data.")
# Create the Estimator
tf.logging.info("Model Directory: %s", MODEL_DIR)
mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir=MODEL_DIR)
# Set up logging
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"mfccs": train_data},
y=train_labels,
batch_size=lingua_franca_config.batch_size,
num_epochs=None,
shuffle=True
)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=20000, hooks=[logging_hook])
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"mfccs": eval_data},
y=eval_labels,
num_epochs=1,
shuffle=False
)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)
tf.estimator.train_and_evaluate(mnist_classifier, train_spec, eval_spec)
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
print("Eval Results: %s" % eval_results)
export_dir = mnist_classifier.export_savedmodel(
export_dir_base=MODEL_DIR,
serving_input_receiver_fn=serving_input_receiver_fn)
print("Model exported to: {0}".format(export_dir))
with open(os.path.join(export_dir, b"languages.csv"), 'w') as language_csv:
for language in language_list:
language_csv.write(language)
language_csv.write("\n")
if __name__ == "__main__":
args = parser.parse_args()
if args.v:
tf.logging.set_verbosity(tf.logging.DEBUG)
MODEL_DIR = args.modeldir
# run_options = tf.RunOptions(report_tensor_allocations_upon_oom=True)
tf.app.run()