无法使用 CoreML 工具将 MobileNet V2 PyTorch 转换为 mlmodel

发布于 2025-01-16 22:31:02 字数 3088 浏览 4 评论 0原文

我想使用 coremltools 将 PyTorch MobileNet V2 预训练模型转换为 .mlmodel。这是我的代码:

    import torchvision
    import torch
    import coremltools as ct

    # Load a pre-trained version of MobileNetV2
    torch_model = torchvision.models.mobilenet_v2(pretrained=True)
    # Set the model in evaluation mode.
    torch_model.eval()

    # Trace the model with random data.
    example_input = torch.rand(1, 3, 224, 224) 
    traced_model = torch.jit.trace(torch_model, example_input)
    out = traced_model(example_input)

    # Using image_input in the inputs parameter:
    # Convert to Core ML using the Unified Conversion API.
    model = ct.convert(
        traced_model,
        inputs=[ct.TensorType(shape=example_input.shape)]
     )

    # Save the converted model.
    model.save("mobilenet_v2.mlmodel")

它在 Google Colab 上运行良好,但是当我在本地计算机(MacBook)上运行它时,出现以下错误

Converting Frontend ==> MIL Ops: 100%|▉| 390/391 [00:00<00:00, 647.56
Running MIL Common passes:   0%|         | 0/34 [00:00<?, ? passes/s]anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/mil/passes/name_sanitization_utils.py:101: UserWarning: Input, 'input.1', of the source model, has been renamed to 'input_1' in the Core ML model.
  warnings.warn(msg.format(var.name, new_name))
anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/mil/passes/name_sanitization_utils.py:129: UserWarning: Output, '830', of the source model, has been renamed to 'var_830' in the Core ML model.
  warnings.warn(msg.format(var.name, new_name))
Running MIL Common passes: 100%|█| 34/34 [00:00<00:00, 41.87 passes/s
Running MIL Clean up passes: 100%|█| 9/9 [00:00<00:00, 80.15 passes/s
Translating MIL ==> NeuralNetwork Ops: 100%|█| 495/495 [00:00<00:00, 
Traceback (most recent call last):
  File "convert_models.py", line 24, in <module>
    model = ct.convert(
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 352, in convert
    mlmodel = mil_convert(
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 183, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 231, in _mil_convert
    return modelClass(proto,
  File "anaconda3/lib/python3.8/site-packages/coremltools/models/model.py", line 346, in __init__
    self.__proxy__, self._spec, self._framework_error = _get_proxy_and_spec(
  File "anaconda3/lib/python3.8/site-packages/coremltools/models/model.py", line 123, in _get_proxy_and_spec
    specification = _load_spec(filename)
  File "/anaconda3/lib/python3.8/site-packages/coremltools/models/utils.py", line 210, in load_spec
    raise Exception(
Exception: Unable to load libmodelpackage. Cannot make save spec.

我正在使用以下版本的库:

  • torchvision: '0.9.0'
  • torch: ' 1.8.0'
  • coremltools: '5.2.0'

I want to convert PyTorch MobileNet V2 pre-trained model to .mlmodel using coremltools. here is my code:

    import torchvision
    import torch
    import coremltools as ct

    # Load a pre-trained version of MobileNetV2
    torch_model = torchvision.models.mobilenet_v2(pretrained=True)
    # Set the model in evaluation mode.
    torch_model.eval()

    # Trace the model with random data.
    example_input = torch.rand(1, 3, 224, 224) 
    traced_model = torch.jit.trace(torch_model, example_input)
    out = traced_model(example_input)

    # Using image_input in the inputs parameter:
    # Convert to Core ML using the Unified Conversion API.
    model = ct.convert(
        traced_model,
        inputs=[ct.TensorType(shape=example_input.shape)]
     )

    # Save the converted model.
    model.save("mobilenet_v2.mlmodel")

It worked well on Google Colab, but when I run it my local machine (MacBook), I've got the following error

Converting Frontend ==> MIL Ops: 100%|▉| 390/391 [00:00<00:00, 647.56
Running MIL Common passes:   0%|         | 0/34 [00:00<?, ? passes/s]anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/mil/passes/name_sanitization_utils.py:101: UserWarning: Input, 'input.1', of the source model, has been renamed to 'input_1' in the Core ML model.
  warnings.warn(msg.format(var.name, new_name))
anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/mil/passes/name_sanitization_utils.py:129: UserWarning: Output, '830', of the source model, has been renamed to 'var_830' in the Core ML model.
  warnings.warn(msg.format(var.name, new_name))
Running MIL Common passes: 100%|█| 34/34 [00:00<00:00, 41.87 passes/s
Running MIL Clean up passes: 100%|█| 9/9 [00:00<00:00, 80.15 passes/s
Translating MIL ==> NeuralNetwork Ops: 100%|█| 495/495 [00:00<00:00, 
Traceback (most recent call last):
  File "convert_models.py", line 24, in <module>
    model = ct.convert(
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 352, in convert
    mlmodel = mil_convert(
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 183, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
  File "anaconda3/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 231, in _mil_convert
    return modelClass(proto,
  File "anaconda3/lib/python3.8/site-packages/coremltools/models/model.py", line 346, in __init__
    self.__proxy__, self._spec, self._framework_error = _get_proxy_and_spec(
  File "anaconda3/lib/python3.8/site-packages/coremltools/models/model.py", line 123, in _get_proxy_and_spec
    specification = _load_spec(filename)
  File "/anaconda3/lib/python3.8/site-packages/coremltools/models/utils.py", line 210, in load_spec
    raise Exception(
Exception: Unable to load libmodelpackage. Cannot make save spec.

I am using the following versions of libraries:

  • torchvision: '0.9.0'
  • torch: '1.8.0'
  • coremltools: '5.2.0'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2025-01-23 22:31:02

我通过将 macOS 从 10.14.6 Mojave 更新到 11.5.2 MacOS Big Sur 解决了这个问题。从我创建的 Github issue 中,他们向我解释了版本“5.2我之前的 MacOS 版本不支持 coremltools 的“”。因此,通过更新您的 macOS,它应该可以工作。

I solved this problem by updating the macOS from 10.14.6 Mojave to 11.5.2 MacOS Big Sur. From the Github issue that I have created, they explained to me that the version "5.2" of coremltools is not supported in my previous MacOS version. So, by updating your macOS it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文