-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
426 lines (398 loc) · 19.7 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import tensorflow as tf
from datetime import datetime
from logger.logger import Logger
from models.vision import ResNet18_v1
from models.audition import HearModel
from models.audition import DualCamHybridModel
from trainer.trainer import Trainer as Trainer
from trainer.trainer_andres import Trainer as TrainerDistillation
from trainer.trainer_three import Trainer as TrainerTripletLoss
from trainer.trainer_audio import Trainer as TrainerAudio
from dataloader.actions_data import ActionsDataLoader as DataLoader
flags = tf.app.flags
flags.DEFINE_string('mode', None, 'Execution mode, it can be either \'train\' or \'test\'')
flags.DEFINE_string('model', None, 'Model type, it can be one of \'ResNet18_v1\', \'DualCamHybridNet\', or \'HearNet\'')
flags.DEFINE_string('train_file', None, 'Path to the plain text file for the training set')
flags.DEFINE_string('valid_file', None, 'Path to the plain text file for the validation set')
flags.DEFINE_string('test_file', None, 'Path to the plain text file for the testing set')
flags.DEFINE_string('exp_name', None, 'Name of the experiment')
flags.DEFINE_string('init_checkpoint', None, 'Checkpoint file for model initialization')
flags.DEFINE_string('visual_init_checkpoint', None, 'Checkpoint file for visual model initialization')
flags.DEFINE_string('acoustic_init_checkpoint', None, 'Checkpoint file for acoustic model initialization')
flags.DEFINE_string('restore_checkpoint', None, 'Checkpoint file for session restoring')
flags.DEFINE_integer('batch_size', 4, 'Size of the mini-batch')
flags.DEFINE_float('learning_rate', 0.001, 'Learning rate')
flags.DEFINE_integer('display_freq', 1, 'How often must be shown training results')
flags.DEFINE_integer('num_epochs', 100, 'Number of iterations through dataset')
flags.DEFINE_integer('total_length', 30, 'Length in seconds of a full sequence')
#sample length is 2 s
flags.DEFINE_integer('sample_length', 1, 'Length in seconds of a sequence sample')
#number of crops 30 for 1 s
flags.DEFINE_integer('number_of_crops', 30, 'Number of crops')
flags.DEFINE_integer('buffer_size', 1, 'Size of pre-fetch buffer')
flags.DEFINE_string('tensorboard', None, 'Directory for storing logs')
flags.DEFINE_string('checkpoint_dir', None, 'Directory for storing models')
flags.DEFINE_integer('temporal_pooling', 1, 'Flag to indicate whether to use average pooling over time')
flags.DEFINE_integer('embedding', 1, 'Say if you are training 128 vectors')
flags.DEFINE_string('model_1', None, 'Model type, it can be one of \'ResNet18_v1\', \'DualCamHybridNet\', ')
flags.DEFINE_string('model_2', None, 'Model type, it can be one of \'DualCamHybridNet\', \'HearNet\'')
flags.DEFINE_float('margin', 0.2, 'margin') # between 0 and 11 for 128 vector
flags.DEFINE_integer('transfer', 0, 'Say if you are doing transfer')
flags.DEFINE_integer('distillation', 0, 'Say if you are doing distillation')
# in temporal models TemporalResNet50 and ResNet18
flags.DEFINE_integer('block_size', 1, 'Number of frames to pick randomly for each second') #12
flags.DEFINE_string('loss', 'Triplet', 'Loss type, it can be one of \'Triplet\'')
flags.DEFINE_integer('num_class', 128, 'Classes')
flags.DEFINE_float('alpha', 0.1, 'How much weighting the loss')
FLAGS = flags.FLAGS
def main(_):
# Instantiate logger
if FLAGS.transfer == 0:
transfer = False
else:
transfer = True
logger = Logger('{}/{}'.format(FLAGS.tensorboard, FLAGS.exp_name))
# Create data loaders according to the received program arguments
print('{}: {} - Creating data loaders'.format(datetime.now(), FLAGS.exp_name))
# random_pick = (FLAGS.model == 'TemporalResNet50' or FLAGS.model_1 == 'TemporalResNet50') or (FLAGS.model == 'ResNet18' or FLAGS.model_1 == 'ResNet18')
# if we are randomly picking total number of frames, we can set random pick to False
nr_frames = FLAGS.block_size * FLAGS.sample_length
if (FLAGS.model == 'ResNet18_v1' or FLAGS.model_1 == 'ResNet18_v1') and nr_frames < 12*FLAGS.sample_length:
random_pick = True
else:
random_pick = False
build_spectrogram = (FLAGS.model_2 == 'HearNet' or FLAGS.model == 'HearNet')
normalize = (FLAGS.model_2 == 'HearNet' or FLAGS.model == 'HearNet')
modalities = []
if FLAGS.embedding:
# model 1 is video
# model2 is audio or acoustic images
if transfer:
modalities.append(0)
if FLAGS.model_2 == 'DualCamHybridNet' or FLAGS.model_1 == 'DualCamHybridNet':
modalities.append(0)
if FLAGS.model_2 == 'HearNet':
modalities.append(1)
if FLAGS.model_1 == 'ResNet18_v1':
modalities.append(2)
else:
if FLAGS.model == 'DualCamHybridNet':
modalities.append(0)
elif FLAGS.model == 'HearNet':
modalities.append(1)
elif FLAGS.model == 'ResNet18_v1':
modalities.append(2)
else:
print('Not existing model')
with tf.device('/cpu:0'):
if FLAGS.train_file is None:
train_data = None
else:
train_data = DataLoader(FLAGS.train_file, 'training', FLAGS.batch_size, num_epochs=1,
total_length=FLAGS.total_length, sample_length=FLAGS.sample_length,
number_of_crops=FLAGS.number_of_crops, buffer_size=FLAGS.buffer_size,
shuffle=True, normalize=normalize, random_pick=random_pick,
build_spectrogram=build_spectrogram, modalities=modalities, nr_frames=nr_frames)
if FLAGS.valid_file is None:
valid_data = None
else:
valid_data = DataLoader(FLAGS.valid_file, 'inference', FLAGS.batch_size, num_epochs=1,
total_length=FLAGS.total_length, sample_length=FLAGS.sample_length,
buffer_size=FLAGS.buffer_size, shuffle=False, normalize=normalize,
random_pick=random_pick, build_spectrogram=build_spectrogram, modalities=modalities, nr_frames=nr_frames)
if FLAGS.test_file is None:
test_data = None
else:
test_data = DataLoader(FLAGS.test_file, 'inference', FLAGS.batch_size, num_epochs=1,
total_length=FLAGS.total_length, sample_length=FLAGS.sample_length,
buffer_size=FLAGS.buffer_size, shuffle=False, normalize=normalize,
random_pick=random_pick, build_spectrogram=build_spectrogram, modalities=modalities, nr_frames=nr_frames)
# Build model
print('{}: {} - Building model'.format(datetime.now(), FLAGS.exp_name))
if FLAGS.embedding:
with tf.device('/gpu:0'):
if FLAGS.distillation:
model_1 = DualCamHybridModel(input_shape=[36, 48, 12], num_classes=FLAGS.num_class, embedding=0)
model_2 = HearModel(input_shape=[200, 1, 257], num_classes=FLAGS.num_class, embedding=0)
elif transfer:
model_1 = ResNet18_v1(input_shape=[224, 298, 3], num_classes=FLAGS.num_class, map=True)
model_2 = HearModel(input_shape=[200, 1, 257], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
model_transfer = DualCamHybridModel(input_shape=[36, 48, 12], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
else:
#visual model
if FLAGS.model_1 == 'ResNet18_v1':
#map=True map of features, otherwise 10 classes
model_1 = ResNet18_v1(input_shape=[224, 298, 3], num_classes=FLAGS.num_class, map=True)
elif FLAGS.model_1 == 'DualCamHybridNet':
model_1 = DualCamHybridModel(input_shape=[36, 48, 12], num_classes=FLAGS.num_class,
embedding=FLAGS.embedding)
else:
print('Not existing model 1')
#audio or acoustic model
if FLAGS.model_2 == 'DualCamHybridNet':
model_2 = DualCamHybridModel(input_shape=[36, 48, 12], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
elif FLAGS.model_2 == 'HearNet':
model_2 = HearModel(input_shape=[200, 1, 257], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
else:
print('Not existing model 2')
# Build trainer
print('{}: {} - Building trainer'.format(datetime.now(), FLAGS.exp_name))
if transfer:
trainer = TrainerAudio(model_1, model_2, model_transfer, logger, display_freq=FLAGS.display_freq,
learning_rate=FLAGS.learning_rate,
num_epochs=FLAGS.num_epochs, temporal_pooling=FLAGS.temporal_pooling,
nr_frames=nr_frames, num_classes=FLAGS.num_class)
elif FLAGS.distillation:
trainer = TrainerDistillation(model_1, model_2, logger,
learning_rate=FLAGS.learning_rate,
num_epochs=FLAGS.num_epochs, temporal_pooling=FLAGS.temporal_pooling,
nr_frames=nr_frames, num_classes=FLAGS.num_class)
elif FLAGS.loss == 'Triplet':
trainer = TrainerTripletLoss(model_1, model_2, logger, display_freq=FLAGS.display_freq, learning_rate=FLAGS.learning_rate,
num_epochs=FLAGS.num_epochs, temporal_pooling=FLAGS.temporal_pooling, nr_frames=nr_frames, num_classes=FLAGS.num_class)
else:
raise ValueError('Unknown loss')
if FLAGS.mode == 'train':
checkpoint_dir = '{}/{}'.format(FLAGS.checkpoint_dir, FLAGS.exp_name)
if not tf.gfile.Exists(checkpoint_dir):
tf.gfile.MakeDirs(checkpoint_dir)
# Train model
with open('{}/{}'.format(FLAGS.checkpoint_dir, FLAGS.exp_name) + "/configuration.txt", "w") as outfile:
outfile.write('Experiment: {} \nVisual model: {} \nAudio model: {} \nLearning_rate: {}\n'.format(FLAGS.exp_name, FLAGS.model_1, FLAGS.model_2,
FLAGS.learning_rate))
outfile.write(
'Num_epochs: {} \nTotal_length: {} \nSample_length: {}\n'.format(FLAGS.num_epochs, FLAGS.total_length,
FLAGS.sample_length))
outfile.write(
'Number_of_crops: {} \nMargin: {}\nNumber of classes: {}\n'.format(FLAGS.number_of_crops, FLAGS.margin, FLAGS.num_class))
outfile.write(
'Block_size: {} \nLoss: {} \nEmbedding: {}\n'.format(FLAGS.block_size,
FLAGS.loss,
FLAGS.embedding))
outfile.write(
'Train_file: {} \nValid_file: {} \nTest_file: {}\n'.format(FLAGS.train_file,
FLAGS.valid_file,
FLAGS.test_file))
outfile.write(
'Mode: {} \nVisual_init_checkpoint: {} \nAcoustic_init_checkpoint: {} \nRestore_checkpoint: {}\n'.format(FLAGS.mode,
FLAGS.visual_init_checkpoint,
FLAGS.acoustic_init_checkpoint,
FLAGS.restore_checkpoint))
outfile.write('Checkpoint_dir: {} \nLog dir: {} \nBatch_size: {}\n'.format(FLAGS.checkpoint_dir, FLAGS.tensorboard, FLAGS.batch_size))
print('{}: {} - Training started'.format(datetime.now(), FLAGS.exp_name))
trainer.train(train_data=train_data, valid_data=valid_data)
elif FLAGS.mode == 'test':
# Test model
print('{}: {} - Testing started'.format(datetime.now(), FLAGS.exp_name))
trainer.test(test_data=test_data)
else:
raise ValueError('Unknown execution mode')
else:
with tf.device('/gpu:0'):
if FLAGS.model == 'ResNet18_v1':
model = ResNet18_v1(input_shape=[224, 298, 3], num_classes=FLAGS.num_class, map=False)
elif FLAGS.model == 'DualCamHybridNet':
model = DualCamHybridModel(input_shape=[36, 48, 12], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
elif FLAGS.model == 'HearNet':
model = HearModel(input_shape=[200, 1, 257], num_classes=FLAGS.num_class, embedding=FLAGS.embedding)
else:
# Not necessary but set model to None to avoid warning about using unassigned local variable
model = None
raise ValueError('Unknown model type')
# Build trainer
print('{}: {} - Building trainer'.format(datetime.now(), FLAGS.exp_name))
trainer = Trainer(model, logger, display_freq=FLAGS.display_freq, learning_rate=FLAGS.learning_rate, num_classes=FLAGS.num_class,
num_epochs=FLAGS.num_epochs, temporal_pooling=FLAGS.temporal_pooling, nr_frames=nr_frames)
if FLAGS.mode == 'train':
checkpoint_dir = '{}/{}'.format(FLAGS.checkpoint_dir, FLAGS.exp_name)
if not tf.gfile.Exists(checkpoint_dir):
tf.gfile.MakeDirs(checkpoint_dir)
# Train model
with open('{}/{}'.format(FLAGS.checkpoint_dir, FLAGS.exp_name) + "/configuration.txt", "w") as outfile:
outfile.write('Experiment: {} \nBatch_size: {}\n'.format(FLAGS.exp_name,
FLAGS.batch_size))
outfile.write(
'Model: {} \nLearning_rate: {}\nNumber of classes: {}\n'.format(FLAGS.model, FLAGS.learning_rate, FLAGS.num_class))
outfile.write(
'Num_epochs: {} \nTotal_length: {} \nSample_length: {}\n'.format(FLAGS.num_epochs,
FLAGS.total_length,
FLAGS.sample_length))
outfile.write(
'Number_of_crops: {} \nCheckpoint_dir: {} \nLog dir: {}\n'.format(FLAGS.number_of_crops, FLAGS.checkpoint_dir,
FLAGS.tensorboard))
outfile.write(
'Train_file: {} \nValid_file: {} \nTest_file: {}\n'.format(FLAGS.train_file,
FLAGS.valid_file,
FLAGS.test_file))
outfile.write(
'Mode: {} \nInit_checkpoint: {} \nRestore_checkpoint: {}\n'.format(FLAGS.mode,
FLAGS.init_checkpoint,
FLAGS.restore_checkpoint))
# Train model
print('{}: {} - Training started'.format(datetime.now(), FLAGS.exp_name))
trainer.train(train_data=train_data, valid_data=valid_data)
elif FLAGS.mode == 'test':
# Test model
print('{}: {} - Testing started'.format(datetime.now(), FLAGS.exp_name))
trainer.test(test_data=test_data)
else:
raise ValueError('Unknown execution mode')
if __name__ == '__main__':
flags.mark_flags_as_required(['mode', 'exp_name'])
tf.app.run()
# --mode
# train
# --model
# DualCamHybridNet
# --train_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/training.txt"
# --valid_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/validation.txt"
# --test_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/testing.txt"
# --exp_name
# train_resnet
# --batch_size
# 16
# --total_length
# 30
# --number_of_crops
# 15
# --sample_length
# 2
# --buffer_size
# 10
# --init_checkpoint
# /data/vsanguineti/checkpoints/Nuno/model.ckpt-98277 /resnet/resnet_v1_50.ckpt
# --tensorboard
# /data/vsanguineti/tensorboard/
# --checkpoint_dir
# /data/vsanguineti/checkpoints2/
# --num_epochs
# 300
# --learning_rate
# 0.000001
# --restore_checkpoint
# /data/vsanguineti/checkpoints2/embeddingAcousticScalar2MapFarDifferentDot0.00001savemodel/model_100.ckpt
# --temporal_pooling
# True
# --embedding
# False
# --mode
# train
# --model
# ResNet18_v1
# --train_file
# / media / vsanguineti / TOSHIBAEXT / tfrecords / lists / training.txt
# --valid_file
# / media / vsanguineti / TOSHIBAEXT / tfrecords / lists / validation.txt
# --test_file
# / media / vsanguineti / TOSHIBAEXT / tfrecords / lists / testing.txt
# --exp_name
# ResNettfrecords
# --batch_size
# 2
# --total_length
# 2
# --number_of_crops
# 1
# --sample_length
# 2
# --buffer_size
# 10
# --tensorboard
# / home / vsanguineti / Documents / Code / audio - video / tensorboard /
# --checkpoint_dir
# / home / vsanguineti / Documents / Code / audio - video / checkpoints /
# --num_epochs
# 100
# --learning_rate
# 0.0001
# --embedding
# 0
# --temporal_pooling
# 1
# --num_class
# 10
#How to use with two models
# --mode
# train
# --train_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/training.txt"
# --valid_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/validation.txt"
# --test_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/testing.txt"
# --model_1
# ResNet18_v1
# --model_2
# HearNet
# --exp_name
# train_resnet
# --batch_size
# 2
# --total_length
# 30
# --number_of_crops
# 15
# --sample_length
# 2
# --buffer_size
# 1
# --learning_rate
# 0.0001
# --tensorboard
# /data/vsanguineti/tensorboard/
# --checkpoint_dir
# /data/vsanguineti/checkpoints2/
# --embedding
# True
# --temporal_pooling
# True
# --num_class
# 128
#How to use transfer
# --mode
# train
# --train_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/training.txt"
# --valid_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/validation.txt"
# --test_file
# "/data/vsanguineti/dualcam_actions_dataset/30_seconds/lists/testing.txt"
# --model_1
# ResNet18_v1
# --model_2
# HearNet
# --exp_name
# train_resnet
# --batch_size
# 1
# --total_length
# 30
# --number_of_crops
# 15
# --sample_length
# 2
# --buffer_size
# 1
# --learning_rate
# 0.0001
# --tensorboard
# / data / vsanguineti / tensorboard /
# --checkpoint_dir
# / data / vsanguineti / checkpoints2 /
# --embedding
# True
# --temporal_pooling
# True
# --num_class
# 128
# --restore_checkpoint
# / data / vsanguineti / checkpoints2 / embeddingAcousticScalar2MapFarDifferentDot0
# .00001
# vers2_1 / model_100.ckpt
# --transfer
# 1
#--alpha
#0.1