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= ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
|
7 |
+
'resnet152']
|
8 |
+
|
9 |
+
|
10 |
+
model_urls = {
|
11 |
+
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
|
12 |
+
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
|
13 |
+
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
|
14 |
+
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
|
15 |
+
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
|
16 |
+
}
|
17 |
+
|
18 |
+
|
19 |
+
def conv3x3(in_planes, out_planes, stride=1):
|
20 |
+
"3x3 convolution with padding"
|
21 |
+
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
|
22 |
+
padding=1, bias=False)
|
23 |
+
|
24 |
+
|
25 |
+
class BasicBlock(nn.Module):
|
26 |
+
expansion = 1
|
27 |
+
|
28 |
+
def __init__(self, inplanes, planes, stride=1, downsample=None):
|
29 |
+
super(BasicBlock, self).__init__()
|
30 |
+
self.conv1 = conv3x3(inplanes, planes, stride)
|
31 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
32 |
+
self.relu = nn.ReLU(inplace=True)
|
33 |
+
self.conv2 = conv3x3(planes, planes)
|
34 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
35 |
+
self.downsample = downsample
|
36 |
+
self.stride = stride
|
37 |
+
|
38 |
+
def forward(self, x):
|
39 |
+
residual = x
|
40 |
+
|
41 |
+
out = self.conv1(x)
|
42 |
+
out = self.bn1(out)
|
43 |
+
out = self.relu(out)
|
44 |
+
|
45 |
+
out = self.conv2(out)
|
46 |
+
out = self.bn2(out)
|
47 |
+
|
48 |
+
if self.downsample is not None:
|
49 |
+
residual = self.downsample(x)
|
50 |
+
|
51 |
+
out += residual
|
52 |
+
out = self.relu(out)
|
53 |
+
|
54 |
+
return out
|
55 |
+
|
56 |
+
|
57 |
+
class Bottleneck(nn.Module):
|
58 |
+
expansion = 4
|
59 |
+
|
60 |
+
def __init__(self, inplanes, planes, stride=1, downsample=None):
|
61 |
+
super(Bottleneck, self).__init__()
|
62 |
+
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
|
63 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
64 |
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
|
65 |
+
padding=1, bias=False)
|
66 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
67 |
+
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
|
68 |
+
self.bn3 = nn.BatchNorm2d(planes * 4)
|
69 |
+
self.relu = nn.ReLU(inplace=True)
|
70 |
+
self.downsample = downsample
|
71 |
+
self.stride = stride
|
72 |
+
|
73 |
+
def forward(self, x):
|
74 |
+
residual = x
|
75 |
+
|
76 |
+
out = self.conv1(x)
|
77 |
+
out = self.bn1(out)
|
78 |
+
out = self.relu(out)
|
79 |
+
|
80 |
+
out = self.conv2(out)
|
81 |
+
out = self.bn2(out)
|
82 |
+
out = self.relu(out)
|
83 |
+
|
84 |
+
out = self.conv3(out)
|
85 |
+
out = self.bn3(out)
|
86 |
+
|
87 |
+
if self.downsample is not None:
|
88 |
+
residual = self.downsample(x)
|
89 |
+
|
90 |
+
out += residual
|
91 |
+
out = self.relu(out)
|
92 |
+
|
93 |
+
return out
|
94 |
+
|
95 |
+
|
96 |
+
class ResNet(nn.Module):
|
97 |
+
|
98 |
+
def __init__(self, block, layers, num_classes=1000):
|
99 |
+
self.inplanes = 64
|
100 |
+
super(ResNet, self).__init__()
|
101 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
|
102 |
+
bias=False)
|
103 |
+
self.bn1 = nn.BatchNorm2d(64)
|
104 |
+
self.relu = nn.ReLU(inplace=True)
|
105 |
+
#self.maxpool = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) # previous stride is 2
|
106 |
+
self.layer1 = self._make_layer(block, 64, layers[0])
|
107 |
+
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
108 |
+
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
109 |
+
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
110 |
+
self.avgpool = nn.AvgPool2d(14)
|
111 |
+
self.fc = nn.Linear(512 * block.expansion, num_classes)
|
112 |
+
|
113 |
+
for m in self.modules():
|
114 |
+
if isinstance(m, nn.Conv2d):
|
115 |
+
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
|
116 |
+
m.weight.data.normal_(0, math.sqrt(2. / n))
|
117 |
+
elif isinstance(m, nn.BatchNorm2d):
|
118 |
+
#m.weight.data.fill_(1)
|
119 |
+
#m.bias.data.zero_()
|
120 |
+
nn.init.constant_(m.weight, 1)
|
121 |
+
nn.init.constant_(m.bias, 0)
|
122 |
+
|
123 |
+
def _make_layer(self, block, planes, blocks, stride=1):
|
124 |
+
downsample = None
|
125 |
+
if stride != 1 or self.inplanes != planes * block.expansion:
|
126 |
+
downsample = nn.Sequential(
|
127 |
+
nn.Conv2d(self.inplanes, planes * block.expansion,
|
128 |
+
kernel_size=1, stride=stride, bias=False),
|
129 |
+
nn.BatchNorm2d(planes * block.expansion),
|
130 |
+
)
|
131 |
+
|
132 |
+
layers = []
|
133 |
+
layers.append(block(self.inplanes, planes, stride, downsample))
|
134 |
+
self.inplanes = planes * block.expansion
|
135 |
+
for i in range(1, blocks):
|
136 |
+
layers.append(block(self.inplanes, planes))
|
137 |
+
|
138 |
+
return nn.Sequential(*layers)
|
139 |
+
|
140 |
+
def forward(self, x):
|
141 |
+
x = self.conv1(x)
|
142 |
+
x = self.bn1(x)
|
143 |
+
x = self.relu(x)
|
144 |
+
#x = self.maxpool(x)
|
145 |
+
|
146 |
+
x = self.layer1(x)
|
147 |
+
x = self.layer2(x)
|
148 |
+
x = self.layer3(x)
|
149 |
+
x = self.layer4(x)
|
150 |
+
|
151 |
+
x = self.avgpool(x)
|
152 |
+
x = x.view(x.size(0), -1)
|
153 |
+
x = self.fc(x)
|
154 |
+
|
155 |
+
return x
|
156 |
+
|
157 |
+
|
158 |
+
def resnet18(pretrained=False, **kwargs):
|
159 |
+
"""Constructs a ResNet-18 model.
|
160 |
+
|
161 |
+
Args:
|
162 |
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
163 |
+
"""
|
164 |
+
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
|
165 |
+
if pretrained:
|
166 |
+
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
|
167 |
+
return model
|
168 |
+
|
169 |
+
|
170 |
+
def resnet34(pretrained=False, **kwargs):
|
171 |
+
"""Constructs a ResNet-34 model.
|
172 |
+
|
173 |
+
Args:
|
174 |
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
175 |
+
"""
|
176 |
+
model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
|
177 |
+
if pretrained:
|
178 |
+
model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
|
179 |
+
return model
|
180 |
+
|
181 |
+
|
182 |
+
def resnet50(pretrained=False, **kwargs):
|
183 |
+
"""Constructs a ResNet-50 model.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
187 |
+
"""
|
188 |
+
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
|
189 |
+
if pretrained:
|
190 |
+
model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
|
191 |
+
return model
|
192 |
+
|
193 |
+
|
194 |
+
def resnet101(pretrained=False, **kwargs):
|
195 |
+
"""Constructs a ResNet-101 model.
|
196 |
+
|
197 |
+
Args:
|
198 |
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
199 |
+
"""
|
200 |
+
model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
|
201 |
+
if pretrained:
|
202 |
+
model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
|
203 |
+
return model
|
204 |
+
|
205 |
+
|
206 |
+
def resnet152(pretrained=False, **kwargs):
|
207 |
+
"""Constructs a ResNet-152 model.
|
208 |
+
|
209 |
+
Args:
|
210 |
+
pretrained (bool): If True, returns a model pre-trained on ImageNet
|
211 |
+
"""
|
212 |
+
model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
|
213 |
+
if pretrained:
|
214 |
+
model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
|
215 |
+
return model
|