forked from raquelduenass/Air-writing-initial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_mod.py
241 lines (196 loc) · 9.24 KB
/
train_mod.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
import tensorflow as tf
import numpy as np
import os
import sys
import gflags
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import TensorBoard
from keras.utils import plot_model
from keras import backend as K
import logz
import nets
import cifar10_resnet_mod
import utils
import data_utils_mod
import log_utils
from common_flags import FLAGS
from time import time, strftime, localtime
# Constants
TRAIN_PHASE = 1
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin'
def getModel(num_img, img_height, img_width, output_dim, weights_path):
"""
Initialize model.
# Arguments
img_width: Target image widht.
img_height: Target image height.
num_img: Target images per block
output_dim: Dimension of model output (number of classes).
weights_path: Path to pre-trained model.
# Returns
model: A Model instance.
"""
model = nets.resnet50(num_img, img_height, img_width, output_dim)
if weights_path:
try:
model.load_weights(weights_path)
print("Loaded model from {}".format(weights_path))
except:
print("Impossible to find weight path. Returning untrained model")
return model
def getModelResnet(n, version, num_img, img_height, img_width, output_dim, weights_path):
"""
Initialize model.
# Arguments
n: parameter that determines the net depth.
version: 1 for resnet v1 or 2 for v2.
img_width: Target image widht.
img_height: Target image height.
num_img: Target images per block
output_dim: Dimension of model output (number of classes).
weights_path: Path to pre-trained model.
# Returns
model: A Model instance.
"""
# Computed depth from supplied model parameter n
if version == 1:
depth = n * 6 + 2
elif version == 2:
depth = n * 9 + 2
input_shape = (num_img, img_height, img_width);
if version == 2:
model = cifar10_resnet_mod.resnet_v2(input_shape=input_shape, depth=depth, num_classes=output_dim)
else:
model = cifar10_resnet_mod.resnet_v1(input_shape=input_shape, depth=depth, num_classes=output_dim)
# Model name, depth and version
model_type = 'ResNet%dv%d' % (depth, version)
print(model_type)
print(model.summary())
if weights_path:
try:
model.load_weights(weights_path)
print("Loaded model from {}".format(weights_path))
except:
print("Impossible to find weight path. Returning untrained model")
return model
def trainModel(train_data_generator, val_data_generator, model, initial_epoch):
"""
Model training.
# Arguments
train_data_generator: Training data generated batch by batch.
val_data_generator: Validation data generated batch by batch.
model: A Model instance.
initial_epoch: Epoch from which training starts.
"""
# Configure training process
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=cifar10_resnet_mod.lr_schedule(0)),
metrics=['categorical_accuracy'])
# Save model with the lowest validation loss
weights_path = os.path.join(FLAGS.experiment_rootdir, 'weights_{epoch:03d}.h5')
writeBestModel = 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)
saveModelAndLoss = log_utils.MyCallback(filepath=FLAGS.experiment_rootdir)
# Train model
steps_per_epoch = int(np.ceil(train_data_generator.samples / FLAGS.batch_size))
validation_steps = int(np.ceil(val_data_generator.samples / FLAGS.batch_size))-1
lr_scheduler = LearningRateScheduler(cifar10_resnet_mod.lr_schedule)
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
cooldown=0,
patience=5,
min_lr=0.5e-6)
# TENSORBOARD SIRVE PARA VISUALIZAR LOS RESULTADOS DE LAS ITERACIONES
# HASTA AQUI SE HARÁ UN PROCESO ITERATIVO QUE GUARDARÁ DE TODOS LOS CASOS
# EL MODELO CON EL MEJOR RESULTADO SOBRE LOS DATOS DE VALIDACIÓN
strTime = strftime("%Y%b%d_%Hh%Mm%Ss", localtime(time()))
tensorboard = TensorBoard(log_dir="logs/{}".format(strTime), histogram_freq=0)
callbacks = [writeBestModel, saveModelAndLoss, lr_reducer, lr_scheduler, tensorboard]
model.fit_generator(train_data_generator,
epochs=FLAGS.epochs, steps_per_epoch = steps_per_epoch,
callbacks=callbacks,
validation_data=val_data_generator,
validation_steps = validation_steps,
initial_epoch=initial_epoch)
def _main():
# 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)
# Set training phase
K.set_learning_phase(TRAIN_PHASE)
# Create the experiment rootdir if not already there: create a model if the name of the one in the parameters doesn't exist
if not os.path.exists(FLAGS.experiment_rootdir):
os.makedirs(FLAGS.experiment_rootdir)
# Split the database into training, validation and test sets
data_utils_mod.cross_val_create(FLAGS.data_path)
# Input image dimensions
num_img, img_width, img_height = FLAGS.num_img, FLAGS.img_width, FLAGS.img_height
# Output dimension (4 classes/gestures)
num_classes = 4
# Generate training data with real-time augmentation: HABRÁ QUE CAMBIARLO
# TODO LO QUE SE HAGA SOBRE LOS DATOS AQUI SE TENDRA QUE HACER EN LOS DATOS PARA EL TEST
train_datagen = data_utils_mod.DataGenerator(rescale = 1./255)
# Iterator object containing training data to be generated batch by batch
train_generator = train_datagen.flow_from_directory('train', #FLAGS.train_dir,
num_classes,
shuffle = True,
img_mode = FLAGS.img_mode,
target_size=(num_img, img_height, img_width),
batch_size = FLAGS.batch_size)
# Check if the number of classes in dataset corresponds to the one specified
assert train_generator.num_classes == num_classes, \
" Not macthing output dimensions in training data."
# Generate validation data with real-time augmentation
val_datagen = data_utils_mod.DataGenerator(rescale = 1./255)
# Iterator object containing validation data to be generated batch by batch
val_generator = val_datagen.flow_from_directory('val', #FLAGS.val_dir,
num_classes,
shuffle = False,
img_mode = FLAGS.img_mode,
target_size=(num_img, img_height, img_width),
batch_size = FLAGS.batch_size)
# Check if the number of classes in dataset corresponds to the one specified
assert val_generator.num_classes == num_classes, \
" Not matching output dimensions in validation data."
# Weights to restore
weights_path = FLAGS.initial_weights
# Epoch from which training starts
initial_epoch = 0
if not FLAGS.restore_model:
# In this case weights are initialized randomly
weights_path = None
else:
# In this case weigths are initialized as specified in pre-trained model
initial_epoch = FLAGS.initial_epoch
# Define model: SE DEFINE LA RED CON LOS PARAMETROS QUE QUERAMOS O SOBRE LOS PESOS QUE YA TENGAMOS ANTES
n = 1
version = 2 # 1 o 2
model = getModelResnet(n, version, num_img, img_height, img_width,
num_classes, weights_path)
# Save the architecture of the network as png: IMAGEN DE LA ESTRUCTURA DE LA RED
plot_arch_path = os.path.join(FLAGS.experiment_rootdir, 'architecture.png')
plot_model(model, to_file=plot_arch_path)
# Serialize model into json: SE GUARDA EL "ESQUELETO" DE LA RED: NUMERO DE CAPAS, NEURONAS... LOS PESOS SE GUARDAN A PARTE
json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
utils.modelToJson(model, json_model_path)
# Train model
trainModel(train_generator, val_generator, model, initial_epoch)
# Plot training and validation losses
utils.plot_loss(FLAGS.experiment_rootdir)
def main(argv):
# Utility main to load flags
try:
argv = FLAGS(argv) # parse flags
except gflags.FlagsError:
print ('Usage: %s ARGS\\n%s' % (sys.argv[0], FLAGS))
sys.exit(1)
_main()
if __name__ == "__main__":
main(sys.argv)