-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
214 lines (151 loc) · 6.55 KB
/
model.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
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
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
expansion = 1
def __init__(self, n_in, n_out, stride = 1, downsample=False):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels=n_in, out_channels=n_out, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(num_features=n_out)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels=n_out, out_channels=n_out*self.expansion, kernel_size=3,
stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(num_features=n_out)
self.downsample = None
if downsample:
self.downsample = nn.Sequential(OrderedDict([
('conv', nn.Conv2d(in_channels=n_in, out_channels=n_out*self.expansion, kernel_size=1,
stride=stride, padding=0, bias=False)),
('bn', nn.BatchNorm2d(num_features=n_out*self.expansion))
]))
def forward(self, x):
x_shortcut = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
if self.downsample:
x_shortcut = self.downsample(x_shortcut)
x = x + x_shortcut
x = self.relu(x)
return x
class ResidualBottleneckBlock(nn.Module):
expansion = 4
def __init__(
self, n_in, n_out, stride = 1, downsample=False):
super(ResidualBottleneckBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels=n_in, out_channels=n_out, kernel_size=1,
stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(n_out)
self.conv2 = nn.Conv2d(in_channels=n_out, out_channels=n_out, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(n_out)
self.conv3 = nn.Conv2d(in_channels=n_out, out_channels=n_out*self.expansion, kernel_size=1,
stride=1, padding=0, bias=False)
self.bn3 = nn.BatchNorm2d(n_out*self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = None
if downsample:
self.downsample = nn.Sequential(OrderedDict([
('conv', nn.Conv2d(in_channels=n_in, out_channels=n_out*self.expansion, kernel_size=1,
stride=stride, padding=0, bias=False)),
('bn', nn.BatchNorm2d(num_features=n_out*self.expansion))
]))
def forward(self, x):
x_shortcut = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu(x)
x = self.conv3(x)
x = self.bn3(x)
if self.downsample:
x_shortcut = self.downsample(x_shortcut)
x = x + x_shortcut
x = self.relu(x)
return x
class ResNet(nn.Module):
"""
ResNet18 = ResNet(layers=[2,2,2,2])
ResNet34 = ResNet(layers=[3,4,6,3])
ResNet50 = ResNet(layers=[3,4,6,3], bottleneck=True)
ResNet101 = ResNet(layers=[3,4,23,3],bottleneck=True)
ResNet152 = ResNet(layers=[3,8,36,3],bottleneck=True)
"""
def __init__(self, layers = [2, 2, 2, 2], num_classes = 1000, inplanes = 3, bottleneck=False):
super(ResNet, self).__init__()
self.inplanes = 64
block = ResidualBlock
self.n = sum(layers)*2 + 2
if bottleneck:
block = ResidualBottleneckBlock
self.n = sum(layers)*3 + 2
self.conv1 = nn.Conv2d(in_channels=inplanes, out_channels=64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(num_features=64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
self.layer2 = self._make_layer(block, 128, layers[1])
self.layer3 = self._make_layer(block, 256, layers[2])
self.layer4 = self._make_layer(block, 512, layers[3])
self.avgpool = nn.AvgPool2d(kernel_size=7)
# self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1,1))
self.fc = nn.Linear(in_features=512*block.expansion,
out_features=num_classes)
# Weight initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, channels, num_residuals, stride = 2) -> nn.Sequential:
block_layers = []
downsample = False
if self.inplanes != channels*block.expansion:
downsample = True
block_layers.append(
(f'block{1}', block(self.inplanes, channels, stride, downsample)))
for i in range(1, num_residuals):
block_layers.append(
(f'block{i+1}', block(channels*block.expansion, channels)))
self.inplanes = channels*block.expansion
return nn.Sequential(OrderedDict(block_layers))
def _get_name(self):
return self.__class__.__name__ + str(self.n)
def semi_forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(input=x, start_dim=1)
x = self.fc(x)
return x
def forward(self, triplet):
out = self.semi_forward(triplet)
out = F.normalize(out, p=2, dim=1)
return out
def ResNet18(**kwargs):
return ResNet(layers=[2,2,2,2], **kwargs)
def ResNet34(**kwargs):
return ResNet(layers=[3,4,6,3], **kwargs)
def ResNet50(**kwargs):
return ResNet(layers=[3,4,6,3], bottleneck=True, **kwargs)
def ResNet101(**kwargs):
return ResNet(layers=[3,4,23,3],bottleneck=True, **kwargs)
def ResNet152(**kwargs):
return ResNet(layers=[3,8,36,3],bottleneck=True, **kwargs)
# ref : https://arxiv.org/abs/1512.03385
# ref : https://towardsdatascience.com/understanding-and-visualizing-resnets-442284831be8
# ref : https://d2l.ai/chapter_convolutional-modern/resnet.html
# ref : https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py#L144