This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcifar_resnet.py
178 lines (129 loc) · 4.71 KB
/
cifar_resnet.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
"""
ResNet for CIFAR dataset proposed in He+15, p 7. and
https://github.com/facebook/fb.resnet.torch/blob/master/models/resnet.lua
"""
import torch.nn as nn
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 BasicBlock(nn.Module):
def __init__(self, inplanes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
if inplanes != planes:
self.downsample = nn.Sequential(nn.Conv2d(inplanes, planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes))
else:
self.downsample = lambda x: x
self.stride = stride
def forward(self, x):
residual = self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.relu(out)
return out
class PreActBasicBlock(BasicBlock):
def __init__(self, inplanes, planes, stride):
super(PreActBasicBlock, self).__init__(inplanes, planes, stride)
if inplanes != planes:
self.downsample = nn.Sequential(nn.Conv2d(inplanes, planes, kernel_size=1, stride=stride, bias=False))
else:
self.downsample = lambda x: x
self.bn1 = nn.BatchNorm2d(inplanes)
def forward(self, x):
residual = self.downsample(x)
out = self.bn1(x)
out = self.relu(out)
out = self.conv1(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out += residual
return out
class ResNet(nn.Module):
def __init__(self, block, n_size, num_classes=10):
super(ResNet, self).__init__()
self.inplane = 16
self.conv1 = nn.Conv2d(3, self.inplane, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(self.inplane)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(block, 16, blocks=n_size, stride=1)
self.layer2 = self._make_layer(block, 32, blocks=n_size, stride=2)
self.layer3 = self._make_layer(block, 64, blocks=n_size, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Linear(64, num_classes)
self.initialize()
def initialize(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal(m.weight)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant(m.weight, 1)
nn.init.constant(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride):
strides = [stride] + [1] * (blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.inplane, planes, stride))
self.inplane = planes
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class PreActResNet(ResNet):
def __init__(self, block, n_size, num_classes=10):
super(PreActResNet, self).__init__(block, n_size, num_classes)
self.bn1 = nn.BatchNorm2d(self.inplane)
self.initialize()
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.bn1(x)
x = self.relu(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def resnet20(**kwargs):
model = ResNet(BasicBlock, 3, **kwargs)
return model
def resnet32(**kwargs):
model = ResNet(BasicBlock, 5, **kwargs)
return model
def resnet56(**kwargs):
model = ResNet(BasicBlock, 9, **kwargs)
return model
def resnet110(**kwargs):
model = ResNet(BasicBlock, 18, **kwargs)
return model
def preact_resnet20(**kwargs):
model = PreActResNet(PreActBasicBlock, 3, **kwargs)
return model
def preact_resnet32(**kwargs):
model = PreActResNet(PreActBasicBlock, 5, **kwargs)
return model
def preact_resnet56(**kwargs):
model = PreActResNet(PreActBasicBlock, 9, **kwargs)
return model
def preact_resnet110(**kwargs):
model = PreActResNet(PreActBasicBlock, 18, **kwargs)
return model