赞
踩
前言
本篇博客主要讲解YOLOv5主干网络的替换,使用MobileNetv3实现模型轻量化,平衡速度和精度。以下为改进的具体流程~
目录
第二步:在yolo.py的parse_model函数中添加类名
将以下代码复制到common.py末尾
- # Mobilenetv3Small
-
- class h_sigmoid(nn.Module):
- def __init__(self, inplace=True):
- super(h_sigmoid, self).__init__()
- self.relu = nn.ReLU6(inplace=inplace)
-
- def forward(self, x):
- return self.relu(x + 3) / 6
-
-
- class h_swish(nn.Module):
- def __init__(self, inplace=True):
- super(h_swish, self).__init__()
- self.sigmoid = h_sigmoid(inplace=inplace)
-
- def forward(self, x):
- return x * self.sigmoid(x)
-
-
- class SELayer(nn.Module):
- def __init__(self, channel, reduction=4):
- super(SELayer, self).__init__()
- # Squeeze操作
- self.avg_pool = nn.AdaptiveAvgPool2d(1)
- # Excitation操作(FC+ReLU+FC+Sigmoid)
- self.fc = nn.Sequential(
- nn.Linear(channel, channel // reduction),
- nn.ReLU(inplace=True),
- nn.Linear(channel // reduction, channel),
- h_sigmoid()
- )
-
- def forward(self, x):
- b, c, _, _ = x.size()
- y = self.avg_pool(x)
- y = y.view(b, c)
- y = self.fc(y).view(b, c, 1, 1) # 学习到的每一channel的权重
- return x * y
-
-
- class conv_bn_hswish(nn.Module):
- """
- This equals to
- def conv_3x3_bn(inp, oup, stride):
- return nn.Sequential(
- nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
- nn.BatchNorm2d(oup),
- h_swish()
- )
- """
-
- def __init__(self, c1, c2, stride):
- super(conv_bn_hswish, self).__init__()
- self.conv = nn.Conv2d(c1, c2, 3, stride, 1, bias=False)
- self.bn = nn.BatchNorm2d(c2)
- self.act = h_swish()
-
- def forward(self, x):
- return self.act(self.bn(self.conv(x)))
-
- def fuseforward(self, x):
- return self.act(self.conv(x))
-
-
- class MobileNetV3(nn.Module):
- def __init__(self, inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs):
- super(MobileNetV3, self).__init__()
- assert stride in [1, 2]
-
- self.identity = stride == 1 and inp == oup
-
- # 输入通道数=扩张通道数 则不进行通道扩张
- if inp == hidden_dim:
- self.conv = nn.Sequential(
- # dw
- nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
- bias=False),
- nn.BatchNorm2d(hidden_dim),
- h_swish() if use_hs else nn.ReLU(inplace=True),
- # Squeeze-and-Excite
- SELayer(hidden_dim) if use_se else nn.Sequential(),
- # 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),
- h_swish() if use_hs else nn.ReLU(inplace=True),
- # dw
- nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
- bias=False),
- nn.BatchNorm2d(hidden_dim),
- # Squeeze-and-Excite
- SELayer(hidden_dim) if use_se else nn.Sequential(),
- h_swish() if use_hs else nn.ReLU(inplace=True),
- # pw-linear
- nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
- nn.BatchNorm2d(oup),
- )
-
- def forward(self, x):
- y = self.conv(x)
- if self.identity:
- return x + y
- else:
- return y
(1)在YOLOv5/v7项目文件下的models/yolo.py中在文件首部添加代码
from models.common import *
(2)添加内容:
h_sigmoid, h_swish, SELayer, conv_bn_hswish, MobileNetV3
(3)添加效果:
1、复制models/yolov5s.yaml文件,并重命名
2、根据MobileNetv3的网络结构修改配置文件
根据网络结构我们可以看出MobileNetV3模块包含六个参数[out_ch, hidden_ch, kernel_size, stride, use_se, use_hs]:
• out_ch: 输出通道
• hidden_ch: 表示在Inverted residuals中的扩张通道数
• kernel_size: 卷积核大小
• stride: 步长
• use_se: 表示是否使用 SELayer,使用了是1,不使用是0
• use_hs: 表示使用 h_swish 还是 ReLU,使用h_swish是1,使用 ReLU是0
修改的时候,需要注意/8,/16,/32等位置特征图的变换
3、修改head部分concat的层数
修改完成代码如下:
- # YOLOv5 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/894240推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。