-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
246 lines (198 loc) · 8.91 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
from keras.applications.inception_v3 import InceptionV3
from keras.layers import Dense, Activation, Dropout, Flatten, Input
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, LearningRateScheduler, TensorBoard, EarlyStopping
from keras.optimizers import Adam
from keras.models import Model
from keras import backend as k
import os
import numpy as np
import sys
import gflags
from . import __name__
import data_utils
import logz
import log_utils
from common_flags import FLAGS
import utils
from utils import json_to_model, model_to_json
from time import time, strftime, localtime
TRAIN_PHASE = 1
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 10, 15, 20, 25 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = FLAGS.initial_lr
if epoch > 150:
lr *= 0.5e-3
elif epoch > 100:
lr *= 1e-2
elif epoch > 50:
lr *= 1e-1
elif epoch > 0:
lr *= 1
print('Learning rate: ', lr)
return lr
def train_model(train_generator, val_generator, model, initial_epoch):
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=lr_schedule(0)),
metrics=['accuracy'])
# Save model with the lowest validation loss
weights_path = os.path.join(FLAGS.experiment_rootdir, 'weights_{epoch:03d}.h5')
write_best_model = ModelCheckpoint(filepath=weights_path, monitor='val_loss',
save_best_only=True, save_weights_only=True)
# Save training and validation losses.
logz.configure_output_dir(FLAGS.experiment_rootdir)
save_model_and_loss = log_utils.MyCallback(filepath=FLAGS.experiment_rootdir)
# Train model
lr_scheduler = LearningRateScheduler(lr_schedule, verbose=FLAGS.verbose)
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
cooldown=0,
patience=5,
verbose=FLAGS.verbose,
min_lr=0.5e-6)
# earlystopping = EarlyStopping(monitor='val_loss', patience=3, verbose=FLAGS.verbose)
str_time = strftime("%Y%b%d_%Hh%Mm%Ss", localtime(time()))
tensorboard = TensorBoard(log_dir="logs/{}".format(str_time), histogram_freq=0)
callbacks = [write_best_model, save_model_and_loss, lr_reducer, lr_scheduler, tensorboard]
model.fit_generator(train_generator, validation_data=val_generator,
epochs=FLAGS.epochs,
verbose=FLAGS.verbose,
callbacks=callbacks,
initial_epoch=initial_epoch,
use_multiprocessing=True)
def _main():
# Set training phase
k.set_learning_phase(TRAIN_PHASE)
# Create the experiment rootdir if not already there:
if not os.path.exists(FLAGS.experiment_rootdir):
os.makedirs(FLAGS.experiment_rootdir)
# Split the data into training, validation and test sets
if FLAGS.initial_epoch == 0:
data_utils.cross_val_create(FLAGS.data_path)
# Input image dimensions
img_width, img_height = FLAGS.img_width, FLAGS.img_height
print('Tamaño de los mel-espectrogramas: Alto: {}, Ancho: {}'.format(img_height, img_width))
# Generate training data with real-time augmentation
train_data_gen = data_utils.DataGenerator()
# Iterator object containing training data to be generated batch by batch
train_generator = train_data_gen.flow_from_directory('train',
shuffle=True,
target_size=(img_height, img_width),
classes=FLAGS.num_classes,
batch_size=FLAGS.batch_size)
# Generate validation data with real-time augmentation
val_data_gen = data_utils.DataGenerator()
# Iterator object containing validation data to be generated batch by batch
val_generator = val_data_gen.flow_from_directory('val',
shuffle=False,
target_size=(img_height, img_width),
classes=FLAGS.num_classes,
batch_size=FLAGS.batch_size)
# Check if the number of classes in data corresponds to the one specified
assert train_generator.num_classes == FLAGS.num_classes, \
" Not matching output dimensions in training data."
# Check if the number of classes in data corresponds to the one specified
assert val_generator.num_classes == FLAGS.num_classes, \
" Not matching output dimensions in validation data."
# Weights to restore
weights_path = FLAGS.initial_weights
# Epoch from which training starts
initial_epoch = FLAGS.initial_epoch
if FLAGS.restore_model:
# In this case weights are initialized as specified in pre-trained model
# initial_epoch = FLAGS.initial_epoch
try:
# Carga estructura de la red
json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
model = json_to_model(json_model_path)
# Carga los pesos
model.load_weights(weights_path)
print("Loaded model from {}".format(weights_path))
except ImportError:
print("Impossible to find weight path. Returning untrained model")
else:
# In this case weights are initialized randomly
weights_path = None
# Define model
bot_model = InceptionV3(weights=None, include_top=False,
input_shape=[img_height, img_width, 1],
classes=train_generator.num_classes)
bot_model.summary()
input = Input(shape=[img_height, img_width, 1])
top = bot_model(input)
# intermediate = Dropout()(top)
# top = Flatten()(intermediate)
top = Flatten()(top)
top = Dense(FLAGS.num_classes, activation='softmax', name='predictions')(top)
model = Model(inputs=input, outputs=top)
model.summary()
# Serialize model into json
json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
model_to_json(model, json_model_path)
# Train model
train_model(train_generator, val_generator, model, initial_epoch)
# Plot training and validation losses
utils.plot_loss(FLAGS.experiment_rootdir)
if __name__ == "__main__":
try:
argv = FLAGS(sys.argv) # parse flags
except gflags.FlagsError:
print('Usage: %s ARGS\\n%s' % (sys.argv[0], FLAGS))
sys.exit(1)
_main()
'''
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(ps, ref=np.max), y_axis='mel', x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('Mel spectrogram')
plt.tight_layout()
img = image.load_img(ps, target_size=(96, 431))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
model = Sequential
model.add(Dense(16, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# model = InceptionV3(weights=None)
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy'])
model.fit(training_data, target_data, epochs=2)
scores = model.evaluate(training_data, target_data)
print('\n%s: %.2f%%' % (model.metrics_names[1], scores[1]*100))
print(model.predict(training_data).round())
preds = model.predict(x)
print('Prediction:', decode_predictions(preds, top=1)[0][0])
# Set random seed
if FLAGS.random_seed:
seed = np.random.randint(0, 2 * 31 - 1)
else:
seed = 5
np.random.seed(seed)
tf.set_random_seed(seed)
# Define model
cond = ask_create_model()
if cond:
bot_model = InceptionV3(weights=None, include_top=False,
input_shape=[img_height, img_width, 1],
classes=train_generator.num_classes)
input = Input(shape=[img_height, img_width, 1])
top = bot_model(input)
top = Flatten()(top)
top = Dense(FLAGS.num_classes, activation='softmax', name='predictions')(top)
model = Model(inputs=input, outputs=top)
else:
if weights_path:
try:
json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
model = json_to_model(json_model_path)
model.load_weights(weights_path)
print("Loaded model from {}".format(weights_path))
except ImportError:
print("Impossible to find weight path. Returning untrained model")
'''