onnx/onnx-coreml

Error while converting op of type: Slice. Error message: Input shape not available

shuiqingliu opened this issue · 20 comments

🐞Describe the bug

I have a pytorch model that has been convert to onnx model, and then when I convert onnx model to coreml with onnx-coreml ,the error occured as the bug title

Trace

87/165: Converting Node Type Slice
Traceback (most recent call last):
  File "convert1.py", line 36, in <module>
    image_output_names=['2147'],minimum_ios_deployment_target="13")
  File "/home/***/.pyenv/versions/test/lib/python3.7/site-packages/onnx_coreml/converter.py", line 626, in convert
    _convert_node_nd(builder, node, graph, err)
  File "/home/***/.pyenv/versions/test/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 2387, in _convert_node_nd
    return converter_fn(builder, node, graph, err)
  File "/home/***/.pyenv/versions/test/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py", line 1958, in _convert_slice
    err.unsupported_op_configuration(builder, node, graph, "Input shape not available")
  File "/home/**"/.pyenv/versions/test/lib/python3.7/site-packages/onnx_coreml/_error_utils.py", line 60, in unsupported_op_configuration
    self.rerun_suggestion)
TypeError: Error while converting op of type: Slice. Error message: Input shape not available
 Please try converting with higher minimum_ios_deployment_target.
You can also provide custom function/layer to convert the model.

To Reproduce

  • If a python script can reproduce the error, please paste the code snippet
tmp_onnx = "xxxx.onnx"
#convert and save ONNX

coreml_path= "xxx.mlmodel"
# Convert ONNX to CoreML model
model_file = open(tmp_onnx, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
coreml_model = convert(model_proto,
                       image_input_names=['0'],
                       image_output_names=['2147'],minimum_ios_deployment_target="13")
coreml_model.save(coreml_path)
# Paste code snippet here

System environment (please complete the following information):

  • coremltools version (e.g., 3.0b5):3.3
  • onnx-coreml version (e.g. 1.0b2):1.2
  • OS (e.g., MacOS, Linux): Ubuntu18.04
  • macOS version (if applicable):
  • How you install python (anaconda, virtualenv, system): virtualenv
  • python version (e.g. 3.7):3.7.5
  • any other relevant information:

Additional context

I'm try to debug this error, I found the error come from

err.unsupported_op_configuration(builder, node, graph, "Input shape not available")

I open the onnx model in Netron , the node slice is like this:
2222

sjf18 commented

hello , i have the same issue, have you known how to fix the bug?

I am also facing the same issue...

I have no way to solve this problem, I implemented the model with keras and then directly converted to coreml instead of going through with onnx.

I'm also blocked on the same error. Any chance anybody's made a custom layer for this?

Can you please share a script/python code snippet of the pytorch model to reproduce the error?
(The code snippet pasted above does not have enough information to reproduce the error)

In my case the slice was right before output, so I just moved it outside the model (i.e., in Pytorch)—I can slice it in Swift easily enough. The conversion runs okay now.
If nobody else provides anything I can upload my model and script later.

Hmm... spoke too soon about conversion running okay. That was on my encoder; the generator fails with Error while converting op of type: BatchNormalization. Error message: provided number axes 2 not supported .
I'm trying to convert the encoder and generator from ClusterGAN: https://github.com/eriklindernoren/PyTorch-GAN/tree/master/implementations/cluster_gan

Any update on how you managed to get around this bug, besides omitting it from model ?

I think I may have the same problem as the original poster, but can add a concrete example. The linked ONNX graph similarly has a slice node after a shape node, and produces an Input shape not available when I try to convert it. If I set add_custom_layers=True, it instead produces a KeyError: 110.

import onnx_coreml
mlmodel = onnx_coreml.convert(model='./crowd-count.onnx',
    minimum_ios_deployment_target='13', image_input_names=['image'])

I encountered the same issue. Then I found it is caused by the interpolation or upsampling of pytorch. It solved after replacing upsampling with transposed convolution with an upsampling filter.

I replaced x = nn.functional.interpolate(x, scale_factor=2) with

def get_nearestup_weight(cin):
    filt=torch.Tensor([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]])[None, None, ...]
    weight = np.zeros((cin, cin, 4, 4),
                      dtype=np.float64)
    weight[range(cin), range(cin), :, :] = filt
    return torch.from_numpy(weight).float()

upweight = get_nearestup_weight(x.size(1))
x = F.conv_transpose2d(x, upweight.detach(),stride=2, padding=1)

@aseemw I have made a code snippet to reproduce the error:

import torch
import torch.nn as nn
import torch.onnx
from onnx_coreml import convert
import numpy as np

def get_nearestup_weight(cin):
    filt=torch.Tensor([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]])[None, None, ...]
    weight = np.zeros((cin, cin, 4, 4),
                      dtype=np.float64)
    weight[range(cin), range(cin), :, :] = filt
    return torch.from_numpy(weight).float()


class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 3, kernel_size=3, padding=1)

    def forward(self, x):
        x = self.conv1(x)

        # this leads to that error
        x = nn.functional.interpolate(x, scale_factor=2)

        # this solves the error
        #upweight = get_nearestup_weight(x.size(1))
        #x = nn.functional.conv_transpose2d(x, upweight.detach(), stride=2, padding=1)
        return x

net = ConvNet()
net.eval()
x = torch.zeros(1, 3, 64, 64)
torch.onnx.export(net,               # model being run
                  x,  # model input (or a tuple for multiple inputs)
                  "myconvnet.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=11,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable lenght axes
                                'output' : {0 : 'batch_size'}
                                })


# Load the ONNX model as a CoreML model
model = convert(model='myconvnet.onnx',minimum_ios_deployment_target='13')

# Save the CoreML model
model.save('myconvnet.mlmodel')

For pytorch '1.3.0'+onnx_coreml '1.3', an error occurs at torch.onnx.export()

For pytorch '1.6.0.dev20200528', the torch.onnx.export() works but this error occurs at model = convert(

the error message is

TypeError: Error while converting op of type: Slice. Error message: Input shape not available 
 Please try converting with higher minimum_ios_deployment_target.
You can also provide custom function/layer to convert the model.

I still have the same issue, are there any solutions other than modifying the model? I've been trying to convert a yolo v5 pytorch model to coreml.

I still have the same issue, are there any solutions other than modifying the model? I've been trying to convert a yolo v5 pytorch model to coreml.

Same issue with a YOLOV5 PyTorch model.

Environment:

  • coremltools: 3.4
  • onnx: 1.7.0
  • onnx-coreml: 1.3

Any thoughts on this?

Thanks

I still have the same issue, are there any solutions other than modifying the model? I've been trying to convert a 79999_iter.pth pytorch model to coreml from https://github.com/zllrunning/face-parsing.PyTorch ?

Any thoughts on this?
Thanks

There is so some issue in the Onnx conversion of the PyTorch model as the slice node is not able to infer the shape correctly. I would suggest you create a new onnx model using the existing one using Onnx Simiplifier and make sure you choose opset 9 while converting the model. It worked for me.

Hi @mohitnihalani ,
Thanks for your answer. Howerver, I had another problem :
/usr/local/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py in _convert_resize(builder, node, graph, err)
1732 builder.add_upsample(
1733 name=node.name,
-> 1734 scaling_factor_h=scale[-2],
1735 scaling_factor_w=scale[-1],
1736 input_name=node.inputs[0],

IndexError: index -2 is out of bounds for axis 0 with size 0

@quangphap208 could you share your onnx graph so I can have a look at it.

hi @mohitnihalani,
i have pytorch model from https://github.com/zllrunning/face-parsing.PyTorch, i have convert to onnx, after i use Onnx Simiplifier.
Pytorch -> onnx -> onnx simiplified -> error index -2 is out of bounds for axis 0 with size 0.
https://drive.google.com/file/d/156jYu7vRm9HUsfkp9-gCXaAstxMabPZP/view?usp=sharing
Thanks for your answer

hi @bhushan23, Thanks for your answer.
i had issue #581