-
Notifications
You must be signed in to change notification settings - Fork 77
/
mobilenetv3.py
146 lines (127 loc) · 5.95 KB
/
mobilenetv3.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
"""Searching for MobileNetV3"""
import torch.nn as nn
from light.nn import _Hswish, _ConvBNHswish, Bottleneck, SEModule
__all__ = ['MobileNetV3', 'get_mobilenet_v3', 'mobilenet_v3_large_1_0', 'mobilenet_v3_small_1_0']
class MobileNetV3(nn.Module):
def __init__(self, nclass=1000, mode='large', width_mult=1.0, dilated=False, norm_layer=nn.BatchNorm2d):
super(MobileNetV3, self).__init__()
if mode == 'large':
layer1_setting = [
# k, exp_size, c, se, nl, s
[3, 16, 16, False, 'RE', 1],
[3, 64, 24, False, 'RE', 2],
[3, 72, 24, False, 'RE', 1], ]
layer2_setting = [
[5, 72, 40, True, 'RE', 2],
[5, 120, 40, True, 'RE', 1],
[5, 120, 40, True, 'RE', 1], ]
layer3_setting = [
[3, 240, 80, False, 'HS', 2],
[3, 200, 80, False, 'HS', 1],
[3, 184, 80, False, 'HS', 1],
[3, 184, 80, False, 'HS', 1],
[3, 480, 112, True, 'HS', 1],
[3, 672, 112, True, 'HS', 1],
[5, 672, 112, True, 'HS', 1], ]
layer4_setting = [
[5, 672, 160, True, 'HS', 2],
[5, 960, 160, True, 'HS', 1], ]
elif mode == 'small':
layer1_setting = [
# k, exp_size, c, se, nl, s
[3, 16, 16, True, 'RE', 2], ]
layer2_setting = [
[3, 72, 24, False, 'RE', 2],
[3, 88, 24, False, 'RE', 1], ]
layer3_setting = [
[5, 96, 40, True, 'HS', 2],
[5, 240, 40, True, 'HS', 1],
[5, 240, 40, True, 'HS', 1],
[5, 120, 48, True, 'HS', 1],
[5, 144, 48, True, 'HS', 1], ]
layer4_setting = [
[5, 288, 96, True, 'HS', 2],
[5, 576, 96, True, 'HS', 1],
[5, 576, 96, True, 'HS', 1], ]
else:
raise ValueError('Unknown mode.')
# building first layer
self.in_channels = int(16 * width_mult) if width_mult > 1.0 else 16
self.conv1 = _ConvBNHswish(3, self.in_channels, 3, 2, 1, norm_layer=norm_layer)
# building bottleneck blocks
self.layer1 = self._make_layer(Bottleneck, layer1_setting,
width_mult, norm_layer=norm_layer)
self.layer2 = self._make_layer(Bottleneck, layer2_setting,
width_mult, norm_layer=norm_layer)
self.layer3 = self._make_layer(Bottleneck, layer3_setting,
width_mult, norm_layer=norm_layer)
if dilated:
self.layer4 = self._make_layer(Bottleneck, layer4_setting,
width_mult, dilation=2, norm_layer=norm_layer)
else:
self.layer4 = self._make_layer(Bottleneck, layer4_setting,
width_mult, norm_layer=norm_layer)
# building last several layers
classifier = list()
if mode == 'large':
last_bneck_channels = int(960 * width_mult) if width_mult > 1.0 else 960
self.layer5 = _ConvBNHswish(self.in_channels, last_bneck_channels, 1, norm_layer=norm_layer)
classifier.append(nn.AdaptiveAvgPool2d(1))
classifier.append(nn.Conv2d(last_bneck_channels, 1280, 1))
classifier.append(_Hswish(True))
classifier.append(nn.Conv2d(1280, nclass, 1))
elif mode == 'small':
last_bneck_channels = int(576 * width_mult) if width_mult > 1.0 else 576
self.layer5 = _ConvBNHswish(self.in_channels, last_bneck_channels, 1, norm_layer=norm_layer)
classifier.append(SEModule(last_bneck_channels))
classifier.append(nn.AdaptiveAvgPool2d(1))
classifier.append(nn.Conv2d(last_bneck_channels, 1280, 1))
classifier.append(_Hswish(True))
classifier.append(nn.Conv2d(1280, nclass, 1))
else:
raise ValueError('Unknown mode.')
self.classifier = nn.Sequential(*classifier)
self._init_weights()
def _make_layer(self, block, block_setting, width_mult, dilation=1, norm_layer=nn.BatchNorm2d):
layers = list()
for k, exp_size, c, se, nl, s in block_setting:
out_channels = int(c * width_mult)
stride = s if (dilation == 1) else 1
exp_channels = int(exp_size * width_mult)
layers.append(block(self.in_channels, out_channels, exp_channels, k, stride, dilation, se, nl, norm_layer))
self.in_channels = out_channels
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.classifier(x)
x = x.view(x.size(0), x.size(1))
return x
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
if m.bias is not None:
nn.init.zeros_(m.bias)
def get_mobilenet_v3(mode='small', width_mult=1.0, pretrained=False, root='~/,torch/models', **kwargs):
model = MobileNetV3(mode=mode, width_mult=width_mult, **kwargs)
if pretrained:
raise ValueError("Not support pretrained")
return model
def mobilenet_v3_large_1_0(**kwargs):
return get_mobilenet_v3('large', 1.0, **kwargs)
def mobilenet_v3_small_1_0(**kwargs):
return get_mobilenet_v3('small', 1.0, **kwargs)
if __name__ == '__main__':
model = mobilenet_v3_large_1_0()