Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load CoreML.framework. Cannot make predictions. #372

Closed
pokidyshev opened this issue Jun 10, 2019 · 38 comments
Closed

Unable to load CoreML.framework. Cannot make predictions. #372

pokidyshev opened this issue Jun 10, 2019 · 38 comments

Comments

@pokidyshev
Copy link

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))})
@TobyRoseman
Copy link
Collaborator

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

@pokidyshev
Copy link
Author

pokidyshev commented Jun 11, 2019

@TobyRoseman
I’m using python 3.6 and coremltools 3.0b1
The same thing with 2.1.0

@TobyRoseman
Copy link
Collaborator

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:

    334 
    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

@pokidyshev
Copy link
Author

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.

@staubda
Copy link

staubda commented Jun 13, 2019

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.

@Necross
Copy link
Contributor

Necross commented Jul 2, 2019

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

@TobyRoseman
Copy link
Collaborator

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

@bhushan23
Copy link
Contributor

bhushan23 commented Jul 6, 2019

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?

@TobyRoseman
Copy link
Collaborator

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

@TobyRoseman
Copy link
Collaborator

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

@motasay
Copy link

motasay commented Aug 7, 2019

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.

@TobyRoseman
Copy link
Collaborator

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

@TobyRoseman TobyRoseman reopened this Aug 7, 2019
@josephsieh
Copy link

josephsieh commented Aug 23, 2019

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.

@Necross
Copy link
Contributor

Necross commented Aug 23, 2019

@josephsieh which operating system are you using?

@josephsieh
Copy link

@Necross macOS Mojave 10.14.6

@zhao15
Copy link

zhao15 commented Sep 14, 2019

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

@aseemw
Copy link
Collaborator

aseemw commented Sep 14, 2019

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

@josephsieh
Copy link

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

@1duo
Copy link
Collaborator

1duo commented Sep 18, 2019

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

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

@1duo
Copy link
Collaborator

1duo commented Sep 18, 2019

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

Thanks for verifying!

@aseemw aseemw closed this as completed Sep 18, 2019
@yaysummeriscoming
Copy link

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?

@AryanGold
Copy link

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

@wmpauli
Copy link

wmpauli commented Jul 28, 2020

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

@Vozf
Copy link
Contributor

Vozf commented Aug 19, 2020

It still reproduces with 4.0b3

@bhushan23
Copy link
Contributor

@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

@Vozf
Copy link
Contributor

Vozf commented Aug 19, 2020

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

@wmpauli
Copy link

wmpauli commented Aug 19, 2020

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

@TobyRoseman
Copy link
Collaborator

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

@TobyRoseman TobyRoseman reopened this Aug 19, 2020
@TobyRoseman
Copy link
Collaborator

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

@TobyRoseman
Copy link
Collaborator

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.

@glenn-jocher
Copy link

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.

@GodsonDsilva
Copy link

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

@TobyRoseman
Copy link
Collaborator

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

@vade
Copy link

vade commented Dec 31, 2020

This appears to be an issue with latest CoreML Tools with Miniconda / M1 Mac. I am using the following method to install CoreMLtools 4.0 on macOS 11 on an M1 Mac mini:

#1011 (comment)

@andrewk1
Copy link

andrewk1 commented Jan 4, 2021

This appears to be an issue with latest CoreML Tools with Miniconda / M1 Mac. I am using the following method to install CoreMLtools 4.0 on macOS 11 on an M1 Mac mini:

#1011 (comment)

I was having the same issue as well, but was able to fix it by compiling and installing coremltools from source (on M1 Macbook Air)

@pokidyshev
Copy link
Author

LMAO: was googling to find a solution to a similar bug and stumbled upon this thread. 2 years passed since I've opened this issue and the bug is still present 😂

exception loading model proxy: No module named 'coremltools.libcoremlpython'

Traceback (most recent call last):
  File "/Users/sandpiturtle/Developer/tavour/yolov5/detect_coreml.py", line 193, in <module>
    detect(opt=opt)
  File "/Users/sandpiturtle/Developer/tavour/yolov5/detect_coreml.py", line 75, in detect
    pred = mlmodel.predict({'image': img})
  File "/Users/sandpiturtle/opt/miniconda3/envs/yolov5/lib/python3.9/site-packages/coremltools/models/model.py", line 345, in predict
    raise Exception(
Exception: Unable to load CoreML.framework. Cannot make predictions.

@TobyRoseman
Copy link
Collaborator

@pokidyshev - This issue should be fixed now. It looks like you're using Python 3.9. Coremltools doesn't support Python 3.9 yet.

I think this is an installation issue. I think you must have installed coremltools using an egg rather than a wheel. Building libcoremlpython must have failed during installation. Did you receive an error message during installation?

I suggest just using Python 3.8 for now.

@poormag
Copy link

poormag commented Sep 19, 2021

coremltools5 beta is ok for python3.9+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests