Repository navigation
Expand file tree
/
Copy pathnetwork.py
More file actions
349 lines (313 loc) · 12.6 KB
/
Copy pathnetwork.py
File metadata and controls
349 lines (313 loc) · 12.6 KB
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
import numpy as np
import torch
import torch.nn as nn
import torchvision
from torchvision import models
from torch.autograd import Variable
import torch.utils.model_zoo as model_zoo
import math
def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)
def init_weights(m):
classname = m.__class__.__name__
if classname.find('Conv2d') != -1 or classname.find('ConvTranspose2d') != -1:
nn.init.kaiming_uniform_(m.weight)
nn.init.zeros_(m.bias)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight, 1.0, 0.02)
nn.init.zeros_(m.bias)
elif classname.find('Linear') != -1:
nn.init.xavier_normal_(m.weight)
nn.init.zeros_(m.bias)
resnet_dict = {"ResNet18":models.resnet18, "ResNet34":models.resnet34, "ResNet50":models.resnet50, "ResNet101":models.resnet101, "ResNet152":models.resnet152}
def grl_hook(coeff):
def fun1(grad):
return -coeff*grad.clone()
return fun1
def scale_hook(coeff):
def fun1(grad):
return coeff*grad.clone()
return fun1
from torch.nn.utils.weight_norm import WeightNorm
class distLinear(nn.Module):
def __init__(self, indim, outdim, scale_factor=10):
super(distLinear, self).__init__()
self.indim = indim
self.outdim = outdim
self.L = nn.Linear( indim, outdim, bias = False)
nn.init.xavier_normal_(self.L.weight)
self.class_wise_learnable_norm = True #See the issue#4&8 in the github
if self.class_wise_learnable_norm:
WeightNorm.apply(self.L, 'weight', dim=0) #split the weight update component to direction and norm
self.scale_factor = 10; #in omniglot, a larger scale factor is required to handle >1000 output classes.
def forward(self, x_normalized):
if not self.class_wise_learnable_norm:
L_norm = torch.norm(self.L.weight.data, p=2, dim =1).unsqueeze(1).expand_as(self.L.weight.data)
self.L.weight.data = self.L.weight.data.div(L_norm + 0.00001)
cos_dist = self.L(x_normalized) #matrix product by forward function, but when using WeightNorm, this also multiply the cosine distance by a class-wise learnable norm, see the issue#4&8 in the github
scores = self.scale_factor* (cos_dist)
return scores
class ResNetFc(nn.Module):
def __init__(self, resnet_name, use_bottleneck=True, bottleneck_dim=256, new_cls=False, class_num=1000,
cos_dist=False):
super(ResNetFc, self).__init__()
model_resnet = resnet_dict[resnet_name](pretrained=True)
self.conv1 = model_resnet.conv1
self.bn1 = model_resnet.bn1
self.relu = model_resnet.relu
self.maxpool = model_resnet.maxpool
self.layer1 = model_resnet.layer1
self.layer2 = model_resnet.layer2
self.layer3 = model_resnet.layer3
self.layer4 = model_resnet.layer4
self.avgpool = model_resnet.avgpool
self.feature_layers = nn.Sequential(self.conv1, self.bn1, self.relu, self.maxpool, \
self.layer1, self.layer2, self.layer3, self.layer4, self.avgpool)
self.use_bottleneck = use_bottleneck
self.new_cls = new_cls
self.cos_dist = cos_dist
self.class_num = class_num
if new_cls:
if self.use_bottleneck:
self.bottleneck = nn.Linear(model_resnet.fc.in_features, bottleneck_dim)
self.bottleneck.apply(init_weights)
if cos_dist:
self.fc = distLinear(bottleneck_dim, class_num, scale_factor=cos_dist)
else:
self.fc = nn.Linear(bottleneck_dim, class_num)
self.fc.apply(init_weights)
self.__in_features = bottleneck_dim
else:
if cos_dist:
self.fc = distLinear(bottleneck_dim, class_num, scale_factor=cos_dist)
else:
self.fc = nn.Linear(model_resnet.fc.in_features, class_num)
self.fc.apply(init_weights)
self.__in_features = model_resnet.fc.in_features
else:
self.fc = model_resnet.fc
self.__in_features = model_resnet.fc.in_features
def forward(self, x):
x = self.feature_layers(x)
x = x.view(x.size(0), -1)
if self.use_bottleneck and self.new_cls:
x = self.bottleneck(x)
if self.cos_dist:
x_norm = torch.norm(x, p=2, dim =1).unsqueeze(1).expand_as(x)
x = x.div(x_norm+ 0.00001)
y = self.fc(x)
return x, y
def output_num(self):
return self.__in_features
def get_parameters(self):
if self.new_cls:
if self.use_bottleneck:
parameter_list = [{"params":self.feature_layers.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.bottleneck.parameters(), "lr_mult":10, 'decay_mult':2}, \
{"params":self.fc.parameters(), "lr_mult":10, 'decay_mult':2}]
else:
parameter_list = [{"params":self.feature_layers.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.fc.parameters(), "lr_mult":10, 'decay_mult':2}]
else:
parameter_list = [{"params":self.parameters(), "lr_mult":1, 'decay_mult':2}]
return parameter_list
vgg_dict = {"VGG11":models.vgg11, "VGG13":models.vgg13, "VGG16":models.vgg16, "VGG19":models.vgg19, "VGG11BN":models.vgg11_bn, "VGG13BN":models.vgg13_bn, "VGG16BN":models.vgg16_bn, "VGG19BN":models.vgg19_bn}
class VGGFc(nn.Module):
def __init__(self, vgg_name, use_bottleneck=True, bottleneck_dim=256, new_cls=False, class_num=1000):
super(VGGFc, self).__init__()
model_vgg = vgg_dict[vgg_name](pretrained=True)
self.features = model_vgg.features
self.classifier = nn.Sequential()
for i in range(6):
self.classifier.add_module("classifier"+str(i), model_vgg.classifier[i])
self.feature_layers = nn.Sequential(self.features, self.classifier)
self.use_bottleneck = use_bottleneck
self.new_cls = new_cls
if new_cls:
if self.use_bottleneck:
self.bottleneck = nn.Linear(4096, bottleneck_dim)
self.fc = nn.Linear(bottleneck_dim, class_num)
self.bottleneck.apply(init_weights)
self.fc.apply(init_weights)
self.__in_features = bottleneck_dim
else:
self.fc = nn.Linear(4096, class_num)
self.fc.apply(init_weights)
self.__in_features = 4096
else:
self.fc = model_vgg.classifier[6]
self.__in_features = 4096
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
if self.use_bottleneck and self.new_cls:
x = self.bottleneck(x)
y = self.fc(x)
return x, y
def output_num(self):
return self.__in_features
def get_parameters(self):
if self.new_cls:
if self.use_bottleneck:
parameter_list = [{"params":self.features.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.classifier.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.bottleneck.parameters(), "lr_mult":10, 'decay_mult':2}, \
{"params":self.fc.parameters(), "lr_mult":10, 'decay_mult':2}]
else:
parameter_list = [{"params":self.feature_layers.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.classifier.parameters(), "lr_mult":1, 'decay_mult':2}, \
{"params":self.fc.parameters(), "lr_mult":10, 'decay_mult':2}]
else:
parameter_list = [{"params":self.parameters(), "lr_mult":1, 'decay_mult':2}]
return parameter_list
# For SVHN dataset
class SVHN_EnsembNet(nn.Module):
def __init__(self):
super().__init__()
self.conv_params = nn.Sequential (
nn.Conv2d(3, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Dropout2d(0.5),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Dropout2d(0.5)
)
self.fc_params = nn.Sequential (
nn.Conv2d(256, 512, kernel_size=3, padding=0),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 256, kernel_size=1, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 128, kernel_size=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.AvgPool2d(6)
)
self.classifier = nn.Linear(128, 10)
self.__in_features = 128
def forward(self, x):
x = self.conv_params(x)
x = self.fc_params(x)
x = x.view(x.size(0), -1)
y = self.classifier(x)
return x, y
def output_num(self):
return self.__in_features
class USPS_EnsembNet(nn.Module):
def __init__(self):
super().__init__()
self.conv_params = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=5),
nn.BatchNorm2d(32),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Dropout2d(p=0.5),
)
self.fc_params = nn.Sequential(nn.Linear(64*4*4, 256), nn.ReLU())
self.classifier = nn.Linear(256, 10)
self.__in_features = 256
def forward(self, x):
x = self.conv_params(x)
x = x.view(x.size(0), -1)
x = self.fc_params(x)
y = self.classifier(x)
return x, y
def output_num(self):
return self.__in_features
class AdversarialNetwork(nn.Module):
def __init__(self, in_feature, hidden_size):
super(AdversarialNetwork, self).__init__()
self.ad_layer1 = nn.Linear(in_feature, hidden_size)
self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
self.ad_layer3 = nn.Linear(hidden_size, 1)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.dropout1 = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.5)
self.sigmoid = nn.Sigmoid()
self.apply(init_weights)
self.iter_num = 0
self.alpha = 10
self.low = 0.0
self.high = 1.0
self.max_iter = 10000.0
def forward(self, x):
if self.training:
self.iter_num += 1
coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
x = x * 1.0
x.register_hook(grl_hook(coeff))
x = self.ad_layer1(x)
x = self.relu1(x)
x = self.dropout1(x)
x = self.ad_layer2(x)
x = self.relu2(x)
x = self.dropout2(x)
y = self.ad_layer3(x)
y = self.sigmoid(y)
return y
def output_num(self):
return 1
def get_parameters(self):
return [{"params":self.parameters(), "lr_mult":10, 'decay_mult':2}]
class Multi_AdversarialNetwork(nn.Module):
def __init__(self, in_feature, hidden_size, class_num):
super(Multi_AdversarialNetwork, self).__init__()
self.ad_layer1 = nn.Linear(in_feature, hidden_size)
self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
self.ad_layer3 = nn.Linear(hidden_size, class_num)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.dropout1 = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.5)
self.sigmoid = nn.Sigmoid()
self.apply(init_weights)
self.iter_num = 0
self.alpha = 10
self.low = 0.0
self.high = 1.0
self.max_iter = 10000.0
def forward(self, x, grl=True):
if self.training:
self.iter_num += 1
if grl and self.training:
coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
x = x * 1.0
x.register_hook(grl_hook(coeff))
x = self.ad_layer1(x)
x = self.relu1(x)
x = self.dropout1(x)
x = self.ad_layer2(x)
x = self.relu2(x)
x = self.dropout2(x)
y = self.ad_layer3(x)
return y
def output_num(self):
return 1
def get_parameters(self):
return [{"params":self.parameters(), "lr_mult":10, 'decay_mult':2}]