-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeNN.py
680 lines (541 loc) · 23 KB
/
makeNN.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
#os.environ['TF_CPP_MIN_VLOG_LEVEL'] = '1'
os.environ['TF_ENABLE_AUTO_MIXED_PRECISION'] = '1'
os.environ['TF_CUDNN_USE_AUTOTUNE'] = "0"
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto(allow_soft_placement=False, device_count = {'GPU': 1})
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "0"
config.gpu_options.per_process_gpu_memory_fraction = 0.9
config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1
set_session(tf.Session(config=config))
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense, BatchNormalization, GaussianNoise
from keras import initializers
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, CSVLogger
from data_generator import DataGenerator
from keras.utils import multi_gpu_model
#ensuring reproducable results
#THIS SHOULD BE REMOVED DURING FINAL TRAINING
import numpy as np
import random as rn
'''
# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(42)
# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
rn.seed(12345)
# Force TensorFlow to use single thread.
# Multiple threads are a potential source of non-reproducible results.
# For further details, see: https://stackoverflow.com/questions/42022950/
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
from keras import backend as K
# The below tf.set_random_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see:
# https://www.tensorflow.org/api_docs/python/tf/set_random_seed
tf.set_random_seed(1234)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
'''
trainSetFolder='./data/train/'
validSetFolder='./data/valid/'
target_size=(96,96)
trainDataLen=128909+87757
validDataLen=2000+1361
trainDataLenP=2000
validDataLenP=512
def train_generator_with_batch_schedule(
model,epochs,name,target_size,batch_size,
model_save_filepath):
epochs=epochs//2
max_queue_size=[16,8]
train_gen = DataGenerator(
data_folder=trainSetFolder,
rescale=1./255,
horizontal_flip=True,
vertical_flip=True,
target_size=target_size,
batch_size=batch_size,
rotation_range=4)
valid_gen = DataGenerator(
data_folder=validSetFolder,
rescale=1./255,
target_size=target_size,
batch_size=batch_size)
for x in range(2):
print('Training Model:',name,'Session:',x+1,'Batch Size:',batch_size)
train_gen.update_batch_size(batch_size)
valid_gen.update_batch_size(batch_size)
model=trainAndSaveGenerator(
model,epochs*(x+1),name,target_size,batch_size,max_queue_size[x],
model_save_filepath,epochs*x,train_gen,valid_gen
)
batch_size=batch_size*2
acc=model.evaluate_generator(
generator=valid_gen,
max_queue_size=max_queue_size[-1],
workers=4,
use_multiprocessing=True,
verbose=1)[1]
return round(acc,4)*100
#using generator
def trainAndSaveGenerator(
model,epochs,name,target_size,batch_size,
max_queue_size,model_save_filepath,
initial_epoch,train_gen,valid_gen):
model.fit_generator(
generator=train_gen,
steps_per_epoch=trainDataLen // batch_size,
#steps_per_epoch=trainDataLenP // batch_size,
epochs=epochs,
validation_data=valid_gen,
validation_steps=validDataLen // batch_size,
#validation_steps=validDataLenP // batch_size,
verbose=1,
max_queue_size=max_queue_size,
use_multiprocessing=True,
initial_epoch=initial_epoch,
workers=4,
callbacks=[
EarlyStopping(patience=4, monitor='val_acc', restore_best_weights=True),
#ReduceLROnPlateau(patience=2,factor=0.2),
ModelCheckpoint(model_save_filepath, monitor='val_acc', save_best_only=True),
CSVLogger('./models/'+name+'/'+name+'-log.csv', separator=',', append=True)
])
return model
'''
ideas for next run:
massively increase kernal numbers, start with 128 in place of 32 and increase with the same schedule
continue calculating receptive field size and ensure that image size does not become too much smaller than it
try model2 again but with much larger kernal sizes, maybe drop the last conv layer as the RFS is quite a bit bigger than the image size
impliment modelcheckpoint callback function
increase learning rate factor to 0.4-0.7 to push past ~92% barrier
'''
#added stride, removed some conv2d and dropout layers, using nadam
def model1():
dropout=0.8
kernel_size=(5,5)
pool_size=(2,2)
image_size=96
epochs=60
name='model-1'
max_queue_size=16
batch_size=32
stride=(2,2)
filepath='./models/model-1/checkpoints/model-1-{val_acc:.3f}-{epoch:02d}.hdf5'
GN=0.3
model = Sequential()
model.add(GaussianNoise(GN,input_shape=(image_size, image_size, 3)))
model.add(Conv2D(64, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(64, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(64, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(GaussianNoise(GN))
model.add(Dense(512, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(256, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(64, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(32, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(
loss='binary_crossentropy',
optimizer='nadam',
metrics=['accuracy'])
acc=train_generator_with_batch_schedule(model,epochs,name,target_size,batch_size,filepath)
model.save('./models/model-1/model-1_'+str(acc)+'_best.hdf5')
#added stride, removed some conv2d and dropout layers
def model2():
dropout=0.9
kernel_size=(5,5)
pool_size=(2,2)
image_size=96
epochs=60
name='model-2'
max_queue_size=10
batch_size=128
stride=(2,2)
filepath='./models/model-2/checkpoints/model-2-{val_acc:.3f}-{epoch:02d}.hdf5'
GN=0.3
model = Sequential()
model.add(GaussianNoise(GN,input_shape=(image_size, image_size, 3)))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(GaussianNoise(GN))
model.add(Dense(2048, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(1024, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(512, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(256, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='nadam',
metrics=['accuracy'])
acc=train_generator_with_batch_schedule(model,epochs,name,target_size,batch_size,filepath)
model.save('./models/model-2/model-2_'+str(acc)+'_best.hdf5')
#removed a max pooling layers, removed all dropout, added noise layer at beginning
def model3():
dropout=0.8
kernel_size=(5,5)
pool_size=(2,2)
image_size=96
epochs=60
name='model-3'
batch_size=32
filepath='./models/model-3/checkpoints/model-3-{val_acc:.3f}-{epoch:02d}.hdf5'
GN=0.3
model = Sequential()
model.add(GaussianNoise(GN,input_shape=(image_size, image_size, 3)))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(GaussianNoise(GN))
model.add(Dense(512, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(256, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(64, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(32, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='nadam',
metrics=['accuracy'])
acc=train_generator_with_batch_schedule(model,epochs,name,target_size,batch_size,filepath)
model.save('./models/model-3/model-3_'+str(acc)+'_best.hdf5')
def model4():
dropout=0.8
kernel_size=(5,5)
pool_size=(2,2)
image_size=96
epochs=60
name='model-4'
batch_size=32
filepath='./models/model-4/checkpoints/model-4-{val_acc:.3f}-{epoch:02d}.hdf5'
GN=0.3
model = Sequential()
model.add(GaussianNoise(GN,input_shape=(image_size, image_size, 3)))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(GaussianNoise(GN))
model.add(Dense(512, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(512, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(256, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(256, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(64, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='nadam',
metrics=['accuracy'])
acc=train_generator_with_batch_schedule(model,epochs,name,target_size,batch_size,filepath)
model.save('./models/model-4/model-4_'+str(acc)+'_best.hdf5')
'''
#97.05%!!! Best Model So Far
def model4():
dropout=0.8
kernel_size=(5,5)
pool_size=(2,2)
image_size=96
epochs=60
name='model-4'
batch_size=64
filepath='./models/model-4/model-4-{val_acc:.3f}-{epoch:02d}.hdf5'
GN=0.3
model = Sequential()
model.add(GaussianNoise(GN,input_shape=(image_size, image_size, 3)))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(128, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
#model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(256, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(GaussianNoise(GN))
model.add(Conv2D(512, kernel_size=kernel_size, padding="same", kernel_initializer=initializers.he_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(GaussianNoise(GN))
model.add(Dense(128, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(64, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(32, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(GaussianNoise(GN))
model.add(Dense(16, kernel_initializer=initializers.lecun_normal()))
model.add(Activation('relu'))
model.add(BatchNormalization(momentum=0.99, epsilon=0.001))
model.add(Dropout(dropout))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='nadam',
metrics=['accuracy'])
train_generator_with_batch_schedule(model,epochs,name,target_size,batch_size,filepath)
'''
#method allows for restarting models which are trainging poorly
def modelStart(modelName):
try:
modelName()
return True
except KeyboardInterrupt as e:
print('KeyboardInterrupt detected, ending training')
return False
def main():
'''
while not modelStart(model1):
if input('Would you like to restart this model? (y or n) ')=="n":
break
'''
while not modelStart(model2):
if input('Would you like to restart this model? (y or n) ')=="n":
break
'''
while not modelStart(model3):
if input('Would you like to restart this model? (y or n) ')=="n":
break
while not modelStart(model4):
if input('Would you like to restart this model? (y or n) ')=="n":
break
'''
if __name__ == '__main__':
main()