-
Notifications
You must be signed in to change notification settings - Fork 1
/
resnet.py
193 lines (172 loc) · 5.54 KB
/
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import torch
import torch.nn as nn
# "the convolutional layers mostly have 3×3 filters and follow two simple design rules: ..."
# He et al., ‘Deep Residual Learning for Image Recognition’
RESNET_KERNEL_SIZE = 3
# used to match dimensions of input to output, done by a 1x1 convolution
# He et al., ‘Deep Residual Learning for Image Recognition’ page 4
def projection_shortcut(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
# "when the shortcuts go across feature maps of two sizes, they are performed with a stride of 2"
# He et al., ‘Deep Residual Learning for Image Recognition’.
stride=2,
kernel_size=1,
),
nn.BatchNorm2d(out_channels),
)
class ResidualBlock(nn.Module):
def __init__(
self, in_channels, out_channels, stride=1, shortcut=None, *args, **kwargs
):
super().__init__(*args, **kwargs)
self.conv0 = nn.Sequential(
nn.Conv2d(
in_channels, out_channels, kernel_size=3, stride=stride, padding=1
),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
self.conv1 = nn.Sequential(
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
)
self.relu = nn.ReLU()
self.out_channels = out_channels
self.shortcut = shortcut
def forward(self, x):
residual = x
out = self.conv0(x)
out = self.conv1(out)
if self.shortcut:
out += self.shortcut(residual)
else:
out += residual
out = self.relu(out)
return out
# MAI in ResNet with 34 layers
# He et al., ‘Deep Residual Learning for Image Recognition’.
class MaiRes(nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
# first 7x7 conv layer
self.conv = nn.Conv2d(
in_channels=3,
out_channels=64,
stride=2,
padding=3,
kernel_size=RESNET_KERNEL_SIZE,
)
self.maxpool = nn.MaxPool2d(kernel_size=RESNET_KERNEL_SIZE, stride=2)
# layers are named after the colors used for each group
# in the diagram presented in the ResNet paper
# 3 residual blocks for a total of 6 layers
self.layer_purple = nn.Sequential(
ResidualBlock(
in_channels=64,
out_channels=64,
stride=1,
),
ResidualBlock(
in_channels=64,
out_channels=64,
stride=1,
),
ResidualBlock(
in_channels=64,
out_channels=64,
stride=1,
),
)
# 4 residual blocks for a total of 8 layers
self.layer_green = nn.Sequential(
ResidualBlock(
in_channels=64,
out_channels=128,
stride=2,
shortcut=projection_shortcut(in_channels=64, out_channels=128),
),
ResidualBlock(
in_channels=128,
out_channels=128,
stride=1,
),
ResidualBlock(
in_channels=128,
out_channels=128,
stride=1,
),
ResidualBlock(
in_channels=128,
out_channels=128,
stride=1,
),
)
# 6 residual blocks for a total of 12 layers
self.layer_red = nn.Sequential(
ResidualBlock(
in_channels=128,
out_channels=256,
stride=2,
shortcut=projection_shortcut(in_channels=128, out_channels=256),
),
ResidualBlock(
in_channels=256,
out_channels=256,
stride=1,
),
ResidualBlock(
in_channels=256,
out_channels=256,
stride=1,
),
ResidualBlock(
in_channels=256,
out_channels=256,
stride=1,
),
ResidualBlock(
in_channels=256,
out_channels=256,
stride=1,
),
ResidualBlock(
in_channels=256,
out_channels=256,
stride=1,
),
)
# 3 residual blocks for a total of 6 layers
self.layer_blue = nn.Sequential(
ResidualBlock(
in_channels=256,
out_channels=512,
stride=2,
shortcut=projection_shortcut(in_channels=256, out_channels=512),
),
ResidualBlock(
in_channels=512,
out_channels=512,
stride=1,
),
ResidualBlock(
in_channels=512,
out_channels=512,
stride=1,
),
)
self.avgpool = nn.AvgPool2d(kernel_size=RESNET_KERNEL_SIZE)
self.fc = nn.Linear(in_features=2048, out_features=1)
def forward(self, x):
x = self.conv(x)
x = self.maxpool(x)
x = self.layer_purple(x)
x = self.layer_green(x)
x = self.layer_red(x)
x = self.layer_blue(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x