当前位置:网站首页>Yolov5 replaces the backbone network of "Megvii Lightweight Convolutional Neural Network ShuffleNetv2"
Yolov5 replaces the backbone network of "Megvii Lightweight Convolutional Neural Network ShuffleNetv2"
2022-08-04 08:02:00 【Di Mr Herman】
《ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design》
这篇是2018年发表在ECCV上的论文,At the same time, this paper has also obtainedVALSEOutstanding Paper Award of the Year
原文地址
官方代码
ShuffleNet V2It is a relatively classic lightweight network,通过大量实验提出四条轻量化网络设计准则,对输入输出通道、分组卷积组数、网络碎片化程度、逐元素操作对不同硬件上的速度和内存访问量MAC的影响进行了详细分析.
提出ShuffleNet V2模型,通过Channel Split替代分组卷积,满足四条设计准则,达到了速度和精度的最优权衡.

YOLOv5更换方法,三步搞定
第一步;添加如下代码到common.py
# 通道重排,跨group信息交流
def channel_shuffle(x, groups):
batchsize, num_channels, height, width = x.data.size()
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups,
channels_per_group, height, width)
x = torch.transpose(x, 1, 2).contiguous()
# flatten
x = x.view(batchsize, -1, height, width)
return x
class CBRM(nn.Module): #conv BN ReLU Maxpool2d
def __init__(self, c1, c2): # ch_in, ch_out
super(CBRM, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(c1, c2, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(c2),
nn.ReLU(inplace=True),
)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
def forward(self, x):
return self.maxpool(self.conv(x))
class Shuffle_Block(nn.Module):
def __init__(self, ch_in, ch_out, stride):
super(Shuffle_Block, self).__init__()
if not (1 <= stride <= 2):
raise ValueError('illegal stride value')
self.stride = stride
branch_features = ch_out // 2
assert (self.stride != 1) or (ch_in == branch_features << 1)
if self.stride > 1:
self.branch1 = nn.Sequential(
self.depthwise_conv(ch_in, ch_in, kernel_size=3, stride=self.stride, padding=1),
nn.BatchNorm2d(ch_in),
nn.Conv2d(ch_in, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
self.branch2 = nn.Sequential(
nn.Conv2d(ch_in if (self.stride > 1) else branch_features,
branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
self.depthwise_conv(branch_features, branch_features, kernel_size=3, stride=self.stride, padding=1),
nn.BatchNorm2d(branch_features),
nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
@staticmethod
def depthwise_conv(i, o, kernel_size, stride=1, padding=0, bias=False):
return nn.Conv2d(i, o, kernel_size, stride, padding, bias=bias, groups=i)
def forward(self, x):
if self.stride == 1:
x1, x2 = x.chunk(2, dim=1) # 按照维度1进行split
out = torch.cat((x1, self.branch2(x2)), dim=1)
else:
out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)
out = channel_shuffle(out, 2)
return out
第二步;yolo.py里加上CBRM和Shuffle_Block

第三步;修改配置文件
# 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:
# [from, number, module, args]
# Shuffle_Block: [out, stride]
[[ -1, 1, CBRM, [ 32 ] ], # 0-P2/4
[ -1, 1, Shuffle_Block, [ 128, 2 ] ], # 1-P3/8
[ -1, 3, Shuffle_Block, [ 128, 1 ] ], # 2
[ -1, 1, Shuffle_Block, [ 256, 2 ] ], # 3-P4/16
[ -1, 7, Shuffle_Block, [ 256, 1 ] ], # 4
[ -1, 1, Shuffle_Block, [ 512, 2 ] ], # 5-P5/32
[ -1, 3, Shuffle_Block, [ 512, 1 ] ], # 6
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P4
[-1, 1, C3, [256, False]], # 10
[-1, 1, Conv, [128, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 2], 1, Concat, [1]], # cat backbone P3
[-1, 1, C3, [128, False]], # 14 (P3/8-small)
[-1, 1, Conv, [128, 3, 2]],
[[-1, 11], 1, Concat, [1]], # cat head P4
[-1, 1, C3, [256, False]], # 17 (P4/16-medium)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 7], 1, Concat, [1]], # cat head P5
[-1, 1, C3, [512, False]], # 20 (P5/32-large)
[[14, 17, 20], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]

For more detailed network structure reproduction, please seeShuffleNet v2网络结构复现(Pytorch版)
本人更多Yolov5(v6.1)实战内容导航
1.手把手带你调参Yolo v5 (v6.1)(一)强烈推荐
4.手把手带你Yolov5 (v6.1)添加注意力机制(一)(并附上30多种顶会Attention原理图)
5.手把手带你Yolov5 (v6.1)添加注意力机制(二)(在C3模块中加入注意力机制)
8.Yolov5更换上采样方式( 最近邻 / 双线性 / 双立方 / 三线性 / 转置卷积)
9.Yolov5如何更换EIOU / alpha IOU / SIoU?
10.持续更新中
有问题欢迎大家指正,如果感觉有帮助的话请点赞支持下
!!转载请注明出处!!
边栏推荐
- 金仓数据库KingbaseES客户端编程接口指南-JDBC(8. JDBC 元数据处理)
- 全国职业院校技能大赛网络安全竞赛之应急响应
- ShuffleNet v2网络结构复现(Pytorch版)
- 【虚幻引擎UE】UE5基于Gltf加载插件实现gltf格式骨骼动画在线/本地导入和切换
- Linux之Redis 缓存雪崩,击穿,穿透
- ConstraintSet of animation of ContrstrainLayout
- Typora颜色公式代码大全
- New Questions in Module B of Secondary Vocational Network Security Competition
- 一天学会JDBC03:Statement的用法
- LeetCode每日五题01:两数之和 (均1200题)
猜你喜欢

Secondary network security competition C module MS17-010 batch scanning

YOLOv5应用轻量级通用上采样算子CARAFE

DWB主题事实及ST数据应用层构建,220803,,

The national vocational skills contest competition of network security emergency response

【剑指Offer】二分法例题

【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解

inject() can only be used inside setup() or functional components.

最近的一些杂感-20220731

一天学会JDBC06:PrepaerdStatemtnt

并查集介绍和基于并查集解决问题——LeetCode 952 按公因数计算最大组件大小
随机推荐
【电脑录制屏】如何使用bandicam录游戏 设置图文教程
8.2学习记录
「PHP基础知识」转换数据类型
设计信息录入界面,完成人员基本信息的录入工作,
小程序如何使用订阅消息(PHP代码+小程序js代码)
The school to apply for link
使用GBase 8c数据库的时候,遇到这种报错
电脑系统数据丢失了是什么原因?找回方法有哪些?
金仓数据库的单节点如何转集群?
Mysql insert on duplicate key 死锁问题定位与解决
金仓数据库KingbaseES客户端编程接口指南-JDBC(8. JDBC 元数据处理)
redis stream 实现消息队列
一天学会JDBC06:PrepaerdStatemtnt
Use of MotionLayout
解决报错: YarnScheduler: Initial job has not accepted any resources
form表单提交到数据库储存
尚医通【预约挂号系统】总结
登录拦截实现过程
2022爱分析· 银行数字化厂商全景报告
【UE虚幻引擎】UE5三步骤实现AI漫游与对话行为