-
Notifications
You must be signed in to change notification settings - Fork 35
/
transfer_learning.py
320 lines (280 loc) · 9.97 KB
/
transfer_learning.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
import logging
from typing import Dict, List, Optional
import glob
import numpy as np
import tensorflow as tf
import sys
import embedding.input_data as input_data
def transfer_learn(
target,
train_files,
val_files,
unknown_files,
num_epochs,
num_batches,
batch_size,
primary_lr,
backprop_into_embedding,
embedding_lr,
model_settings: Dict,
base_model_path: os.PathLike,
base_model_output: str,
UNKNOWN_PERCENTAGE: float = 50.0,
bg_datadir: os.PathLike = "/home/mark/tinyspeech_harvard/speech_commands/_background_noise_/",
csvlog_dest: Optional[os.PathLike] = None,
verbose=1,
):
"""this only words for single-target models: see audio_dataset and CATEGORIES"""
tf.get_logger().setLevel(logging.ERROR)
base_model = tf.keras.models.load_model(base_model_path)
tf.get_logger().setLevel(logging.INFO)
xfer = tf.keras.models.Model(
name="TransferLearnedModel",
inputs=base_model.inputs,
outputs=base_model.get_layer(name=base_model_output).output,
)
xfer.trainable = False
# dont use softmax unless losses from_logits=False
CATEGORIES = 3 # silence + unknown + target_keyword
xfer = tf.keras.models.Sequential(
[
xfer,
tf.keras.layers.Dense(units=18, activation="tanh"),
tf.keras.layers.Dense(units=CATEGORIES, activation="softmax"),
]
)
xfer.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=primary_lr),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["accuracy"],
)
# TODO(mmaz): use keras class weights?
audio_dataset = input_data.AudioDataset(
model_settings=model_settings,
commands=[target],
background_data_dir=bg_datadir,
unknown_files=unknown_files,
unknown_percentage=UNKNOWN_PERCENTAGE,
spec_aug_params=input_data.SpecAugParams(percentage=80),
)
AUTOTUNE = tf.data.experimental.AUTOTUNE
init_train_ds = audio_dataset.init_single_target(
AUTOTUNE, train_files, is_training=True
)
init_val_ds = audio_dataset.init_single_target(
AUTOTUNE, val_files, is_training=False
)
train_ds = init_train_ds.shuffle(buffer_size=1000).repeat().batch(batch_size)
val_ds = init_val_ds.batch(batch_size)
if csvlog_dest is not None:
callbacks = [tf.keras.callbacks.CSVLogger(csvlog_dest, append=False)]
else:
callbacks = []
history = xfer.fit(
train_ds,
validation_data=val_ds,
steps_per_epoch=batch_size * num_batches,
epochs=num_epochs,
callbacks=callbacks,
verbose=verbose,
)
if backprop_into_embedding:
# https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/#transfer-learning-from-pretrained-weights
# We unfreeze the top 20 layers while leaving BatchNorm layers frozen
for layer in xfer.layers[-20:]:
if not isinstance(layer, tf.keras.layers.BatchNormalization):
layer.trainable = True
xfer.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=embedding_lr),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["accuracy"],
)
history = xfer.fit(
train_ds,
validation_data=val_ds,
steps_per_epoch=batch_size * num_batches,
epochs=num_epochs,
callbacks=callbacks,
)
va = history.history["val_accuracy"][-1]
name = f"xfer_epochs_{num_epochs}_bs_{batch_size}_nbs_{num_batches}_val_acc_{va:0.2f}_target_{target}"
details = dict(
num_epochs=num_epochs,
batch_size=batch_size,
num_batches=num_batches,
val_accuracy=va,
target=target,
)
return name, xfer, details
def random_sample_transfer_models(
NUM_MODELS,
N_SHOTS,
VAL_UTTERANCES,
oov_words,
dest_dir,
unknown_files,
EPOCHS,
data_dir,
model_settings,
base_model_path: os.PathLike,
base_model_output="dense_2",
UNKNOWN_PERCENTAGE=50.0,
NUM_BATCHES=1,
bg_datadir="/home/mark/tinyspeech_harvard/frequent_words/en/clips/_background_noise_/",
):
assert os.path.isdir(dest_dir), f"dest dir {dest_dir} not found"
models = np.random.choice(oov_words, NUM_MODELS, replace=False)
for target in models:
wavs = glob.glob(data_dir + target + "/*.wav")
selected = np.random.choice(wavs, N_SHOTS + VAL_UTTERANCES, replace=False)
train_files = selected[:N_SHOTS]
np.random.shuffle(train_files)
val_files = selected[N_SHOTS:]
print(len(train_files), "shot:", target)
utterances_fn = target + "_utterances.txt"
utterances = dest_dir + utterances_fn
print("saving", utterances)
with open(utterances, "w") as fh:
fh.write("\n".join(train_files))
transfer_learn(
dest_dir=dest_dir,
target=target,
train_files=train_files,
val_files=val_files,
unknown_files=unknown_files,
EPOCHS=EPOCHS,
model_settings=model_settings,
base_model_path=base_model_path,
base_model_output=base_model_output,
UNKNOWN_PERCENTAGE=UNKNOWN_PERCENTAGE,
NUM_BATCHES=NUM_BATCHES,
bg_datadir=bg_datadir,
)
def evaluate_fast_multiclass(
words_to_evaluate: List[str],
target_id: int,
data_dir: os.PathLike,
utterances_per_word: int,
model: tf.keras.Model,
model_settings: Dict,
):
correct_confidences = []
incorrect_confidences = []
specs = []
for word in words_to_evaluate:
wavs = glob.glob(data_dir + word + "/*.wav")
if len(wavs) > utterances_per_word:
fs = np.random.choice(wavs, utterances_per_word, replace=False)
else:
print("using all wavs for ", word)
fs = wavs
specs.extend([input_data.file2spec(model_settings, f) for f in fs])
specs = np.array(specs)
preds = model.predict(np.expand_dims(specs, -1))
# softmaxes = np.max(preds,axis=1)
# unknown_other_words_confidences.extend(softmaxes.tolist())
cols = np.argmax(preds, axis=1)
# figure out how to fancy-index this later
for row, col in enumerate(cols):
confidence = preds[row][col]
if col == target_id:
correct_confidences.append(confidence)
else:
incorrect_confidences.append(confidence)
return {
"correct": correct_confidences,
"incorrect": incorrect_confidences,
}
def evaluate_fast_single_target(
words_to_evaluate: List[str],
target_id: int,
data_dir: os.PathLike,
utterances_per_word: int,
model: tf.keras.Model,
model_settings: Dict,
):
specs = []
for word in words_to_evaluate:
wavs = glob.glob(data_dir + word + "/*.wav")
if len(wavs) > utterances_per_word:
fs = np.random.choice(wavs, utterances_per_word, replace=False)
else:
print("using all wavs for ", word)
fs = wavs
specs.extend([input_data.file2spec(model_settings, f) for f in fs])
specs = np.array(specs)
preds = model.predict(np.expand_dims(specs, -1))
return preds[:, target_id], preds
def evaluate_files_multiclass(
files_to_evaluate: List[os.PathLike],
target_id: int,
model: tf.keras.Model,
model_settings: Dict,
):
correct_confidences = []
incorrect_confidences = []
specs = [input_data.file2spec(model_settings, f) for f in files_to_evaluate]
specs = np.array(specs)
preds = model.predict(np.expand_dims(specs, -1))
# softmaxes = np.max(preds,axis=1)
# unknown_other_words_confidences.extend(softmaxes.tolist())
cols = np.argmax(preds, axis=1)
# figure out how to fancy-index this later
for row, col in enumerate(cols):
confidence = preds[row][col]
if col == target_id:
correct_confidences.append(confidence)
else:
incorrect_confidences.append(confidence)
return dict(correct=correct_confidences, incorrect=incorrect_confidences)
def evaluate_files_single_target(
files_to_evaluate: List[os.PathLike],
target_id: int,
model: tf.keras.Model,
model_settings: Dict,
):
specs = [input_data.file2spec(model_settings, f) for f in files_to_evaluate]
specs = np.array(specs)
preds = model.predict(np.expand_dims(specs, -1))
return preds[:, target_id], preds
def evaluate_and_track(
words_to_evaluate: List[str],
target_id: int,
data_dir: os.PathLike,
utterances_per_word: int,
model: tf.keras.Model,
model_settings: Dict,
):
# TODO(mmaz) rewrite and combine with evaluate_fast
raise ValueError("this only works for multiclass, see other evaluation functions")
correct_confidences = []
incorrect_confidences = []
track_correct = {}
track_incorrect = {}
for word in words_to_evaluate:
fs = np.random.choice(
glob.glob(data_dir + word + "/*.wav"), utterances_per_word, replace=False
)
track_correct[word] = []
track_incorrect[word] = []
specs = np.array([input_data.file2spec(model_settings, f) for f in fs])
preds = model.predict(np.expand_dims(specs, -1))
# softmaxes = np.max(preds,axis=1)
# unknown_other_words_confidences.extend(softmaxes.tolist())
cols = np.argmax(preds, axis=1)
# figure out how to fancy-index this later
for row, col in enumerate(cols):
confidence = preds[row][col]
if col == target_id:
correct_confidences.append(confidence)
track_correct[word].append(confidence)
else:
incorrect_confidences.append(confidence)
track_incorrect[word].append(confidence)
return {
"correct": correct_confidences,
"incorrect": incorrect_confidences,
"track_correct": track_correct,
"track_incorrect": track_incorrect,
}