MobileNetV3是由Google在2019年3月21日提出的网络架构,参考arXiv的论文,其中包括两个子版本,即Large和Small。
源码参考:https://github.com/SpikeKing/mobilenet_v3/blob/master/mn3_model.py
重点:
网络结构:
MobileNetV3的网络结构可以分为三个部分:
网络框架如下,其中参数是Large体系:
源码如下:
def forward(self, x):
# 起始部分
out = self.init_conv(x)
# 中间部分
out = self.block(out)
# 最后部分
out = self.out_conv1(out)
batch, channels, height, width = out.size()
out = F.avg_pool2d(out, kernel_size=[height, width])
out = self.out_conv2(out)
out = out.view(batch, -1)
return out
起始部分,在Large和Small中均相同,也就是结构列表中的第1个卷积层,其中包括3个部分,即卷积层、BN层、h-switch激活层。
源码如下:
init_conv_out = _make_divisible(16 * multiplier)
self.init_conv = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=init_conv_out, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(init_conv_out),
h_swish(inplace=True),
)
h-switch是非线性激活函数,公式如下:
图形如下:
源码:
out = F.relu6(x + 3., self.inplace) / 6.
return out * x
h-sigmoid是非线性激活函数,用于SE结构:
源码:
return F.relu6(x + 3., inplace=self.inplace) / 6.
图形如下:
公式:
N = (W − F + 2P ) / S + 1
其中,向下取整,多余的像素不参于计算。
中间部分是多个含有卷积层的块(MobileBlock)的网络结构,参考,Large的网络结构,Small类似:
其中:
每一行都是一个MobileBlock,即bneck。
源码:
self.block = []
for in_channels, out_channels, kernal_size, stride, nonlinear, se, exp_size in layers:
in_channels = _make_divisible(in_channels * multiplier)
out_channels = _make_divisible(out_channels * multiplier)
exp_size = _make_divisible(exp_size * multiplier)
self.block.append(MobileBlock(in_channels, out_channels, kernal_size, stride, nonlinear, se, exp_size))
self.block = nn.Sequential(*self.block)
三个必要步骤:
两个可选步骤:
其中激活函数有两种:ReLU和h-swish。
结构如下,参数为特定,非通用:
源码:
def forward(self, x):
# MobileNetV2
out = self.conv(x) # 1x1卷积
out = self.depth_conv(out) # 深度卷积
# Squeeze and Excite
if self.SE:
out = self.squeeze_block(out)
# point-wise conv
out = self.point_conv(out)
# connection
if self.use_connect:
return x + out
else:
return out
子步骤如下:
self.conv = nn.Sequential(
nn.Conv2d(in_channels, exp_size, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(exp_size),
activation(inplace=True)
)
groups是exp值,每个通道对应一个卷积,参考,并且不含有激活层。
self.depth_conv = nn.Sequential(
nn.Conv2d(exp_size, exp_size, kernel_size=kernal_size, stride=stride, padding=padding, groups=exp_size),
nn.BatchNorm2d(exp_size),
)
self.point_conv = nn.Sequential(
nn.Conv2d(exp_size, out_channels, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(out_channels),
activation(inplace=True)
)
源码:
class SqueezeBlock(nn.Module):
def __init__(self, exp_size, divide=4):
super(SqueezeBlock, self).__init__()
self.dense = nn.Sequential(
nn.Linear(exp_size, exp_size // divide),
nn.ReLU(inplace=True),
nn.Linear(exp_size // divide, exp_size),
h_sigmoid()
)
def forward(self, x):
batch, channels, height, width = x.size()
out = F.avg_pool2d(x, kernel_size=[height, width]).view(batch, -1)
out = self.dense(out)
out = out.view(batch, channels, 1, 1)
return out * x
最终的输出与原值相加,源码如下:
self.use_connect = (stride == 1 and in_channels == out_channels)
if self.use_connect:
return x + out
else:
return out
最后部分(Last Stage),通过将Avg Pooling提前,减少计算量,将Squeeze操作省略,直接使用1x1的卷积,如图:
源码:
out = self.out_conv1(out)
batch, channels, height, width = out.size()
out = F.avg_pool2d(out, kernel_size=[height, width])
out = self.out_conv2(out)
第1个卷积层conv1,SE结构同上,源码:
out_conv1_in = _make_divisible(96 * multiplier)
out_conv1_out = _make_divisible(576 * multiplier)
self.out_conv1 = nn.Sequential(
nn.Conv2d(out_conv1_in, out_conv1_out, kernel_size=1, stride=1),
SqueezeBlock(out_conv1_out),
h_swish(inplace=True),
)
第2个卷积层conv2:
out_conv2_in = _make_divisible(576 * multiplier)
out_conv2_out = _make_divisible(1280 * multiplier)
self.out_conv2 = nn.Sequential(
nn.Conv2d(out_conv2_in, out_conv2_out, kernel_size=1, stride=1),
h_swish(inplace=True),
nn.Conv2d(out_conv2_out, self.num_classes, kernel_size=1, stride=1),
)
最后,调用resize方法,将Cx1x1转换为类别,即可
out = out.view(batch, -1)
除此之外,还可以设置multiplier参数,等比例的增加和减少通道的个数,满足8的倍数,源码如下:
def _make_divisible(v, divisor=8, min_value=None):
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
至此,MobileNet V3的网络结构已经介绍完成。