-
Notifications
You must be signed in to change notification settings - Fork 5
/
standard_neural_network_architectures.py
1168 lines (945 loc) · 50.9 KB
/
standard_neural_network_architectures.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
997
998
999
1000
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class Conv2dNormLeakyReLU(nn.Module):
def __init__(self, input_shape, num_filters, kernel_size, dilation=1, stride=1, groups=1, padding=0, use_bias=False,
normalization=True, weight_attention=False):
super(Conv2dNormLeakyReLU, self).__init__()
self.input_shape = list(input_shape)
self.num_filters = num_filters
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.use_bias = use_bias
self.normalization = normalization
self.dilation = dilation
self.weight_attention = weight_attention
self.groups = groups
self.num_mac = None
self.layer_dict = nn.ModuleDict()
self.build_network()
def build_network(self):
x = torch.ones(self.input_shape)
out = x
self.layer_dict['conv'] = nn.Conv2d(in_channels=out.shape[1], out_channels=self.num_filters,
kernel_size=self.kernel_size, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups, bias=self.use_bias)
out = self.layer_dict['conv'].forward(out)
if self.normalization:
self.layer_dict['norm_layer'] = nn.BatchNorm2d(num_features=out.shape[1])
out = self.layer_dict['norm_layer'](out)
self.layer_dict['relu'] = nn.LeakyReLU()
out = self.layer_dict['relu'](out)
print(out.shape)
def forward(self, x):
out = x
out = self.layer_dict['conv'].forward(out)
if self.normalization:
out = self.layer_dict['norm_layer'](out)
out = self.layer_dict['relu'](out)
return out
class DenseNetActivationNormNetwork(nn.Module):
def __init__(self, im_shape, num_filters, num_stages, num_blocks_per_stage, dropout_rate, average_pool_output,
reduction_rate, conv_type):
"""
Builds a multilayer convolutional network. It also provides functionality for passing external parameters to be
used at inference time. Enables inner loop optimization readily.
:param im_shape: The input image batch shape.
:param num_output_classes: The number of output classes of the network.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run this on.
:param meta_classifier: A flag indicating whether the system's meta-learning (inner-loop) functionalities should
be enabled.
"""
super(DenseNetActivationNormNetwork, self).__init__()
self.input_shape = list(im_shape)
self.num_filters = num_filters
self.num_stages = num_stages
self.dropout_rate = dropout_rate
self.reduction_rate = reduction_rate
self.average_pool_output = average_pool_output
self.conv_type = conv_type
# self.num_output_classes = num_output_classes
self.num_blocks_per_stage = num_blocks_per_stage
self.layer_dict = nn.ModuleDict()
self.build_network()
def build_network(self):
"""
Builds the network before inference is required by creating some dummy inputs with the same input as the
self.im_shape tuple. Then passes that through the network and dynamically computes input shapes and
sets output shapes for each layer.
"""
x = torch.zeros(self.input_shape)
out = x
self.layer_dict['stem_conv'] = self.conv_type(input_shape=out.shape, num_filters=64,
kernel_size=3, padding=1)
out = self.layer_dict['stem_conv'](out)
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)] = self.conv_type(input_shape=out.shape,
num_filters=self.num_filters,
kernel_size=1, padding=0)
cur = self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)](out)
self.layer_dict['conv_{}_{}'.format(i, j)] = self.conv_type(input_shape=cur.shape,
num_filters=self.num_filters,
kernel_size=3, padding=1)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](cur)
cur = F.dropout(cur, p=self.dropout_rate, training=True)
out = torch.cat([out, cur], dim=1)
out = F.avg_pool2d(out, 2)
print(out.shape)
self.layer_dict['transition_layer_{}'.format(i)] = self.conv_type(input_shape=out.shape,
num_filters=int(out.shape[
1] * self.reduction_rate),
kernel_size=1, padding=0)
out = self.layer_dict['transition_layer_{}'.format(i)](out)
if self.average_pool_output:
out = F.avg_pool2d(out, out.shape[2])
out = out.view(out.shape[0], -1)
else:
out = F.adaptive_avg_pool2d(out, output_size=(5, 5))
# self.layer_dict['adaptor_layer'] = Conv2dNormLeakyReLU(input_shape=out.shape,
# num_filters=64,
# kernel_size=1, padding=0)
# out = self.layer_dict['adaptor_layer'].forward(out)
print(out.shape)
def forward(self, x, dropout_training):
"""
Forward propages through the network. If any params are passed then they are used instead of stored params.
:param x: Input image batch.
:param num_step: The current inner loop step number
:param params: If params are None then internal parameters are used. If params are a dictionary with keys the
same as the layer names then they will be used instead.
:param training: Whether this is training (True) or eval time.
:param backup_running_statistics: Whether to backup the running statistics in their backup store. Which is
then used to reset the stats back to a previous state (usually after an eval loop, when we want to throw away stored statistics)
:return: Logits of shape b, num_output_classes.
"""
out = x
out = self.layer_dict['stem_conv'](out)
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
cur = self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)](out)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](cur)
cur = F.dropout(cur, p=self.dropout_rate, training=dropout_training)
out = torch.cat([out, cur], dim=1)
out = F.avg_pool2d(out, 2)
out = self.layer_dict['transition_layer_{}'.format(i)](out)
if self.average_pool_output:
out = F.avg_pool2d(out, out.shape[2])
out = out.view(out.shape[0], -1)
else:
out = F.adaptive_avg_pool2d(out, output_size=(5, 5))
# out = self.layer_dict['adaptor_layer'].forward(out)
return out
class SqueezeExciteDenseNet(nn.Module):
def __init__(self, im_shape, num_filters, num_stages, num_blocks_per_stage, dropout_rate, average_pool_output,
reduction_rate, output_spatial_dim, use_channel_wise_attention):
"""
Builds a multilayer convolutional network. It also provides functionality for passing external parameters to be
used at inference time. Enables inner loop optimization readily.
:param im_shape: The input image batch shape.
:param num_output_classes: The number of output classes of the network.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run this on.
:param meta_classifier: A flag indicating whether the system's meta-learning (inner-loop) functionalities should
be enabled.
"""
super(SqueezeExciteDenseNet, self).__init__()
self.input_shape = list(im_shape)
self.num_filters = num_filters
self.num_stages = num_stages
self.dropout_rate = dropout_rate
self.reduction_rate = reduction_rate
self.average_pool_output = average_pool_output
# self.num_output_classes = num_output_classes
self.num_blocks_per_stage = num_blocks_per_stage
self.output_spatial_dim = output_spatial_dim
self.conv_type = Conv2dNormLeakyReLU
self.layer_dict = nn.ModuleDict()
self.use_channel_wise_attention = use_channel_wise_attention
self.build_network()
def build_network(self):
"""
Builds the network before inference is required by creating some dummy inputs with the same input as the
self.im_shape tuple. Then passes that through the network and dynamically computes input shapes and
sets output shapes for each layer.
"""
x = torch.zeros(self.input_shape)
out = x
self.layer_dict['stem_conv'] = Conv2dNormLeakyReLU(input_shape=out.shape, num_filters=64,
kernel_size=3, padding=1, groups=1)
out = self.layer_dict['stem_conv'](out)
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
if self.use_channel_wise_attention:
attention_network_out = F.avg_pool2d(out, out.shape[-1]).squeeze()
self.layer_dict['channel_wise_attention_output_fcc_{}_{}'.format(j, i)] = nn.Linear(
in_features=attention_network_out.shape[1], out_features=out.shape[1], bias=True)
channel_wise_attention_regions = self.layer_dict[
'channel_wise_attention_output_fcc_{}_{}'.format(j, i)].forward(attention_network_out)
channel_wise_attention_regions = F.sigmoid(channel_wise_attention_regions)
out = out * channel_wise_attention_regions.unsqueeze(2).unsqueeze(2)
self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)] = self.conv_type(input_shape=out.shape,
num_filters=self.num_filters,
kernel_size=1, padding=0)
cur = self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)](out)
self.layer_dict['conv_{}_{}'.format(i, j)] = self.conv_type(input_shape=cur.shape,
num_filters=self.num_filters,
kernel_size=3, padding=1, groups=1)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](cur)
cur = F.dropout(cur, p=self.dropout_rate, training=True)
out = torch.cat([out, cur], dim=1)
out = F.avg_pool2d(out, 2)
print(out.shape)
self.layer_dict['transition_layer_{}'.format(i)] = Conv2dNormLeakyReLU(input_shape=out.shape,
num_filters=int(out.shape[
1] * self.reduction_rate),
kernel_size=1, padding=0)
out = self.layer_dict['transition_layer_{}'.format(i)](out)
if self.average_pool_output:
out = F.avg_pool2d(out, out.shape[2])
out = out.view(out.shape[0], -1)
else:
out = F.adaptive_avg_pool2d(out, output_size=(self.output_spatial_dim, self.output_spatial_dim))
print(out.shape)
def forward(self, x, dropout_training):
"""
Forward propages through the network. If any params are passed then they are used instead of stored params.
:param x: Input image batch.
:param num_step: The current inner loop step number
:param params: If params are None then internal parameters are used. If params are a dictionary with keys the
same as the layer names then they will be used instead.
:param training: Whether this is training (True) or eval time.
:param backup_running_statistics: Whether to backup the running statistics in their backup store. Which is
then used to reset the stats back to a previous state (usually after an eval loop, when we want to throw away stored statistics)
:return: Logits of shape b, num_output_classes.
"""
out = x
out = self.layer_dict['stem_conv'](out)
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
# out_channels = F.avg_pool2d(out, out.shape[-1]).squeeze()
if self.use_channel_wise_attention:
out_channels = F.avg_pool2d(out, out.shape[-1]).squeeze()
channel_wise_attention_regions = self.layer_dict[
'channel_wise_attention_output_fcc_{}_{}'.format(j, i)].forward(out_channels)
channel_wise_attention_regions = F.sigmoid(channel_wise_attention_regions)
out = out * channel_wise_attention_regions.unsqueeze(2).unsqueeze(2)
cur = self.layer_dict['conv_bottleneck_{}_{}'.format(i, j)](out)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](cur)
cur = F.dropout(cur, p=self.dropout_rate, training=dropout_training)
out = torch.cat([out, cur], dim=1)
out = F.avg_pool2d(out, 2)
out = self.layer_dict['transition_layer_{}'.format(i)](out)
if self.average_pool_output:
out = F.avg_pool2d(out, out.shape[2])
out = out.view(out.shape[0], -1)
else:
out = F.adaptive_avg_pool2d(out, output_size=(self.output_spatial_dim, self.output_spatial_dim))
return out
class Conv1dNormLeakyReLU(nn.Module):
def __init__(self, input_shape, num_filters, kernel_size, dilation=1, stride=1, groups=1, padding=0, use_bias=False,
normalization=True):
super(Conv1dNormLeakyReLU, self).__init__()
self.input_shape = list(input_shape)
self.num_filters = num_filters
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.use_bias = use_bias
self.normalization = normalization
self.dilation = dilation
self.groups = groups
self.layer_dict = nn.ModuleDict()
self.build_network()
def build_network(self):
x = torch.ones(self.input_shape)
out = x
self.layer_dict['conv'] = nn.Conv1d(in_channels=out.shape[1], out_channels=self.num_filters,
kernel_size=self.kernel_size, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups, bias=self.use_bias)
out = self.layer_dict['conv'](out)
if self.normalization:
self.layer_dict['norm_layer'] = nn.BatchNorm1d(num_features=out.shape[1])
out = self.layer_dict['norm_layer'](out)
self.layer_dict['relu'] = nn.LeakyReLU()
out = self.layer_dict['relu'](out)
print(out.shape)
def forward(self, x):
out = x
out = self.layer_dict['conv'](out)
if self.normalization:
out = self.layer_dict['norm_layer'](out)
out = self.layer_dict['relu'](out)
return out
class DilatedDenseNetActivationNormNetwork(nn.Module):
def __init__(self, im_shape, num_filters, num_stages, num_blocks_per_stage, per_param_biases=False):
"""
Builds a multilayer convolutional network. It also provides functionality for passing external parameters to be
used at inference time. Enables inner loop optimization readily.
:param im_shape: The input image batch shape.
:param num_output_classes: The number of output classes of the network.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run this on.
:param meta_classifier: A flag indicating whether the system's meta-learning (inner-loop) functionalities should
be enabled.
"""
super(DilatedDenseNetActivationNormNetwork, self).__init__()
self.input_shape = list(im_shape)
self.num_filters = num_filters
self.num_stages = num_stages
# self.num_output_classes = num_output_classes
self.num_blocks_per_stage = num_blocks_per_stage
self.use_per_param_biases = per_param_biases
self.layer_dict = nn.ModuleDict()
self.build_network()
def build_network(self):
"""
Builds the network before inference is required by creating some dummy inputs with the same input as the
self.im_shape tuple. Then passes that through the network and dynamically computes input shapes and
sets output shapes for each layer.
"""
x = torch.zeros(self.input_shape)
out = x
self.layer_dict['stem_conv'] = Conv2dNormLeakyReLU(input_shape=out.shape, num_filters=self.num_filters,
kernel_size=3, padding=1)
out = self.layer_dict['stem_conv'](out)
for i in range(2):
for j in range(8):
dilation = 2 ** j
self.layer_dict['conv_{}_{}'.format(i, j)] = Conv2dNormLeakyReLU(input_shape=out.shape,
num_filters=8,
kernel_size=3, padding=dilation,
dilation=dilation)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](out)
out = torch.cat([out, cur], dim=1)
self.layer_dict['out_conv'] = nn.Conv2d(in_channels=out.shape[1], out_channels=self.input_shape[1], bias=True,
kernel_size=3, padding=1)
out = self.layer_dict['out_conv'](out)
if self.use_per_param_biases:
biases = torch.zeros(out.shape)
self.bias_params = nn.Parameter(biases, requires_grad=True)
out = out + self.bias_params
def forward(self, x):
"""
Forward propages through the network. If any params are passed then they are used instead of stored params.
:param x: Input image batch.
:param num_step: The current inner loop step number
:param params: If params are None then internal parameters are used. If params are a dictionary with keys the
same as the layer names then they will be used instead.
:param training: Whether this is training (True) or eval time.
:param backup_running_statistics: Whether to backup the running statistics in their backup store. Which is
then used to reset the stats back to a previous state (usually after an eval loop, when we want to throw away stored statistics)
:return: Logits of shape b, num_output_classes.
"""
out = x
out = self.layer_dict['stem_conv'](out)
for i in range(2):
for j in range(8):
dilation = 2 ** j
cur = self.layer_dict['conv_{}_{}'.format(i, j)](out)
out = torch.cat([out, cur], dim=1)
out = self.layer_dict['out_conv'](out)
if self.use_per_param_biases:
out = out + self.bias_params
return out
class Dilated1dDenseNetActivationNormNetwork(nn.Module):
def __init__(self, im_shape, num_filters, num_stages, num_blocks_per_stage):
"""
Builds a multilayer convolutional network. It also provides functionality for passing external parameters to be
used at inference time. Enables inner loop optimization readily.
:param im_shape: The input image batch shape.
:param num_output_classes: The number of output classes of the network.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run this on.
:param meta_classifier: A flag indicating whether the system's meta-learning (inner-loop) functionalities should
be enabled.
"""
super(Dilated1dDenseNetActivationNormNetwork, self).__init__()
self.input_shape = list(im_shape)
self.num_filters = num_filters
self.num_stages = num_stages
# self.num_output_classes = num_output_classes
self.num_blocks_per_stage = num_blocks_per_stage
self.layer_dict = nn.ModuleDict()
self.build_network()
def build_network(self):
"""
Builds the network before inference is required by creating some dummy inputs with the same input as the
self.im_shape tuple. Then passes that through the network and dynamically computes input shapes and
sets output shapes for each layer.
"""
x = torch.zeros(self.input_shape)
out = x
self.layer_dict['stem_conv'] = Conv1dNormLeakyReLU(input_shape=out.shape, num_filters=self.num_filters,
kernel_size=3, padding=1)
out = self.layer_dict['stem_conv'](out)
for i in range(1):
for j in range(11):
dilation = 2 ** j
self.layer_dict['conv_{}_{}'.format(i, j)] = Conv1dNormLeakyReLU(input_shape=out.shape,
num_filters=self.num_filters,
kernel_size=3, padding=dilation,
dilation=dilation)
cur = self.layer_dict['conv_{}_{}'.format(i, j)](out)
out = torch.cat([out, cur], dim=1)
self.layer_dict['out_conv'] = nn.Conv1d(in_channels=out.shape[1], out_channels=self.input_shape[1], bias=True,
kernel_size=3, padding=1)
out = self.layer_dict['out_conv'](out)
def forward(self, x):
"""
Forward propages through the network. If any params are passed then they are used instead of stored params.
:param x: Input image batch.
:param num_step: The current inner loop step number
:param params: If params are None then internal parameters are used. If params are a dictionary with keys the
same as the layer names then they will be used instead.
:param training: Whether this is training (True) or eval time.
:param backup_running_statistics: Whether to backup the running statistics in their backup store. Which is
then used to reset the stats back to a previous state (usually after an eval loop, when we want to throw away stored statistics)
:return: Logits of shape b, num_output_classes.
"""
out = x
out = self.layer_dict['stem_conv'](out)
for i in range(1):
for j in range(11):
dilation = 2 ** j
cur = self.layer_dict['conv_{}_{}'.format(i, j)](out)
out = torch.cat([out, cur], dim=1)
out = self.layer_dict['out_conv'](out)
return out
class CriticNetwork(nn.Module):
def __init__(self, task_embedding_shape, logit_shape, support_set_feature_shape, target_set_feature_shape,
support_set_classifier_pre_last_features,
target_set_classifier_pre_last_features,
support_set_label_shape,
num_classes_per_set, num_support_samples,
num_target_samples, conditional_information):
"""
Builds a multilayer convolutional network. It also provides functionality for passing external parameters to be
used at inference time. Enables inner loop optimization readily.
:param im_shape: The input image batch shape.
:param num_output_classes: The number of output classes of the network.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run this on.
:param meta_classifier: A flag indicating whether the system's meta-learning (inner-loop) functionalities should
be enabled.
"""
super(CriticNetwork, self).__init__()
self.layer_dict = nn.ModuleDict()
self.num_target_samples = num_target_samples
self.num_samples_per_class = num_support_samples
self.num_classes_per_set = num_classes_per_set
self.logit_shape = logit_shape
self.task_embedding_shape = task_embedding_shape
self.conditional_information = conditional_information
self.support_set_feature_shape = support_set_feature_shape
self.target_set_feature_shape = target_set_feature_shape
self.support_set_classifier_pre_last_features = support_set_classifier_pre_last_features
self.target_set_classifier_pre_last_features = target_set_classifier_pre_last_features
self.support_set_label_shape = support_set_label_shape
self.build_network()
def build_network(self):
"""
Builds the network before inference is required by creating some dummy inputs with the same input as the
self.im_shape tuple. Then passes that through the network and dynamically computes input shapes and
sets output shapes for each layer.
"""
processed_feature_list = []
if 'preds' in self.conditional_information:
logits = torch.ones(self.logit_shape)
logits_abs_diff_targets = torch.abs(logits)
logits_square_diff_targets = logits ** 2
sign_logits = torch.sign(logits)
logit_targets_features = torch.cat(
[logits, logits_abs_diff_targets, logits_square_diff_targets, sign_logits], dim=1)
logit_targets_features = logit_targets_features.view(logit_targets_features.shape[0], 1,
logit_targets_features.shape[1])
processed_feature_list.append(logit_targets_features)
if 'task_embedding' in self.conditional_information:
task_embedding = torch.zeros(self.task_embedding_shape)
task_embed_batched = task_embedding.view(1, 1, -1)
if 'preds' in self.conditional_information:
task_embed_batched = task_embed_batched.repeat(processed_feature_list[0].shape[0], 1, 1)
processed_feature_list.append(task_embed_batched)
# print(param_features_batched.shape, logit_targets_features.shape)
for item in processed_feature_list:
print('this one', item.shape)
mixed_features = torch.cat(processed_feature_list, dim=2)
feature_sets = [mixed_features]
out = torch.cat(feature_sets, dim=1)
out = out.view(out.shape[0], -1)
self.layer_dict['linear_0'] = nn.Linear(in_features=out.shape[1],
out_features=16, bias=False)
out = self.layer_dict['linear_0'](out)
out = F.leaky_relu(out)
self.layer_dict['linear_1'] = nn.Linear(in_features=out.shape[1],
out_features=16, bias=False)
out = self.layer_dict['linear_1'](out)
out = F.leaky_relu(out)
self.layer_dict['linear_preds'] = nn.Linear(in_features=out.shape[1],
out_features=1, bias=False)
out = self.layer_dict['linear_preds'](out)
out = out.sum()
print("VGGNetwork build", out.shape)
def forward(self, logits, task_embedding, return_sum=True):
"""
Forward propages through the network. If any params are passed then they are used instead of stored params.
:param x: Input image batch.
:param num_step: The current inner loop step number
:param params: If params are None then internal parameters are used. If params are a dictionary with keys the
same as the layer names then they will be used instead.
:param training: Whether this is training (True) or eval time.
:param backup_running_statistics: Whether to backup the running statistics in their backup store. Which is
then used to reset the stats back to a previous state (usually after an eval loop, when we want to throw away stored statistics)
:return: Logits of shape b, num_output_classes.
"""
# print(logits.shape, task_embedding.shape, support_set_features.shape, target_set_features.shape,
# support_set_classifier_pre_last_layer.shape, target_set_classifier_pre_last_layer.shape,
# support_set_labels.shape)
processed_feature_list = []
if 'preds' in self.conditional_information:
logits_abs_diff_targets = torch.abs(logits)
logits_square_diff_targets = logits ** 2
sign_logits = torch.sign(logits)
logit_targets_features = torch.cat(
[logits, logits_abs_diff_targets, logits_square_diff_targets, sign_logits], dim=1)
logit_targets_features = logit_targets_features.view(logit_targets_features.shape[0], 1,
logit_targets_features.shape[1])
processed_feature_list.append(logit_targets_features)
if 'task_embedding' in self.conditional_information:
task_embed_batched = task_embedding.view(1, 1, -1)
if 'preds' in self.conditional_information:
task_embed_batched = task_embed_batched.repeat(processed_feature_list[0].shape[0], 1, 1)
processed_feature_list.append(task_embed_batched)
mixed_features = torch.cat(processed_feature_list, dim=2)
feature_sets = [mixed_features]
out = torch.cat(feature_sets, dim=1)
out = out.view(out.shape[0], -1)
out = self.layer_dict['linear_0'](out)
out = F.leaky_relu(out)
print(out.shape)
out = self.layer_dict['linear_1'](out)
out = F.leaky_relu(out)
print(out.shape)
out = self.layer_dict['linear_preds'](out)
print(out.shape)
if return_sum:
out = out.sum()
return out
class TaskRelationalEmbedding(nn.Module):
def __init__(self, input_shape, num_samples_per_support_class, num_classes_per_set):
super(TaskRelationalEmbedding, self).__init__()
self.input_shape = input_shape
self.block_dict = nn.ModuleDict()
self.num_samples_per_class = num_samples_per_support_class
self.num_classes_per_set = num_classes_per_set
self.first_time = True
self.build_block()
def build_block(self):
out_img = torch.zeros(self.input_shape)
"""g"""
b, f = out_img.shape
print(out_img.shape)
out_img = out_img.view(b, f)
print(out_img.shape)
# x_flat = (64 x 25 x 24)
self.coord_tensor = []
for i in range(b):
self.coord_tensor.append(torch.Tensor(np.array([i])))
self.coord_tensor = torch.stack(self.coord_tensor, dim=0)
out_img = torch.cat([out_img, self.coord_tensor], dim=1)
x_i = torch.unsqueeze(out_img, 0) # (1xh*wxc)
x_i = x_i.repeat(b, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 1) # (h*wx1xc)
x_j = x_j.repeat(1, b, 1) # (h*wxh*wxc)
# concatenate all together
out = torch.cat([x_i, x_j], 2) # (h*wxh*wx2*c)
prev_shape = out.shape
out = out.view(out.shape[0] * out.shape[1], out.shape[-1])
for idx_layer in range(3):
self.block_dict['g_fcc_{}'.format(idx_layer)] = nn.Linear(out.shape[1], out_features=32)
out = F.relu(self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out))
# reshape again and sum
out = out.view(prev_shape[0], prev_shape[1], out.shape[-1])
out = out.sum(1)
out = out.view(self.num_classes_per_set, self.num_samples_per_class, -1)
out = out.mean(1).view(1, -1)
print('Task Relational Network Block built with output volume shape', out.shape)
def forward(self, x_img):
out_img = x_img
# print("input", out_img.shape)
"""g"""
b, f = out_img.shape
out_img = out_img.view(b, f)
out_img = torch.cat([out_img, self.coord_tensor.to(x_img.device)], dim=1)
# x_flat = (64 x 25 x 24)
# print('out_img', out_img.shape)
x_i = torch.unsqueeze(out_img, 0) # (1xh*wxc)
x_i = x_i.repeat(b, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 1) # (h*wx1xc)
x_j = x_j.repeat(1, b, 1) # (h*wxh*wxc)
# concatenate all together
out = torch.cat([x_i, x_j], 2) # (h*wxh*wx2*c)
prev_shape = out.shape
out = out.view(out.shape[0] * out.shape[1], out.shape[-1])
for idx_layer in range(3):
print(idx_layer, out.shape)
out = F.relu(self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out))
# reshape again and sum
# print(out.shape)
out = out.view(prev_shape[0], prev_shape[1], out.shape[-1])
out = out.sum(1)
out = out.view(self.num_classes_per_set, self.num_samples_per_class, -1)
out = out.mean(1).view(1, -1)
# """f"""
# out = self.post_processing_layer.forward(out)
# out = F.relu(out)
# out = self.output_layer.forward(out)
# # print('Block built with output volume shape', out.shape)
return out
class RelationalModule(nn.Module):
def __init__(self, input_shape):
super(RelationalModule, self).__init__()
self.input_shape = input_shape
self.block_dict = nn.ModuleDict()
self.first_time = True
self.build_block()
def build_block(self):
out_img = torch.zeros(self.input_shape)
"""g"""
c, h, w = out_img.shape
print(out_img.shape)
out_img = out_img.view(c, h * w)
out_img = out_img.permute([1, 0]) # h*w, c
print(out_img.shape)
# x_flat = (64 x 25 x 24)
self.coord_tensor = []
for i in range(h * w):
self.coord_tensor.append(torch.Tensor(np.array([i])))
self.coord_tensor = torch.stack(self.coord_tensor, dim=0)
out_img = torch.cat([out_img, self.coord_tensor], dim=1)
x_i = torch.unsqueeze(out_img, 0) # (1xh*wxc)
x_i = x_i.repeat(h * w, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 1) # (h*wx1xc)
x_j = x_j.repeat(1, h * w, 1) # (h*wxh*wxc)
# concatenate all together
out = torch.cat([x_i, x_j], 2) # (h*wxh*wx2*c)
out = out.view(out.shape[0] * out.shape[1], out.shape[2])
for idx_layer in range(2):
self.block_dict['g_fcc_{}'.format(idx_layer)] = nn.Linear(out.shape[1], out_features=32)
out = F.leaky_relu(self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out))
# reshape again and sum
print(out.shape)
out = out.sum(0).view(1, -1)
"""f"""
self.post_processing_layer = nn.Linear(in_features=out.shape[1], out_features=32)
out = self.post_processing_layer.forward(out)
out = F.relu(out)
self.output_layer = nn.Linear(in_features=out.shape[1], out_features=32)
out = self.output_layer.forward(out)
print('Block built with output volume shape', out.shape)
def forward(self, x_img):
out_img = x_img
# print("input", out_img.shape)
"""g"""
c, h, w = out_img.shape
out_img = out_img.view(c, h * w)
out_img = out_img.permute([1, 0]) # h*w, c
out_img = torch.cat([out_img, self.coord_tensor.to(x_img.device)], dim=1)
# x_flat = (64 x 25 x 24)
# print('out_img', out_img.shape)
x_i = torch.unsqueeze(out_img, 0) # (1xh*wxc)
x_i = x_i.repeat(h * w, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 1) # (h*wx1xc)
x_j = x_j.repeat(1, h * w, 1) # (h*wxh*wxc)
# concatenate all together
out = torch.cat([x_i, x_j], 2) # (h*wxh*wx2*c)
out = out.view(out.shape[0] * out.shape[1], out.shape[2])
for idx_layer in range(2):
out = F.leaky_relu(self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out))
# reshape again and sum
# print(out.shape)
out = out.sum(0).view(1, -1)
"""f"""
out = self.post_processing_layer.forward(out)
out = F.relu(out)
out = self.output_layer.forward(out)
# print('Block built with output volume shape', out.shape)
return out
class BatchRelationalModule(nn.Module):
def __init__(self, input_shape, use_coordinates=True, num_layers=2, num_units=64):
super(BatchRelationalModule, self).__init__()
self.input_shape = input_shape
self.block_dict = nn.ModuleDict()
self.first_time = True
self.use_coordinates = use_coordinates
self.num_layers = num_layers
self.num_units = num_units
self.build_block()
def build_block(self):
out_img = torch.zeros(self.input_shape)
"""g"""
if len(out_img.shape) > 3:
b, c, h, w = out_img.shape
print(out_img.shape)
out_img = out_img.view(b, c, h * w)
out_img = out_img.permute([0, 2, 1]) # h*w, c
b, length, c = out_img.shape
print(out_img.shape)
# x_flat = (64 x 25 x 24)
if self.use_coordinates:
self.coord_tensor = []
for i in range(length):
self.coord_tensor.append(torch.Tensor(np.array([i])))
self.coord_tensor = torch.stack(self.coord_tensor, dim=0).unsqueeze(0)
if self.coord_tensor.shape[0] != out_img.shape[0]:
self.coord_tensor = self.coord_tensor[0].unsqueeze(0).repeat([out_img.shape[0], 1, 1])
out_img = torch.cat([out_img, self.coord_tensor], dim=2)
x_i = torch.unsqueeze(out_img, 1) # (1xh*wxc)
x_i = x_i.repeat(1, length, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 2) # (h*wx1xc)
x_j = x_j.repeat(1, 1, length, 1) # (h*wxh*wxc)
# concatenate all together
per_location_feature = torch.cat([x_i, x_j], 3) # (h*wxh*wx2*c)
out = per_location_feature.view(
per_location_feature.shape[0] * per_location_feature.shape[1] * per_location_feature.shape[2],
per_location_feature.shape[3])
print(out.shape)
for idx_layer in range(self.num_layers):
self.block_dict['g_fcc_{}'.format(idx_layer)] = nn.Linear(out.shape[1], out_features=self.num_units,
bias=True)
out = self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out)
self.block_dict['LeakyReLU_{}'.format(idx_layer)] = nn.LeakyReLU()
out = self.block_dict['LeakyReLU_{}'.format(idx_layer)].forward(out)
# reshape again and sum
print(out.shape)
out = out.view(per_location_feature.shape[0], per_location_feature.shape[1], per_location_feature.shape[2], -1)
out = out.sum(1).sum(1)
print('here', out.shape)
"""f"""
self.post_processing_layer = nn.Linear(in_features=out.shape[1], out_features=self.num_units)
out = self.post_processing_layer.forward(out)
self.block_dict['LeakyReLU_post_processing'] = nn.LeakyReLU()
out = self.block_dict['LeakyReLU_post_processing'].forward(out)
self.output_layer = nn.Linear(in_features=out.shape[1], out_features=self.num_units)
out = self.output_layer.forward(out)
self.block_dict['LeakyReLU_output'] = nn.LeakyReLU()
out = self.block_dict['LeakyReLU_output'].forward(out)
print('Block built with output volume shape', out.shape)
def forward(self, x_img):
out_img = x_img
# print("input", out_img.shape)
"""g"""
if len(out_img.shape) > 3:
b, c, h, w = out_img.shape
out_img = out_img.view(b, c, h * w)
out_img = out_img.permute([0, 2, 1]) # h*w, c
b, length, c = out_img.shape
if self.use_coordinates:
if self.coord_tensor.shape[0] != out_img.shape[0]:
self.coord_tensor = self.coord_tensor[0].unsqueeze(0).repeat([out_img.shape[0], 1, 1])
out_img = torch.cat([out_img, self.coord_tensor.to(x_img.device)], dim=2)
# x_flat = (64 x 25 x 24)
# print('out_img', out_img.shape)
x_i = torch.unsqueeze(out_img, 1) # (1xh*wxc)
x_i = x_i.repeat(1, length, 1, 1) # (h*wxh*wxc)
x_j = torch.unsqueeze(out_img, 2) # (h*wx1xc)
x_j = x_j.repeat(1, 1, length, 1) # (h*wxh*wxc)
# concatenate all together
per_location_feature = torch.cat([x_i, x_j], 3) # (h*wxh*wx2*c)
out = per_location_feature.view(
per_location_feature.shape[0] * per_location_feature.shape[1] * per_location_feature.shape[2],
per_location_feature.shape[3])
for idx_layer in range(2):
out = self.block_dict['g_fcc_{}'.format(idx_layer)].forward(out)
out = self.block_dict['LeakyReLU_{}'.format(idx_layer)].forward(out)
# reshape again and sum
# print(out.shape)
out = out.view(per_location_feature.shape[0], per_location_feature.shape[1], per_location_feature.shape[2], -1)
out = out.sum(1).sum(1)
"""f"""
out = self.post_processing_layer.forward(out)
out = self.block_dict['LeakyReLU_post_processing'].forward(out)
out = self.output_layer.forward(out)
out = self.block_dict['LeakyReLU_output'].forward(out)
# print('Block built with output volume shape', out.shape)
return out
class DenseEmbeddingSmallNetwork(nn.Module):
def __init__(self, im_shape, num_filters, num_blocks_per_stage, num_stages, dropout_rate,
output_spatial_dimensionality, average_pool_outputs=True, use_vgg_features=False):
super(DenseEmbeddingSmallNetwork, self).__init__()
b, c, self.h, self.w = im_shape
self.total_layers = 0
self.input_shape = list(im_shape)
self.num_filters = num_filters
self.num_blocks_per_stage = num_blocks_per_stage
self.num_stages = num_stages
self.average_pool_outputs = average_pool_outputs
self.use_vgg_features = use_vgg_features
self.output_spatial_dimensionality = output_spatial_dimensionality
self.dropout_rate = dropout_rate
self.layer_dict = nn.ModuleDict()
self.build_block()
def build_block(self):
x = torch.ones(self.input_shape)
out = x
self.layer_dict['dense_net_features'] = DenseNetActivationNormNetwork(im_shape=x.shape,
num_filters=self.num_filters,
num_stages=self.num_stages,
num_blocks_per_stage=self.num_blocks_per_stage,
dropout_rate=self.dropout_rate,
reduction_rate=1.0,
average_pool_output=self.average_pool_outputs)
out = self.layer_dict['dense_net_features'].forward(out, dropout_training=False)
print("DenseEmbeddingSmallNetwork output shape", out.shape)
return out
def forward(self, x, dropout_training):
out = x
# print("inputs", x.shape)
out = self.layer_dict['dense_net_features'].forward(out, dropout_training=dropout_training)
# out = out.view(out.shape[0], out.shape[1], 1, 1)
# b, c, h, w = out.shape
return out
def reinitialize(self):