-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpplcnet.py
279 lines (208 loc) · 6.91 KB
/
pplcnet.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
"""
Creates a PP-LCNet Model as defined in:
C. Cui. T. Gao, S. Wei et al (2021).
PP-LCNet: A Lightweight CPU Convolutional Neural Network
https://arxiv.org/pdf/2109.15099.pdf.
import from https://github.com/ngnquan/PP-LCNet
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
__all___ = [
'PPLCNet_x0_25', 'PPLCNet_x0_35', 'PPLCNet_x0_5', 'PPLCNet_x0_75',
'PPLCNet_x1_0', 'PPLCNet_x1_5', 'PPLCNet_x2_0', 'PPLCNet_x2_5'
]
def swish(x):
return x * x.sigmoid()
def hard_sigmoid(x, inplace=False):
return nn.ReLU6(inplace=inplace)(x + 3) / 6
def hard_swish(x, inplace=False):
return x * hard_sigmoid(x, inplace)
class HardSigmoid(nn.Module):
def __init__(self, inplace=False):
super(HardSigmoid, self).__init__()
self.inplace = inplace
def forward(self, x):
return hard_sigmoid(x, inplace=self.inplace)
class HardSwish(nn.Module):
def __init__(self, inplace=False):
super(HardSwish, self).__init__()
self.inplace = inplace
def forward(self, x):
return hard_swish(x, inplace=self.inplace)
def _make_divisible(v, divisor=8, min_value=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
:param v:
:param divisor:
:param min_value:
:return:
"""
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 SELayer(nn.Module):
def __init__(self, inp, oup, reduction=4):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Conv2d(oup, _make_divisible(inp // reduction), 1, 1, 0,),
nn.ReLU(),
nn.Conv2d(_make_divisible(inp // reduction), oup, 1, 1, 0),
HardSigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x)
y = self.fc(y).view(b, c, 1, 1)
return x * y
class DepSepConv(nn.Module):
def __init__(self, inp, oup, kernel_size, stride, use_se):
super(DepSepConv, self).__init__()
assert stride in [1, 2]
padding = (kernel_size - 1) // 2
if use_se:
self.conv = nn.Sequential(
# dw
nn.Conv2d(inp, inp, kernel_size, stride, padding, groups=inp, bias=False),
nn.BatchNorm2d(inp),
HardSwish(),
# SE
SELayer(inp, inp),
# pw-linear
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
HardSwish(),
)
else:
self.conv = nn.Sequential(
# dw
nn.Conv2d(inp, inp, kernel_size, stride, padding, groups=inp, bias=False),
nn.BatchNorm2d(inp),
HardSwish(),
# pw-linear
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
HardSwish()
)
def forward(self, x):
return self.conv(x)
class PPLCNet(nn.Module):
def __init__(self, scale=1.0, num_classes=1000, dropout_prob=0.2):
super(PPLCNet, self).__init__()
self.cfgs = [
# k, c, s, SE
[3, 32, 1, 0],
[3, 64, 2, 0],
[3, 64, 1, 0],
[3, 128, 2, 0],
[3, 128, 1, 0],
[5, 256, 2, 0],
[5, 256, 1, 0],
[5, 256, 1, 0],
[5, 256, 1, 0],
[5, 256, 1, 0],
[5, 256, 1, 0],
[5, 512, 2, 1],
[5, 512, 1, 1],
]
self.scale = scale
input_channel = _make_divisible(16 * scale)
layers = [nn.Conv2d(3, input_channel, 3, 2, 1, bias=False), HardSwish()]
block = DepSepConv
for k, c, s, use_se in self.cfgs:
output_channel = _make_divisible(c * scale)
layers.append(block(input_channel, output_channel, k, s, use_se))
input_channel = output_channel
self.features = nn.Sequential(*layers)
# # building last several layers
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Conv2d(input_channel, 1280, 1, 1, 0)
self.hwish = HardSwish()
self.dropout = nn.Dropout(p=dropout_prob)
self.classifier = nn.Linear(1280, num_classes)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = self.fc(x)
x = self.hwish(x)
x = self.dropout(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
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):
m.weight.data.normal_(0, 0.001)
m.bias.data.zero_()
def PPLCNet_x0_25(**kwargs):
"""
Constructs PPLCNet_x0_25 model
"""
model = PPLCNet(scale=0.25, **kwargs)
return model
def PPLCNet_x0_35(**kwargs):
"""
Constructs PPLCNet_x0_35 model
"""
model = PPLCNet(scale=0.35, **kwargs)
return model
def PPLCNet_x0_5(**kwargs):
"""
Constructs PPLCNet_x0_5 model
"""
model = PPLCNet(scale=0.5, **kwargs)
return model
def PPLCNet_x0_75(**kwargs):
"""
Constructs PPLCNet_x0_75 model
"""
model = PPLCNet(scale=0.75, **kwargs)
return model
def PPLCNet_x1_0(**kwargs):
"""
Constructs PPLCNet_x1_0 model
"""
model = PPLCNet(scale=1.0, **kwargs)
return model
def PPLCNet_x1_5(**kwargs):
"""
Constructs PPLCNet_x1_5 model
"""
model = PPLCNet(scale=1.5, **kwargs)
return model
def PPLCNet_x2_0(**kwargs):
"""
Constructs PPLCNet_x2_0 model
"""
model = PPLCNet(scale=2.0, **kwargs)
return model
def PPLCNet_x2_5(**kwargs):
"""
Constructs PPLCNet_x2_5 model
"""
model = PPLCNet(scale=2.5, **kwargs)
return model
if __name__ == "__main__":
model = PPLCNet_x0_25()
sample = torch.rand([8, 3, 224, 224])
out = model(sample)
print("Number of parameters: ", sum([p.numel() for p in model.parameters() if p.requires_grad]))
print(model.__class__.__name__)