网上有很多代码都是结合MobileNetV3-small-YOLOv5,结合MobileNetV3-large代码不全,他们区别在于yaml文件中head中concat连接不同,深度因子和宽度因子不同
MobileNetV3-small-YOLOv5
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# Parameters
nc: 20 # number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 v6.0 backbone
backbone:
# MobileNetV3-small 11层
# [from, number, module, args]
# MobileNet_Block: [out_ch, hidden_ch, kernel_size, stride, use_se, use_hs]
# hidden_ch表示在Inverted residuals中的扩张通道数
# use_se 表示是否使用 SELayer, use_hs 表示使用 h_swish 还是 ReLU
[[-1, 1, conv_bn_hswish, [16, 2]], # 0-p1/2
[-1, 1, MobileNet_Block, [16, 16, 3, 2, 1, 0]], # 1-p2/4
[-1, 1, MobileNet_Block, [24, 72, 3, 2, 0, 0]], # 2-p3/8
[-1, 1, MobileNet_Block, [24, 88, 3, 1, 0, 0]], # 3-p3/8
[-1, 1, MobileNet_Block, [40, 96, 5, 2, 1, 1]], # 4-p4/16
[-1, 1, MobileNet_Block, [40, 240, 5, 1, 1, 1]], # 5-p4/16
[-1, 1, MobileNet_Block, [40, 240, 5, 1, 1, 1]], # 6-p4/16
[-1, 1, MobileNet_Block, [48, 120, 5, 1, 1, 1]], # 7-p4/16
[-1, 1, MobileNet_Block, [48, 144, 5, 1, 1, 1]], # 8-p4/16
[-1, 1, MobileNet_Block, [96, 288, 5, 2, 1, 1]], # 9-p5/32
[-1, 1, MobileNet_Block, [96, 576, 5, 1, 1, 1]], # 10-p5/32
[-1, 1, MobileNet_Block, [96, 576, 5, 1, 1, 1]], # 11-p5/32
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 8], 1, Concat, [1]], # cat backbone P4
[-1, 1, C3, [256, False]], # 15
[-1, 1, Conv, [128, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 3], 1, Concat, [1]], # cat backbone P3
[-1, 1, C3, [128, False]], # 19 (P3/8-small)
[-1, 1, Conv, [128, 3, 2]],
[[-1, 16], 1, Concat, [1]], # cat head P4
[-1, 1, C3, [256, False]], # 22 (P4/16-medium)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 12], 1, Concat, [1]], # cat head P5
[-1, 1, C3, [512, False]], # 25 (P5/32-large)
[[19, 22, 25], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
注意由于 MobileNetV3-small作为主干网格参数很少,这里宽度因子和深度因子可以设置为1,且head部分通道数缩减,最初的作者可能想进一步轻量化
MobileNetV3-large
# Parameters
nc: 20 # number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 v6.0 backbone
backbone:
# MobileNetV3-small 11层
# [from, number, module, args]
# MobileNet_Block: [out_ch, hidden_ch, kernel_size, stride, use_se, use_hs]
# hidden_ch表示在Inverted residuals中的扩张通道数
# use_se 表示是否使用 SELayer, use_hs 表示使用 h_swish 还是 ReLU
# MobileNetV3-large
# [from, number, module, args]
[[-1, 1, conv_bn_hswish, [16, 2]], # 0-p1/2
[-1, 1, MobileNet_Block, [ 16, 16, 3, 1, 0, 0]], # 1-p1/2
[-1, 1, MobileNet_Block, [ 24, 64, 3, 2, 0, 0]], # 2-p2/4
[-1, 1, MobileNet_Block, [ 24, 72, 3, 1, 0, 0]], # 3-p2/4
[-1, 1, MobileNet_Block, [ 40, 72, 5, 2, 1, 0]], # 4-p3/8
[-1, 1, MobileNet_Block, [ 40, 120, 5, 1, 1, 0]], # 5-p3/8
[-1, 1, MobileNet_Block, [ 40, 120, 5, 1, 1, 0]], # 6-p3/8
[-1, 1, MobileNet_Block, [ 80, 240, 3, 2, 0, 1]], # 7-p4/16
[-1, 1, MobileNet_Block, [ 80, 200, 3, 1, 0, 1]], # 8-p4/16
[-1, 1, MobileNet_Block, [ 80, 184, 3, 1, 0, 1]], # 9-p4/16
[-1, 1, MobileNet_Block, [ 80, 184, 3, 1, 0, 1]], # 10-p4/16
[-1, 1, MobileNet_Block, [112, 480, 3, 1, 1, 1]], # 11-p4/16
[-1, 1, MobileNet_Block, [112, 672, 3, 1, 1, 1]], # 12-p4/16
[-1, 1, MobileNet_Block, [160, 672, 5, 1, 1, 1]], # 13-p4/16
[-1, 1, MobileNet_Block, [160, 960, 5, 2, 1, 1]], # 14-p5/32 原672改为原算法960
[-1, 1, MobileNet_Block, [160, 960, 5, 1, 1, 1]], # 15-p5/32
]
# YOLOv5 v6.0 head
head:
[ [ -1, 1, Conv, [ 256, 1, 1 ] ],
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
[ [ -1, 13], 1, Concat, [ 1 ] ], # cat backbone P4
[ -1, 1, C3, [ 256, False ] ], # 13
[ -1, 1, Conv, [ 128, 1, 1 ] ],
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
[ [ -1, 6 ], 1, Concat, [ 1 ] ], # cat backbone P3
[ -1, 1, C3, [ 128, False ] ], # 17 (P3/8-small)
[ -1, 1, Conv, [ 128, 3, 2 ] ],
[ [ -1, 20 ], 1, Concat, [ 1 ] ], # cat head P4
[ -1, 1, C3, [ 256, False ] ], # 20 (P4/16-medium)
[ -1, 1, Conv, [ 256, 3, 2 ] ],
[ [ -1, 16 ], 1, Concat, [ 1 ] ], # cat head P5
[ -1, 1, C3, [ 512, False ] ], # 23 (P5/32-large)
[ [ 23, 26, 29 ], 1, Detect, [ nc, anchors ] ], # Detect(P3, P4, P5)
]
MobileNetV3-large模型比MobileNetV3-small多了更多的MobileNet_Block结构,残差倒置结构中通道数维度也增大了许多,速度比YOLOv5s慢将近一半,但是参数变少,效果介乎MobileNetV3-small和YOLOv5s之间,可以作为模型对比,凸显自己模型优势