作者提出了一個(gè)新的網(wǎng)絡(luò)架構(gòu) MobileNetV2,該架構(gòu)基于反轉(zhuǎn)殘差結(jié)構(gòu),其中的跳躍連接位于較瘦的瓶頸層之間。中間的擴(kuò)展層則利用輕量級(jí)的深度卷積來提取特征引入非線性,而且,為了維持網(wǎng)絡(luò)的表示能力作者去除了較窄層的非線性激活函數(shù)。
1、反轉(zhuǎn)殘差(Inverted ResidualBlock)
MobileNet V2利用殘差結(jié)構(gòu)取代了原始的卷積堆疊方式,提出了一個(gè)Inverted ResidualBlock結(jié)構(gòu)。
MobileNetV2之所以采用這種先升維,再降維的方法,是因?yàn)?MobileNetV2 將residuals block 的 bottleneck 替換為了 Depthwise Convolutions,因其參數(shù)少,提取的特征就會(huì)相對(duì)的少,如果再進(jìn)行壓縮的話,能提取的特征就更少了,因此MobileNetV2 就執(zhí)行了擴(kuò)張→卷積特征提取→壓縮的過程。
普通ResNet結(jié)構(gòu)和反轉(zhuǎn)殘差的對(duì)比:

左圖:輸入特征圖的通道數(shù)通過1x1卷積(PC,d/4維),從d維降到d/4維,然后使用3x3普通卷積計(jì)算,最后再通過1x1卷積(PC, d維)將特征圖的通道數(shù)提高到d維度。總的來說,就是先降維度,然后升維度。當(dāng)然還需要shortcut連接,以及之后的ReLU激活層。
右圖:先將維度升高,然后進(jìn)行深度可分離卷積計(jì)算,之后再將維度降低。也會(huì)存在shortcut連接,但之后沒有使用ReLU激活層。
MobileNet V2兩種殘差塊

2、線性瓶頸結(jié)構(gòu)
長期以來,人們一直認(rèn)為神經(jīng)網(wǎng)絡(luò)中的興趣流形(mainfold of interest)也就是激活特征,可以被嵌入到低維子空間中?;谶@個(gè)事實(shí),我們可以通過減少某一層網(wǎng)絡(luò)的維度也就是通道數(shù)來減少激活特征的空間維度。MobileNetV1 中的寬度因子就是用來減少激活空間的維度的,直到激活特征可以擴(kuò)展出整個(gè)空間,我們就找到了一個(gè)最佳的參數(shù)。
但是,神經(jīng)網(wǎng)絡(luò)中還有非線性激活函數(shù),這時(shí)候,上面的直覺就不成立了。比如 ReLU 會(huì)把負(fù)的激活值變?yōu)榱?,換句話說,深度網(wǎng)絡(luò)僅在輸出域的非零部分具有線性分類器的功能。如果 ReLU 使得某一個(gè)通道的一些值變?yōu)榱?,這會(huì)不可避免地帶來那個(gè)通道的信息損失,但如果通道數(shù)比較多,我們就可以通過一種結(jié)構(gòu)用其它通道的激活值來補(bǔ)償這個(gè)損失。

用一個(gè)隨機(jī)矩陣T將左邊的螺旋線嵌入到n維空間,然后用ReLU激活,再用投影回去??梢钥吹?img class="math-inline" src="https://math.jianshu.com/math?formula=n%3D2%2C%203" alt="n=2, 3" mathimg="1">時(shí)信息損失非常大,而維度較高時(shí)則恢復(fù)得比較好。
因此,為了避免損失太多信息,作者采用線性瓶頸層,也就是在通道數(shù)比較少的瓶頸層不采用非線性激活函數(shù)。
3、網(wǎng)絡(luò)結(jié)構(gòu)
- Bottleneck

其中,t為通道數(shù)擴(kuò)增的倍數(shù)
- mobileNet v2 完整結(jié)構(gòu)

如上表所示,第一層是標(biāo)準(zhǔn)卷積,然后后面是前述的bottleneck結(jié)構(gòu)。其中t 是擴(kuò)展因子,c 是輸出通道數(shù), n 是重復(fù)次數(shù),s 代表步長。如果步長為 2 ,代表當(dāng)前重復(fù)結(jié)構(gòu)的第一個(gè)塊步長為 2,其余的步長為 1,步長為 2 時(shí)則沒有跳躍連接,如下圖所示。

此外,也可以像 MobileNetV1 那樣繼續(xù)利用寬度乘子和分辨率乘子進(jìn)一步降低模型的大小。
4、實(shí)驗(yàn)結(jié)果
在 ImageNet 上的分類結(jié)果如下所示:

在 COCO 數(shù)據(jù)集上的目標(biāo)檢測(cè)結(jié)果如下圖所示:

此外,作者還對(duì)比了不同的跳躍連接方式和是否采用線性瓶頸結(jié)構(gòu),進(jìn)一步驗(yàn)證了網(wǎng)絡(luò)設(shè)計(jì)的合理性。

5、代碼
def conv_bn(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
def conv_1x1_bn(inp, oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride, expand_ratio):
super(InvertedResidual, self).__init__()
self.stride = stride
assert stride in [1, 2]
hidden_dim = round(inp * expand_ratio)
self.use_res_connect = self.stride == 1 and inp == oup
if expand_ratio == 1:
self.conv = nn.Sequential(
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
class MobileNetV2(nn.Module):
def __init__(self, n_class=1000, input_size=224, width_mult=1.):
super(MobileNetV2, self).__init__()
block = InvertedResidual
input_channel = 32
last_channel = 1280
interverted_residual_setting = [
# t, c, n, s
[1, 16, 1, 1],
[6, 24, 2, 2],
[6, 32, 3, 2],
[6, 64, 4, 2],
[6, 96, 3, 1],
[6, 160, 3, 2],
[6, 320, 1, 1],
]
# building first layer
assert input_size % 32 == 0
input_channel = int(input_channel * width_mult)
self.last_channel = int(last_channel * width_mult) if width_mult > 1.0 else last_channel
self.features = [conv_bn(3, input_channel, 2)]
# building inverted residual blocks
for t, c, n, s in interverted_residual_setting:
output_channel = int(c * width_mult)
for i in range(n):
if i == 0:
self.features.append(block(input_channel, output_channel, s, expand_ratio=t))
else:
self.features.append(block(input_channel, output_channel, 1, expand_ratio=t))
input_channel = output_channel
# building last several layers
self.features.append(conv_1x1_bn(input_channel, self.last_channel))
# make it nn.Sequential
self.features = nn.Sequential(*self.features)
# building classifier
self.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(self.last_channel, n_class),
)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = x.mean(3).mean(2)
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):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()