how to use python predict on image?

I've converted model from caffe and trying to run predict like this:


e = np.zeros((1,3,224,224))
d = {}
d['data'] = e
r = coreml_model.predict(d)

And have

RuntimeError: {

NSLocalizedDescription = "The model expects input feature data to be an image, but the input is of type 5.";

}

Any ideas?

Thanks!

Answered by in 236602022

For Images, CoreML uses python's Pillow library (pip install Pillow). Here is the code snippet that should work for you


import coremltools

# Load an image using PIL
from PIL import Image
rose = Image.open('rose.jpg')

coreml_model.predict({'data': rose})


An example of doing this is also availiable in Session 710's video (https://developer.apple.com/videos/play/wwdc2017/710/)


Note that in your code above, you were passing a Numpy object which gets converted to an MLMultiArray in CoreML.

Accepted Answer

For Images, CoreML uses python's Pillow library (pip install Pillow). Here is the code snippet that should work for you


import coremltools

# Load an image using PIL
from PIL import Image
rose = Image.open('rose.jpg')

coreml_model.predict({'data': rose})


An example of doing this is also availiable in Session 710's video (https://developer.apple.com/videos/play/wwdc2017/710/)


Note that in your code above, you were passing a Numpy object which gets converted to an MLMultiArray in CoreML.

It works, thanks!

I'm trying to do the same, following the WWDC video, but I'm receiving an error:


Error: value type not convertible:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=200x200 at 0x121AC04D0>
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-38-82fd85392a7f> in <module>()
----> 1 model.predict({'stampImage': image})


/anaconda/envs/coremltools_py2/lib/python2.7/site-packages/coremltools/models/model.pyc in predict(self, data, **kwargs)
    236         """
    237         if self.__proxy__:
--> 238             return self.__proxy__.predict(data)
    239         else:
    240             if _sys.platform != 'darwin' or float('.'.join(_platform.mac_ver()[0].split('.')[:2])) < 10.13:


RuntimeError: value type not convertible


The Keras model works fine... Do you have any idea about what is going wrong?

e = np.zeros((3,224,224)) 
d = {} 
d['data'] = e 
r = coreml_model.predict(d)


The model expects the input to be of size 1, 3 or 5. So just change (1,3,224,224) which is of size 4 to (3,224,224) which is of size 3.

how to use python predict on image?
 
 
Q