-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoriginal_model.py
348 lines (296 loc) · 11.9 KB
/
original_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import torch.nn as nn
BASE_LATENCY = 10
def _make_divisible(v, divisor, min_value=None):
# ensure that all layers have a channel number that is divisible by 8
if min_value is None:
min_value = divisor
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swish(nn.Module):
def __init__(self, inplace=True):
super(h_swish, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, x):
return x * self.sigmoid(x)
class SELayer(nn.Module):
def __init__(self, channel, reduction=4):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, _make_divisible(channel // reduction, 8)),
nn.ReLU(inplace=True),
nn.Linear(_make_divisible(channel // reduction, 8), channel),
h_sigmoid(),
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
def conv_3x3_bn(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False), nn.BatchNorm2d(oup), h_swish()
)
def conv_1x1_bn(inp, oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), h_swish()
)
def depthwise_conv(in_c, out_c, k=3, s=1, p=0):
return nn.Sequential(
nn.Conv2d(in_c, in_c, kernel_size=k, padding=p, groups=in_c, stride=s),
nn.BatchNorm2d(num_features=in_c),
nn.ReLU6(inplace=True),
nn.Conv2d(in_c, out_c, kernel_size=1),
)
class InvertedResidualBlock(nn.Module):
def __init__(self, inp, oup, stride, expansion, use_se=False, use_hs=False):
super(InvertedResidualBlock, self).__init__()
assert stride in [1, 2]
hidden_dim = expansion * inp
self.identity = stride == 1 and inp == oup
if inp == hidden_dim:
self.conv = nn.Sequential(
# dw
nn.Conv2d(
hidden_dim,
hidden_dim,
3,
stride,
(3 - 1) // 2,
groups=hidden_dim,
bias=False,
),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Identity(),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# dw
nn.Conv2d(
hidden_dim,
hidden_dim,
3,
stride,
(3 - 1) // 2,
groups=hidden_dim,
bias=False,
),
nn.BatchNorm2d(hidden_dim),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Identity(),
h_swish() if use_hs else nn.ReLU(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
self.latency = self.get_latency(inp, oup, hidden_dim)
def get_latency(self, inp, oup, hidden_dim):
latency = 0
if inp != hidden_dim:
latency += inp * hidden_dim * BASE_LATENCY # pw
latency += hidden_dim * BASE_LATENCY # bn
latency += BASE_LATENCY # act
latency += hidden_dim * hidden_dim * BASE_LATENCY # dw
latency += hidden_dim * BASE_LATENCY # bn
latency += BASE_LATENCY # act
latency += BASE_LATENCY # se
latency += hidden_dim * oup * BASE_LATENCY # pw-linear
latency += oup * BASE_LATENCY # bn
return latency
def forward(self, x):
if self.identity:
return x + self.conv(x), self.latency
else:
return self.conv(x), self.latency
class UpInvertedResidualBlock(nn.Module):
def __init__(self, inp, oup, stride=2, expansion=6, use_se=False, use_hs=False):
super(UpInvertedResidualBlock, self).__init__()
assert stride in [1, 2]
hidden_dim = expansion * inp
self.identity = stride == 1 and inp == oup
if inp == hidden_dim:
self.conv = nn.Sequential(
# dw
nn.ConvTranspose2d(
hidden_dim,
hidden_dim,
4,
stride,
(4 - 1) // 2,
groups=hidden_dim,
bias=False,
),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Identity(),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
h_swish() if use_hs else nn.ReLU(inplace=True),
# dw
nn.ConvTranspose2d(
hidden_dim,
hidden_dim,
4,
stride,
(4 - 1) // 2,
groups=hidden_dim,
bias=False,
),
nn.BatchNorm2d(hidden_dim),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Identity(),
h_swish() if use_hs else nn.ReLU(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
self.latency = self.get_latency(inp, oup, hidden_dim)
def get_latency(self, inp, oup, hidden_dim):
latency = 0
if inp != hidden_dim:
latency += inp * hidden_dim * BASE_LATENCY # pw
latency += hidden_dim * BASE_LATENCY # bn
latency += BASE_LATENCY # act
latency += hidden_dim * hidden_dim * BASE_LATENCY # dw
latency += hidden_dim * BASE_LATENCY # bn
latency += BASE_LATENCY # act
latency += BASE_LATENCY # se
latency += hidden_dim * oup * BASE_LATENCY # pw-linear
latency += oup * BASE_LATENCY # bn
return latency
def forward(self, x):
if self.identity:
return x + self.conv(x), self.latency
else:
return self.conv(x), self.latency
class UNetMobileNetv3(nn.Module):
"""
Modified UNet with inverted residual block and depthwise seperable convolution
"""
def __init__(self, out_size):
super(UNetMobileNetv3, self).__init__()
self.out_size = out_size
expansion = 6
# encoding arm
self.conv3x3 = self.depthwise_conv(3, 16, p=1, s=2)
self.irb_bottleneck1 = self.irb_bottleneck(16, 24, 1, 1, 1)
self.irb_bottleneck2 = self.irb_bottleneck(24, 32, 2, 2, expansion)
self.irb_bottleneck3 = self.irb_bottleneck(32, 48, 3, 2, expansion)
self.irb_bottleneck4 = self.irb_bottleneck(48, 96, 4, 2, expansion)
self.irb_bottleneck5 = self.irb_bottleneck(96, 128, 4, 2, expansion)
self.irb_bottleneck6 = self.irb_bottleneck(128, 256, 3, 1, expansion)
self.irb_bottleneck7 = self.irb_bottleneck(256, 320, 1, 2, expansion)
# decoding arm
self.D_irb1 = self.irb_bottleneck(320, 128, 1, 2, expansion, True)
self.D_irb2 = self.irb_bottleneck(128, 96, 1, 2, expansion, True)
self.D_irb3 = self.irb_bottleneck(96, 48, 1, 2, expansion, True)
self.D_irb4 = self.irb_bottleneck(48, 32, 1, 2, expansion, True)
self.D_irb5 = self.irb_bottleneck(32, 24, 1, 2, expansion, True)
self.D_irb6 = self.irb_bottleneck(24, 16, 1, 2, expansion, True)
self.D_irb7 = self.irb_bottleneck(16, 3, 1, 1, expansion, False)
# self.DConv4x4 = nn.ConvTranspose2d(32, 16, 4, 2, 1, groups=16, bias=False)
# Final layer: output channel number can be changed as per the usecase
# self.conv1x1_decode = nn.Conv2d(16, 3, kernel_size=1, stride=1)
def depthwise_conv(self, in_c, out_c, k=3, s=1, p=0):
"""
optimized convolution by combining depthwise convolution and
pointwise convolution.
"""
conv = nn.Sequential(
nn.Conv2d(in_c, in_c, kernel_size=k, padding=p, groups=in_c, stride=s),
nn.BatchNorm2d(num_features=in_c),
nn.ReLU6(inplace=True),
nn.Conv2d(in_c, out_c, kernel_size=1),
)
return conv
def irb_bottleneck(self, in_c, out_c, n, s, t, d=False):
"""
create a series of inverted residual blocks.
"""
convs = []
if d:
xx = UpInvertedResidualBlock(in_c, out_c, s, t)
convs.append(xx)
if n > 1:
for _ in range(1, n):
xx = UpInvertedResidualBlock(out_c, out_c, 1, t)
convs.append(xx)
conv = nn.Sequential(*convs)
else:
xx = InvertedResidualBlock(in_c, out_c, s, t)
convs.append(xx)
if n > 1:
for _ in range(1, n):
xx = InvertedResidualBlock(out_c, out_c, 1, t)
convs.append(xx)
conv = nn.Sequential(*convs)
return conv
def irb_forward(self, blocks, x):
latency = 0
for i in range(len(blocks)):
block = blocks[i]
x, cur_lat = block(x)
latency += cur_lat
return x, latency
def forward(self, x):
x1 = self.conv3x3(x) # (32, 256, 256)
x2, lat2 = self.irb_forward(self.irb_bottleneck1, x1) # (16,256,256) s1
x3, lat3 = self.irb_forward(self.irb_bottleneck2, x2) # (24,128,128) s2
x4, lat4 = self.irb_forward(self.irb_bottleneck3, x3) # (32,64,64) s3
x5, lat5 = self.irb_forward(self.irb_bottleneck4, x4) # (64,32,32)
x6, lat6 = self.irb_forward(self.irb_bottleneck5, x5) # (96,16,16) s4
x7, lat7 = self.irb_forward(self.irb_bottleneck6, x6) # (160,16,16)
x8, lat8 = self.irb_forward(self.irb_bottleneck7, x7) # (240,8,8)
# Right arm / Decoding arm with skip connections
d1, lat9 = self.irb_forward(self.D_irb1, x8)
d1 += x6
d2, lat10 = self.irb_forward(self.D_irb2, d1)
d2 += x5
d3, lat11 = self.irb_forward(self.D_irb3, d2)
d3 += x4
d4, lat12 = self.irb_forward(self.D_irb4, d3)
d4 += x3
d5, lat13 = self.irb_forward(self.D_irb5, d4)
d5 += x2
d6, lat14 = self.irb_forward(self.D_irb6, d5)
d7, lat15 = self.irb_forward(self.D_irb7, d6)
return d7, sum([lat2,lat3,lat4,lat5,lat6,lat7,lat8,lat9,lat10,lat11,lat12,lat13,lat14,lat15,])
import torch
input = torch.randn(1, 3, 512, 512)
model = UNetMobileNetv3(512)
out, latency = model(input)
print(out.shape, latency)
um_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Number of model parameters: ", um_params)
"""
13,295,908, 344,216,100
150,450,000
5324810
12954
"""