yolo5工程化

本文详细介绍YOLOv5模型的使用与部署流程,包括模型结构、预训练模型下载、模型训练、模型转换为ONNX格式的方法及C++部署过程中的后处理技巧,并对比了YOLO系列模型。

yolov5

模型使用

  1. 工程
git clone https://github.com/ultralytics/yolov5.git
  1. 预训练模型
    首次运行时,attempt_download函数会下载对应的模型。模型参数配置在models/*.yaml里。
  2. yolov5网络组成
    Focus + Backbone + Detect
  3. coco数据集训练
    下载coco数据集,放在于yolov5同一目录下。
python3 train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
                                         yolov5m                               40
                                         yolov5l                               24
                                         yolov5x                               16

模型转换部署

pytorch模型部署,一般会转化为onnx。下面来讲讲关于yolov5的转换过程。

  1. Focus部分修改
    ./models/common.py中
class Focus(nn.Module):
    # Focus wh information into c-space
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups
        super(Focus, self).__init__()
        self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
        self.contract = Contract(gain=2)
    
    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
    	# 使用Contract中的方法
        # x = self.contract(x)
        N, C, H, W = x.size()  # assert (H / s == 0) and (W / s == 0), 'Indivisible gain'
        s = 2
        x = x.view(N, C, H // s, s, W // s, s)  # x(1,64,40,2,40,2)
        x = x.permute(0, 3, 5, 1, 2, 4).contiguous()  # x(1,2,2,64,40,40)
        x = x.view(N, C * s * s, H // s, W // s)  # x(1,256,40,40)
        return self.conv(x)
        # 转换slice会有问题,注释掉
		# return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) 
  1. Detect部分修改
    ./models/yolo.py中
class Detect(nn.Module):
    stride = None  # strides computed during build
    export = True  # onnx export 此处改为True
    .......
    .......
    .......
    def forward(self, x):
        # x = x.copy()  # for profiling
        z = []  # inference output
        self.training |= self.export
        
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值