-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodels.py
208 lines (159 loc) · 6.03 KB
/
models.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
import torch
import torch.nn as nn
def standardize(x, bn_stats):
if bn_stats is None:
return x
bn_mean, bn_var = bn_stats
view = [1] * len(x.shape)
view[1] = -1
x = (x - bn_mean.view(view)) / torch.sqrt(bn_var.view(view) + 1e-5)
# if variance is too low, just ignore
x *= (bn_var.view(view) != 0).float()
return x
def clip_data(data, max_norm):
norms = torch.norm(data.reshape(data.shape[0], -1), dim=-1)
scale = (max_norm / norms).clamp(max=1.0)
data *= scale.reshape(-1, 1, 1, 1)
return data
def get_num_params(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
class StandardizeLayer(nn.Module):
def __init__(self, bn_stats):
super(StandardizeLayer, self).__init__()
self.bn_stats = bn_stats
def forward(self, x):
return standardize(x, self.bn_stats)
class ClipLayer(nn.Module):
def __init__(self, max_norm):
super(ClipLayer, self).__init__()
self.max_norm = max_norm
def forward(self, x):
return clip_data(x, self.max_norm)
class CIFAR10_CNN(nn.Module):
def __init__(self, in_channels=3, input_norm=None, **kwargs):
super(CIFAR10_CNN, self).__init__()
self.in_channels = in_channels
self.features = None
self.classifier = None
self.norm = None
self.build(input_norm, **kwargs)
def build(self, input_norm=None, num_groups=None,
bn_stats=None, size=None):
if self.in_channels == 3:
if size == "small":
cfg = [16, 16, 'M', 32, 32, 'M', 64, 'M']
else:
cfg = [32, 32, 'M', 64, 64, 'M', 128, 128, 'M']
self.norm = nn.Identity()
else:
if size == "small":
cfg = [16, 16, 'M', 32, 32]
else:
cfg = [64, 'M', 64]
if input_norm is None:
self.norm = nn.Identity()
elif input_norm == "GroupNorm":
self.norm = nn.GroupNorm(num_groups, self.in_channels, affine=False)
else:
self.norm = lambda x: standardize(x, bn_stats)
layers = []
act = nn.Tanh
c = self.in_channels
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(c, v, kernel_size=3, stride=1, padding=1)
layers += [conv2d, act()]
c = v
self.features = nn.Sequential(*layers)
if self.in_channels == 3:
hidden = 128
self.classifier = nn.Sequential(nn.Linear(c * 4 * 4, hidden), act(), nn.Linear(hidden, 10))
else:
self.classifier = nn.Linear(c * 4 * 4, 10)
def forward(self, x):
if self.in_channels != 3:
x = self.norm(x.view(-1, self.in_channels, 8, 8))
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
class MNIST_CNN(nn.Module):
def __init__(self, in_channels=1, input_norm=None, **kwargs):
super(MNIST_CNN, self).__init__()
self.in_channels = in_channels
self.features = None
self.classifier = None
self.norm = None
self.build(input_norm, **kwargs)
def build(self, input_norm=None, num_groups=None,
bn_stats=None, size=None):
if self.in_channels == 1:
ch1, ch2 = (16, 32) if size is None else (32, 64)
cfg = [(ch1, 8, 2, 2), 'M', (ch2, 4, 2, 0), 'M']
self.norm = nn.Identity()
else:
ch1, ch2 = (16, 32) if size is None else (32, 64)
cfg = [(ch1, 3, 2, 1), (ch2, 3, 1, 1)]
if input_norm == "GroupNorm":
self.norm = nn.GroupNorm(num_groups, self.in_channels, affine=False)
elif input_norm == "BN":
self.norm = lambda x: standardize(x, bn_stats)
else:
self.norm = nn.Identity()
layers = []
c = self.in_channels
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=1)]
else:
filters, k_size, stride, pad = v
conv2d = nn.Conv2d(c, filters, kernel_size=k_size, stride=stride, padding=pad)
layers += [conv2d, nn.Tanh()]
c = filters
self.features = nn.Sequential(*layers)
hidden = 32
self.classifier = nn.Sequential(nn.Linear(c * 4 * 4, hidden),
nn.Tanh(),
nn.Linear(hidden, 10))
def forward(self, x):
if self.in_channels != 1:
x = self.norm(x.view(-1, self.in_channels, 7, 7))
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
class ScatterLinear(nn.Module):
def __init__(self, in_channels, hw_dims, input_norm=None, classes=10, clip_norm=None, **kwargs):
super(ScatterLinear, self).__init__()
self.K = in_channels
self.h = hw_dims[0]
self.w = hw_dims[1]
self.fc = None
self.norm = None
self.clip = None
self.build(input_norm, classes=classes, clip_norm=clip_norm, **kwargs)
def build(self, input_norm=None, num_groups=None, bn_stats=None, clip_norm=None, classes=10):
self.fc = nn.Linear(self.K * self.h * self.w, classes)
if input_norm is None:
self.norm = nn.Identity()
elif input_norm == "GroupNorm":
self.norm = nn.GroupNorm(num_groups, self.K, affine=False)
else:
self.norm = lambda x: standardize(x, bn_stats)
if clip_norm is None:
self.clip = nn.Identity()
else:
self.clip = ClipLayer(clip_norm)
def forward(self, x):
x = self.norm(x.view(-1, self.K, self.h, self.w))
x = self.clip(x)
x = x.reshape(x.size(0), -1)
x = self.fc(x)
return x
CNNS = {
"cifar10": CIFAR10_CNN,
"fmnist": MNIST_CNN,
"mnist": MNIST_CNN,
}