Spaces:
Sleeping

File size: 28,707 Bytes
2431a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
from keras.layers import Activation, Convolution2D, Dropout, Conv2D, Dense, Concatenate, Add, PReLU, LeakyReLU, concatenate, Lambda
from keras.layers import AveragePooling2D, BatchNormalization
from keras.layers import GlobalAveragePooling2D, ZeroPadding2D, UpSampling2D
from keras.models import Sequential
from keras.layers import Flatten
from keras.models import Model
from keras.layers import Input
from keras.layers import MaxPooling2D
from keras.layers import SeparableConv2D
from keras import layers
from keras.regularizers import l2
from keras import regularizers
from keras.activations import linear
from keras.layers import multiply

def count_params(weights):
    """Counts the total number of parameters in a list of weights."""
    return int(sum(tf.keras.backend.count_params(w) for w in weights))


from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
# from keras.applications import MobileNet, MobileNetV2  # Unused importfrom keras.applications.xception import Xception
# from keras.applications import InceptionV3  # Unused import# from keras.applications import DenseNet121  # Unused import# from keras.applications import NASNetMobile  # Unused import
import tensorflow as tf

import numpy as np
import cv2

from Attention import cbam_block, se_block, self_attention_block, custom_attention
# , MultiHeadSelfAttention, LayerNormalization
#from Inception import inception_module


# def resize_like(target, ref):
#     """Resize `target` tensor to match the spatial dimensions of `ref` tensor."""
#     target_height, target_width = ref.shape[1], ref.shape[2]
#     return Lambda(lambda x: tf.image.resize(x, [target_height, target_width]))(target)

def count_model_params(model):
    total_params = count_params(model.trainable_weights) + count_params(model.non_trainable_weights)
    trainable_params = count_params(model.trainable_weights)
    non_trainable_params = count_params(model.non_trainable_weights)
    return total_params, trainable_params, non_trainable_params


def inception_module(layer_in, f1_1x1, f1_5x5, f2_1x1, f2_3x3, f3_1x1, f4_1x1):
    # Branch 1: 1x1 DWConv -> 5x5 DWConv
    branch1 = SeparableConv2D(f1_1x1, (1, 1), padding='same', activation='relu')(layer_in)
    branch1 = SeparableConv2D(f1_5x5, (5, 5), padding='same', activation='relu')(branch1)

    # Branch 2: 1x1 DWConv -> 3x3 DWConv
    branch2 = SeparableConv2D(f2_1x1, (1, 1), padding='same', activation='relu')(layer_in)
    branch2 = SeparableConv2D(f2_3x3, (3, 3), padding='same', activation='relu')(branch2)

    # Branch 3: 3x3 Max Pooling -> 1x1 DWConv
    branch3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(layer_in)
    branch3 = SeparableConv2D(f3_1x1, (1, 1), padding='same', activation='relu')(branch3)

    # Branch 4: 1x1 DWConv only
    branch4 = SeparableConv2D(f4_1x1, (1, 1), padding='same', activation='relu')(layer_in)

    # Concatenate all branches
    layer_out = concatenate([branch1, branch2, branch3, branch4], axis=-1)
    return layer_out

def combine_4SC_2_1_V3(input_shape, num_classes, l2_regularization=0.01):
    regularization = l2(l2_regularization)

    # Block1 - 16 channels
    img_input = Input(input_shape)
    x = SeparableConv2D(16, (7, 7), strides=(1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(img_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(16, (7, 7), strides=(1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = AveragePooling2D((3, 3), strides=(2, 2))(x)
#     x = se_block(x)
#     x = cbam_block(x)
#     x = self_attention_block(x)
    # block-1 shape
    # print("block-1:", x.shape)
    x = custom_attention(x)
    # block-1 shape
    # print("block-1:", x.shape)
    x = Dropout(0.5)(x)

    model_block1 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_block1)
    print(f"Block 1 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    # Block2 - 32 channels
    
    x = SeparableConv2D(32, (5, 5), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(32, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = AveragePooling2D((3, 3), strides=(2, 2), padding='same')(x)
#     x = inception_module(x, 32, 24, 32, 24, 32, 32)
#     x = se_block(x)
#     x = cbam_block(x)
#     x = self_attention_block(x)
    # block-2 shape
    # print("block-2:", x.shape)
    x = custom_attention(x)
    # block-2 shape
    # print("block-2:", x.shape)    
    x = Dropout(0.5)(x)

    model_block2 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_block2)
    print(f"Block 2 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    
    # Block3 - 64 channels
    residual = SeparableConv2D(32, (3, 3), strides=(1, 1),padding='same', use_bias=False)(x)
    residual = AveragePooling2D((3, 3), strides=(2, 2), padding='same')(residual)
    residual = SeparableConv2D(64, (1, 1), strides=(1, 1), padding='same', use_bias=False)(residual)
    # residual = BatchNormalization()(residual)
    
#     residual = se_block(residual)
    # residual = cbam_block(residual)
    # block-3 shape
    # print("block-3 res before att:", residual.shape)
#     residual = custom_attention(residual)
    # block-3 shape
    # print("block-3 res after att:", residual.shape)

    model_c1 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_c1)
    print(f"Connection 1 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    
    # 2nd Inception and CBAM
    
    x = SeparableConv2D(64, (5, 5), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(64, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    # x = BatchNormalization()(x)
    # x = Activation('relu')(x)
    x = AveragePooling2D((3, 3),padding='same', strides=(2, 2))(x)
#     x = inception_module(x, 32, 16, 32, 16, 16, 16)
#     x = se_block(x)
#     x = cbam_block(x)
#     x = self_attention_block(x)
    x = custom_attention(x)
    x = Dropout(0.5)(x)
    
    model_block3 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_block3)
    print(f"Block 3 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    #Connection 1
    x = Add()([x, residual])
    
    model_c1a = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_c1a)
    print(f"After adding Connection 1 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    

    # Block4 - 128 channels
    residual = SeparableConv2D(128, (3, 3), strides=(1, 1),padding='same', use_bias=False)(x)
    residual = AveragePooling2D((3, 3), strides=(2, 2), padding='same')(residual)
    residual = SeparableConv2D(256, (1, 1), strides=(1, 1), padding='same', use_bias=False)(residual)
    residual = BatchNormalization()(residual)
    # residual = custom_attention(residual)
    # residual = Activation('relu')(residual)

    x = SeparableConv2D(64, (5, 5), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(128, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    # x = BatchNormalization()(x)
    # x = Activation('relu')(x)
    x = AveragePooling2D((3, 3), padding='same', strides=(2, 2))(x)
#     x = inception_module(x, 128, 32, 128, 32, 32, 32)
#     x = se_block(x)
#     x = cbam_block(x)
#     x = self_attention_block(x)
    # block-4 shape
    # print("block-4:", x.shape)
    x = custom_attention(x)
    # block-4 shape
    # print("block-4:", x.shape)
    
    x = Dropout(0.5)(x)
    
    model_block4 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_block4)
    print(f"Block 4 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
    
    # Final block
    residual2 = SeparableConv2D(128, (3, 3), strides=(1, 1), padding='same', use_bias=False)(x)
    # residual2 = BatchNormalization()(residual2)
    # residual2 = Activation('relu')(residual2)
    residual2 = SeparableConv2D(256, (1, 1), strides=(1, 1), padding='same', use_bias=False)(residual2)
    residual2 = BatchNormalization()(residual2)
    # residual2 = Activation('relu')(residual2)
    # residual = cbam_block(residual)
    # block-5 shape
    # print("block-5 before res:", residual.shape)
    # residual = custom_attention(residual)
    # block-5 shape
    # print("block-5 after res:", residual.shape)

    model_c2 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_c2)
    print(f"Connection 2 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")

    x = SeparableConv2D(256, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    
    # 4th Inception and CBAM
#     x = inception_module(x, 256, 64, 256, 64, 64, 64)
#     x = se_block(x)
#     x = cbam_block(x)
#     x = self_attention_block(x)
    # block-5 shape
    # print("block-5:", x.shape)
    x = custom_attention(x)
    # block-5 shape
    # print("block-5:", x.shape)

    #Connection 2
    x = Add()([x, residual])
    #Connection 3
    x = Add()([x, residual2])
    
    model_c2a = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_c2a)
    print(f"After Adding Connection 2 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")

    x = SeparableConv2D(256, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    
    model_block5 = Model(inputs=img_input, outputs=x)
    total_params, trainable_params, non_trainable_params = count_model_params(model_block5)
    print(f"Block 5 Parameters: Total={total_params}, Trainable={trainable_params}, Non-trainable={non_trainable_params}")
    
#     #condition 1
#     x = SeparableConv2D(128, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = SeparableConv2D(2, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
    
#     flattened = Flatten()(x)

# # # Fully connected layer with softmax activation
#     output = Dense(2, activation='softmax')(flattened)
    
    #condition 2
    x = SeparableConv2D(2, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)
    
    output = Dense(num_classes, activation='softmax', name='predictions')(x)

    model = Model(img_input, output)
    return model


# def combine_4SC_2_1_V3(input_shape, num_classes, l2_regularization=0.01):
#     regularization = l2(l2_regularization)

#     # Block1 - 16 channels
#     img_input = Input(input_shape)
#     x = Conv2D(16, (7, 7), strides=(1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(img_input)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = Conv2D(16, (7, 7), strides=(1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = AveragePooling2D((3, 3), strides=(2, 2))(x)
#     x = se_block(x)
# #     x = cbam_block(x)
# #     x = self_attention_block(x)
#     x = Dropout(0.5)(x)

#     # Block2 - 32 channels
    
#     x = Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = SeparableConv2D(32, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = AveragePooling2D((3, 3), strides=(2, 2), padding='same')(x)
#     x = inception_module(x, 32, 24, 32, 24, 32, 32)
#     x = se_block(x)
# #     x = cbam_block(x)
# #     x = self_attention_block(x)    
#     x = Dropout(0.5)(x)

#     # Module 3 - 64 channels
#     residual = SeparableConv2D(32, (3, 3), strides=(1, 1),padding='same', use_bias=False)(x)
#     residual = AveragePooling2D((3, 3), strides=(2, 2), padding='same')(residual)
#     residual = SeparableConv2D(64, (1, 1), strides=(1, 1), padding='same', use_bias=False)(residual)
#     residual = BatchNormalization()(residual)
    
    
#     # 2nd Inception and CBAM
    
#     x = Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = SeparableConv2D(64, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = AveragePooling2D((3, 3),padding='same', strides=(2, 2))(x)
#     x = inception_module(x, 32, 16, 32, 16, 16, 16)
#     x = se_block(x)
# #     x = cbam_block(x)
# #     x = self_attention_block(x)
#     x = Dropout(0.5)(x)
    
#     #Connection 1
#     x = Add()([x, residual])

#     # Module 4 - 128 channels
 
#     x = Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = SeparableConv2D(128, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = AveragePooling2D((3, 3), padding='same', strides=(2, 2))(x)
#     x = inception_module(x, 128, 32, 128, 32, 32, 32)
#     x = se_block(x)
# #     x = cbam_block(x)
# #     x = self_attention_block(x)
    
#     x = Dropout(0.5)(x)

#     # Final block
#     residual = Conv2D(128, (3, 3), strides=(1, 1), padding='same', use_bias=False)(x)
#     residual = SeparableConv2D(256, (1, 1), strides=(1, 1), padding='same', use_bias=False)(residual)
#     residual = BatchNormalization()(residual)
    

    
#     x = SeparableConv2D(256, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
    
#     # 4th Inception and CBAM
#     x = inception_module(x, 256, 64, 256, 64, 64, 64)
#     x = se_block(x)
# #     x = cbam_block(x)
# #     x = self_attention_block(x)

#     x = SeparableConv2D(256, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
    
#     x = Add()([x, residual])
    
# #     #condition 1
#     x = SeparableConv2D(128, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
#     x = SeparableConv2D(2, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
#     x = BatchNormalization()(x)
#     x = Activation('relu')(x)
    
#     flattened = Flatten()(x)

# # Fully connected layer with softmax activation
#     output = Dense(2, activation='softmax')(flattened)
    
# #     #condition 2
# #     x = SeparableConv2D(2, (1, 1), padding='same', kernel_regularizer=regularization, use_bias=False)(x)
    
#     # x = GlobalAveragePooling2D()(x)
    
# #     output = Dense(num_classes, activation='softmax', name='predictions')(x)

#     model = Model(img_input, output)
#     return model

#SqueezeNet
def fire_module(input_fire, s1, e1, e3, weight_decay_l2, fireID):  
    '''
    A wrapper to build fire module
    
    # Arguments
        input_fire: input activations
        s1: number of filters for squeeze step
        e1: number of filters for 1x1 expansion step
        e3: number of filters for 3x3 expansion step
        weight_decay_l2: weight decay for conv layers
        fireID: ID for the module
    
    # Return
        Output activations
    '''
    
    # Squezee step
    output_squeeze = Convolution2D(
        s1, (1, 1), activation='relu', 
        kernel_initializer='glorot_uniform',
        kernel_regularizer=regularizers.l2(weight_decay_l2),
        padding='same', name='fire' + str(fireID) + '_squeeze',
        data_format="channels_last")(input_fire)
    # Expansion steps
    output_expand1 = Convolution2D(
        e1, (1, 1), activation='relu', 
        kernel_initializer='glorot_uniform',
        kernel_regularizer=regularizers.l2(weight_decay_l2),
        padding='same', name='fire' + str(fireID) + '_expand1',
        data_format="channels_last")(output_squeeze)
    output_expand2 = Convolution2D(
        e3, (3, 3), activation='relu',
        kernel_initializer='glorot_uniform',
        kernel_regularizer=regularizers.l2(weight_decay_l2),
        padding='same', name='fire' + str(fireID) + '_expand2',
        data_format="channels_last")(output_squeeze)
    # Merge expanded activations
    output_fire = Concatenate(axis=3)([output_expand1, output_expand2])
    return output_fire

def SqueezeNet(input_shape, num_classes, weight_decay_l2=0.0001):
    '''
    A wrapper to build SqueezeNet Model
    
    # Arguments
        num_classes: number of classes defined for classification task
        weight_decay_l2: weight decay for conv layers
        inputs: input image dimensions
    
    # Return
        A SqueezeNet Keras Model
    '''
    input_img = Input(shape=input_shape)
    
    conv1 = Convolution2D(
        32, (7, 7), activation='relu', kernel_initializer='glorot_uniform',
        strides=(2, 2), padding='same', name='conv1',
        data_format="channels_last")(input_img)
    
    maxpool1 = MaxPooling2D(
        pool_size=(2, 2), strides=(2, 2), name='maxpool1',
        data_format="channels_last")(conv1)
    
    fire2 = fire_module(maxpool1, 8, 16, 16, weight_decay_l2, 2)    
    fire3 = fire_module(fire2, 8, 16, 16, weight_decay_l2, 3)
    fire4 = fire_module(fire3, 16, 32, 32, weight_decay_l2, 4)
    
    maxpool4 = MaxPooling2D(
        pool_size=(2, 2), strides=(2, 2), name='maxpool4',
        data_format="channels_last")(fire4)
    
    fire5 = fire_module(maxpool4, 16, 32, 32, weight_decay_l2, 5)
    fire6 = fire_module(fire5, 32, 64, 64, weight_decay_l2, 6)
    fire7 = fire_module(fire6, 32, 64, 64, weight_decay_l2, 7)
    fire8 = fire_module(fire7, 64, 128, 128, weight_decay_l2, 8)
    
    maxpool8 = MaxPooling2D(
        pool_size=(2, 2), strides=(2, 2), name='maxpool8',
        data_format="channels_last")(fire8)
    
    fire9 = fire_module(maxpool8, 64, 128, 128, weight_decay_l2, 9)
    fire9_dropout = Dropout(0.5, name='fire9_dropout')(fire9)
    
    conv10 = Convolution2D(
        num_classes, (1, 1), activation='relu', kernel_initializer='glorot_uniform',
        padding='valid', name='conv10',
        data_format="channels_last")(fire9_dropout)

    global_avgpool10 = GlobalAveragePooling2D(data_format='channels_last')(conv10)
    softmax = Activation("softmax", name='softmax')(global_avgpool10)
    
    return Model(inputs=input_img, outputs=softmax)


# MobileNet V2
def MobileNetV2_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_MobileNet_1 = MobileNetV2(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_MobileNet_1.layers:
        layer.trainable = False
        
    x = model_MobileNet_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_MobileNet_1.input, output = predictions)
    
    return model

# NASNetMobile
def NASNetMobile_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_NASNetMobile_1 = NASNetMobile(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_NASNetMobile_1.layers:
        layer.trainable = False
        
    x = model_NASNetMobile_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_NASNetMobile_1.input, output = predictions)
    
    return model
# DenseNet121
def DenseNet121_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_DenseNet121_1 = DenseNet121(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_DenseNet121_1.layers:
        layer.trainable = False
        
    x = model_DenseNet121_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_DenseNet121_1.input, output = predictions)
    
    return model
# InceptionV3
def InceptionV3_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_InceptionV3_1 = InceptionV3(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_InceptionV3_1.layers:
        layer.trainable = False
        
    x = model_InceptionV3_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_InceptionV3_1.input, output = predictions)
    
    return model


#InceptionV3
# Model 16   
def VGG13_LRELU_2(input_shape, num_classes):
    #model = Sequential()
    
    img_input = Input(input_shape)

    x = Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(img_input)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.4)(x)
    # layer2 48*48*64
    x = Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.4)(x)

    

    # layer3 24*24*64
    x = Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.4)(x)
    # layer4 24*24*128
    x = Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.4)(x)

    

    # layer5 12*12*128
    x = Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.4)(x)
    # layer6 12*12*256
    x = Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    # layer7 12*12*256
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.4)(x)

    

    # layer8 6*6*256
    x = Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.4)(x)
    # layer9 6*6*512
    x = Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = BatchNormalization()(x)
    # layer10 6*6*512
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.4)(x)

    
    # layer11 3*3*512
    #x = Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    #x = LeakyReLU(alpha=0.01)(x)
    #x = BatchNormalization()(x)
    #x = Dropout(0.4)(x)
    # layer12 3*3*512
    #x = Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(0.01))(x)
    #x = LeakyReLU(alpha=0.01)(x)
    #x = BatchNormalization()(x)
    # layer13 3*3*512
    #x = MaxPooling2D(pool_size=(2, 2))(x)
    #x = Dropout(0.4)(x)


    # layer14 1*1*512
    x = GlobalAveragePooling2D()(x)
    #x = Flatten()(x)  
    
    #x = Dropout(0.4)(x)
    #x = Dense(128)(x)
    x = Dense(1024)(x)
    x = LeakyReLU(alpha=0.01)(x)
    x = Dropout(0.5)(x)

    x = Dense(num_classes, activation='softmax')(x)

    model = Model(img_input, x)
    return model

# Xception
def Xception_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_Xception_1 = VGG16(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_Xception_1.layers:
        layer.trainable = False
        
    x = model_Xception_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_Xception_1.input, output = predictions)
    
    return model

# VGG19
def VGG19_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_vgg19_1 = VGG19(weights="imagenet", include_top=False)
    
    # Freeze all layers of the pre-trained model
    for layer in model_vgg19_1.layers:
    	layer.trainable = False
    	
    
        
    x = model_vgg19_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_vgg19_1.input, output = predictions)
    
    return model
# MobileNet
def MobileNet_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_MobileNet_1 = MobileNet(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_MobileNet_1.layers:
        layer.trainable = False
        
    x = model_MobileNet_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_MobileNet_1.input, output = predictions)
    
    return model
# VGG16
def VGG16_MODEL(input_shape, num_classes):
    # Remove fully connected layer and replace
    # with softmax for classifying 10 classes
    model_vgg16_1 = VGG16(weights="imagenet", include_top=False)

    # Freeze all layers of the pre-trained model
    for layer in model_vgg16_1.layers:
        layer.trainable = False
        
    x = model_vgg16_1.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation = 'softmax')(x)

    model = Model(input = model_vgg16_1.input, output = predictions)
    
    return model

# Lenet
def Lenet(input_shape, num_classes):
    # initialize the model
    model = Sequential()
    #Input(input_shape)

    # if we are using "channel_first", update the input shape
    #if K.image_data_format() == "channel_first":
    #    inputShape = (depth, height, width)

    # first set of CONV => RELU => POOL layers
    model.add(Conv2D(20, (5, 5), padding = "same", input_shape=input_shape))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2)))

    # second set of CONV => RELU => POOL layers
    model.add(Conv2D(50, (5, 5), padding = "same"))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2)))

    # first set of FC => RELU layers
    model.add(Flatten())
    model.add(Dense(500))
    model.add(Activation("relu"))

    # softmax classififer
    model.add(Dense(num_classes))
    model.add(Activation("softmax"))

    # return the constructed network architecture
    return model