无法使用 CoreML 工具将 MobileNet V2 PyTorch 转换为 mlmodel
Couldn't convert MobileNet V2 PyTorch to mlmodel using CoreML tools
我想使用 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'
- 手电筒:'1.8.0'
- coremltools: '5.2.0'
我通过将 macOS 从 10.14.6 Mojave 更新到 11.5.2 MacOS Big Sur 解决了这个问题。从我创建的 Github issue 中,他们向我解释说我以前的 MacOS 版本不支持 coremltools 版本“5.2”。因此,通过更新您的 macOS 它应该可以正常工作。
我想使用 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'
- 手电筒:'1.8.0'
- coremltools: '5.2.0'
我通过将 macOS 从 10.14.6 Mojave 更新到 11.5.2 MacOS Big Sur 解决了这个问题。从我创建的 Github issue 中,他们向我解释说我以前的 MacOS 版本不支持 coremltools 版本“5.2”。因此,通过更新您的 macOS 它应该可以正常工作。