-
Notifications
You must be signed in to change notification settings - Fork 4
/
vovnet.py
198 lines (181 loc) · 5.95 KB
/
vovnet.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
""" VoVNet as per https://arxiv.org/pdf/1904.09730.pdf (v1) and
https://arxiv.org/pdf/1911.06667.pdf (v2). """
import collections
import torch
from torch import nn
# The paper is unclear as to where to downsample, so the downsampling was
# derived from the pretrained model graph as visualized by Netron. V2 simply
# enables ESE and identity connections here, nothing else changes.
CONFIG = {
# Introduced in V2. Difference is 3 repeats instead of 5 within each block.
"vovnet19": [
# kernel size, inner channels, layer repeats, output channels, downsample
[3, 64, 3, 128, True],
[3, 80, 3, 256, True],
[3, 96, 3, 348, True],
[3, 112, 3, 512, True],
],
"vovnet27_slim": [
[3, 64, 5, 128, True],
[3, 80, 5, 256, True],
[3, 96, 5, 348, True],
[3, 112, 5, 512, True],
],
"vovnet39": [
[3, 128, 5, 256, True],
[3, 160, 5, 512, True],
[3, 192, 5, 768, True], # x2
[3, 192, 5, 768, False],
[3, 224, 5, 1024, True], # x2
[3, 224, 5, 1024, False],
],
"vovnet57": [
[3, 128, 5, 256, True],
[3, 160, 5, 512, True],
[3, 192, 5, 768, True], # x4
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 224, 5, 1024, True], # x3
[3, 224, 5, 1024, False],
[3, 224, 5, 1024, False],
],
"vovnet99": [
[3, 128, 5, 256, True],
[3, 160, 5, 512, True], # x3
[3, 160, 5, 512, False],
[3, 160, 5, 512, False],
[3, 192, 5, 768, True], # x9
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 192, 5, 768, False],
[3, 224, 5, 1024, True], # x3
[3, 224, 5, 1024, False],
[3, 224, 5, 1024, False],
],
}
class _ESE(nn.Module):
def __init__(self, channels: int) -> None:
# TODO: Might want to experiment with bias=False. At least for
# MobileNetV3 it leads to better accuracy on detection.
super().__init__()
self.conv = nn.Conv2d(channels, channels, 1, padding=0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
y = x.mean([2, 3], keepdim=True)
y = self.conv(y)
# Hard sigmoid multiplied by input.
return x * (nn.functional.relu6(y + 3, inplace=True) / 6.0)
class _ConvBnRelu(nn.Sequential):
def __init__(self, in_ch: int, out_ch: int, kernel_size: int = 3, stride: int = 1):
super().__init__(
nn.Conv2d(
in_ch,
out_ch,
kernel_size,
stride=stride,
padding=kernel_size // 2,
bias=False,
),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
)
class _OSA(nn.Module):
def __init__(
self,
in_ch: int,
inner_ch: int,
out_ch: int,
repeats: int = 5,
kernel_size: int = 3,
stride: int = 1,
downsample: bool = False,
) -> None:
super().__init__()
self.downsample = downsample
self.layers = nn.ModuleList(
[
_ConvBnRelu(
in_ch if r == 0 else inner_ch,
inner_ch,
kernel_size=kernel_size,
stride=stride,
)
for r in range(repeats)
]
)
self.exit_conv = _ConvBnRelu(in_ch + repeats * inner_ch, out_ch, kernel_size=1)
self.ese = _ESE(out_ch)
def forward(self, x: torch.Tensor) -> torch.Tensor:
# Pass through all modules, but retain outputs.
input = x
if self.downsample:
x = nn.functional.max_pool2d(x, 3, stride=2, padding=1)
features = [x]
for l in self.layers:
features.append(l(x))
x = features[-1]
x = torch.cat(features, dim=1)
x = self.exit_conv(x)
x = self.ese(x)
# All non-downsampling V2 layers have a residual. They also happen to
# not change the number of channels.
if not self.downsample:
x += input
return x
class VoVNet(nn.Module):
def __init__(
self,
in_ch: int = 3,
num_classes: int = 1000,
model_type: str = "vovnet39",
has_classifier: bool = True,
dropout: float = 0.2,
):
""" Usage:
>>> net = VoVNet(3, 1000)
>>> net = net.eval()
>>> with torch.no_grad():
... y = net(torch.rand(2, 3, 64, 64))
>>> print(list(y.shape))
[2, 1000]
"""
super().__init__()
# Input stage.
self.stem = nn.Sequential(
_ConvBnRelu(in_ch, 64, kernel_size=3, stride=2),
_ConvBnRelu(64, 64, kernel_size=3, stride=1),
_ConvBnRelu(64, 128, kernel_size=3, stride=1),
)
body_layers = collections.OrderedDict()
conf = CONFIG[model_type]
in_ch = 128
for idx, block in enumerate(conf):
kernel_size, inner_ch, repeats, out_ch, downsample = block
body_layers[f"osa{idx}"] = _OSA(
in_ch,
inner_ch,
out_ch,
repeats=repeats,
kernel_size=kernel_size,
downsample=downsample,
)
in_ch = out_ch
self.body = nn.Sequential(body_layers)
self.has_classifier = has_classifier
self.classifier = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Dropout(p=dropout, inplace=True),
nn.Linear(in_ch, num_classes, bias=True),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
y = self.stem(x)
y = self.body(y)
if self.has_classifier:
y = self.classifier(y)
return y