-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMobileNetV2_dynamicFPN.py
More file actions
182 lines (153 loc) · 7.21 KB
/
MobileNetV2_dynamicFPN.py
File metadata and controls
182 lines (153 loc) · 7.21 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride, expand_ratio):
super(InvertedResidual, self).__init__()
self.stride = stride
assert stride in [1, 2]
self.use_res_connect = self.stride == 1 and inp == oup
self.conv = nn.Sequential(
# pointwise convolution
nn.Conv2d(in_channels=inp, out_channels=inp * expand_ratio,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(inp * expand_ratio),
nn.ReLU6(inplace=True),
# depthwise convolution via groups
nn.Conv2d(in_channels=inp * expand_ratio, out_channels=inp * expand_ratio,
kernel_size=3, stride=stride, padding=1, groups=inp * expand_ratio, bias=False),
nn.BatchNorm2d(inp * expand_ratio),
nn.ReLU6(inplace=True),
# pointwise linear convolution
nn.Conv2d(in_channels=inp * expand_ratio, out_channels=oup,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
class MobileNetV2_dynamicFPN(nn.Module):
def __init__(self, width_mult=1.):
super(MobileNetV2_dynamicFPN, self).__init__()
self.input_channel = int(32 * width_mult)
self.width_mult = width_mult
# First layer
self.first_layer = nn.Sequential(
nn.Conv2d(3, self.input_channel, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(self.input_channel),
nn.ReLU6(inplace=True)
)
# Inverted residual blocks (each n layers)
self.inverted_residual_setting = [
{'expansion_factor': 1, 'width_factor': 16, 'n': 1, 'stride': 1},
{'expansion_factor': 6, 'width_factor': 24, 'n': 2, 'stride': 2},
{'expansion_factor': 6, 'width_factor': 32, 'n': 3, 'stride': 2},
{'expansion_factor': 6, 'width_factor': 64, 'n': 4, 'stride': 2},
{'expansion_factor': 6, 'width_factor': 96, 'n': 3, 'stride': 1},
{'expansion_factor': 6, 'width_factor': 160, 'n': 3, 'stride': 2},
{'expansion_factor': 6, 'width_factor': 320, 'n': 1, 'stride': 1},
]
self.inverted_residual_blocks = nn.ModuleList(
[self._make_inverted_residual_block(**setting)
for setting in self.inverted_residual_setting])
# reduce feature maps to one pixel
# allows to upsample semantic information of every part of the image
self.average_pool = nn.AdaptiveAvgPool2d(1)
# Top layer
# input channels = last width factor
self.top_layer = nn.Conv2d(
int(self.inverted_residual_setting[-1]['width_factor'] * self.width_mult),
256, kernel_size=1, stride=1, padding=0)
# Lateral layers
# exclude last setting as this lateral connection is the the top layer
# build layer only if resulution has decreases (stride > 1)
self.lateral_setting = [setting for setting in self.inverted_residual_setting[:-1]
if setting['stride'] > 1]
self.lateral_layers = nn.ModuleList([
nn.Conv2d(int(setting['width_factor'] * self.width_mult),
256, kernel_size=1, stride=1, padding=0)
for setting in self.lateral_setting])
# Smooth layers
# n = lateral layers + 1 for top layer
self.smooth_layers = nn.ModuleList([nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)] *
(len(self.lateral_layers) + 1))
self._initialize_weights()
def _make_inverted_residual_block(self, expansion_factor, width_factor, n, stride):
inverted_residual_block = []
output_channel = int(width_factor * self.width_mult)
for i in range(n):
# except the first layer, all layers have stride 1
if i != 0:
stride = 1
inverted_residual_block.append(
InvertedResidual(self.input_channel, output_channel, stride, expansion_factor))
self.input_channel = output_channel
return nn.Sequential(*inverted_residual_block)
def _upsample_add(self, x, y):
'''Upsample and add two feature maps.
Args:
x: (Variable) top feature map to be upsampled.
y: (Variable) lateral feature map.
Returns:
(Variable) added feature map.
Note in PyTorch, when input size is odd, the upsampled feature map
with `F.upsample(..., scale_factor=2, mode='nearest')`
maybe not equal to the lateral feature map size.
e.g.
original input size: [N,_,15,15] ->
conv2d feature map size: [N,_,8,8] ->
upsampled feature map size: [N,_,16,16]
So we choose bilinear upsample which supports arbitrary output sizes.
'''
_, _, H, W = y.size()
return F.upsample(x, size=(H, W), mode='bilinear', align_corners=False) + y
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def forward(self, x):
# bottom up
x = self.first_layer(x)
# loop through inverted_residual_blocks (mobile_netV2)
# save lateral_connections to lateral_tensors
# track how many lateral connections have been made
lateral_tensors = []
n_lateral_connections = 0
for i, block in enumerate(self.inverted_residual_blocks):
output = block(x) # run block of mobile_net_V2
if self.inverted_residual_setting[i]['stride'] > 1 \
and n_lateral_connections < len(self.lateral_layers):
lateral_tensors.append(self.lateral_layers[n_lateral_connections](output))
n_lateral_connections += 1
x = output
x = self.average_pool(x)
# connect m_layer with previous m_layer and lateral layers recursively
m_layers = [self.top_layer(x)]
# reverse lateral tensor order for top down
lateral_tensors.reverse()
for lateral_tensor in lateral_tensors:
m_layers.append(self._upsample_add(m_layers[-1], lateral_tensor))
# smooth all m_layers
assert len(self.smooth_layers) == len(m_layers)
p_layers = [smooth_layer(m_layer) for smooth_layer, m_layer in zip(self.smooth_layers, m_layers)]
return p_layers
def test():
net = MobileNetV2_dynamicFPN()
print(net)
output = net(torch.randn(1, 3, 512, 512))
for feature_map in output:
print(feature_map.size())
test()