-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
executable file
·634 lines (505 loc) · 25.6 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 17 13:21:14 2020
@author: Michi
"""
import argparse
import os
import numpy as np
import tensorflow.compat.v2 as tf
import tensorflow_probability as tfp
tf.enable_v2_behavior()
tfd = tfp.distributions
from data_generator import create_generators
from models import *
from utils import DummyHist, plot_hist, str2bool, Logger, get_flags
import sys
import time
@tf.function
def train_on_batch(x, y, model, optimizer, loss, train_acc_metric, bayesian=False, n_train_example=60000):
#print('train_on_batch call')
with tf.GradientTape() as tape:
tape.watch(model.trainable_variables)
for layer in model.layers: # In order to support frozen weights
x = layer(x, training=layer.trainable)
logits=x
if bayesian:
kl = sum(model.losses)/n_train_example
loss_value = loss(y, logits, kl)
else:
loss_value = loss(y, logits)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
proba = tf.nn.softmax(logits)
prediction = tf.argmax(proba, axis=1)
train_acc_metric.update_state(tf.argmax(y, axis=1), prediction)
return loss_value
@tf.function
def val_step(x, y, model, loss, val_acc_metric, bayesian=False, n_val_example=10000):
val_logits = model(x, training=False)
if bayesian:
val_kl = sum(model.losses)/n_val_example
val_loss_value = loss(y, val_logits, val_kl)
else:
val_loss_value = loss(y, val_logits)
val_proba = tf.nn.softmax(val_logits)
val_prediction = tf.argmax(val_proba, axis=1)
val_acc_metric.update_state(tf.argmax(y, axis=1), val_prediction)
return val_loss_value
@tf.function
def my_loss(y, logits):
loss_f = tf.keras.losses.CategoricalCrossentropy(from_logits=True) #tf.nn.softmax_cross_entropy_with_logits(y, logits)
return loss_f(y, logits)
@tf.function
def ELBO(y, logits, kl):
neg_log_likelihood = my_loss(y, logits)
return neg_log_likelihood + kl
def my_train(model, optimizer, loss,
epochs,
train_generator,
val_generator, manager, ckpt,
train_acc_metric, val_acc_metric,
restore=False, patience=100,
bayesian=False, save_ckpt=False, decayed_lr_value=None,
):
fname_hist = manager.directory+'/hist'
fname_idxs_train = manager.directory+'/idxs_train.txt'
fname_idxs_val = manager.directory+'/idxs_val.txt'
if not restore:
history = {'loss': [], 'val_loss': [], 'accuracy': [], 'val_accuracy':[] }
best_loss=np.infty
print("Initializing checkpoint from scratch.")
else:
# this is to fix bug in restoring optimizer. See https://github.com/tensorflow/tensorflow/issues/33150
#print('Training on one batch to properly restore model....')
#x, y = train_generator[0]
#_ = train_on_batch(x, y, model, optimizer, loss, train_acc_metric,
# bayesian=bayesian, n_train_example=train_generator.batch_size*train_generator.n_batches)
#train_acc_metric.reset_states()
#optimizer.iterations # this is to fix bug in restoring optimizer. See https://gist.github.com/yoshihikoueno/4ff0694339f88d579bb3d9b07e609122
print('Restoring ckpt...')
ckpt.restore(manager.latest_checkpoint)
print('ckpt step: %s' %ckpt.step)
hist_start=int(ckpt.step)
print('Starting from history at step %s' %hist_start)
history = {'loss': np.loadtxt(fname_hist+'_loss.txt').tolist()[0:hist_start],
'val_loss': np.loadtxt(fname_hist+'_val_loss.txt').tolist()[0:hist_start],
'accuracy': np.loadtxt(fname_hist+'_accuracy.txt').tolist()[0:hist_start],
'val_accuracy':np.loadtxt(fname_hist+'_val_accuracy.txt').tolist()[0:hist_start] }
for key in history.keys():
fname = fname_hist+'_'+key+'.txt'
fname_new = fname_hist+'_'+key+'_original.txt'
os.rename(fname, fname_new)
print('Saved copy of original histories.')
if manager.latest_checkpoint:
print("Restoring checkpoint from {}".format(manager.latest_checkpoint))
best_train_loss = history['loss'][-1]
best_loss = history['val_loss'][-1]
print('Starting from (loss, val_loss) = %.4f, %.4f' %(best_train_loss, best_loss ))
else:
print("Checkpoint not found. Initializing checkpoint from scratch.")
print('Last learning rate was %s' %ckpt.optimizer.learning_rate)
#if decayed_lr_value is not None:
#lr_fn = tf.optimizers.schedules.ExponentialDecay(FLAGS.lr, len(training_generator), FLAGS.decay)
# ckpt.optimizer.learning_rate = decayed_lr_value(hist_start) #FLAGS.lr
# print('Learning rate set to %s' %ckpt.optimizer.learning_rate)
#else:
# print('Re-starting from this value for the learing rate')
n_val_example=val_generator.batch_size*val_generator.n_batches
n_train_example=train_generator.batch_size*train_generator.n_batches
count = 0
for epoch in range(epochs):
print("Epoch %d" % (epoch,))
start_time = time.time()
# Run train loop
for batch_idx, batch in enumerate(train_generator):
x_batch_train, y_batch_train = batch #train_generator[batch_idx]
loss_value = train_on_batch(x_batch_train, y_batch_train, model, optimizer, loss, train_acc_metric, bayesian=bayesian, n_train_example=n_train_example)
# Run validation loop
val_loss_value = 0.
for val_batch_idx, val_batch in enumerate(val_generator):
x_batch_val, y_batch_val = val_batch #val_generator[val_batch_idx]
lv = val_step(x_batch_val, y_batch_val, model, loss, val_acc_metric, bayesian=bayesian, n_val_example=n_val_example)/ float(val_generator.n_batches)
val_loss_value += lv
if val_loss_value.numpy()<best_loss: #int(ckpt.step) % 10 == 0:
if save_ckpt:
save_path = manager.save()
print("Validation loss decreased. Saved checkpoint for step {}: {}".format(int(ckpt.step), save_path))
else:
#print('Creating directory %s' %manager.directory)
tf.io.gfile.makedirs(manager.directory)
best_loss = val_loss_value.numpy()
#print("New loss {:1.2f}".format(best_loss))
count = 0
else:
count +=1
print('Loss did not decrease. Count = %s' %count)
if count==patience:
print('Max patience reached. ')
break
ckpt.step.assign_add(1)
train_acc = train_acc_metric.result().numpy()
train_loss = loss_value.numpy()
history['loss'].append(train_loss)
history['accuracy'].append(train_acc)
train_acc_metric.reset_states()
val_acc = val_acc_metric.result().numpy()
val_loss = val_loss_value.numpy()
history['val_loss'].append(val_loss)
history['val_accuracy'].append(val_acc)
val_acc_metric.reset_states()
if epoch==0:
if restore:
for key in history.keys():
fname = fname_hist+'_'+key+'.txt'
with open(fname, 'a') as fh:
for el in history[key][:-1]:
fh.write(str(el) +'\n')
print('Re-wrote histories until epoch %s' %str(len(history['val_accuracy'][:-1])) )
with open(fname_idxs_train, 'a') as fit:
for ID in train_generator.list_IDs:
fit.write(str(ID) +'\n')
with open(fname_idxs_val, 'a') as fiv:
for ID in val_generator.list_IDs:
fiv.write( str(ID) +'\n' )
for key in history.keys():
fname = fname_hist+'_'+key+'.txt'
with open(fname, 'a') as fh:
fh.write(str(history[key][-1])+'\n')
###
# Uncomment if training in a jupyter notebook, to print the status on epoch bar
###
#epoch_bar.set_postfix(train_loss=loss_value.numpy(), val_loss=val_loss_value.numpy(),
# train_accuracy = train_acc.numpy(), val_accuracy=val_acc.numpy())
#print("Time taken: %.2fs" % (time.time() - start_time))
print("Time: %.2fs, ---- Loss: %.4f, Acc.: %.4f, Val. Loss: %.4f, Val. Acc.: %.4f\n" % (time.time() - start_time, train_loss, train_acc, val_loss, val_acc))
return model, history
def compute_loss(generator, model, bayesian=False):
x_batch_train, y_batch_train = generator[0]
logits = model(x_batch_train, training=False)
if bayesian:
kl = sum(model.losses)/generator.batch_size/generator.n_batches
loss_0 = ELBO(y_batch_train, logits, kl)
else:
loss_0 = my_loss(y_batch_train, logits)
return loss_0
def main():
in_time=time.time()
## Read params from stdin
parser = argparse.ArgumentParser()
parser.add_argument("--bayesian", default=True, type=str2bool, required=False)
parser.add_argument("--test_mode", default=False, type=str2bool, required=False)
parser.add_argument("--n_test_idx", default=2, type=int, required=False)
parser.add_argument("--seed", default=1312, type=int, required=False)
parser.add_argument("--fine_tune", default=False, type=str2bool, required=False)
parser.add_argument("--one_vs_all", default=False, type=str2bool, required=False)
parser.add_argument("--c_0", nargs='+', default=['lcdm'], required=False)
parser.add_argument("--c_1", nargs='+', default=['fR', 'dgp', 'wcdm', 'rand'], required=False)
parser.add_argument("--dataset_balanced", default=False, type=str2bool, required=False)
parser.add_argument("--include_last", default=False, type=str2bool, required=False)
parser.add_argument("--log_path", default='', type=str, required=False)
parser.add_argument("--restore", default=False, type=str2bool, required=False)
# FNAMES ETC
parser.add_argument("--fname", default='my_model', type=str, required=False)
parser.add_argument("--model_name", default='custom', type=str, required=False)
parser.add_argument("--my_path", default=None, type=str, required=False)
parser.add_argument("--DIR", default='data/train_data/', type=str, required=False)
parser.add_argument("--TEST_DIR", default='data/test_data/', type=str, required=False)
parser.add_argument("--models_dir", default='models/', type=str, required=False)
parser.add_argument("--save_ckpt", default=True, type=str2bool, required=False)
parser.add_argument("--out_path_overwrite", default=False, type=str2bool, required=False)
# INPUT DATA DIMENSION
parser.add_argument("--im_depth", default=500, type=int, required=False)
parser.add_argument("--im_width", default=1, type=int, required=False)
parser.add_argument("--im_channels", default=4, type=int, required=False)
parser.add_argument("--swap_axes", default=True, type=str2bool, required=False)
# PARAMETERS TO GENERATE DATA
parser.add_argument("--sort_labels", default=True, type=str2bool, required=False)
parser.add_argument("--normalization", default='stdcosmo', type=str, required=False)
parser.add_argument("--sample_pace", default=4, type=int, required=False)
parser.add_argument("--k_max", default=2.5, type=float, required=False)
parser.add_argument("--i_max", default=None, type=int, required=False)
parser.add_argument("--add_noise", default=True, type=str2bool, required=False)
parser.add_argument("--n_noisy_samples", default=10, type=int, required=False)
parser.add_argument("--add_shot", default=True, type=str2bool, required=False)
parser.add_argument("--add_sys", default=True, type=str2bool, required=False)
parser.add_argument("--sigma_sys", default=5., type=float, required=False)
parser.add_argument('--z_bins', nargs='+', default=[0,1,2,3], required=False)
# NET STRUCTURE
parser.add_argument("--n_dense", default=1, type=int, required=False)
parser.add_argument("--filters", nargs='+', default=[8,16,32], required=False)
parser.add_argument("--kernel_sizes", nargs='+', default=[10,5,2], required=False)
parser.add_argument("--strides", nargs='+', default=[2,2,1], required=False)
parser.add_argument("--pool_sizes", nargs='+', default=[2,2,0], required=False)
parser.add_argument("--strides_pooling", nargs='+', default=[2,1,0], required=False)
# FINE TUNING OPTIONS
parser.add_argument("--add_FT_dense", default=False, type=str2bool, required=False)
parser.add_argument("--trainable", default=False, type=str2bool, required=False)
parser.add_argument("--unfreeze", default=False, type=str2bool, required=False)
# PARAMETERS FOR TRAINING
parser.add_argument("--lr", default=0.01, type=float, required=False)
parser.add_argument("--drop", default=0.5, type=float, required=False)
parser.add_argument("--n_epochs", default=70, type=int, required=False)
parser.add_argument("--val_size", default=0.15, type=float, required=False)
parser.add_argument("--test_size", default=0., type=float, required=False)
parser.add_argument("--batch_size", default=2500, type=int, required=False)
parser.add_argument("--patience", default=100, type=int, required=False)
parser.add_argument("--GPU", default=True, type=str2bool, required=False)
parser.add_argument("--decay", default=0.95, type=float, required=False)
parser.add_argument("--BatchNorm", default=True, type=str2bool, required=False)
FLAGS = parser.parse_args()
FLAGS.z_bins = [int(z) for z in FLAGS.z_bins]
FLAGS.filters = [int(z) for z in FLAGS.filters]
FLAGS.kernel_sizes = [int(z) for z in FLAGS.kernel_sizes]
FLAGS.strides = [int(z) for z in FLAGS.strides]
FLAGS.pool_sizes = [int(z) for z in FLAGS.pool_sizes]
FLAGS.strides_pooling = [int(z) for z in FLAGS.strides_pooling]
FLAGS.c_1.sort()
FLAGS.c_0.sort()
#if not FLAGS.fine_tune:
# if not FLAGS.dataset_balanced and FLAGS.one_vs_all:
# raise ValueError('dataset_balanced must be true in one vs all mode')
#if not FLAGS.one_vs_all and not FLAGS.dataset_balanced:
# raise ValueError('when not in one vs all mode, dataset_balanced must be true')
log_fname_add=''
if FLAGS.fine_tune:
log_fname_add+='_'
FLAGS_ORIGINAL = get_flags(FLAGS.log_path)
if len(FLAGS.c_1)>1:
add_ckpt_name = ''
temp_dict={ label:'non_lcdm' for label in FLAGS.c_1}
if not FLAGS.one_vs_all:
#raise ValueError('one vs all must be true when fine tuning against one label')
print('Fine tuning reauires ne vs all to be true. Correcting original flag')
FLAGS.one_vs_all=True
else:
# fine tuning 1vs 1
temp_dict={ label:label for label in FLAGS.c_1}
add_ckpt_name = '_'+('-').join(FLAGS.c_1)+'vs'+('-').join(FLAGS.c_0)
log_fname_add+='_'+('-').join(FLAGS.c_1)+'vs'+('-').join(FLAGS.c_0)
if not FLAGS.dataset_balanced:
add_ckpt_name += '_unbalanced'
log_fname_add+='_unbalanced'
else:
add_ckpt_name += '_balanced'
log_fname_add += '_balanced'
ft_ckpt_name_base_unfreezing=add_ckpt_name+'_frozen_weights'
if not FLAGS.trainable:
add_ckpt_name+='_frozen_weights'
log_fname_add+='_frozen_weights'
else:
add_ckpt_name+='_all_weights'
log_fname_add+='_all_weights'
if FLAGS.include_last:
add_ckpt_name+='_include_last'
log_fname_add+='_include_last'
else:
add_ckpt_name+='_without_last'
log_fname_add+='_without_last'
if FLAGS.unfreeze:
add_ckpt_name+='_unfrozen'
log_fname_add+='_unfrozen'
#FLAGS.group_lab_dict = temp_dict
if not FLAGS.out_path_overwrite:
out_path = FLAGS_ORIGINAL.models_dir+FLAGS_ORIGINAL.fname
else:
out_path = FLAGS.models_dir+FLAGS.fname
elif FLAGS.one_vs_all:
if len(FLAGS.c_1)>1:
add_ckpt_name = ''
temp_dict={ label:'non_lcdm' for label in FLAGS.c_1}
else:
# training 1vs 1
temp_dict={ label:label for label in FLAGS.c_1}
add_ckpt_name = '_'+('-').join(FLAGS.c_1)+'vs'+('-').join(FLAGS.c_0)
out_path = FLAGS.models_dir+FLAGS.fname
else:
out_path = FLAGS.models_dir+FLAGS.fname
if FLAGS.one_vs_all or FLAGS.fine_tune:
FLAGS.group_lab_dict = temp_dict
for i in range(len(FLAGS.c_0) ):
FLAGS.group_lab_dict[FLAGS.c_0[i]]=FLAGS.c_0[i]
if FLAGS.test_mode and not FLAGS.fine_tune:
out_path=out_path+'_test'
###
# Uncomment the parts below to redirect output to file.
# Does not work on Google Colab
###
if not os.path.exists(out_path):
print('Creating directory %s' %out_path)
tf.io.gfile.makedirs(out_path)
else:
print('Directory %s not created' %out_path)
logfile = os.path.join(out_path, FLAGS.fname+log_fname_add+'_log.txt')
myLog = Logger(logfile)
sys.stdout = myLog
#with open(out_path+'/params.txt', 'w') as fpar:
# print('Opened params file %s. Writing params' %(out_path+'/params.txt'))
print('\n -------- Parameters:')
for key,value in vars(FLAGS).items():
print (key,value)
# fpar.write(' : '.join([str(key), str(value)])+'\n')
print('\n------------ CREATING DATA GENERATORS ------------')
training_generator, validation_generator = create_generators(FLAGS)
if FLAGS.fine_tune:
print('\n------------ CREATING ORIGINAL DATA GENERATORS FOR CHECK------------')
or_training_generator, or_validation_generator = create_generators(FLAGS_ORIGINAL)
n_classes = or_training_generator.n_classes_out # in order to build correctly original model
model_name = FLAGS_ORIGINAL.model_name
bayesian=FLAGS_ORIGINAL.bayesian
else:
n_classes = training_generator.n_classes_out
model_name = FLAGS.model_name
bayesian = FLAGS.bayesian
print('------------ DONE ------------\n')
print('------------ BUILDING MODEL ------------')
if FLAGS.swap_axes:
input_shape = ( int(training_generator.dim[0]),
int(training_generator.n_channels))
else:
input_shape = ( int(training_generator.dim[0]),
int(training_generator.dim[1]),
int(training_generator.n_channels))
print('Input shape %s' %str(input_shape))
if FLAGS.test_mode:
drop=0
else:
drop=FLAGS.drop
if FLAGS.fine_tune:
try:
BatchNorm=FLAGS_ORIGINAL.BatchNorm
except AttributeError:
print(' #### FLAGS.BatchNorm not found! #### \n Probably loading an older model. Using BatchNorm=True')
BatchNorm=True
filters, kernel_sizes, strides, pool_sizes, strides_pooling, n_dense= FLAGS_ORIGINAL.filters, FLAGS_ORIGINAL.kernel_sizes, FLAGS_ORIGINAL.strides, FLAGS_ORIGINAL.pool_sizes, FLAGS_ORIGINAL.strides_pooling, FLAGS_ORIGINAL.n_dense
else:
try:
BatchNorm=FLAGS.BatchNorm
except AttributeError:
print(' #### FLAGS.BatchNorm not found! #### \n Probably loading an older model. Using BatchNorm=True')
BatchNorm=True
filters, kernel_sizes, strides, pool_sizes, strides_pooling, n_dense = FLAGS.filters, FLAGS.kernel_sizes, FLAGS.strides, FLAGS.pool_sizes, FLAGS.strides_pooling, FLAGS.n_dense
model=make_model( model_name=model_name,
drop=drop,
n_labels=n_classes,
input_shape=input_shape,
padding='valid',
filters=filters,
kernel_sizes=kernel_sizes,
strides=strides,
pool_sizes=pool_sizes,
strides_pooling=strides_pooling,
activation=tf.nn.leaky_relu,
bayesian=bayesian,
n_dense=n_dense, swap_axes=FLAGS.swap_axes, BatchNorm=BatchNorm
)
model.build(input_shape=input_shape)
print(model.summary())
if FLAGS.fine_tune:
loss_0 = compute_loss(or_training_generator, model, bayesian=FLAGS.bayesian)
print('Loss before loading weights/ %s\n' %loss_0.numpy())
if FLAGS.decay is not None:
lr_fn = tf.optimizers.schedules.ExponentialDecay(FLAGS.lr, len(training_generator), FLAGS.decay)
optimizer = tf.keras.optimizers.Adam(lr_fn)
else:
optimizer = tf.keras.optimizers.Adam(lr=FLAGS.lr)
if FLAGS.restore and FLAGS.decay is not None:
decayed_lr_value = lambda step: FLAGS.lr * FLAGS.decay**(step / len(training_generator))
#optimizer.iterations # this access will invoke optimizer._iterations method and create optimizer.iter attribute
#if FLAGS.decay is not None:
# optimizer.decay = tf.Variable(tf.Variable(FLAGS.decay))
if not FLAGS.unfreeze:
ckpts_path = out_path+'/tf_ckpts/'
else:
ckpts_path=out_path+'/tf_ckpts_fine_tuning'+ft_ckpt_name_base_unfreezing+'/'
ckpt_name = 'ckpt'
ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=optimizer, net=model)
if FLAGS.fine_tune:
print('Loading ckpt from %s' %ckpts_path)
latest = tf.train.latest_checkpoint(ckpts_path)
print('Loading ckpt %s' %latest)
if not FLAGS.test_mode:
ckpts_path = out_path+'/tf_ckpts_fine_tuning'+add_ckpt_name+'/'
else:
ckpts_path = out_path+'/tf_ckpts_fine_tuning_test'+add_ckpt_name+'/'
ckpt_name = ckpt_name+'_fine_tuning'+add_ckpt_name
if FLAGS.test_mode:
ckpt_name+='_test'
ckpt.restore(latest)
print('Last learning rate was %s' %ckpt.optimizer.learning_rate)
ckpt.optimizer.learning_rate = FLAGS.lr
print('Learning rate set to %s' %ckpt.optimizer.learning_rate)
loss_1 = compute_loss(or_training_generator, model, bayesian=FLAGS.bayesian)
print('Loss after loading weights/ %s\n' %loss_1.numpy())
if FLAGS.add_FT_dense:
if not FLAGS.swap_axes:
dense_dim=filters[-1]
else:
dense_dim=filters[-1]
else:
dense_dim=0
if not FLAGS.unfreeze:
model = make_fine_tuning_model(base_model=model, input_shape=input_shape,
n_out_labels=training_generator.n_classes_out,
dense_dim= dense_dim, bayesian=bayesian,
trainable=FLAGS.trainable,
drop=drop, BatchNorm=FLAGS.BatchNorm, include_last=FLAGS.include_last)
else:
model = make_unfreeze_model(base_model=model, input_shape=input_shape,
n_out_labels=training_generator.n_classes_out,
dense_dim= dense_dim, bayesian=bayesian,
drop=drop, BatchNorm=FLAGS.BatchNorm)
model.build(input_shape=input_shape)
print(model.summary())
ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=optimizer, net=model)
elif FLAGS.one_vs_all:
if not FLAGS.test_mode:
ckpts_path = out_path+'/tf_ckpts'+add_ckpt_name+'/'
else:
ckpts_path = out_path+'/tf_ckpts_test'+add_ckpt_name+'/'
ckpt_name = ckpt_name+add_ckpt_name
if FLAGS.test_mode:
ckpt_name+='_test'
manager = tf.train.CheckpointManager(ckpt, ckpts_path,
max_to_keep=2,
checkpoint_name=ckpt_name)
train_acc_metric = tf.keras.metrics.Accuracy()
val_acc_metric = tf.keras.metrics.Accuracy()
if FLAGS.GPU:
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
#raise SystemError('GPU device not found')
print('GPU device not found ! Device: %s' %device_name)
else: print('Found GPU at: {}'.format(device_name))
print('------------ TRAINING ------------\n')
if FLAGS.bayesian:
loss=ELBO
else:
loss=my_loss
#print('Model n_classes : %s ' %n_classes)
print('Features shape: %s' %str(training_generator[0][0].shape))
print('Labels shape: %s' %str(training_generator[0][1].shape))
model, history = my_train(model, optimizer, loss,
FLAGS.n_epochs,
training_generator,
validation_generator, manager, ckpt,
train_acc_metric, val_acc_metric,
patience=FLAGS.patience, restore=FLAGS.restore,
bayesian=bayesian, save_ckpt=FLAGS.save_ckpt, decayed_lr_value=None #not(FLAGS.test_mode)
)
hist_path = out_path+'/hist.png'
if FLAGS.fine_tune:
if FLAGS.test_mode:
hist_path = out_path +'/hist_fine_tuning'+add_ckpt_name+'_test.png'
else:
hist_path = out_path +'/hist_fine_tuning'+add_ckpt_name+'.png'
plot_hist(DummyHist(history), epochs=len(history['loss']), save=True, path=hist_path, show=False)
###
# Uncoment if saving output on file, to properly close
###
sys.stdout = sys.__stdout__
myLog.close()
print('Done in %.2fs' %(time.time() - in_time))
if __name__=='__main__':
main()