无法使用 CoreML 工具将 MobileNet V2 PyTorch 转换为 mlmodel
我想使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将 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.