以替换MobileNetv2为例。首先,需要在models/common.py里,实现MobileNetv2的 bottleneck 和 PWconv。
1、Mobilenetv2的bottleneck: InvertedResidual
#mobilenet Bottleneck InvertedResidual
class BottleneckMOB(nn.Module):
#c1:inp c2:oup s:stride expand_ratio:t
def __init__(self, c1, c2, s, expand_ratio):
super(BottleneckMOB, self).__init__()
self.s = s
hidden_dim = round(c1 * expand_ratio)
self.use_res_connect = self.s == 1 and c1 == c2
if expand_ratio == 1:
self.conv = nn.Sequential(
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, s, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, c2, 1, 1, 0, bias=False),
nn.BatchNorm2d(c2),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(c1, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# dw
nn.Conv2d(hidden_dim, hidden_dim, 3, s, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, c2, 1, 1, 0, bias=False),
nn.BatchNorm2d(c2),
)
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
2、Pointwise Convolution
class PW_Conv(nn.Module):
def __init__(self, c1, c2): # ch_in, ch_out
super(PW_Conv, self).__init__()
self.conv = nn.Conv2d(c1, c2, 1, 1, 0, bias=False)
self.bn = nn.BatchNorm2d(c2)
self.act = nn.ReLU6(inplace=True)
def forward(self, x):
return self.act(self.bn(self.conv(x)))
接着需要在yolov5的读取模型配置文件的代码(models/yolo.py的parse_model函数)进行修改,使得能够调用到上面的模块,只需修改下面这部分代码。(不改动源代码在258行)
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
if m in [Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost,PW_Conv, BottleneckMOB]:
c1, c2 = ch[f], args[0]
然后就是搭建我们的模型配置文件,在yolov5s.yaml的基础上进行修改,将yolov5s的backbone替换成mobilenetv2,重新建立了一个模型配置文件mobilenet.yaml。
# parameters
nc: 1 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
# anchors
anchors:
- [116,90, 156,198, 373,326] # P5/32
- [30,61, 62,45, 59,119] # P4/16
- [10,13, 16,30, 33,23] # P3/8
# YOLOv5 backbone: mobilenet v2
backbone:
# [from, number, module, args]
[[-1, 1, nn.Conv2d, [32, 3, 2]], # 0-P1/2 oup, k, s 640
[-1, 1, BottleneckMOB, [16, 1, 1]], # 1-P2/4 oup, s, t 320
[-1, 2, BottleneckMOB, [24, 2, 6]], # 320
[-1, 1, PW_Conv, [256]], #4 output p3 160
[-1, 3, BottleneckMOB, [32, 2, 6]], # 3-P3/8 160
[-1, 4, BottleneckMOB, [64, 1, 6]], # 5 80
[-1, 1, PW_Conv, [512]], #7 output p4 6 40
[-1, 3, BottleneckMOB, [96, 2, 6]], # 7 80
[-1, 3, BottleneckMOB, [160, 1, 6,]], # 40
[-1, 1, BottleneckMOB, [320, 1, 6,]], # 40
[-1, 1, nn.Conv2d, [1280, 1, 1]], # 40
[-1, 1, SPP, [1024, [5, 9, 13]]], #11 # 40
] ```
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 7], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
到这就实现了将yolov5的backbone替换成了mobilenetv2。在使用时只需要将网络结构配置参数—cfg修改成 –cfg mobilenet.yaml。
训练指令:
```python
python train.py --data coco.yaml --cfg yolov5-mobilenet.yaml--weights '' --batch-size 128