Search code examples
pytorchcoremltoolsmobilenetmlmodel

Couldn't convert MobileNet V2 PyTorch to mlmodel using CoreML tools


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'

Solution

  • 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.