Skip to content

NCNN: Unsupported unsqueeze axes ! #118

@MHGL

Description

@MHGL

🐛 Bug

I get this error while convert yolov5 Focus module to ncnn

To Reproduce

Steps to reproduce the behavior:

  1. code example
import torch

# init module
class Focus(torch.nn.Module):
    def __init__(self):
        super(Focus, self).__init__()
        ...

    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
        x = torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)
        return x

torch_model = Focus().eval()

# torch.onnx.export
torch.onnx.export(torch_model,
        torch.randn(1, 3, 224, 224),
        "./tmp.onnx",
        input_names=["inputs"],
        output_names=["outputs"],
        dynamic_axes={"inputs": {0: "batch", 2: "height", 3: "width"}, "outputs": {0: "batch", 1: "class", 2: "height", 3: "width"}},
        opset_version=11,
        export_params=True)

# onnx simplify
import os
import onnx
from onnxsim import simplify

onnx_file = os.path.join(os.getcwd(), "tmp.onnx")
model_op, check_ok = simplify(onnx_file, 
        check_n=3, 
        perform_optimization=True, 
        skip_fuse_bn=True,  
        skip_shape_inference=False, 
        input_shapes={"inputs": (1, 3, 224, 224)}, 
        skipped_optimizers=None, 
        dynamic_input_shape={"inputs": {0: "batch", 2: "height", 3: "width"}, "outputs": {0: "batch", 1: "class", 2: "height", 3: "width"}})
onnx.save(model_op, "./tmp.onnx")

# onnx -> ncnn
# !!!
# you should build onnx2ncnn binary file first
os.system("/bin/onnx2ncnn {} tmp.params tmp.bin".format(onnx_file))
  1. stack traces
Checking 0/3...
Checking 1/3...
Checking 2/3...
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !
Unsupported slice step !

Expected behavior

Environment

  • PyTorch Version: 1.9.0
  • OS (e.g., MacOS, Linux): Ubuntu20.04 LTS
  • How you install python (anaconda, virtualenv, system): miniconda
  • python version (e.g. 3.7): 3.8.5
  • any other relevant information:
    • gpu: GeForce GTX 1650
    • driver: Driver Version: 460.80
    • CUDA: CUDA Version: 11.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions