forked from TarinZ/whale-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netDefinition.py
executable file
·1004 lines (796 loc) · 30.8 KB
/
netDefinition.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
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
from dependencies import *
from helperFunctions import *
class Net(nn.Module):
"""
The class that defines the graph architecture of the DNN to be used. Definition of convolutional
layers, fully connected layers, batch-normalization operations, etc are created here.
Args:
None: Network definition within the initialization routine.
Returns:
Instantiates a Net object, with member function forward_prop. The forward_prop member function
input [x] takes in the input signal, and returns the output signal at the output of the net. """
def __init__(self, arch, w_init_scheme = 'He', bias_inits = 0.0, incep_layers = -1, nEmbed = -1, nClasses = -1):
super(Net, self).__init__()
# Initialization scheme
self.weightsInitsScheme = w_init_scheme
self.biasesInitTo = bias_inits
self.N_incep_layers = incep_layers
# Save off the selected DNN arch as a member.
self.arch = arch
# Dictionary of supported DNN architectures.
switcher = {'cnn_108x108': self.cnn_108x108,
'inceptionModuleV1_108x108': self.inceptionModuleV1_108x108,
'inceptionModuleV1_75x45': self.inceptionModuleV1_75x45,
'inceptionTwoModulesV1_75x45': self.inceptionTwoModulesV1_75x45,
'inceptionTwoModulesV1_root1_75x45': self.inceptionTwoModulesV1_root1_75x45,
'inceptionV1_modularized': self.inceptionV1_modularized,
'inceptionV1_modularized_mnist': self.inceptionV1_modularized_mnist,
'centerlossSimple': self.centerlossSimple
}
# Select the net definition given by arch.
netDefinition = switcher.get(arch)
# Initialize the architecture selected.
try:
if self.arch == 'centerlossSimple':
assert(nEmbed > 0)
assert(nClasses > 0)
netDefinition(nEmbed, nClasses)
else:
netDefinition()
print "DNN arch: ", self.arch
except:
print 'Specified DNN architecture not implemented.'
sys.exit()
# How the percentage of each layer's trainable parameters as a
# function of the total number of trainable parameters
self.show_layer_parameter_percentages()
# Initialize the DNN with the specified scheme.
self.initialize_layers()
######################################## Member functions ########################################
def show_layer_parameter_percentages(self):
# Compute the total number of learnable parameters:
paramIterator = list(self.parameters())
self.N_dnnParameters = 0
for pp in paramIterator:
if len(pp.size()) == 1:
self.N_dnnParameters += pp.size()[0]
else:
self.N_dnnParameters += np.prod(pp.size())
# Show number of learnable parameters as function of module.
self.N_runningParams = 0
for m in self.modules():
if isinstance(m, Net):
continue
elif isinstance(m, nn.Sequential):
print '\n'
continue
elif isinstance(m, nn.ModuleList):
print '\n'
continue
else:
params = list(m.parameters())
N_lenParams = len(params)
N_currentParams = 0
if N_lenParams > 0:
for pp in xrange(N_lenParams):
N_currentParams += np.prod(params[pp].size())
else:
N_currentParams = 0
self.N_runningParams += N_currentParams
print ('Module: ' + m.__class__.__name__ + ' params: %2.2f')%(100.0*N_currentParams / float(self.N_dnnParameters))
print ('\n')
print ('Total number of trainable parameters: %5d\n')%(self.N_dnnParameters)
assert(self.N_runningParams == self.N_dnnParameters)
def initialize_layers(self):
print ('Initializing layers via: ' + self.weightsInitsScheme + ', biases to: %2.2f')%(self.biasesInitTo)
# Initialize selected layers
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
# Compute the fan in.
if isinstance(m, nn.Conv2d):
fanIn = m.in_channels * m.weight.size()[2] * m.weight.size()[3]
elif isinstance(m, nn.Linear):
fanIn = m.in_features
# Print the layer:
print ('Module: ' + m.__class__.__name__ + ' FanIn: %1d')%(fanIn)
# Perform initialization on the weights.
if self.weightsInitsScheme == 'He':
m.weight.data.normal_(0, np.sqrt(2.0 / fanIn))
# Perform initializatio of the biases
try:
m.bias.data.fill_(self.biasesInitTo)
except:
print "No biases found for this layer."
# Definition of the forward prop
def forward(self, x):
if self.arch == 'cnn_108x108':
x = self.cnn(x)
x = x.view(-1, self.num_flat_features(x))
x = self.classifier(x)
return x
elif self.arch == 'inceptionModuleV1_108x108' or self.arch == 'inceptionModuleV1_75x45':
root = self.root(x)
b_1x1 = self.b_1x1(root)
b_3x3 = self.b_3x3(root)
b_5x5 = self.b_5x5(root)
b_pool = self.b_pool(root)
concat = torch.cat( (b_1x1, b_3x3, b_5x5, b_pool), 1)
redux = self.redux(concat)
redux = redux.view(-1, self.num_flat_features(redux))
logits = self.classifier(redux)
return logits
elif self.arch == 'inceptionTwoModulesV1_75x45':
root = self.root(x)
b_1x1 = self.b_1x1(root)
b_3x3 = self.b_3x3(root)
b_5x5 = self.b_5x5(root)
b_pool = self.b_pool(root)
concat = torch.cat( (b_1x1, b_3x3, b_5x5, b_pool), 1)
b2_1x1 = self.b2_1x1(concat)
b2_3x3 = self.b2_3x3(concat)
b2_5x5 = self.b2_5x5(concat)
b2_pool = self.b2_pool(concat)
concat2 = torch.cat( (b2_1x1, b2_3x3, b2_5x5, b2_pool), 1)
redux = self.redux(concat2)
redux = redux.view(-1, self.num_flat_features(redux))
logits = self.classifier(redux)
return logits
elif self.arch == 'inceptionTwoModulesV1_root1_75x45':
root = self.root(x)
b_1x1 = self.b_1x1(root)
b_3x3 = self.b_3x3(root)
b_5x5 = self.b_5x5(root)
b_pool = self.b_pool(root)
concat = torch.cat( (b_1x1, b_3x3, b_5x5, b_pool), 1)
b2_1x1 = self.b2_1x1(concat)
b2_3x3 = self.b2_3x3(concat)
b2_5x5 = self.b2_5x5(concat)
b2_pool = self.b2_pool(concat)
concat2 = torch.cat( (b2_1x1, b2_3x3, b2_5x5, b2_pool), 1)
redux = self.redux(concat2)
redux = redux.view(-1, self.num_flat_features(redux))
logits = self.classifier(redux)
return logits
elif (self.arch == 'inceptionV1_modularized') or (self.arch =='inceptionV1_modularized_mnist'):
# Forward prop through the root first.
# pdb.set_trace()
root = self.root(x)
# Loop through each each inception layer
for ii in xrange(self.N_incep_layers):
# If processing the next inception layer, the new 'root' signal is the inception output (incepOut) of the previous layer.
if ii > 0:
root = incepOut
# Loop through each branch of the current inception layer.
for bb in xrange(len(self.masterList[ii])):
temp = self.masterList[ii][bb](root)
if bb == 0:
incepOut = temp
else:
incepOut = torch.cat((incepOut, temp), 1)
# Forward through the redux layer.
redux = self.redux(incepOut)
redux = redux.view(-1, self.num_flat_features(redux))
# Forward through the final fully connected layer.
self.x = self.fc1(redux)
# Save intermediate x
# self.x = x.cpu().data.numpy()
# reld = self.r1(self.x)
# logits = self.fc2(reld)
logits = self.fc2(self.x)
# pdb.set_trace()
# Return logits
return logits
elif self.arch == 'centerlossSimple':
root = self.root(x)
root = root.view(-1, self.num_flat_features(root))
self.x = self.latent(root)
logits = self.logits(self.x)
return logits
# Helper function to flatten an input vector from
def num_flat_features(self, x):
# all dimensions except the batch dimension
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
def centerlossSimple(self, nEmbed, nClasses):
# Initialize the centroids:
self.centroids = torch.from_numpy(0.01*np.random.randn(nEmbed, nClasses)).cuda(0)
# The root
self.root = nn.Sequential(
nn.Conv2d(1,32, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(32,32, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 32x14x14
# input: 32x14x14
nn.Conv2d(32, 64, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 64x7x7
# input: 64x7x7
nn.Conv2d(64, 128, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, 5, stride = (1,1), padding = (2,2), bias=True),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 128x3x3
)
# input: 128x3x3
self.latent = nn.Linear(1152, nEmbed, bias=True)
# output: nEmbed x 1
# input: nEmbedx1
self.logits = nn.Linear(nEmbed, nClasses, bias=False)
# output: nClasses x 1
"""inceptionV1_modularized_mnist """
def inceptionV1_modularized_mnist(self):
# Initialize the centroids:
self.centroids = torch.from_numpy(np.random.randn(2, 10)).cuda(0)
# self.cGradients = torch.zeros(2,10).cuda(0)
# self.centroids = Variable(torch.from_numpy(np.random.randn(2, 10)), requires_grad=False ).cuda(0)
# self.centroids = torch.random(2,10)
# pdb.set_trace()
assert(isinstance(self.N_incep_layers, int))
if self.N_incep_layers <= 0:
print "Selected inceptionV1_modularized, but number of inception layers wanted is less than 0."
sys.exit()
# Root layers
self.root = nn.Sequential(
# input: 1x28x28
nn.Conv2d(1,64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 1x14x14
# input: 1x14x14
nn.Conv2d(64, 256, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True)
# output: 256x14x14
)
# Inception Chains.
self.masterList = nn.ModuleList()
for ii in xrange(self.N_incep_layers):
incep = nn.ModuleList()
incep += self.create_inception_module_v1()
self.masterList += [incep]
# Redux layers
self.redux = nn.Sequential(
# input: 256x14x14
nn.Conv2d(256, 64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace = True),
# output: 64x14x14
# input: 64x14x14
nn.Conv2d(64, 32, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# # output: 32x7x7
# input: 32x7x7
nn.Conv2d(32, 16, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 16x3x3
# input: 16x3x3
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(4),
nn.ReLU(inplace = True)
# output: 4x3x3
)
self.fc1 = nn.Linear(36, 2)
self.fc2 = nn.Linear(2, 10, bias=False)
return None
"""inceptionV1_modularized """
def inceptionV1_modularized(self):
# pdb.set_trace()
assert(isinstance(self.N_incep_layers, int))
if self.N_incep_layers <= 0:
print "Selected inceptionV1_modularized, but number of inception layers wanted is less than 0."
sys.exit()
# Root layers
self.root = nn.Sequential(
# input: 1x75x45
nn.Conv2d(1,64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 1x37x22
# input: 1x37x22
nn.Conv2d(64, 256, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True)
# output: 192x18x11
)
# Inception Chains.
self.masterList = nn.ModuleList()
for ii in xrange(self.N_incep_layers):
incep = nn.ModuleList()
incep += self.create_inception_module_v1()
self.masterList += [incep]
# Redux layers
self.redux = nn.Sequential(
# input: 256x37x22
nn.Conv2d(256, 64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace = True),
# output: 64x18x11
# input: 64x18x11
nn.Conv2d(64, 32, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# # output: 32x9x5
# input: 32x9x5
nn.Conv2d(32, 16, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 16x4x2
# input: 16x4x2
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(4),
nn.ReLU(inplace = True)
# output: 4x4x2
)
self.classifier = nn.Sequential(
# input: 1x32
nn.Linear(180, 2)
# output: 1x2
)
return None
"""create_inception_module_v1"""
def create_inception_module_v1(self):
# First inception v1 module
b_1x1 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(256, 64, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x37x22
)
b_3x3 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(256, 96, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x37x22
)
b_5x5 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(256, 16, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
b_pool = nn.Sequential(
# input: 192x37x22
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(256, 32, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
# Combine the inception branches into a list and return. List consumed by nn.ModuleList()
return [b_1x1, b_3x3, b_5x5, b_pool]
"""Definition of the cnn_108x108 arch."""
def cnn_108x108(self):
self.cnn = nn.Sequential(
nn.Conv2d(1, 8, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(8),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
nn.Conv2d(8, 16, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
nn.Conv2d(16, 32, 3, stride = (1,1), padding = (0,0)),
nn.Dropout2d(),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
nn.Conv2d(32, 64, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
nn.Conv2d(64, 64, 3, stride = (3,3), padding = (0,0)),
nn.Dropout2d(),
nn.ReLU(inplace=True)
)
self.classifier = nn.Sequential(
nn.Linear(256, 32),
nn.Dropout(),
nn.Linear(32, 16),
nn.Linear(16, 2)
)
# Definition of the inceptionModuleV1_108x108 arch.
def inceptionModuleV1_108x108(self):
self.root = nn.Sequential(
# input: 1x108x108
nn.Conv2d(1, 64, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
nn.Conv2d(64, 192, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2))
# output: 192x27x27
)
self.b_1x1 = nn.Sequential(
# input: 192x27x27
nn.Conv2d(192, 64, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x27x27
)
self.b_3x3 = nn.Sequential(
# input: 192x27x27
nn.Conv2d(192, 96, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x27x27
)
self.b_5x5 = nn.Sequential(
# input: 192x27x27
nn.Conv2d(192, 16, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x27x27
)
self.b_pool = nn.Sequential(
# input: 192x27x27
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(192, 32, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x27x27
)
self.redux = nn.Sequential(
# input: 256x27x27
nn.Conv2d(256, 64, 2, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 64x14x14
# input: 64x14x14
nn.Conv2d(64, 32, 1, stride = (1,1), padding = (0,0)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 32x7x7
# input: 32x7x7
nn.Conv2d(32, 16, 1, stride = (1,1), padding = (0,0)),
nn.ReLU(inplace = True),
nn.MaxPool2d((3,3), stride = (2,2), padding = (0,0)),
# output: 16x3x3
# input: 16x3x3
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0)),
nn.ReLU(inplace = True)
# output: 4x3x3
)
self.classifier = nn.Sequential(
# input: 1x36
nn.Linear(36, 2)
# output: 1x2
)
# Definition of the inceptionModuleV1_75x45 arch.
def inceptionModuleV1_75x45(self):
# pdb.set_trace()
self.root = nn.Sequential(
# input: 1x75x45
nn.Conv2d(1, 64, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 1x37x22
# input: 1x37x22
nn.Conv2d(64, 192, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2))
# output: 192x18x11
)
self.b_1x1 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 64, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x18x11
)
self.b_3x3 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 96, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x18x11
)
self.b_5x5 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 16, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
self.b_pool = nn.Sequential(
# input: 192x18x11
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(192, 32, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
self.redux = nn.Sequential(
# input: 256x18x11
nn.Conv2d(256, 64, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
# output: 64x18x11
# input: 64x18x11
nn.Conv2d(64, 32, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# # output: 32x9x5
# input: 32x9x5
nn.Conv2d(32, 16, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 16x4x2
# input: 16x4x2
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0)),
nn.ReLU(inplace = True)
# output: 4x4x2
)
self.classifier = nn.Sequential(
# input: 1x32
nn.Linear(32, 2)
# output: 1x2
)
# Definition of the inceptionTwoModulesV1_75x45 arch.
def inceptionTwoModulesV1_75x45(self):
# pdb.set_trace()
self.root = nn.Sequential(
# input: 1x75x45
nn.Conv2d(1, 64, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 1x37x22
# input: 1x37x22
nn.Conv2d(64, 192, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2))
# output: 192x18x11
)
# First inception v1 module
self.b_1x1 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 64, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x18x11
)
self.b_3x3 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 96, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x18x11
)
self.b_5x5 = nn.Sequential(
# input: 192x18x11
nn.Conv2d(192, 16, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
self.b_pool = nn.Sequential(
# input: 192x18x11
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(192, 32, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
# Second inception v1 module
self.b2_1x1 = nn.Sequential(
# input: 256x18x11
nn.Conv2d(256, 64, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x18x11
)
self.b2_3x3 = nn.Sequential(
# input: 256x18x11
nn.Conv2d(256, 96, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1)),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x18x11
)
self.b2_5x5 = nn.Sequential(
# input: 256x18x11
nn.Conv2d(256, 16, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
self.b2_pool = nn.Sequential(
# input: 256x18x11
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(256, 32, 1, stride = (1,1), padding = (0,0)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x18x11
)
self.redux = nn.Sequential(
# input: 256x18x11
nn.Conv2d(256, 64, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
# output: 64x18x11
# input: 64x18x11
nn.Conv2d(64, 32, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# # output: 32x9x5
# input: 32x9x5
nn.Conv2d(32, 16, 3, stride = (1,1), padding = (1,1)),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 16x4x2
# input: 16x4x2
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0)),
nn.ReLU(inplace = True)
# output: 4x4x2
)
self.classifier = nn.Sequential(
# input: 1x32
nn.Linear(32, 2)
# output: 1x2
)
# Definition of the inceptionTwoModulesV1_root1_75x45 arch.
def inceptionTwoModulesV1_root1_75x45(self):
self.root = nn.Sequential(
# input: 1x75x45
nn.Conv2d(1,64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((2,2), stride=(2,2)),
# output: 1x37x22
# input: 1x37x22
nn.Conv2d(64, 192, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(192),
nn.ReLU(inplace=True)
# output: 192x18x11
)
# First inception v1 module
self.b_1x1 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(192, 64, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x37x22
)
self.b_3x3 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(192, 96, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x37x22
)
self.b_5x5 = nn.Sequential(
# input: 192x37x22
nn.Conv2d(192, 16, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
self.b_pool = nn.Sequential(
# input: 192x37x22
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(192, 32, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
# Second inception v1 module
self.b2_1x1 = nn.Sequential(
# input: 256x37x22
nn.Conv2d(256, 64, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
# output: 64x37x22
)
self.b2_3x3 = nn.Sequential(
# input: 256x37x22
nn.Conv2d(256, 96, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 128, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
# output: 128x37x22
)
self.b2_5x5 = nn.Sequential(
# input: 256x37x22
nn.Conv2d(256, 16, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 32, 5, stride = (1,1), padding = (2,2), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
self.b2_pool = nn.Sequential(
# input: 256x37x22
nn.MaxPool2d((3,3), stride = (1, 1), padding = (1,1)),
nn.Conv2d(256, 32, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
# output: 32x37x22
)
self.redux = nn.Sequential(
# input: 256x37x22
nn.Conv2d(256, 64, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace = True),
# output: 64x18x11
# input: 64x18x11
nn.Conv2d(64, 32, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(32),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# # output: 32x9x5
# input: 32x9x5
nn.Conv2d(32, 16, 3, stride = (1,1), padding = (1,1), bias=False),
nn.BatchNorm2d(16),
nn.ReLU(inplace = True),
nn.MaxPool2d((2,2), stride = (2,2)),
# output: 16x4x2
# input: 16x4x2
nn.Conv2d(16, 4, 1, stride = (1,1), padding = (0,0), bias=False),
nn.BatchNorm2d(4),
nn.ReLU(inplace = True)
# output: 4x4x2
)
self.classifier = nn.Sequential(
# input: 1x32
nn.Linear(180, 2)
# output: 1x2
)