MobileNetV3是由Google在2019年3月21日提出的網絡架構,參考arXiv的論文,其中包括兩個子版本,即Large和Small。python
源碼參考:github.com/SpikeKing/m…git
重點:github
網絡結構:bash
MobileNetV3的網絡結構能夠分爲三個部分:網絡
網絡框架以下,其中參數是Large體系:架構
源碼以下:app
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激活層。框架
源碼以下:ide
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的網絡結構已經介紹完成。