-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN.py
518 lines (475 loc) · 21.3 KB
/
CNN.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
# Main libraries needed to handle images ---------------------------------------
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
# Machine learning libraries ---------------------------------------------------
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D , MaxPooling2D , Dropout
from tensorflow.keras import Model
from tensorflow.keras.regularizers import l2
from tensorflow.keras.models import load_model, save_model
# import subprocess
# import sys
# Options: EfficientNetB0, EfficientNetB1, EfficientNetB2, EfficientNetB3
# Higher the number, the more complex the model is.
from efficientnet.tfkeras import EfficientNetB0
from efficientnet.tfkeras import EfficientNetB3
from efficientnet.tfkeras import EfficientNetB1
from efficientnet.tfkeras import EfficientNetB7
from efficientnet.tfkeras import center_crop_and_resize, preprocess_input
from tensorflow.keras import models
from tensorflow.keras import layers
def Classic_nn ():
model = tf.keras.models.Sequential([
# Note the input shape is the desired size of the image 200x 200 with 3 bytes color
# The first convolution
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(200, 200, 2)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
# The second convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.3),
# The third convolution
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.3),
# The fourth convolution #CNN 1
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.3),
# The fifth convolution
tf.keras.layers.Conv2D(256, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.3),
# Flatten the results to feed into a dense layer
tf.keras.layers.Flatten(),
# 128 neuron in the fully-connected layer
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.1),
# 5 output neurons for 5 classes with the softmax activation
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
def micro_nn():
model = tf.keras.models.Sequential([
# Note the input shape is the desired size of the image 150x150 with 3 bytes color
# This is the first convolution
tf.keras.layers.Conv2D(16, 4, activation='relu',input_shape=(200, 200,1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Flatten(), #CNN 2
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
def nano_nn():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(200, 200, 2)),
tf.keras.layers.Conv2D(16, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
def Mario_test():
model = tf.keras.models.Sequential([
# This is the first convolution
tf.keras.layers.Conv2D(32, 4, activation='relu',input_shape=(200, 200,2)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(32, 4, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.BatchNormalization(),
# The second convolution
tf.keras.layers.Conv2D(64, 4, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(64, 4, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
# The third convolution
tf.keras.layers.Conv2D(128,3, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(128, 3, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
# # The fifth convolution
tf.keras.layers.Conv2D(256, 2, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
# # The fifth convolution
tf.keras.layers.Conv2D(256, 2, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.5),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
def Mario_original():
model = tf.keras.models.Sequential([
# This is the first convolution
tf.keras.layers.Conv2D(8, 20, activation='relu',input_shape=(200, 200,2)),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(8, 20, activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.2),
# The second convolution
tf.keras.layers.Conv2D(16, 15, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(16, 15, activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.3),
# The third convolution
tf.keras.layers.Conv2D(32,10, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(64, 10, activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.4),
# # The fifth convolution
tf.keras.layers.Conv2D(64, 5, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.4),
# # The fifth convolution
tf.keras.layers.Conv2D(64, 5, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.BatchNormalization(),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
# 512 neuron hidden layer
tf.keras.layers.Dense(2048, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
def res_net_block_small(input_data, filters, conv_size):
s = input_data[:,2:input_data.shape[1]-2,2:input_data.shape[2]-2,:]
if filters != s.shape[3]:
s = tf.keras.layers.Conv2D(filters, 1, activation=None)(s)
x = tf.keras.layers.Conv2D(filters, conv_size, activation='relu')(input_data)
x = tf.keras.layers.Conv2D(filters, conv_size, activation=None)(x)
x = tf.keras.layers.Add()([x, s])
x = tf.keras.layers.Activation('relu')(x)
return x
def res_net_block_big(input_data, filters, conv_size):
s = input_data[:,3:input_data.shape[1]-3,3:input_data.shape[2]-3,:]
if filters != s.shape[3]:
s = tf.keras.layers.Conv2D(filters, 1, activation=None)(s)
x = tf.keras.layers.Conv2D(filters, conv_size, activation='relu')(input_data)
x = tf.keras.layers.Conv2D(filters, conv_size, activation='relu')(x)
x = tf.keras.layers.Conv2D(filters, conv_size, activation=None)(x)
x = tf.keras.layers.Add()([x, s])
x = tf.keras.layers.Activation('relu')(x)
return x
def Big_nn():
inputs = tf.keras.Input(shape=(200, 200, 2))
x = tf.keras.layers.Conv2D(16, 2, activation='relu')(inputs)
x = tf.keras.layers.Dropout(0.4)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = res_net_block_small(x,16,3)
x = tf.keras.layers.Dropout(0.4)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = res_net_block_small(x,16,3)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.MaxPooling2D(2)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = res_net_block_big(x,32,3)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = res_net_block_small(x,32,3)
x = tf.keras.layers.MaxPooling2D(2)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.6)(x)
x = res_net_block_small(x,64,3)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.6)(x)
x = res_net_block_small(x,92,3)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.6)(x)
x = res_net_block_big(x,128,3)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.7)(x)
x = tf.keras.layers.MaxPooling2D(2)(x)
# x = res_net_block_small(x,128,3)
# x = tf.keras.layers.BatchNormalization()(x)
# x = tf.keras.layers.Dropout(0.7)(x)
# x = res_net_block_big(x,128,3)
# x = tf.keras.layers.BatchNormalization()(x)
# x = tf.keras.layers.Dropout(0.8)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.8)(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.8)(x)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.8)(x)
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.Model(inputs, outputs)
return model
def Tripple_nn():
inputs = tf.keras.Input(shape=(200, 200, 2))
x = tf.keras.layers.Conv2D(4, 15, activation='relu')(inputs)
# x = tf.keras.layers.Dropout(0.3)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Conv2D(4, 15, activation='relu')(x)
# x = tf.keras.layers.Dropout(0.3)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Conv2D(8, 10, activation='relu')(x)
# x = tf.keras.layers.Dropout(0.3)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.MaxPooling2D(2, 2)(x)
x = tf.keras.layers.Conv2D(8, 10, activation='relu')(x)
# x = tf.keras.layers.Dropout(0.3)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.MaxPooling2D(2, 2)(x)
x = tf.keras.layers.Conv2D(16, 7, activation='relu')(x)
# x = tf.keras.layers.Dropout(0.4)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Conv2D(32, 7, activation='relu')(x)
# x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1024)(x)
Model_fat = tf.keras.layers.Dense(512)(x)
y = tf.keras.layers.Conv2D(16, 7, activation='relu')(inputs)
# y = tf.keras.layers.Dropout(0.3)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.Conv2D(16, 7, activation='relu')(y)
# y = tf.keras.layers.Dropout(0.3)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.MaxPooling2D(2, 2)(y)
y = tf.keras.layers.Conv2D(32, 7, activation='relu')(y)
# y = tf.keras.layers.Dropout(0.3)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.MaxPooling2D(2, 2)(y)
y = tf.keras.layers.Conv2D(64, 7, activation='relu')(y)
# y = tf.keras.layers.Dropout(0.3)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.MaxPooling2D(2, 2)(y)
y = tf.keras.layers.Conv2D(64, 5, activation='relu')(y)
# y = tf.keras.layers.Dropout(0.4)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.Conv2D(64, 5, activation='relu')(y)
# y = tf.keras.layers.Dropout(0.5)(y)
y = tf.keras.layers.BatchNormalization()(y)
y = tf.keras.layers.Flatten()(y)
y = tf.keras.layers.Dense(512)(y)
Model_medium = tf.keras.layers.Dense(256)(y)
z = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
# z = tf.keras.layers.Dropout(0.3)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.Conv2D(32, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.3)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.MaxPooling2D(2, 2)(z)
z = tf.keras.layers.Conv2D(64, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.3)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.MaxPooling2D(2, 2)(z)
z = tf.keras.layers.Conv2D(64, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.3)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.MaxPooling2D(2, 2)(z)
z = tf.keras.layers.Conv2D(128, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.4)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.MaxPooling2D(2, 2)(z)
z = tf.keras.layers.Conv2D(128, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.4)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.MaxPooling2D(2, 2)(z)
z = tf.keras.layers.Conv2D(128, 3, activation='relu')(z)
# z = tf.keras.layers.Dropout(0.5)(z)
z = tf.keras.layers.BatchNormalization()(z)
z = tf.keras.layers.Flatten()(z)
z = tf.keras.layers.Dense(128)(z)
Model_tiny = tf.keras.layers.Dense(64)(z)
Merged = tf.keras.layers.concatenate([Model_fat, Model_medium,Model_tiny])
final = tf.keras.layers.Flatten()(Merged)
final = tf.keras.layers.Dense(1024, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
final = tf.keras.layers.Dense(1024, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
final = tf.keras.layers.Dense(512, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
final = tf.keras.layers.Dense(512, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
final = tf.keras.layers.Dense(256, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
final = tf.keras.layers.Dense(128, activation='relu')(final)
final = tf.keras.layers.Dropout(0.5)(final)
final = tf.keras.layers.BatchNormalization()(final)
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(final)
model = tf.keras.Model(inputs, outputs)
return model
def nano_nn():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(200, 200, 2)),
tf.keras.layers.Conv2D(16, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
]) #82.7% 37 epochs 5000 images bg gen upgrade batch size 64 learning rate 1e-5 0x6
return model
def TL_ef_net():
# loading pretrained conv base model
conv_base = EfficientNetB1(weights="imagenet", include_top=False, input_shape=(200, 200, 3))
dropout_rate = 0.2
model = models.Sequential()
model.add(conv_base)
# model.add(layers.GlobalMaxPooling2D(name="gap"))
model.add(layers.Flatten(name="flatten"))
model.add(tf.keras.layers.BatchNormalization(),)
model.add(layers.Dense(16, activation="relu"))
model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dropout(dropout_rate))
model.add(layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dropout(dropout_rate))
model.add(layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dropout(dropout_rate))
# model.add(layers.Dense(256, activation='relu', name="fc1"))
model.add(layers.Dense(1, activation="sigmoid", name="fc_out"))
conv_base.trainable = False
return model
def Eff_net():
# loading pretrained conv base model
conv_base = EfficientNetB0(weights="imagenet", include_top=true, input_shape=(200, 200, 3))
dropout_rate = 0.05
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten(name="flatten"))
# model.add(layers.Dense(32, activation="relu"))
# model.add(layers.Dropout(dropout_rate))
# model.add(layers.Dense(16, activation="relu"))
# model.add(layers.Dropout(dropout_rate))
# model.add(layers.Dense(256, activation='relu', name="fc1"))
model.add(layers.Dense(1, activation="sigmoid", name="fc_out"))
conv_base.trainable = False
return model
def Eff_net_B0_simplest(Net_file_path , Trainable = 'All', load = True):
# loading pretrained conv base model
conv_base = EfficientNetB0(weights="imagenet", include_top=False, input_shape=(200, 200, 3))
dropout_rate = 0.2
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten(name="flatten"))
model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dense(32, activation="relu", name="Dense1_add"))
# model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dropout(dropout_rate))
model.add(layers.Dense(16, activation="relu", name="Dense2_add"))
# model.add(tf.keras.layers.BatchNormalization())
model.add(layers.Dropout(dropout_rate))
model.add(layers.Dense(1, activation="sigmoid", name="fc_out"))
if load :
model.load_weights(Net_file_path)
if Trainable == 'Classifier':
conv_base.trainable = False
elif Trainable == 'Class_FirstConv':
conv_base.trainable = True
set_trainable = False
for layer in conv_base.layers:
if layer.name == 'multiply_16':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False
elif Trainable == 'Class_SecondConv':
conv_base.trainable = True
set_trainable = False
for layer in conv_base.layers:
if layer.name == 'multiply_14':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False
elif Trainable == 'Class_ThirdConv':
conv_base.trainable = True
set_trainable = False
for layer in conv_base.layers:
if layer.name == 'multiply_12':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False
return model