-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmeta_neural_network_architectures.py
1091 lines (920 loc) · 51 KB
/
meta_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
import logging
import math
from copy import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.init import _calculate_fan_in_and_fan_out
def extract_top_level_dict(current_dict):
"""
Builds a graph dictionary from the passed depth_keys, value pair. Useful for dynamically passing external params
:param depth_keys: A list of strings making up the name of a variable. Used to make a graph for that params tree.
:param value: Param value
:param key_exists: If none then assume new dict, else load existing dict and add new key->value pairs to it.
:return: A dictionary graph of the params already added to the graph.
"""
output_dict = dict()
for key in current_dict.keys():
name = key.replace("layer_dict.", "")
name = name.replace("layer_dict.", "")
name = name.replace("block_dict.", "")
name = name.replace("module-", "")
top_level = name.split(".")[0]
sub_level = ".".join(name.split(".")[1:])
if top_level not in output_dict:
if sub_level == "":
output_dict[top_level] = current_dict[key]
else:
output_dict[top_level] = {sub_level: current_dict[key]}
else:
new_item = {key: value for key, value in output_dict[top_level].items()}
new_item[sub_level] = current_dict[key]
output_dict[top_level] = new_item
return output_dict
def extract_params_and_check_for_missing_keys(current_dict, layer_dict):
params_dict = extract_top_level_dict(current_dict=current_dict)
for key in layer_dict.keys():
if key not in params_dict:
params_dict[key] = None
return params_dict
class MetaConv1dLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, use_bias, groups=1, dilation_rate=1):
"""
A MetaConv1D layer. Applies the same functionality of a standard Conv2D layer with the added functionality of
being able to receive a parameter dictionary at the forward pass which allows the convolution to use external
weights instead of the internal ones stored in the conv layer. Useful for inner loop optimization in the meta
learning setting.
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param kernel_size: Convolutional kernel size
:param stride: Convolutional stride
:param padding: Convolution padding
:param use_bias: Boolean indicating whether to use a bias or not.
"""
super(MetaConv1dLayer, self).__init__()
num_filters = out_channels
self.stride = int(stride)
self.padding = int(padding)
self.dilation_rate = int(dilation_rate)
self.use_bias = use_bias
self.weight = nn.Parameter(torch.empty(num_filters, in_channels, kernel_size))
nn.init.xavier_uniform_(self.weight)
if self.use_bias:
self.bias = nn.Parameter(torch.zeros(num_filters))
self.groups = groups
def forward(self, x, params=None):
"""
Applies a conv2D forward pass. If params are not None will use the passed params as the conv weights and biases
:param x: Input image batch.
:param params: If none, then conv layer will use the stored self.weights and self.bias, if they are not none
then the conv layer will use the passed params as its parameters.
:return: The output of a convolutional function.
"""
if params is not None:
params = extract_top_level_dict(current_dict=params)
if self.use_bias:
(weight, bias) = params["weight"], params["bias"]
else:
(weight) = params["weight"]
bias = None
else:
if self.use_bias:
weight, bias = self.weight, self.bias
else:
weight = self.weight
bias = None
out = F.conv1d(input=x, weight=weight, bias=bias, stride=self.stride,
padding=self.padding, dilation=self.dilation_rate, groups=self.groups)
return out
class MetaConv2dLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, use_bias, groups=1, dilation_rate=1):
"""
A MetaConv1D layer. Applies the same functionality of a standard Conv2D layer with the added functionality of
being able to receive a parameter dictionary at the forward pass which allows the convolution to use external
weights instead of the internal ones stored in the conv layer. Useful for inner loop optimization in the meta
learning setting.
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param kernel_size: Convolutional kernel size
:param stride: Convolutional stride
:param padding: Convolution padding
:param use_bias: Boolean indicating whether to use a bias or not.
"""
super(MetaConv2dLayer, self).__init__()
num_filters = out_channels
self.stride = stride
self.padding = int(padding)
self.dilation_rate = int(dilation_rate)
self.use_bias = use_bias
self.weight = nn.Parameter(torch.empty(num_filters, in_channels, kernel_size, kernel_size), requires_grad=True)
nn.init.xavier_uniform_(self.weight)
if self.use_bias:
self.bias = nn.Parameter(torch.zeros(num_filters), requires_grad=True)
self.groups = groups
def forward(self, x, params=None):
"""
Applies a conv2D forward pass. If params are not None will use the passed params as the conv weights and biases
:param x: Input image batch.
:param params: If none, then conv layer will use the stored self.weights and self.bias, if they are not none
then the conv layer will use the passed params as its parameters.
:return: The output of a convolutional function.
"""
if params is not None:
# print([key for key in params.keys()])
params = extract_top_level_dict(current_dict=params)
if self.use_bias:
(weight, bias) = params["weight"], params["bias"]
else:
(weight) = params["weight"]
bias = None
else:
if self.use_bias:
weight, bias = self.weight, self.bias
else:
weight = self.weight
bias = None
out = F.conv2d(input=x, weight=weight, bias=bias, stride=self.stride,
padding=self.padding, dilation=self.dilation_rate, groups=self.groups)
return out
class MetaLinearLayer(nn.Module):
def __init__(self, input_shape, num_filters, use_bias):
"""
A MetaLinear layer. Applies the same functionality of a standard linearlayer with the added functionality of
being able to receive a parameter dictionary at the forward pass which allows the convolution to use external
weights instead of the internal ones stored in the linear layer. Useful for inner loop optimization in the meta
learning setting.
:param input_shape: The shape of the input data, in the form (b, f)
:param num_filters: Number of output filters
:param use_bias: Whether to use biases or not.
"""
super(MetaLinearLayer, self).__init__()
self.input_shape = input_shape
b, c = input_shape[:2]
self.use_bias = use_bias
self.weights = nn.Parameter(torch.empty(num_filters, c))
nn.init.xavier_uniform_(self.weights)
logging.debug("debug message", self.weights)
if self.use_bias:
self.bias = nn.Parameter(torch.zeros(num_filters))
def forward(self, x, params=None):
"""
Forward propagates by applying a linear function (Wx + b). If params are none then internal params are used.
Otherwise passed params will be used to execute the function.
:param x: Input data batch, in the form (b, f)
:param params: A dictionary containing 'weights' and 'bias'. If params are none then internal params are used.
Otherwise the external are used.
:return: The result of the linear function.
"""
# print(x.shape)
if params is not None:
params = extract_top_level_dict(current_dict=params)
if self.use_bias:
(weight, bias) = params["weights"], params["bias"]
else:
(weight) = params["weights"]
bias = None
# print(x.shape, params['weights'].shape)
else:
if self.use_bias:
weight, bias = self.weights, self.bias
else:
weight = self.weights
bias = None
# print(x.shape)
out = F.linear(input=x, weight=weight, bias=bias)
# print(out.shape, weight.shape, self.input_shape)
return out
def reset_parameters(self):
self.weights.data = self.weights.data * 0.
fan_in, fan_out = _calculate_fan_in_and_fan_out(self.weights)
std = 1. * math.sqrt(2.0 / (fan_in + fan_out))
a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation
a_array = torch.ones(self.weights.shape) * a
a_array.to(self.weights.device)
self.weights.data = self.weights.data + torch.distributions.Uniform(low=-a_array, high=a_array).rsample().to(
self.weights.device)
if self.use_bias:
self.bias.data = self.bias.data * 0.
class MetaBatchNormLayer(nn.Module):
def __init__(self, num_features, num_support_set_steps, num_target_set_steps,
eps=1e-5, momentum=0.1, affine=True, track_running_stats=True,
use_per_step_bn_statistics=False, learnable_bn_gamma=True, learnable_bn_beta=True):
"""
A MetaBatchNorm layer. Applies the same functionality of a standard BatchNorm layer with the added functionality of
being able to receive a parameter dictionary at the forward pass which allows the convolution to use external
weights instead of the internal ones stored in the conv layer. Useful for inner loop optimization in the meta
learning setting. Also has the additional functionality of being able to store per step running stats and per step beta and gamma.
"""
super(MetaBatchNormLayer, self).__init__()
self.num_features = num_features
self.eps = eps
self.affine = affine
self.track_running_stats = track_running_stats
self.num_features = num_features
self.use_per_step_bn_statistics = use_per_step_bn_statistics
self.learnable_gamma = learnable_bn_gamma
self.learnable_beta = learnable_bn_beta
if use_per_step_bn_statistics:
self.running_mean = nn.Parameter(
torch.zeros(num_support_set_steps + num_target_set_steps + 1, num_features),
requires_grad=False)
self.running_var = nn.Parameter(
torch.ones(num_support_set_steps + num_target_set_steps + 1, num_features),
requires_grad=False)
self.bias = nn.Parameter(
torch.zeros(num_support_set_steps + num_target_set_steps + 1, num_features),
requires_grad=self.learnable_beta)
self.weight = nn.Parameter(
torch.ones(num_support_set_steps + num_target_set_steps + 1, num_features),
requires_grad=self.learnable_gamma)
else:
self.running_mean = nn.Parameter(torch.zeros(num_features), requires_grad=False)
self.running_var = nn.Parameter(torch.zeros(num_features), requires_grad=False)
self.bias = nn.Parameter(torch.zeros(num_features),
requires_grad=self.learnable_beta)
self.weight = nn.Parameter(torch.ones(num_features),
requires_grad=self.learnable_gamma)
self.backup_running_mean = torch.zeros(self.running_mean.shape)
self.backup_running_var = torch.ones(self.running_var.shape)
self.momentum = momentum
def forward(self, input, num_step, training=False, backup_running_statistics=False):
"""
Forward propagates by applying a bach norm function. If params are none then internal params are used.
Otherwise passed params will be used to execute the function.
:param input: input data batch, size either can be any.
:param num_step: The current inner loop step being taken. This is used when we are learning per step params and
collecting per step batch statistics. It indexes the correct object to use for the current time-step
:param params: A dictionary containing 'weight' and 'bias'.
:param training: Whether this is currently the training or evaluation phase.
:param backup_running_statistics: Whether to backup the running statistics. This is used
at evaluation time, when after the pass is complete we want to throw away the collected validation stats.
:return: The result of the batch norm operation.
"""
if self.use_per_step_bn_statistics:
running_mean = self.running_mean[num_step]
running_var = self.running_var[num_step]
weight, bias = self.weight[num_step], self.bias[num_step]
# print(num_step)
else:
running_mean = self.running_mean
running_var = self.running_var
weight, bias = self.weight, self.bias
if backup_running_statistics and self.use_per_step_bn_statistics:
self.backup_running_mean.data = copy(self.running_mean.data)
self.backup_running_var.data = copy(self.running_var.data)
momentum = self.momentum
# print(running_mean.shape, running_var.shape)
output = F.batch_norm(input, running_mean, running_var, weight, bias,
training=True, momentum=momentum, eps=self.eps)
return output
def restore_backup_stats(self):
"""
Resets batch statistics to their backup values which are collected after each forward pass.
"""
if self.use_per_step_bn_statistics:
self.running_mean = nn.Parameter(self.backup_running_mean, requires_grad=False)
self.running_var = nn.Parameter(self.backup_running_var, requires_grad=False)
self.to(self.weight.device)
def extra_repr(self):
return '{num_features}, eps={eps}, momentum={momentum}, affine={affine}, ' \
'track_running_stats={track_running_stats}'.format(**self.__dict__)
class MetaConvNormLayerLeakyReLU(nn.Module):
def __init__(self, input_shape, num_filters, kernel_size, stride, padding, use_bias, per_step_bn_statistics,
num_support_set_steps, num_target_set_steps,
use_normalization=True, groups=1):
"""
Initializes a BatchNorm->Conv->ReLU layer which applies those operation in that order.
:param args: A named tuple containing the system's hyperparameters.
:param device: The device to run the layer on.
:param normalization: The type of normalization to use 'batch_norm' or 'layer_norm'
:param meta_layer: Whether this layer will require meta-layer capabilities such as meta-batch norm,
meta-conv etc.
:param input_shape: The image input shape in the form (b, c, h, w)
:param num_filters: number of filters for convolutional layer
:param kernel_size: the kernel size of the convolutional layer
:param stride: the stride of the convolutional layer
:param padding: the bias of the convolutional layer
:param use_bias: whether the convolutional layer utilizes a bias
"""
super(MetaConvNormLayerLeakyReLU, self).__init__()
self.input_shape = input_shape
self.use_normalization = use_normalization
self.use_per_step_bn_statistics = per_step_bn_statistics
self.num_filters = num_filters
self.kernel_size = kernel_size
self.num_support_set_steps = num_support_set_steps
self.num_target_set_steps = num_target_set_steps
self.stride = stride
self.groups = groups
self.padding = padding
self.use_bias = use_bias
self.layer_dict = nn.ModuleDict()
self.build_block()
def build_block(self):
x = torch.zeros(self.input_shape)
out = x
self.conv = MetaConv2dLayer(in_channels=out.shape[1], out_channels=self.num_filters,
kernel_size=self.kernel_size,
stride=self.stride, padding=self.padding, use_bias=self.use_bias,
groups=self.groups)
out = self.conv(out)
if type(out) == tuple:
out, _ = out
if self.use_normalization:
self.norm_layer = MetaBatchNormLayer(num_features=out.shape[1], track_running_stats=True,
use_per_step_bn_statistics=self.use_per_step_bn_statistics,
num_support_set_steps=self.num_support_set_steps,
num_target_set_steps=self.num_target_set_steps)
# print(out.shape)
out = self.norm_layer.forward(out, num_step=0)
out = F.leaky_relu(out)
print(out.shape)
def forward(self, x, num_step, params=None, training=False, backup_running_statistics=False):
"""
Forward propagates by applying the function. If params are none then internal params are used.
Otherwise passed params will be used to execute the function.
:param input: input data batch, size either can be any.
:param num_step: The current inner loop step being taken. This is used when we are learning per step params and
collecting per step batch statistics. It indexes the correct object to use for the current time-step
:param params: A dictionary containing 'weight' and 'bias'.
:param training: Whether this is currently the training or evaluation phase.
:param backup_running_statistics: Whether to backup the running statistics. This is used
at evaluation time, when after the pass is complete we want to throw away the collected validation stats.
:return: The result of the batch norm operation.
"""
conv_params = None
if params is not None:
params = {key: value for key, value in params.items()}
params = extract_top_level_dict(current_dict=params)
conv_params = params['conv']
# if params is not None:
# print([key for key in params.keys()])
# else:
# print(None)
out = x
out = self.conv(out, params=conv_params)
if type(out) == tuple:
out, _ = out
if self.use_normalization:
out = self.norm_layer.forward(out, num_step=num_step,
training=training,
backup_running_statistics=backup_running_statistics)
out = F.leaky_relu(out)
return out
def restore_backup_stats(self):
"""
Restore stored statistics from the backup, replacing the current ones.
"""
if self.normalization:
self.norm_layer.restore_backup_stats()
class VGGActivationNormNetwork(nn.Module):
def __init__(self, input_shape, num_output_classes, use_channel_wise_attention,
num_stages, num_filters, num_support_set_steps, num_target_set_steps):
"""
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(VGGActivationNormNetwork, self).__init__()
self.total_layers = 0
self.upscale_shapes = []
self.num_filters = num_filters
self.num_stages = num_stages
self.input_shape = input_shape
self.use_channel_wise_attention = use_channel_wise_attention
self.num_output_classes = num_output_classes
self.num_support_set_steps = num_support_set_steps
self.num_target_set_steps = num_target_set_steps
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 = nn.ModuleDict()
for i in range(self.num_stages):
self.layer_dict['conv_{}'.format(i)] = MetaConvNormLayerLeakyReLU(input_shape=out.shape,
num_filters=self.num_filters,
kernel_size=3, stride=1,
padding=1,
use_bias=True,
groups=1, per_step_bn_statistics=True,
num_support_set_steps=self.num_support_set_steps,
num_target_set_steps=self.num_target_set_steps)
out = self.layer_dict['conv_{}'.format(i)](out, training=True, num_step=0)
out = F.max_pool2d(input=out, kernel_size=2, stride=2, padding=0)
out = out.view((out.shape[0], -1))
if type(self.num_output_classes) == list:
for idx, num_output_classes in enumerate(self.num_output_classes):
self.layer_dict['linear_{}'.format(idx)] = MetaLinearLayer(input_shape=out.shape,
num_filters=num_output_classes,
use_bias=True)
pred = self.layer_dict['linear_{}'.format(idx)](out)
else:
self.layer_dict['linear'] = MetaLinearLayer(input_shape=out.shape,
num_filters=self.num_output_classes, use_bias=True)
out = self.layer_dict['linear'](out)
print("VGGNetwork build", out.shape)
def forward(self, x, num_step, dropout_training=None, params=None, training=False,
backup_running_statistics=False, return_features=False):
"""
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.
"""
param_dict = dict()
if params is not None:
params = {key: value[0] for key, value in params.items()}
# print([key for key, value in param_dict.items()])
param_dict = extract_top_level_dict(current_dict=params)
for name, param in list(self.layer_dict.named_parameters()) + list(self.layer_dict.items()):
path_bits = name.split(".")
layer_name = path_bits[0]
if layer_name not in param_dict:
param_dict[layer_name] = None
out = x
# print([key for key, value in param_dict.items() if value is not None])
for i in range(self.num_stages):
out = self.layer_dict['conv_{}'.format(i)](out, params=param_dict['conv_{}'.format(i)], training=training,
backup_running_statistics=backup_running_statistics,
num_step=num_step)
out = F.max_pool2d(input=out, kernel_size=(2, 2), stride=2, padding=0)
features = out
out = out.view(out.size(0), -1)
if type(self.num_output_classes) == list:
pred_list = []
for idx, num_output_classes in enumerate(self.num_output_classes):
cur_pred = self.layer_dict['linear_{}'.format(idx)](out, params=param_dict['linear_{}'.format(idx)])
pred_list.append(cur_pred)
out = pred_list
else:
out = self.layer_dict['linear'](out, params=param_dict['linear'])
if return_features:
return out, features
else:
return out
def restore_backup_stats(self):
"""
Reset stored batch statistics from the stored backup.
"""
for name, module in self.named_modules():
if type(module) == MetaBatchNormLayer:
module.restore_backup_stats()
def zero_grad(self, params=None):
if params is None:
for param in self.parameters():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
else:
for name, param in params.items():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
params[name].grad = None
class FCCActivationNormNetwork(nn.Module):
def __init__(self, im_shape, num_output_classes, args, device, use_bn, num_stages=None, use_bias=True,
meta_classifier=True):
"""
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(FCCActivationNormNetwork, self).__init__()
self.device = device
self.args = args
self.input_shape = list(im_shape)
self.num_output_classes = num_output_classes
self.meta_classifier = meta_classifier
self.num_stages = num_stages
self.use_bias = use_bias
self.use_bn = use_bn
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
out = out.view(out.size(0), -1)
self.layer_dict = nn.ModuleDict()
for i in range(self.num_stages):
self.layer_dict['fcc_{}'.format(i)] = MetaLinearLayer(input_shape=out.shape, num_filters=40, use_bias=False)
out = self.layer_dict['fcc_{}'.format(i)].forward(out)
if self.use_bn:
self.layer_dict['fcc_bn_{}'.format(i)] = MetaBatchNormLayer(num_features=out.shape[1], args=self.args,
use_per_step_bn_statistics=True)
out = self.layer_dict['fcc_bn_{}'.format(i)].forward(out, num_step=0)
out = F.leaky_relu(out)
out = out.view(out.shape[0], -1)
self.layer_dict['preds_linear'] = MetaLinearLayer(input_shape=(out.shape[0], np.prod(out.shape[1:])),
num_filters=self.num_output_classes, use_bias=self.use_bias)
out = self.layer_dict['preds_linear'](out)
print("FCCActivationNormNetwork build", out.shape)
def forward(self, x, num_step, params=None, training=False,
backup_running_statistics=False, return_features=False):
"""
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.
"""
param_dict = dict()
if params is not None:
params = {key: value[0] for key, value in params.items()}
param_dict = extract_top_level_dict(current_dict=params)
for name, param in list(self.layer_dict.named_parameters()) + list(self.layer_dict.items()):
path_bits = name.split(".")
layer_name = path_bits[0]
if layer_name not in param_dict:
param_dict[layer_name] = None
out = x
out = out.view(out.size(0), -1)
for i in range(self.num_stages):
out = self.layer_dict['fcc_{}'.format(i)](out, params=param_dict['fcc_{}'.format(i)])
if self.use_bn:
out = self.layer_dict['fcc_bn_{}'.format(i)].forward(out, num_step=num_step,
params=None, training=training,
backup_running_statistics=backup_running_statistics)
out = F.leaky_relu(out)
features = out
out = out.view(out.size(0), -1)
out = self.layer_dict['preds_linear'](out, param_dict['preds_linear'])
if return_features:
return out, features
else:
return out
def reset_parameters(self):
for name, module in self.named_modules():
if type(module) == MetaLinearLayer:
# print("reset", name)
module.reset_parameters()
def restore_backup_stats(self):
"""
Reset stored batch statistics from the stored backup.
"""
for name, module in self.named_modules():
if type(module) == MetaBatchNormLayer:
module.restore_backup_stats()
def zero_grad(self, params=None):
if params is None:
for param in self.parameters():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
else:
for name, param in params.items():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
params[name].grad = None
class SqueezeExciteLayer(nn.ModuleDict):
def __init__(self, input_shape, num_filters, num_layers, num_support_set_steps, num_target_set_steps):
super(SqueezeExciteLayer, self).__init__()
self.input_shape = input_shape
self.num_filters = num_filters
self.num_layers = num_layers
self.num_support_set_steps = num_support_set_steps
self.num_target_set_steps = num_target_set_steps
self.build_block()
def build_block(self):
self.layer_dict = nn.ModuleDict()
x_dummy = torch.zeros(self.input_shape)
out = x_dummy
out = F.avg_pool2d(out, out.shape[-1]).squeeze()
for i in range(self.num_layers - 1):
self.layer_dict['attention_network_hidden_{}'.format(i)] = MetaLinearLayer(input_shape=out.shape,
use_bias=True,
num_filters=self.num_filters)
out = self.layer_dict['attention_network_hidden_{}'.format(i)].forward(out, params=None)
self.layer_dict['LeakyReLU_{}'.format(i)] = nn.LeakyReLU()
out = self.layer_dict['LeakyReLU_{}'.format(i)].forward(out)
self.layer_dict['attention_network_output_layer'] = MetaLinearLayer(input_shape=out.shape, use_bias=True,
num_filters=x_dummy.shape[1])
channel_wise_attention_regions = self.layer_dict[
'attention_network_output_layer'].forward(
out, params=None)
channel_wise_attention_regions = F.sigmoid(channel_wise_attention_regions)
out = x_dummy * channel_wise_attention_regions.unsqueeze(2).unsqueeze(2)
print('Built', type(self), 'with output', out.shape, self)
def forward(self, x, num_step=0, params=None):
param_dict = dict()
if params is not None:
params = {key: value for key, value in params.items()}
param_dict = extract_top_level_dict(current_dict=params)
for name, param in list(self.layer_dict.named_parameters()) + list(self.layer_dict.items()):
path_bits = name.split(".")
layer_name = path_bits[0]
if layer_name not in param_dict:
param_dict[layer_name] = None
out = x
out = F.avg_pool2d(out, out.shape[-1]).squeeze()
for i in range(self.num_layers - 1):
# print(out.shape)
out = self.layer_dict[
'attention_network_hidden_{}'.format(i)].forward(
out, params=param_dict['attention_network_hidden_{}'.format(i)])
out = self.layer_dict['LeakyReLU_{}'.format(i)].forward(out)
# print(out.shape)
channel_wise_attention_regions = self.layer_dict[
'attention_network_output_layer'].forward(
out, params=param_dict['attention_network_output_layer'])
channel_wise_attention_regions = F.sigmoid(channel_wise_attention_regions)
out = x * channel_wise_attention_regions.unsqueeze(2).unsqueeze(2)
return out
class VGGActivationNormNetworkWithAttention(nn.Module):
def __init__(self, input_shape, num_output_classes, use_channel_wise_attention,
num_stages, num_filters, num_support_set_steps, num_target_set_steps, 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(VGGActivationNormNetworkWithAttention, self).__init__()
self.total_layers = 0
self.upscale_shapes = []
self.num_filters = num_filters
self.num_stages = num_stages
self.input_shape = input_shape
self.use_channel_wise_attention = use_channel_wise_attention
self.num_output_classes = num_output_classes
self.num_blocks_per_stage = num_blocks_per_stage
self.num_support_set_steps = num_support_set_steps
self.num_target_set_steps = num_target_set_steps
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 = nn.ModuleDict()
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
if self.use_channel_wise_attention:
self.layer_dict['attention_layer_{}_{}'.format(i, j)] = SqueezeExciteLayer(input_shape=out.shape,
num_filters=0,
num_layers=0,
num_support_set_steps=self.num_support_set_steps,
num_target_set_steps=self.num_target_set_steps)
out = self.layer_dict['attention_layer_{}_{}'.format(i, j)].forward(out)
self.layer_dict['conv_{}_{}'.format(i, j)] = MetaConvNormLayerLeakyReLU(input_shape=out.shape,
num_filters=self.num_filters,
kernel_size=3, stride=1,
padding=1,
use_bias=True,
groups=1,
per_step_bn_statistics=True,
num_support_set_steps=self.num_support_set_steps,
num_target_set_steps=self.num_target_set_steps)
out = self.layer_dict['conv_{}_{}'.format(i, j)](out, training=True, num_step=0)
out = F.max_pool2d(input=out, kernel_size=(2, 2), stride=2, padding=0)
if self.use_channel_wise_attention:
self.layer_dict['attention_pre_logit_layer'] = SqueezeExciteLayer(input_shape=out.shape,
num_filters=0,
num_layers=0,
num_support_set_steps=self.num_support_set_steps,
num_target_set_steps=self.num_target_set_steps)
out = self.layer_dict['attention_pre_logit_layer'].forward(out)
features_avg = F.avg_pool2d(out, out.shape[-1]).squeeze()
out = features_avg
self.layer_dict['linear'] = MetaLinearLayer(input_shape=out.shape,
num_filters=self.num_output_classes, use_bias=True)
out = self.layer_dict['linear'](out)
print("VGGNetwork build", out.shape)
def forward(self, x, num_step, dropout_training=None, params=None, training=False,
backup_running_statistics=False, return_features=False):
"""
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.
"""
param_dict = dict()
if params is not None:
params = {key: value[0] for key, value in params.items()}
# print([key for key, value in param_dict.items()])
param_dict = extract_top_level_dict(current_dict=params)
for name, param in list(self.layer_dict.named_parameters()) + list(self.layer_dict.items()):
path_bits = name.split(".")
layer_name = path_bits[0]
if layer_name not in param_dict:
param_dict[layer_name] = None
out = x
# print([key for key, value in param_dict.items() if value is not None])
for i in range(self.num_stages):
for j in range(self.num_blocks_per_stage):
if self.use_channel_wise_attention:
out = self.layer_dict['attention_layer_{}_{}'.format(i, j)].forward(out, num_step=num_step,
params=param_dict[
'attention_layer_{}_{}'.format(
i, j)])
out = self.layer_dict['conv_{}_{}'.format(i, j)](out, training=True, num_step=num_step,
params=param_dict['conv_{}_{}'.format(i, j)])
out = F.max_pool2d(input=out, kernel_size=(2, 2), stride=2, padding=0)
if self.use_channel_wise_attention:
out = self.layer_dict['attention_pre_logit_layer'].forward(out, params=param_dict[
'attention_pre_logit_layer'])
features = out
features_avg = F.avg_pool2d(out, out.shape[-1]).squeeze()
# out = F.avg_pool2d(out, out.shape[-1])
# out = self.layer_dict['relational_pool'].forward(out, params=param_dict['relational_pool'], num_step=num_step)
out = features_avg
out = self.layer_dict['linear'](out, param_dict['linear'])
if return_features:
return out, features
else:
return out
def restore_backup_stats(self):
"""
Reset stored batch statistics from the stored backup.
"""
for name, module in self.named_modules():
if type(module) == MetaBatchNormLayer:
module.restore_backup_stats()
def zero_grad(self, params=None):
if params is None:
for param in self.parameters():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
else:
for name, param in params.items():
if param.requires_grad == True:
if param.grad is not None:
if torch.sum(param.grad) > 0:
print(param.grad)
param.grad.zero_()
params[name].grad = None
class MetaBatchRelationalModule(nn.Module):
def __init__(self, input_shape, use_coordinates=True, num_support_set_steps=0, num_target_set_steps=0,
output_units=32):
super(MetaBatchRelationalModule, self).__init__()
self.input_shape = input_shape
self.layer_dict = nn.ModuleDict()
self.first_time = True
self.use_coordinates = use_coordinates
self.num_target_set_steps = num_target_set_steps
self.num_support_set_steps = num_support_set_steps
self.output_units = output_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
if h > 5:
out_img = F.adaptive_avg_pool2d(out_img, output_size=5)
print(out_img.shape)
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
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)