当前位置:网站首页>Yolov5 improvement 12: replace backbone network C3 with lightweight network shufflenetv2
Yolov5 improvement 12: replace backbone network C3 with lightweight network shufflenetv2
2022-07-28 22:49:00 【Artificial Intelligence Algorithm Research Institute】
front said : As the current advanced deep learning target detection algorithm YOLOv5, A large number of trick, But there is still room for improvement , For the detection difficulties in specific application scenarios , There are different ways to improve . Subsequent articles , Focus on YOLOv5 How to improve is introduced in detail , The purpose is to provide their own meager help and reference for those who need innovation in scientific research or friends who need to achieve better results in engineering projects .
solve the problem :YOLOv5 The backbone feature extraction network adopts C3 structure , Bring a large number of parameters , The detection speed is slow , Limited application , In some real application scenarios, such as mobile or embedded devices , Such a large and complex model is difficult to be applied . The first is that the model is too large , Facing the problem of insufficient memory , Second, these scenarios require low latency , In other words, the response speed should be fast , Imagine the pedestrian detection system of self driving cars. What terrible things will happen if the speed is slow? . therefore , Research small and efficient CNN Models are crucial in these scenarios , At least for now , Although the hardware will be faster and faster in the future . This paper attempts to replace the backbone feature extraction network with a lighter ShuffleNetV2 The Internet , To realize the lightweight of the network model , Balance speed and accuracy .
principle :
The article links https://arxiv.org/abs/1807.11164
Recently, , depth CNN Network such as ResNet and DenseNet, The accuracy of image classification has been greatly improved . But in addition to accuracy , So is the computational complexity CNN Important indicators to be considered by the network , An overly complex network can be slow , Some specific scenarios, such as the field of unmanned vehicles, require low latency . In addition, mobile devices also need small models that are both accurate and fast . To meet these needs , Some lightweight CNN Network such as MobileNet and ShuffleNet Proposed , They strike a good balance between speed and accuracy . Today we are going to talk about ShuffleNetv2, It was recently proposed by Kuang Shi ShuffleNet Upgraded version , And be ECCV2018 Included . At the same complexity ,ShuffleNetv2 Than ShuffleNet and MobileNetv2 More accurate . The following figure shows the overall structure .
Fang Law :
Step 1 modify common.py, increase ShuffleNetV2 modular .
def channel_shuffle(x: Tensor, groups: int) -> Tensor:
batchsize, num_channels, height, width = x.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()
return x
class conv_bn_relu_maxpool(nn.Module):
def __init__(self, c1, c2): # ch_in, ch_out
super(conv_bn_relu_maxpool, 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 ShuffleNetV2_InvertedResidual(nn.Module):
def __init__(
self,
inp: int,
oup: int,
stride: int
) -> None:
super(ShuffleNetV2_InvertedResidual, self).__init__()
if not (1 <= stride <= 3):
raise ValueError('illegal stride value')
self.stride = stride
branch_features = oup // 2
if self.stride > 1:
self.branch1 = nn.Sequential(
self.depthwise_conv(inp, inp, kernel_size=3, stride=self.stride, padding=1),
nn.BatchNorm2d(inp),
nn.Conv2d(inp, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
else:
self.branch1 = nn.Sequential()
self.branch2 = nn.Sequential(
nn.Conv2d(inp 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: int,
o: int,
stride: int = 1,
padding: int = 0,
bias: bool = False
) -> nn.Conv2d:
return nn.Conv2d(i, o, kernel_size, stride, padding, bias=bias, groups=i)
def forward(self, x: Tensor) -> Tensor:
if self.stride == 1:
x1, x2 = x.chunk(2, dim=1)
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
The second step : take yolo.py Registration module in ShuffleNetV2.
if m in [Conv,MobileNetV3_InvertedResidual,ShuffleNetV2_InvertedResidual ]:
The third step : modify yaml file
backbone:
# [from, number, module, args]
[[-1, 1, Focus, [64, 3]], # 0-P2/4
[-1, 1, ShuffleNetV2_InvertedResidual, [128, 2]], # 1-P3/8
[-1, 3, ShuffleNetV2_InvertedResidual, [128, 1]], # 2
[-1, 1, ShuffleNetV2_InvertedResidual, [256, 2]], # 3-P4/16
[-1, 7, ShuffleNetV2_InvertedResidual, [256, 1]], # 4
[-1, 1, ShuffleNetV2_InvertedResidual, [512, 2]], # 5-P5/32
[-1, 3, ShuffleNetV2_InvertedResidual, [512, 1]], # 6
]
junction fruit : I have done a lot of experiments on multiple data sets , For different data sets, the effect is different ,map Value down , But the size of the weight model decreases , The parameter quantity decreases .
Let me know : The next article will continue to share network lightweight methods EfficientNetv2 The share of . Interested friends can pay attention to me , If you have questions, you can leave a message or chat with me in private
PS: The replacement of backbone network is not only applicable to improvement YOLOv5, You can also improve others YOLO Network and target detection network , such as YOLOv4、v3 etc. .
Last , I hope I can powder each other , Be a friend , Learn and communicate together .
边栏推荐
- PC side web page effects (client series, scroll series, immediate function execution, sidebar effects)
- Integrating database Ecology: using eventbridge to build CDC applications
- Summary of C language learning content
- Awk blank line filtering
- Why doesn't the icon on the elment plus icon input display
- imx6q gpio复用
- Symbol符号类型
- OSV_ q The size of tensor a (704) must match the size of tensor b (320) at non-singleton dime
- Use PCL to batch convert point cloud.Bin files to.Pcd
- Migration from IPv4 to IPv6
猜你喜欢
Torch.fft.fft 2. () error reporting problem solution
Annaconda installs pytoch and switches environments
Intelligent control -- fuzzy mathematics and control
console.log()控制台显示...解决办法
Command line agent: proxychains configuration
简单的es高亮实战
The blueprint of flask complements openpyxl
Install PCL and VTK under the background of ROS installation, and solve VTK and PCL_ ROS conflict problem
Att & CK Threat Intelligence
How to delete and remove the first row of elements in PHP two-dimensional array
随机推荐
[virtual machine _2]-hyper-v and vmware/virtualbox cannot coexist
Symbol符号类型
Paper reading: deep forest / deep forest /gcforest
Qt+FFmpeg环境搭建
The blueprint of flask complements openpyxl
高等数学解题常用公式笔记总结
Paper reading vision gnn: an image is worth graph of nodes
Vscode ROS configuration GDB debugging error record
Solve various problems of sudo rosdep init and rosdep update
Introduction to structure
775. 倒排单词
Concise history of graphic technology
CS flow [abnormal detection: normalizing flow]
【三维目标检测】3DSSD(二)
842. Arrange numbers
STM32_ Hal library driven framework
Multi activity disaster recovery construction after 713 failure of station B | takintalks share
Image is referred in multiple repositories
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]
Evaluation index of anomaly detection: rocauc et al. [tips]