-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWideResNet.py
More file actions
239 lines (202 loc) · 7.47 KB
/
WideResNet.py
File metadata and controls
239 lines (202 loc) · 7.47 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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import BatchNorm2d
'''
As in the paper, the wide resnet only considers the resnet of the pre-activated version,
and it only considers the basic blocks rather than the bottleneck blocks.
'''
class BasicBlockPreAct(nn.Module):
def __init__(self, in_chan, out_chan, drop_rate=0, stride=1, pre_res_act=False):
super(BasicBlockPreAct, self).__init__()
self.bn1 = BatchNorm2d(in_chan, momentum=0.001)
self.relu1 = nn.LeakyReLU(inplace=True, negative_slope=0.1)
self.conv1 = nn.Conv2d(in_chan, out_chan, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = BatchNorm2d(out_chan, momentum=0.001)
self.relu2 = nn.LeakyReLU(inplace=True, negative_slope=0.1)
self.dropout = nn.Dropout(drop_rate) if not drop_rate == 0 else None
self.conv2 = nn.Conv2d(out_chan, out_chan, kernel_size=3, stride=1, padding=1, bias=False)
self.downsample = None
if in_chan != out_chan or stride != 1:
self.downsample = nn.Conv2d(
in_chan, out_chan, kernel_size=1, stride=stride, bias=False
)
self.pre_res_act = pre_res_act
# self.init_weight()
def forward(self, x):
bn1 = self.bn1(x)
act1 = self.relu1(bn1)
residual = self.conv1(act1)
residual = self.bn2(residual)
residual = self.relu2(residual)
if self.dropout is not None:
residual = self.dropout(residual)
residual = self.conv2(residual)
shortcut = act1 if self.pre_res_act else x
if self.downsample is not None:
shortcut = self.downsample(shortcut)
out = shortcut + residual
return out
def init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in', nonlinearity='leaky_relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
class WideResnetBackbone(nn.Module):
def __init__(self, k=1, n=28, drop_rate=0):
super(WideResnetBackbone, self).__init__()
self.k, self.n = k, n
assert (self.n - 4) % 6 == 0
n_blocks = (self.n - 4) // 6
n_layers = [16, ] + [self.k * 16 * (2 ** i) for i in range(3)] # k=2: n_layers=[16, 32, 64, 128]
self.conv1 = nn.Conv2d(
3,
n_layers[0],
kernel_size=3,
stride=1,
padding=1,
bias=False
)
self.layer1 = self.create_layer(
n_layers[0],
n_layers[1],
bnum=n_blocks,
stride=1,
drop_rate=drop_rate,
pre_res_act=True,
)
self.layer2 = self.create_layer(
n_layers[1],
n_layers[2],
bnum=n_blocks,
stride=2,
drop_rate=drop_rate,
pre_res_act=False,
)
self.layer3 = self.create_layer(
n_layers[2],
n_layers[3],
bnum=n_blocks,
stride=2,
drop_rate=drop_rate,
pre_res_act=False,
)
self.bn_last = BatchNorm2d(n_layers[3], momentum=0.001)
self.relu_last = nn.LeakyReLU(inplace=True, negative_slope=0.1)
self.init_weight()
def create_layer(
self,
in_chan,
out_chan,
bnum,
stride=1,
drop_rate=0,
pre_res_act=False,
):
layers = [
BasicBlockPreAct(
in_chan,
out_chan,
drop_rate=drop_rate,
stride=stride,
pre_res_act=pre_res_act), ]
for _ in range(bnum - 1):
layers.append(
BasicBlockPreAct(
out_chan,
out_chan,
drop_rate=drop_rate,
stride=1,
pre_res_act=False, ))
return nn.Sequential(*layers)
def forward(self, x):
feat = self.conv1(x)
feat = self.layer1(feat)
feat2 = self.layer2(feat) # 1/2
feat4 = self.layer3(feat2) # 1/4
feat4 = self.bn_last(feat4)
feat4 = self.relu_last(feat4)
return feat2, feat4
def init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in', nonlinearity='leaky_relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.bias.data.zero_()
class Normalize(nn.Module):
def __init__(self, power=2):
super(Normalize, self).__init__()
self.power = power
def forward(self, x):
norm = x.pow(self.power).sum(1, keepdim=True).pow(1. / self.power)
out = x.div(norm)
return out
class WideResnet(nn.Module):
'''
for wide-resnet-28-10, the definition should be WideResnet(n_classes, 10, 28)
'''
def __init__(self, n_classes, k=1, n=28, low_dim=64, proj=True, two_classifier=False):
super(WideResnet, self).__init__()
self.n_layers, self.k = n, k
self.backbone = WideResnetBackbone(k=k, n=n)
self.classifier = nn.Linear(64 * self.k, n_classes, bias=True)
self.two_classifier = two_classifier
if two_classifier:
self.classifier_aux = nn.Linear(64 * self.k, n_classes, bias=True) ###
self.proj = proj
if proj:
self.l2norm = Normalize(2)
self.fc1 = nn.Linear(64 * self.k, 64 * self.k)
self.relu_mlp = nn.LeakyReLU(inplace=True, negative_slope=0.1)
self.fc2 = nn.Linear(64 * self.k, low_dim)
def forward(self, x, out_fea=False, out_feat_map=False):
feat = self.backbone(x)[-1] # feat:[bs, 128, 8, 8]
feat_map_last = feat ##
feat = torch.mean(feat, dim=(2, 3)) # GAP
out = self.classifier(feat)
if self.two_classifier:
feat = feat.detach()
out_aux = self.classifier_aux(feat)
if self.proj:
feat = self.fc1(feat)
feat = self.relu_mlp(feat)
feat = self.fc2(feat)
feat = self.l2norm(feat)
return out,feat
elif out_fea:
if out_feat_map:
return out, feat, feat_map_last
else:
return out, feat
elif self.two_classifier: ### Two-classifier for matching distribution of all-data & labeled-data
return out, out_aux
else:
return out
def init_weight(self):
nn.init.xavier_normal_(self.classifier.weight)
if not self.classifier.bias is None:
nn.init.constant_(self.classifier.bias, 0)
if __name__ == "__main__":
x = torch.randn(2, 3, 224, 224)
lb = torch.randint(0, 10, (2,)).long()
net = WideResnetBackbone()
out = net(x)
print(out[0].size())
del net, out
net = WideResnet(n_classes=10)
criteria = nn.CrossEntropyLoss()
out = net(x)
loss = criteria(out, lb)
loss.backward()
print(out.size())