File size: 7,890 Bytes
bdbd148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
MobileNetV3 in PyTorch.

论文: "Searching for MobileNetV3"
参考: https://arxiv.org/abs/1905.02244

主要特点:
1. 引入基于NAS的网络架构搜索
2. 使用改进的SE注意力机块
3. 使用h-swish激活函数
4. 重新设计了网络的最后几层
5. 提供了Large和Small两个版本
'''

import torch
import torch.nn as nn
import torch.nn.functional as F


def get_activation(name):
    '''获取激活函数
    
    Args:
        name: 激活函数名称 ('relu' 或 'hardswish')
    '''
    if name == 'relu':
        return nn.ReLU(inplace=True)
    elif name == 'hardswish':
        return nn.Hardswish(inplace=True)
    else:
        raise NotImplementedError


class SEModule(nn.Module):
    '''Squeeze-and-Excitation模块
    
    通过全局平均池化和两层全连接网络学习通道注意力权重
    
    Args:
        channel: 输入通道数
        reduction: 降维比例
    '''
    def __init__(self, channel, reduction=4):
        super(SEModule, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction, bias=False),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel, bias=False),
            nn.Hardsigmoid(inplace=True)
        )

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)  # squeeze
        y = self.fc(y).view(b, c, 1, 1)  # excitation
        return x * y.expand_as(x)  # scale


class Bottleneck(nn.Module):
    '''MobileNetV3 Bottleneck
    
    包含:
    1. Expansion layer (1x1 conv)
    2. Depthwise layer (3x3 or 5x5 depthwise conv)
    3. SE module (optional)
    4. Projection layer (1x1 conv)
    
    Args:
        in_channels: 输入通道数
        exp_channels: 扩展层通道数
        out_channels: 输出通道数
        kernel_size: 深度卷积核大小
        stride: 步长
        use_SE: 是否使用SE模块
        activation: 激活函数类型
        use_residual: 是否使用残差连接
    '''
    def __init__(self, in_channels, exp_channels, out_channels, kernel_size, 
                 stride, use_SE, activation, use_residual=True):
        super(Bottleneck, self).__init__()
        self.use_residual = use_residual and stride == 1 and in_channels == out_channels
        padding = (kernel_size - 1) // 2

        layers = []
        # Expansion layer
        if exp_channels != in_channels:
            layers.extend([
                nn.Conv2d(in_channels, exp_channels, 1, bias=False),
                nn.BatchNorm2d(exp_channels),
                get_activation(activation)
            ])
        
        # Depthwise conv
        layers.extend([
            nn.Conv2d(
                exp_channels, exp_channels, kernel_size, 
                stride, padding, groups=exp_channels, bias=False
            ),
            nn.BatchNorm2d(exp_channels),
            get_activation(activation)
        ])

        # SE module
        if use_SE:
            layers.append(SEModule(exp_channels))

        # Projection layer
        layers.extend([
            nn.Conv2d(exp_channels, out_channels, 1, bias=False),
            nn.BatchNorm2d(out_channels)
        ])

        self.conv = nn.Sequential(*layers)

    def forward(self, x):
        if self.use_residual:
            return x + self.conv(x)
        else:
            return self.conv(x)


class MobileNetV3(nn.Module):
    '''MobileNetV3网络
    
    Args:
        num_classes: 分类数量
        mode: 'large' 或 'small',选择网络版本
    '''
    def __init__(self, num_classes=10, mode='small'):
        super(MobileNetV3, self).__init__()
        
        if mode == 'large':
            # MobileNetV3-Large架构
            self.config = [
                # k, exp, out, SE, activation, stride
                [3, 16, 16, False, 'relu', 1],
                [3, 64, 24, False, 'relu', 2],
                [3, 72, 24, False, 'relu', 1],
                [5, 72, 40, True, 'relu', 2],
                [5, 120, 40, True, 'relu', 1],
                [5, 120, 40, True, 'relu', 1],
                [3, 240, 80, False, 'hardswish', 2],
                [3, 200, 80, False, 'hardswish', 1],
                [3, 184, 80, False, 'hardswish', 1],
                [3, 184, 80, False, 'hardswish', 1],
                [3, 480, 112, True, 'hardswish', 1],
                [3, 672, 112, True, 'hardswish', 1],
                [5, 672, 160, True, 'hardswish', 2],
                [5, 960, 160, True, 'hardswish', 1],
                [5, 960, 160, True, 'hardswish', 1],
            ]
            init_conv_out = 16
            final_conv_out = 960
        else:
            # MobileNetV3-Small架构
            self.config = [
                # k, exp, out, SE, activation, stride
                [3, 16, 16, True, 'relu', 2],
                [3, 72, 24, False, 'relu', 2],
                [3, 88, 24, False, 'relu', 1],
                [5, 96, 40, True, 'hardswish', 2],
                [5, 240, 40, True, 'hardswish', 1],
                [5, 240, 40, True, 'hardswish', 1],
                [5, 120, 48, True, 'hardswish', 1],
                [5, 144, 48, True, 'hardswish', 1],
                [5, 288, 96, True, 'hardswish', 2],
                [5, 576, 96, True, 'hardswish', 1],
                [5, 576, 96, True, 'hardswish', 1],
            ]
            init_conv_out = 16
            final_conv_out = 576

        # 第一层卷积
        self.conv_stem = nn.Sequential(
            nn.Conv2d(3, init_conv_out, 3, 2, 1, bias=False),
            nn.BatchNorm2d(init_conv_out),
            get_activation('hardswish')
        )

        # 构建Bottleneck层
        features = []
        in_channels = init_conv_out
        for k, exp, out, se, activation, stride in self.config:
            features.append(
                Bottleneck(in_channels, exp, out, k, stride, se, activation)
            )
            in_channels = out
        self.features = nn.Sequential(*features)

        # 最后的卷积层
        self.conv_head = nn.Sequential(
            nn.Conv2d(in_channels, final_conv_out, 1, bias=False),
            nn.BatchNorm2d(final_conv_out),
            get_activation('hardswish')
        )

        # 分类器
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.classifier = nn.Sequential(
            nn.Linear(final_conv_out, num_classes)
        )

        # 初始化权重
        self._initialize_weights()

    def _initialize_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 forward(self, x):
        x = self.conv_stem(x)
        x = self.features(x)
        x = self.conv_head(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x


def test():
    """测试函数"""
    # 测试Large版本
    net_large = MobileNetV3(mode='large')
    x = torch.randn(2, 3, 32, 32)
    y = net_large(x)
    print('Large output size:', y.size())
    
    # 测试Small版本
    net_small = MobileNetV3(mode='small')
    y = net_small(x)
    print('Small output size:', y.size())
    
    # 打印模型结构
    from torchinfo import summary
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    net_small = net_small.to(device)
    summary(net_small, (2, 3, 32, 32))

if __name__ == '__main__':
    test()