-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscattering.py
187 lines (124 loc) · 7.74 KB
/
scattering.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
from subprocess import call
import torch.nn.functional as F
from torch import nn, Tensor, load
from typing import Callable
from gem.pipelines.common import BasicAugmentation
from gem.architectures import rn, rn_cifar, wrn_cifar
class View(nn.Module):
def __init__(self, shape):
super(View, self).__init__()
self.shape = shape
def forward(self, x):
return x.view(*self.shape)
class LambdaLayer(nn.Module):
def __init__(self, lambd):
super(LambdaLayer, self).__init__()
self.lambd = lambd
def forward(self, x):
return self.lambd(x)
class ScatteringModel(nn.Module):
def __init__(self, base_model: nn.Module, arch: str, target_size: int, J: int, input_channels: int) -> None:
super(ScatteringModel, self).__init__()
self.base = base_model
self.input_channels = input_channels
self.J = J
self.target_size = target_size
self.nspace = int(self.target_size / (2 ** self.J))
self.nfscat = int((1 + 8 * self.J + 8 * 8 * self.J * (self.J - 1) / 2))
self.make_scat(arch, base_model.__class__)
def make_scat(self, arch, arch_class):
try:
from kymatio import Scattering2D
except ModuleNotFoundError:
print('Installing kymatio ...')
rc = call(['bash', 'setup_kymatio.sh'])
from kymatio import Scattering2D
if arch_class == rn.ResNet:
self.base.conv1 = nn.Identity()
self.base.bn1 = nn.Identity()
self.base.relu = nn.Identity()
self.base.maxpool = nn.Identity()
self.base.layer1 = Scattering2D(J=self.J, shape=(self.target_size, self.target_size), frontend='torch')
child = self.base.layer3[0].conv1
if child.in_channels == 128: # rn18/rn34 case
l3_ichannels = child.out_channels
new_attr = nn.Conv2d(child.out_channels, child.out_channels, child.kernel_size[0],
stride=(1,1), padding=child.padding, bias=child.bias)
self.base.layer3[0].conv1 = new_attr
self.base.layer3[0].downsample = nn.Identity()
else: # rn50/rn101 case
l3_ichannels = self.base.layer3[0].conv1.in_channels
self.base.layer3[0].conv2.stride = (1,1)
self.base.layer3[0].downsample[0].stride = (1,1)
self.base.layer2 = nn.Sequential(View(shape=(-1, self.input_channels * self.nfscat, self.nspace, self.nspace)),
nn.BatchNorm2d(self.input_channels * self.nfscat, eps=1e-5, momentum=0.9, affine=False),
nn.Conv2d(self.input_channels * self.nfscat, l3_ichannels, kernel_size=3, padding=1),
nn.BatchNorm2d(l3_ichannels),
nn.ReLU(inplace=True),
)
elif arch_class == rn_cifar.ResNet:
self.base.conv1 = Scattering2D(J=self.J, shape=(self.target_size, self.target_size), frontend='torch')
self.base.bn1 = nn.Identity()
l2_ichannels = self.base.layer2[0].conv1.in_channels
self.base.layer1 = nn.Sequential(View(shape=(-1, self.input_channels * self.nfscat, self.nspace, self.nspace)),
nn.BatchNorm2d(self.input_channels * self.nfscat, eps=1e-5, affine=False),
nn.Conv2d(self.input_channels * self.nfscat, l2_ichannels, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(l2_ichannels),
nn.ReLU(inplace=True)
)
self.base.layer2[0].conv1.stride = (1,1)
out_channels2 = self.base.layer2[0].conv1.out_channels
self.base.layer2[0].shortcut = LambdaLayer(lambda x:
F.pad(x, (0, 0, 0, 0, out_channels2//4, out_channels2//4), "constant", 0))
self.base.layer3[0].conv1.stride = (1,1)
out_channels3 = self.base.layer3[0].conv1.out_channels
self.base.layer3[0].shortcut = LambdaLayer(lambda x:
F.pad(x, (0, 0, 0, 0, out_channels3//4, out_channels3//4), "constant", 0))
elif arch_class == wrn_cifar.WideResNet:
self.base.conv1 = Scattering2D(J=self.J, shape=(self.target_size, self.target_size), frontend='torch')
l2_ichannels = self.base.block2.layer[0].conv1.in_channels
self.base.block1 = nn.Sequential(View(shape=(-1, self.input_channels * self.nfscat, self.nspace, self.nspace)),
nn.BatchNorm2d(self.input_channels * self.nfscat, eps=1e-5, affine=False),
nn.Conv2d(self.input_channels * self.nfscat, l2_ichannels, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(l2_ichannels),
nn.ReLU(inplace=True)
)
self.base.block2.layer[0].conv1.stride = (1,1)
self.base.block2.layer[0].convShortcut.stride = (1,1)
self.base.block3.layer[0].conv1.stride = (1,1)
self.base.block3.layer[0].convShortcut.stride = (1,1)
else:
raise ValueError(f'Architecture {arch} is not supported by {self.__class__.__name__}.')
def forward(self, imgs: Tensor) -> Tensor:
logits = self.base(imgs)
return logits
class ScatteringClassifier(BasicAugmentation):
""" Scattering classifier.
See `BasicAugmentation` for a documentation of the available hyper-parameters.
"""
def create_model(self, arch: str, num_classes: int, input_channels: int = 3) -> nn.Module:
model = super(ScatteringClassifier, self).create_model(arch, num_classes=num_classes, input_channels=input_channels)
if not(isinstance(self.hparams['target_size'], int)):
raise(TypeError("The input spatial dimension target_size should be specified as an integer"))
model = ScatteringModel(model, arch, self.hparams['target_size'], self.hparams['J'], input_channels)
return model
def load_weights(self, model: nn.Module, path: str) -> nn.Module:
model_dict = model.state_dict()
loaded_dict = load(path)
# filter out keys with name 'tensor' since are from Scattering2D layer (not-trainable)
filtered_dict = {k: v for k, v in loaded_dict.items() if not('tensor' in k)}
model_dict.update(filtered_dict)
model.load_state_dict(model_dict)
return model
def get_loss_function(self) -> Callable:
return nn.CrossEntropyLoss(reduction='mean')
@staticmethod
def get_pipe_name():
return 'scattering'
@staticmethod
def default_hparams() -> dict:
return {
**super(ScatteringClassifier, ScatteringClassifier).default_hparams(),
'target_size' : 224,
'J' : 3
}