Can't convert Core ML model to Onnx (then to Tensorflow Lite)

帅比萌擦擦* 提交于 2021-01-27 19:51:26

问题


I'm trying to convert a trained Core ML model to TensorFlow Lite. I find I need convert it to Onnx first.

The problems is that I get errors. I've tried with different versions of python, onnxmltools, winmltools and it doesn't seems to work. I also tried docker image of onnx ecosystem with same result. Can any one help me with it? Thanks in advance.

Script I used:

import coremltools
import onnxmltools

input_coreml_model = '../model.mlmodel'
output_onnx_model = '../model.onnx'
coreml_model = coremltools.utils.load_spec(input_coreml_model)
onnx_model = onnxmltools.convert_coreml(coreml_model)
onnxmltools.utils.save_model(onnx_model, output_onnx_model)

IndexError                                Traceback (most recent call last)
<ipython-input-11-94a6dc527869> in <module>
      3 
      4 # Convert the CoreML model into ONNX
----> 5 onnx_model = onnxmltools.convert_coreml(coreml_model)
      6 
      7 # Save as protobuf

/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/main.py in convert_coreml(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators)
     16     from .coreml.convert import convert
     17     return convert(model, name, initial_types, doc_string, target_opset, targeted_onnx,
---> 18                    custom_conversion_functions, custom_shape_calculators)
     19 
     20 

/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/convert.py in convert(model, name, initial_types, doc_string, target_opset, targeted_onnx, custom_conversion_functions, custom_shape_calculators)
     58     target_opset = target_opset if target_opset else get_opset_number_from_onnx()
     59     # Parse CoreML model as our internal data structure (i.e., Topology)
---> 60     topology = parse_coreml(spec, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
     61 
     62     # Parse CoreML description, author, and license. Those information will be attached to the final ONNX model.

/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/_parse.py in parse_coreml(model, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
    465     # Instead of using CoremlModelContainer, we directly pass the model in because _parse_model is CoreML-specific.
    466     _parse_model(topology, scope, model)
--> 467     topology.compile()
    468 
    469     for variable in topology.find_root_and_sink_variables():

/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in compile(self)
    630         self._resolve_duplicates()
    631         self._fix_shapes()
--> 632         self._infer_all_types()
    633         self._check_structure()
    634 

/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in _infer_all_types(self)
    506                 pass  # in Keras converter, the shape calculator can be optional.
    507             else:
--> 508                 operator.infer_types()
    509 
    510     def _resolve_duplicates(self):

/usr/local/lib/python3.6/dist-packages/onnxconverter_common/topology.py in infer_types(self)
    108     def infer_types(self):
    109         # Invoke a core inference function
--> 110         registration.get_shape_calculator(self.type)(self)
    111 
    112 

/usr/local/lib/python3.6/dist-packages/onnxmltools/convert/coreml/shape_calculators/neural_network/Concat.py in calculate_concat_output_shapes(operator)
     22         if variable.type.shape[0] != 'None' and variable.type.shape[0] != output_shape[0]:
     23             raise RuntimeError('Only dimensions along C-axis can be different')
---> 24         if variable.type.shape[2] != 'None' and variable.type.shape[2] != output_shape[2]:
     25             raise RuntimeError('Only dimensions along C-axis can be different')
     26         if variable.type.shape[3] != 'None' and variable.type.shape[3] != output_shape[3]:

IndexError: list index out of range

来源:https://stackoverflow.com/questions/60735629/cant-convert-core-ml-model-to-onnx-then-to-tensorflow-lite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!