首页 > 解决方案 > 如何将 Core ML 模型文件转换为其他格式,以便它可以在服务器上运行

问题描述

训练了一个 Core ML 模型 (.mlmodel),但我发现它只能在 MacOS 上运行。(但我想在 Linux 服务器上运行它)

我试图将其转换为 ONNX 格式,但失败了。

onnxmltools并使用以下python代码进行转换:

import onnxmltools
import coremltools

# Load a Core ML model
coreml_model = coremltools.utils.load_spec('example.mlmodel')

# Convert the Core ML model into ONNX
onnx_model = onnxmltools.convert_coreml(coreml_model, 'Example Model')

# Save as protobuf
onnxmltools.utils.save_model(onnx_model, 'example.onnx')

但是运行上面的程序后显示以下错误消息

Traceback (most recent call last):
  File "convertToOnnx.py", line 8, in <module>
    onnx_model = onnxmltools.convert_coreml(coreml_model, 'Post Rating Model')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/main.py", line 18, in convert_coreml
    custom_conversion_functions, custom_shape_calculators)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/convert.py", line 60, in convert
    topology = parse_coreml(spec, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/_parse.py", line 467, in parse_coreml
    topology.compile()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 632, in compile
    self._infer_all_types()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 508, in _infer_all_types
    operator.infer_types()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 110, in infer_types
    registration.get_shape_calculator(self.type)(self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/shape_calculators/OneHotEncoder.py", line 36, in calculate_one_hot_encoder_output_shapes
    raise ValueError('Categorical indexes are missing')
ValueError: Categorical indexes are missing

如何解决上述错误?或者如何在 Linux 服务器上使用 Core ML 模型?(任何其他的模型转换方式?)

标签: coremlonnxcoremltoolsonnx-coreml

解决方案


推荐阅读