Mobilenets是Google针对手机的智能型嵌入式设备提出的一种轻量级深度卷积神经网络,该网络的核心为深度可分离卷积(depthwise separable convolutions),更加详细的介绍可以参见“MobileNets: Effificient Convolutional Neural Networks for Mobile Vision Applications”
depthwise卷积结构与标准卷积结构的区别可以参见下图, 其中,我们很容易知道标准卷积的参数量为3*3*3*16。而depthwise卷积的参数量为3*3*3*1+1*1*3*16。
MobileNets V1通过可分离卷积减少了参数量,但减弱了通道之间的相关性,于是就用到1x1的标准卷积来增强通道之间的相关性。
class MobileNet(nn.Module):
def __init__(self):
super(MobileNet, self).__init__()
def conv_bn(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU(inplace=True)
)
def conv_dw(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.ReLU(inplace=True),
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU(inplace=True),
)
self.model = nn.Sequential(
conv_bn( 3, 32, 2),
conv_dw( 32, 64, 1),
conv_dw( 64, 128, 2),
conv_dw(128, 128, 1),
conv_dw(128, 256, 2),
conv_dw(256, 256, 1),
conv_dw(256, 512, 2),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 512, 1),
conv_dw(512, 1024, 2),
conv_dw(1024, 1024, 1),
nn.AvgPool2d(7),
)
self.fc = nn.Linear(1024, 1000)
def forward(self, x):
x = self.model(x)
x = x.view(-1, 1024)
x = self.fc(x)
return x