YoloV5 的部署【详解】

YoloV5的源码:

本博客使用的是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

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

    def forward(self, x):
        z = []
        for i in range(self.nl):
            x[i] = self.m[i](x[i])
            bs, _, ny, nx = map(int, x[i].shape)

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

def forward(self, x):
    z = []
    for i in range(self.nl):
        x[i] = self.m[i](x[i])
        bs, _, ny, nx = map(int, x[i].shape)
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

        if not self.training:
            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]
                y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]
            else:
                xy, wh, conf = y.split((2, 2, self.nc + 1), 4)
                xy = (xy * 2 + self.grid[i]) * self.stride[i]
                wh = (wh * 2) ** 2 * self.anchor_grid[i]
                y = torch.cat((xy, wh, conf), 4)

            z.append(y.view(-1, int(y.size(1) * y.size(2) * y.size(3), self.no)))

Original: https://blog.csdn.net/qq_23022733/article/details/125606949
Author: 全息数据
Title: YoloV5 的部署【详解】

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/681201/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球