Coremltools: Unable to load CoreML.framework. Cannot make predictions.

Created on 10 Jun 2019  路  33Comments  路  Source: apple/coremltools

I've got the latest coremltools installed from source.

I can't run model.predict because it gives exception:

Exception                                 Traceback (most recent call last)
<ipython-input-32-c70d9e92a144> in <module>
     54 
     55 
---> 56 coreml_model.predict({"input": np.zeros((1, 10, 4, 1, 1))})
     57 

~/.local/share/virtualenvs/FaceBoxes.PyTorch-kg-NzRFz/src/coremltools/coremltools/models/model.py in predict(self, data, useCPUOnly, **kwargs)
    339 
    340             if not _MLModelProxy:
--> 341                 raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
    342             elif _MLModelProxy.maximum_supported_specification_version() < self._spec.specificationVersion:
    343                 engineVersion = _MLModelProxy.maximum_supported_specification_version()

Exception: Unable to load CoreML.framework. Cannot make predictions.

Here is my snippet:

import onnx
import torch
import numpy as np

from torch import nn
from onnx import onnx_pb
from onnx_coreml import convert


class Dummy(nn.Module):
    def __init__(self):
        super().__init__()
        self.priors = torch.randn(10, 4)
        self.variances = [1, 1]

    def forward(self, loc):
        boxes = torch.cat([
            self.priors[:, :2] + loc[:, :2] * self.variances[0] * self.priors[:, 2:],
            self.priors[:, 2:] * torch.exp(loc[:, 2:] * self.variances[1])], 1)
        boxes = torch.cat([boxes[:, :2] - boxes[:, 2:] / 2, boxes[:, 2:]], 1)
        boxes = torch.cat([boxes[:, :2], boxes[:, 2:] + boxes[:, :2]], 1)
        return boxes

model = Dummy()

dummy_input = torch.randn(10, 4)

model_name = 'dummy'
input_names = ['input']
output_names = ['out']
torch.onnx.export(model, dummy_input, f"{model_name}.onnx", verbose=False,
                  input_names=input_names, output_names=output_names)

model_in = f"{model_name}.onnx"
model_out = f"{model_name}.mlmodel"

model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())

coreml_model = convert(model_proto)
coreml_model.save(model_out)

# this line gives Exception
coreml_model.predict({"input": np.zeros((1, 10, 4, 1, 1))})

Most helpful comment

@motasay - I just tried with Python 3.6 and 3.0b4. You're right it's not working. I'm reopening this issue.

Looks like we've had a regression. This issue is fixed in 3.0b3.

All 33 comments

@pokidyshev - What version of Python are you using? Also are you using the 3.0b1 version of coremltools?

@TobyRoseman
I鈥檓 using python 3.6 and coremltools 3.0b1
The same thing with 2.1.0

I'm seeing a similar issue. I'm on macOS 10.14.6.

I downloaded the MNIST classifier from the Core ML Model page. The following code works fine with coremltools version 2.1.0, for all version of Python support by 2.1.0 (i.e. 2.7, 3.5, 3.6):

import coremltools
from PIL import Image
import numpy as np

data = np.empty((28,28), dtype=np.uint8)
input_image = Image.fromarray(data)

model = coremltools.models.MLModel('./Downloads/MNISTClassifier.mlmodel')
print(model.predict({'image': input_image}))

Using the same code with coremltools version 3.0b1, it only works for Python 2.7.


For Python 3.6, I get the following error when I try to load the model:

Fatal Python error: PyThreadState_Get: no current thread

Abort trap: 6

For Python 3.5 and 3.7 I get the following error when I try to call predict on the model:

Traceback (most recent call last):
  File "/Users/tobyroseman/backup/cmt_issues.py", line 9, in <module>
    print(model.predict({'image': input_image}))
  File "/Users/tobyroseman/anaconda3/envs/env_name/lib/python3.7/site-packages/coremltools/models/model.py", line 341, in predict
    raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
Exception: Unable to load CoreML.framework. Cannot make predictions.

If I modify the install code to remove this line from a try/except block, I get the following error:

    335             #try:
--> 336             from ..libcoremlpython import _MLModelProxy
    337             #except:
    338             #    _MLModelProxy = None

ImportError: dlopen(/Users/tobyroseman/Documents/turicreate-py37/deps/env/lib/python3.7/site-packages/coremltools/libcoremlpython.so, 2): Library not loaded: /Library/Frameworks/Python.framework/Versions/3.7/Python
  Referenced from: /Users/tobyroseman/Documents/turicreate-py37/deps/env/lib/python3.7/site-packages/coremltools/libcoremlpython.so
  Reason: image not found

On my setup it also happens on 2.1.0 because of slice operation (x[:,:,:2]), when I change indexing with torch.split the error disappears.

Also getting the
'Unable to load CoreML.framework. Cannot make predictions.'
error running Python 3.6 and coremltools 3.01b.

Getting further using Python 2.7, although hitting a different error (maybe my fault):
The model expects input feature Preprocessor__sub__0 to be an image, but the input is of type 5.

EDIT:
Got things working by switching to Python 2.7 and feeding in an Image rather than a numpy array.

Are you hitting the same issue with coremltools3.0b2?

@Necross - this is still broken with the 3.0b2 wheel. The code I shared previously in this issues fails if you're using Anaconda Python. It does however work if you are using Python downloaded from python.org.

Problem is missing libcoremlpython.so from

< PYTHON SITE PACKAGE PATH >/coremltools/

In my case PYTHON SITE PACKAGE PATH = /Users/bhushansonawane/miniconda3/envs/py36/lib/python3.6/site-packages/coremltools-3.0b2-py3.6.egg/coremltools

Following worked for me-

1. Build from source - provide correct env path
2. Use setup.py in build directory to copy generated libcoremlpython.so

build from source as follow

1. cd to root of coremltools
2. mkdir build && cd build
3. cmake \
  -DPYTHON_EXECUTABLE:FILEPATH=/Users/bhushansonawane/miniconda3/envs/py35/bin/python \
  -DPYTHON_INCLUDE_DIR=/Users/bhushansonawane/miniconda3/envs/py35/include/python3.5m/ \
  -DPYTHON_LIBRARY=/Users/bhushansonawane/miniconda3/envs/py35/lib/python3.5/ \
../
4. make install
4. cp ../setup.py ./
5. python setup.py install

python setup.py install copies coremltools directly to the package directory and hence misses libcoremlpython library
Not using DPYTHON_LIBRARY during cmake leads to

Fatal Python error: PyThreadState_Get: no current thread

which I am not sure of why yet

I have tested this on python3.5, python3.6 works fine for me.

@TobyRoseman could you please give it a try?

@bhushan23 - that works for me too. Thank you so much for your help!

I was trying something very similar but was missing the -DPYTHON_LIBRARY= parameter. I've created a pull request (#394) to add that to the instructions.

This has been fixed in the most recent release. Please install the fourth beta (pip install coremltools==3.0b4).

I have 3.0b4 and am facing the same issue when trying to predict with the model. Python 3.6.9 and macOS 10.14.6.

> import coremltools

> feature_names = list(map(lambda x: str(x), X_train.columns.values))
> output_names = "card_index"
> coreml_model = coremltools.converters.sklearn.convert(clf, input_features=feature_names,
                                                  output_feature_names=output_names)

> dummy_inputs = {x:0 for x in feature_names}
> coreml_model.predict(dummy_inputs)

exception loading model proxy: dlopen(/Users/motasim/.pyenv/versions/3.6.9/envs/BalootAI-py369/lib/python3.6/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _objc_opt_class
  Referenced from: /Users/xxxx/.pyenv/versions/3.6.9/envs/AI-py369/lib/python3.6/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.15)
  Expected in: /usr/lib/libobjc.A.dylib
 in /Users/xxxx/.pyenv/versions/3.6.9/envs/AI-py369/lib/python3.6/site-packages/coremltools/libcoremlpython.so

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-20-b61708773172> in <module>
      5 
      6 dummy_inputs = {x:0 for x in feature_names}
----> 7 model.predict(dummy_inputs)

~/.pyenv/versions/3.6.9/envs/AI-py369/lib/python3.6/site-packages/coremltools/models/model.py in predict(self, data, useCPUOnly, **kwargs)
    343 
    344             if not _MLModelProxy:
--> 345                 raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
    346             elif _MLModelProxy.maximum_supported_specification_version() < self._spec.specificationVersion:
    347                 engineVersion = _MLModelProxy.maximum_supported_specification_version()

Exception: Unable to load CoreML.framework. Cannot make predictions.

@motasay - I just tried with Python 3.6 and 3.0b4. You're right it's not working. I'm reopening this issue.

Looks like we've had a regression. This issue is fixed in 3.0b3.

python 3.7.4 with coremltools 3.0b5 has the same issue :

exception loading model proxy: dlopen(aanaconda3/envs/coreml/lib/python3.7/site-packages/coremltools/libcoremlpython.so, 2): 
Library not loaded: /Library/Frameworks/Python.framework/Versions/3.7/Python
Referenced from: /aanaconda3/envs/coreml/lib/python3.7/site-packages/coremltools/libcoremlpython.so

python 3.7.4 with coremltools 3.0b3 works properly though.

@josephsieh which operating system are you using?

@Necross macOS Mojave 10.14.6

macOS 10.15 (19A558d), python3.7 , coremltools 3.0b5
same issue.

@zhao15 3.0b6 wheels have been released as well, which should fix the issue. Can you try that as well?

Now it's working again. Tested on python 2.7.16 and python 3.7.4

macOS 10.15 (19A558d), python3.7 , coremltools 3.0b5
same issue.

Can you verify with coremltools==3.0b6? Thanks.

Now it's working again. Tested on python 2.7.16 and python 3.7.4

Thanks for verifying!

I'm getting the same issue. Running 10.14.6, tried both python 3.6 and 3.7. Also tried coremltools 3.0, 3.1 and 3.2 and building from source as per bhushan23's instructions. Any ideas?

I'm getting the same issue. Running 10.14.6, tried both python 3.6 and 3.7. Also tried coremltools 3.0, 3.1 and 3.2 and building from source as per bhushan23's instructions. Any ideas?

You should try manually copy builded library 'libcoremlpython.so' to 'site-packages/..../coremltools' folder.

I builded Coremltools from source as mentioned bhushan23 in folder: "/Users/admin/temp/coremltools/" and then copy library.
sample:
cp /Users/admin/temp/coremltools/coremltools/libcoremlpython.so /Users/admin/Library/Python/3.7/lib/python/site-packages/coremltools-3.3-py3.7.egg/coremltools/
and all works

I'm having the same issue, with coremltools==4.0b2 (also tried down to 3.3).

the library file seems to be in the right place. /lib/python3.7/site-packages/coremltools/libcoremlpython.so

with python=3.7.7 torchvision=0.6.1 and torch=1.5.1

I'm getting this error message:
exception loading model proxy: dlopen(/Users/[...]/miniconda3/envs/[..]/lib/python3.7/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _OBJC_CLASS_$_MLModelConfiguration
Referenced from: /Users/[..]/miniconda3/envs/[..]/lib/python3.7/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.16)
Expected in: /System/Library/Frameworks/CoreML.framework/Versions/A/CoreML
in /Users/[..]/miniconda3/envs/[..]/lib/python3.7/site-packages/coremltools/libcoremlpython.so

CoreML export failure: Unable to load CoreML.framework. Cannot make predictions.

mac os version: 10.13.6

I can't even get it to work on the example from the coremltools documentation:

import coremltools as ct
import torch
import torchvision

model = torchvision.models.mobilenet_v2()
model.eval()
example_input = torch.rand(1, 3, 256, 256)
traced_model = torch.jit.trace(model, example_input)

input = ct.TensorType(name='input_name', shape=(1, 3, 256, 256))
mlmodel = ct.convert(traced_model, inputs=[input], minimum_deployment_target=ct.target.macOS15)
results = mlmodel.predict({"input": example_input.numpy()})
print(results['1651']) # 1651 is the node name given by PyTorch's JIT

It still reproduces with 4.0b3

@Vozf @wmpauli could you please try installing coremltools from following wheels?(https://github.com/apple/coremltools/releases/tag/4.0b3) in following order?

  1. py3 wheels for your Mac version
  2. py3 any wheels

Recreated the whole env and this error is gone for me. Although previous env was with python3.8, so this may have been an issue

@bhushan23 , I was also able by downgrading the python version. I don't have my mac with me right now, but i think both 3.6 and 3.7 worked.

Yes, this is broken again, at least in Python 3.8. It was working properly in 4.0b2. Reopening.

It seems I spoke too soon when I said that it was working properly in 4.0b2. Some of the wheels for that version also do not seem to work correctly. For example coremltools-4.0b2-cp27-none-macosx_10_13_intel.whl does not work. I get the following error when trying to load the CoreML framework:

/venv/lib/python2.7/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _OBJC_CLASS_$_MLModelConfiguration
  Referenced from: /venv/lib/python2.7/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.16)
  Expected in: /System/Library/Frameworks/CoreML.framework/Versions/A/CoreML

I have not checked all of the wheels for 4.0b2 but I'm seeing the same issue for other combinations for macOS/Python-Version: 10.14/2.7, 10.14/3.5, 10.14/3.6, 10.14/3.7 and 10.14/3.8.

Receive this error with coremltools 4.0b3. and Python 3.8 in MacOS. Downgrading to a Python 3.7 environment solved the problem for me.

I am facing the same issue with coremltools 4.0b3, even with Python 3.7 ... I am having MacOS 10.14.7.

According to my tests this is now fixed in coremltools 4.0b4.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nneubauer picture nneubauer  路  4Comments

zantism picture zantism  路  5Comments

DanWBR picture DanWBR  路  4Comments

G-mel picture G-mel  路  4Comments

wschin picture wschin  路  4Comments