作为新人,由于硬件限制,在进行目标检测任务时常因为网络参数过多使得训练时间过长或无法收敛。经大佬提醒可以学习并使用参数较少的轻量级网络MobileNet,该网络用于提取图像特征,可用在图像分类和目标检测等任务中。本文对MobileNet_v1进行学习。
论文地址:MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
MobileNet是基于深度级可分离卷积构建的网络。depthwise separable convolution是将普通的卷积操作分为两个:(1)深度卷积(depthwise convolution)(2)逐点卷积(pointwise convolution)
举个例子:输入尺寸为 DF x DF x M,标准卷积核的尺寸为* DK x DK x M x N*。那么所需的参数量为DKxDKxMxNxDFxDF
但是若将该卷积层分为depthwise convolution(input=M,output=1,kernelsize=DK)和pointwise convolution(input=1,output=N,kernelsize=1)也可以实现同样功能。depthwise部分完成特征提取,pointwise部分完成通道数改变。但是所需的总参数变为:DKxDKxMx1xDFxDF+1x1xNxMxDFxDF=DFxDFxMx(DKxDK+N)
总参数明显减小,使用比值可以推测出若DK为3,即3x3的卷积,则至少可以使参数量减小到1/9。
# 定义普通卷积模块
def conv_bn(inp, oup, stride = 1):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True)
)
# 定义depthwise convolution
def conv_dw(inp, oup, stride = 1):
return nn.Sequential(
# depthwise convolution
nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.ReLU6(inplace=True),
# pointwise convolution
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.ReLU6(inplace=True),
)
class MobileNetV1(nn.Module):
def __init__(self):
super(MobileNetV1, self).__init__()
self.stage1 = nn.Sequential(
# 640,640,3 -> 320,320,32
conv_bn(3, 32, 2),
# 320,320,32 -> 320,320,64
conv_dw(32, 64, 1),
# 320,320,64 -> 160,160,128
conv_dw(64, 128, 2),
conv_dw(128, 128, 1),
# 160,160,128 -> 80,80,256
conv_dw(128, 256, 2),
conv_dw(256, 256, 1),
)
# 80,80,256 -> 40,40,512
self.stage2 = nn.Sequential(
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),
)
# 40,40,512 -> 20,20,1024
self.stage3 = nn.Sequential(
conv_dw(512, 1024, 2),
conv_dw(1024, 1024, 1),
)
self.avg = nn.AdaptiveAvgPool2d((1,1))
self.fc = nn.Linear(1024, 1000)
def forward(self, x):
x = self.stage1(x)
x = self.stage2(x)
x = self.stage3(x)
x = self.avg(x)
# x = self.model(x)
x = x.view(-1, 1024)
x = self.fc(x)
return x
def mobilenet_v1(pretrained=False, progress=True):
model = MobileNetV1()
if pretrained:
print("mobilenet_v1 has no pretrained model")
return model
if __name__ == "__main__":
import torch
from torchsummary import summary
# 需要使用device来指定网络在GPU还是CPU运行
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = mobilenet_v1().to(device)
summary(model, input_size=(3, 416, 416))
————————————————
版权声明:本文为CSDN博主「Bubbliiiing」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44791964/article/details/108585834
1.知乎——MobileNet v1模型详读
2.睿智的目标检测49——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4-lite目标检测平台