onnx / onnx-coreml

ONNX to Core ML Converter
MIT License
395 stars 80 forks source link

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

Closed maplecoder closed 4 years ago

maplecoder commented 4 years ago

🐞Describe the bug

I'm trying to port https://github.com/alinstein/Depth_estimation to iOS.

There is a PyTorch model located at: https://drive.google.com/drive/folders/1rDvtiwUgYbhzk8ZPdQ176abv-u6SaZzI?usp=sharing

First I converted it to a ONNX model, then I converted the ONNX model to CoreML model, but the error was reported

Trace


TypeError Traceback (most recent call last)

in 23 print('[!]convert') 24 coreml_model = convert(model='network.onnx', ---> 25 minimum_ios_deployment_target='13') 26 print('[!]save') 27 coreml_model.save('depth.mlmodel') /opt/anaconda3/lib/python3.7/site-packages/onnx_coreml/converter.py in convert(model, mode, image_input_names, preprocessing_args, image_output_names, deprocessing_args, class_labels, predicted_feature_name, add_custom_layers, custom_conversion_functions, onnx_coreml_input_shape_map, minimum_ios_deployment_target) 625 print("%d/%d: Converting Node Type %s" %(i+1, len(graph.nodes), node.op_type)) 626 if disable_coreml_rank5_mapping: --> 627 _convert_node_nd(builder, node, graph, err) 628 else: 629 _add_const_inputs_if_required(builder, node, graph, err) /opt/anaconda3/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py in _convert_node_nd(builder, node, graph, err) 2387 def _convert_node_nd(builder, node, graph, err): # type: (NeuralNetworkBuilder, Node, Graph, ErrorHandling) -> None 2388 converter_fn = _get_node_converter_fn(builder, node, err) -> 2389 return converter_fn(builder, node, graph, err) 2390 /opt/anaconda3/lib/python3.7/site-packages/onnx_coreml/_operators_nd.py in _convert_slice(builder, node, graph, err) 1956 1957 if node.inputs[0] not in graph.shape_dict: -> 1958 err.unsupported_op_configuration(builder, node, graph, "Input shape not available") 1959 1960 data_shape = graph.shape_dict[node.inputs[0]] /opt/anaconda3/lib/python3.7/site-packages/onnx_coreml/_error_utils.py in unsupported_op_configuration(self, builder, node, graph, err_message) 58 raise TypeError( 59 "Error while converting op of type: {}. Error message: {} {}\n".format(node.op_type, err_message, ---> 60 self.rerun_suggestion) 61 ) 62 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 ``` from onnx import onnx_pb from onnx_coreml import convert from Mobile_model import Model import torch import torch.nn as nn model = Model()#.cuda() model = nn.DataParallel(model) #load the trained model model.load_state_dict(torch.load('6.pth', map_location=torch.device('cpu'))) dummy_input = torch.zeros(1, 3, 640, 480) print('[!]export') # Convert the PyTorch model to ONNX torch.onnx.export(model.module, dummy_input, "network.onnx", opset_version=11, verbose=False, input_names=["image.1"], output_names=["image"]) print('[!]convert') coreml_model = convert(model='network.onnx', minimum_ios_deployment_target='13') print('[!]save') coreml_model.save('depth.mlmodel') print('[!]finished') ``` - If applicable, please attach ONNX model https://drive.google.com/drive/folders/1rDvtiwUgYbhzk8ZPdQ176abv-u6SaZzI?usp=sharing ## System environment (please complete the following information): - coremltools version (e.g., 3.0b5): 3.4 - onnx-coreml version (e.g. 1.0b2): 1.3 - OS (e.g., MacOS, Linux): MacOS - macOS version (if applicable): 10.15.5 - How you install python (anaconda, virtualenv, system): anaconda - python version (e.g. 3.7): 3.7 - any other relevant information: ## Additional context Add any other context about the problem here.
1duo commented 4 years ago

Hi @maplecoder, thanks for reporting this issue! It seems you're converting a model from PyTorch. In latest update of coremltools (https://github.com/apple/coremltools), we've added support for PyTorch -> Core ML directly without going through ONNX, can you give that a try? https://coremltools.readme.io/docs/pytorch-conversion. Please file an issue if you see any errors. Thanks!

mohitnihalani commented 4 years ago

Do you have upsampling layer in the PyTorch model?

maplecoder commented 4 years ago

Hi thanks for contacting me, I don't visit gmail often. I have solved the problem by using a custom conversion function, but the inference result cannot meet my needs, so I have not use that model anymore.

Yuduo Wu notifications@github.com 于2020年6月27日周六 上午8:34写道:

Hi @maplecoder https://github.com/maplecoder, thanks for reporting this issue! It seems you're converting a model from PyTorch. In latest update of coremltools (https://github.com/apple/coremltools), we've added support for PyTorch -> Core ML directly without going through ONNX, can you give that a try? https://coremltools.readme.io/docs/pytorch-conversion. Please file an issue https://github.com/apple/coremltools/issues/new?assignees=&labels=bug&template=---bug-report.md&title= if you see any errors. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/575#issuecomment-650463144, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQDCFVQD7OTGIC6QCD4OA3RYU5BFANCNFSM4N5JOMQA .

maplecoder commented 4 years ago

Hi thanks for contacting me, I don't visit gmail often. I have solved the problem by using a custom conversion function, but the inference result cannot meet my needs, so I have not used that model anymore.

Mohit Nihalani notifications@github.com 于2020年7月3日周五 上午8:03写道:

Do you have upsampling layer in the PyTorch model?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/onnx/onnx-coreml/issues/575#issuecomment-653266272, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQDCFX2RXKNUVTDXC53EDDRZUN3JANCNFSM4N5JOMQA .