YoloV5 的部署【详解】


YoloV5的源码: github链接
本博客使用的是branch版本,也可以使用tag5.0的版本。
首先在命令行进行下载,

wget https://github.com/ultralytics/yolov5/archive/refs/heads/master.zip

再进行解压,

unzip mater.zip

删除master压缩文件

rm mater.zip

进入到文件夹里面去

code yolov5-master/

激活环境,这里需要python版本>3.6的

conda activate torch1.8

执行export.py文件,导出ONNX,

python export.py

我们只需要输出ONNX,所以只需指定ONNX即可,

python export.py --include=onnx

1、避免直接使用shape、size的返回值

int代替shape、size的返回值,这里使用了map函数,避免ONNX导出生成gather、shape等节点,

    def forward(self, x):
        z = []  # inference output
        for i in range(self.nl):
            x[i] = self.m[i](x[i])  # conv
            bs, _, ny, nx = map(int, x[i].shape)  # 原代码:x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)

2、batch维度需要指定为-1

请看代码的最后两句,batch维度指定为-1,

def forward(self, x):
    z = []  # inference output
    for i in range(self.nl):
        x[i] = self.m[i](x[i])  # conv
        bs, _, ny, nx = map(int, x[i].shape)  # 原代码:x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

        if not self.training:  # inference
            if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
                self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

            y = x[i].sigmoid()
            if self.inplace:
                y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i]  # xy
                y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]  # wh
            else:  # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
                xy, wh, conf = y.split((2, 2, self.nc + 1), 4)  # y.tensor_split((2, 4, 5), 4)  # torch 1.8.0
                xy = (xy * 2 + self.grid[i]) * self.stride[i]  # xy
                wh = (wh * 2) ** 2 * self.anchor_grid[i]  # wh
                y = torch.cat((xy, wh, conf), 4)
            # z.append(y.view(bs, -1, self.no))
            z.append(y.view(-1, int(y.size(1) * y.size(2) * y.size(3), self.no)))
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值