Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.46k stars 4.17k forks source link

yolov5 pytorch, 执行onnx2ncnn 报错 #3127

Open zkailinzhang opened 3 years ago

zkailinzhang commented 3 years ago

error log | 日志或报错信息 | ログ

/ncnn-20210720-ubuntu-1804/bin/onnx2ncnn best.onnx best.param best.bin Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Unsupported slice step ! Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unknown data type 0 Unsupported slice axes ! Unsupported slice axes ! Unsupported slice axes ! Unsupported unsqueeze axes ! Unknown data type 0 Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unknown data type 0 Unsupported slice axes ! Unsupported slice axes ! Unsupported slice axes ! Unsupported unsqueeze axes ! Unknown data type 0 Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Shape not supported yet! Gather not supported yet!

axis=0

Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unsupported unsqueeze axes ! Unknown data type 0 Unsupported slice axes ! Unsupported slice axes ! Unsupported slice axes ! Unsupported unsqueeze axes ! Unknown data type 0

context | 编译/运行环境 | バックグラウンド

how to reproduce | 复现步骤 | 再現方法

1. 2. 3.

more | 其他 | その他

zhiqwang commented 3 years ago

ultralytics master 分支的 onnx 导出与之前版本有差异,如果想使用知乎教程 详细记录u版YOLOv5目标检测ncnn实现 来处理的话,需要在 pytorch 导出 onnx 时候将concat的部分去掉。

具体说,需要将 YOLOv5 代码 https://github.com/ultralytics/yolov5/blob/d8f1883/models/yolo.py#L48-L70 改为:

    def forward(self, x):
        # x = x.copy()  # for profiling
        z = []  # inference output
        for i in range(self.nl):
            x[i] = self.m[i](x[i])  # conv
            bs, _, ny, nx = 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()

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

                y = x[i].sigmoid()
                if self.inplace:
                    y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + 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 = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i]  # xy
                    wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].view(1, self.na, 1, 1, 2)  # wh
                    y = torch.cat((xy, wh, y[..., 4:]), -1)
                z.append(y.view(bs, -1, self.no))

        return x if self.training else (torch.cat(z, 1), x)
    """

其他的部分可参考上面知乎教程来处理。

附一些其他的用例:

zkailinzhang commented 3 years ago

注释掉后,训练中test阶段报错, File "/home/code/yuhuankeji/yolov5-5.0/train.py", line 365, in train is_coco=is_coco) File "/homel/code/yuhuankeji/yolov5-5.0/test.py", line 110, in test out, train_out = model(img, augment=augment) # inference and training outputs ValueError: too many values to unpack (expected 2)

zhiqwang commented 3 years ago

这种改法只能用于部署时转换onnx模型来用。或者换种方式来导出 onnx 更稳妥一些,export.py 提供了 --train 的参数也可以控制onnx的导出

nihui commented 3 months ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx