forked from Sakura03/SemiMeta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
219 lines (180 loc) · 7.61 KB
/
model.py
File metadata and controls
219 lines (180 loc) · 7.61 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
import torch, math
from torch import nn
from torch.autograd import Function
from torch.nn import functional as F
####################################################################
###################### ConvLarge Architecture ######################
####################################################################
class ConvLarge(nn.Module):
def __init__(self, input_dim=3, num_classes=10, stochastic=True, top_bn=False):
super(ConvLarge, self).__init__()
self.block1 = self.conv_block(input_dim, 128, 3, 1, 1, 0.1)
self.block2 = self.conv_block(128, 128, 3, 1, 1, 0.1)
self.block3 = self.conv_block(128, 128, 3, 1, 1, 0.1)
self.block4 = self.conv_block(128, 256, 3, 1, 1, 0.1)
self.block5 = self.conv_block(256, 256, 3, 1, 1, 0.1)
self.block6 = self.conv_block(256, 256, 3, 1, 1, 0.1)
self.block7 = self.conv_block(256, 512, 3, 1, 0, 0.1)
self.block8 = self.conv_block(512, 256, 1, 1, 0, 0.1)
self.block9 = self.conv_block(256, 128, 1, 1, 0, 0.1)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
maxpool = [nn.MaxPool2d(kernel_size=2, stride=2)]
if stochastic:
maxpool.append(nn.Dropout2d())
self.maxpool = nn.Sequential(*maxpool)
classifier = [nn.Linear(128, num_classes)]
if top_bn:
classifier.append(nn.BatchNorm1d(num_classes))
self.classifier = nn.Sequential(*classifier)
def conv_block(self, input_dim, out_dim, kernel_size=3, stride=1, padding=1, lrelu_slope=0.01):
return nn.Sequential(
nn.Conv2d(input_dim, out_dim, kernel_size, stride, padding, bias=False),
nn.BatchNorm2d(out_dim),
nn.LeakyReLU(inplace=True, negative_slope=lrelu_slope)
)
def forward(self, x):
out = self.block1(x)
out = self.block2(out)
out = self.block3(out)
out = self.maxpool(out)
out = self.block4(out)
out = self.block5(out)
out = self.block6(out)
out = self.maxpool(out)
out = self.block7(out)
out = self.block8(out)
out = self.block9(out)
feature = self.avg_pool(out)
feature = feature.view(feature.shape[0], -1)
logits = self.classifier(feature)
return logits
######################################################################
###################### Shake-Shake Architecture ######################
######################################################################
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
class ShakeShakeBlock(nn.Module):
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(ShakeShakeBlock, self).__init__()
self.conv_a1 = conv3x3(inplanes, planes, stride)
self.bn_a1 = nn.BatchNorm2d(planes)
self.conv_a2 = conv3x3(planes, planes)
self.bn_a2 = nn.BatchNorm2d(planes)
self.conv_b1 = conv3x3(inplanes, planes, stride)
self.bn_b1 = nn.BatchNorm2d(planes)
self.conv_b2 = conv3x3(planes, planes)
self.bn_b2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
a, b, residual = x, x, x
a = F.relu(a, inplace=False)
a = self.conv_a1(a)
a = self.bn_a1(a)
a = F.relu(a, inplace=True)
a = self.conv_a2(a)
a = self.bn_a2(a)
b = F.relu(b, inplace=False)
b = self.conv_b1(b)
b = self.bn_b1(b)
b = F.relu(b, inplace=True)
b = self.conv_b2(b)
b = self.bn_b2(b)
ab = shake(a, b, training=self.training)
if self.downsample is not None:
residual = self.downsample(x)
return residual + ab
class Shake(Function):
@staticmethod
def forward(ctx, inp1, inp2, training):
assert inp1.size() == inp2.size()
gate = inp1.new(inp1.size(0), 1, 1, 1)
if training:
gate.uniform_(0, 1)
else:
gate.fill_(0.5)
return inp1 * gate + inp2 * (1. - gate)
@staticmethod
def backward(ctx, grad_output):
grad_inp1 = grad_inp2 = grad_training = None
gate = grad_output.data.new(grad_output.size(0), 1, 1, 1).uniform_(0, 1)
if ctx.needs_input_grad[0]:
grad_inp1 = grad_output * gate
if ctx.needs_input_grad[1]:
grad_inp2 = grad_output * (1. - gate)
assert not ctx.needs_input_grad[2]
return grad_inp1, grad_inp2, grad_training
def shake(inp1, inp2, training=False):
return Shake.apply(inp1, inp2, training)
class ShiftConvDownsample(nn.Module):
def __init__(self, in_channels, out_channels):
super(ShiftConvDownsample, self).__init__()
self.relu = nn.ReLU(inplace=True)
self.conv = nn.Conv2d(2*in_channels, out_channels, kernel_size=1, groups=2)
self.bn = nn.BatchNorm2d(out_channels)
def forward(self, x):
x = torch.cat((x[:, :, 0::2, 0::2], x[:, :, 1::2, 1::2]), dim=1)
x = self.relu(x)
x = self.conv(x)
x = self.bn(x)
return x
class ResNet32x32(nn.Module):
def __init__(self, block, layers, channels, num_classes=1000, downsample='basic'):
super(ResNet32x32, self).__init__()
assert len(layers) == 3 and downsample in ['basic', 'shift_conv']
self.downsample_mode = downsample
self.inplanes = 16
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False)
self.layer1 = self._make_layer(block, channels, layers[0])
self.layer2 = self._make_layer(block, channels * 2, layers[1], stride=2)
self.layer3 = self._make_layer(block, channels * 4, layers[2], stride=2)
self.avgpool = nn.AvgPool2d(8)
self.fc = nn.Linear(channels * 4, num_classes)
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))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes:
if self.downsample_mode == 'basic' or stride == 1:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes)
)
elif self.downsample_mode == 'shift_conv':
downsample = ShiftConvDownsample(in_channels=self.inplanes, out_channels=planes)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
return self.fc(x)
def shakeshake26(**kwargs):
model = ResNet32x32(
ShakeShakeBlock,
layers=[4, 4, 4],
channels=96,
downsample='shift_conv', **kwargs
)
return model
if __name__ == '__main__':
model = ConvLarge(input_dim=3)
img = torch.randn(5, 3, 32, 32)
logits = model(img)
print(logits.shape)
model = shakeshake26(num_classes=10)
logits = model(img)
print(logits.shape)