-
Notifications
You must be signed in to change notification settings - Fork 6
/
resnet.py
executable file
·193 lines (148 loc) · 6.92 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
from functools import partial
from typing import Any, Callable, List, Optional, Type, Union
import torch
import torch.nn as nn
from torch import Tensor
def Conv3x3(in_planes, out_planes, stride=1, groups=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, groups=groups, bias=False)
def conv1x1(in_planes: int, out_planes: int, stride: int = 1):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class BasicBlock(nn.Module):
expansion: int = 1
def __init__(self, inplanes: int, planes: int, stride: int = 1, downsample: Optional[nn.Module] = None):
super().__init__()
self.conv1 = Conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = Conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = torch.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = torch.relu(out)
return out
class Bottleneck(nn.Module):
expansion: int = 4
def __init__(self, inplanes, planes, stride, downsample: Optional[nn.Module] = None):
super().__init__()
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = Conv3x3(planes, planes, stride)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = conv1x1(planes, planes * self.expansion)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
def forward(self, x: Tensor) -> Tensor:
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes, large_input, width, zero_init_residual=False):
super().__init__()
self.inplanes = width
if large_input:
self.embed = nn.Sequential(
nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(self.inplanes),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
else:
self.embed = nn.Sequential(
nn.Conv2d(3, self.inplanes, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(self.inplanes),
nn.ReLU(inplace=True)
)
layers_list = []
for depth, stride, multiplier in layers:
layers_list.append(self._make_layer(block, width * multiplier, depth, stride=stride))
self.layers = nn.Sequential(*layers_list)
self.fc = nn.Linear(self.inplanes, num_classes)
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.GroupNorm)):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck) and m.bn3.weight is not None:
nn.init.constant_(m.bn3.weight, 0) # type: ignore[arg-type]
elif isinstance(m, BasicBlock) and m.bn2.weight is not None:
nn.init.constant_(m.bn2.weight, 0) # type: ignore[arg-type]
def _make_layer(self, block, planes, blocks, stride = 1):
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
nn.BatchNorm2d(planes * block.expansion),
)
else:
downsample = None
layers = []
layers.append(
block(
self.inplanes, planes, stride, downsample
)
)
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(
block(
self.inplanes,
planes,
stride = 1,
)
)
return nn.Sequential(*layers)
def forward(self, x):
x = self.embed(x)
x = self.layers(x)
x = x.mean(-1).mean(-1)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def resnet18(num_classes, large_input, width):
return ResNet(BasicBlock, [(2, 1, 1), (2, 2, 2), (2, 2, 4), (2, 2, 8)], num_classes, large_input, width)
def resnet34(num_classes, large_input, width):
return ResNet(BasicBlock, [(3, 1, 1), (4, 2, 2), (6, 2, 4), (3, 2, 8)], num_classes, large_input, width)
def resnet50(num_classes, large_input, width):
return ResNet(Bottleneck, [(3, 1, 1), (4, 2, 2), (6, 2, 4), (3, 2, 8)], num_classes, large_input, width)
def resnet101(num_classes, large_input, width):
return ResNet(Bottleneck, [(3, 1, 1), (4, 2, 2), (23, 2, 4), (3, 2, 8)], num_classes, large_input, width)
def resnet152(num_classes, large_input, width):
return ResNet(Bottleneck, [(3, 1, 1), (8, 2, 2), (36, 2, 4), (3, 2, 8)], num_classes, large_input, width)
def resnet20(num_classes, large_input, width):
return ResNet(BasicBlock, [(3, 1, 1), (3, 2, 2), (3, 2, 4)], num_classes, large_input, width)
def resnet32(num_classes, large_input, width):
return ResNet(BasicBlock, [(5, 1, 1), (5, 2, 2), (5, 2, 4)], num_classes, large_input, width)
def resnet44(num_classes, large_input, width):
return ResNet(BasicBlock, [(7, 1, 1), (7, 2, 2), (7, 2, 4)], num_classes, large_input, width)
def resnet56(num_classes, large_input, width):
return ResNet(BasicBlock, [(9, 1, 1), (9, 2, 2), (9, 2, 4)], num_classes, large_input, width)
def resnet110(num_classes, large_input, width):
return ResNet(BasicBlock, [(18, 1, 1), (18, 2, 2), (18, 2, 4)], num_classes, large_input, width)
def resnet1202(num_classes, large_input, width):
return ResNet(BasicBlock, [(200, 1, 1), (200, 2, 2), (200, 2, 4)], num_classes, large_input, width)
def resnettest(num_classes, large_input, width):
return ResNet(Bottleneck, [(3, 1, 1), (4, 2, 1), (6, 2, 2), (6, 2, 4), (4, 2, 8), (3, 2, 16)], num_classes, False, width)