我正在尝试使用 CoreML 工具将 Keras 模型转换为 coreml,但我在预测时出错。首先我转换了模型:
coreml_model = coremltools.converters.keras.convert(
model,
input_names='stampImage', # Inform CoreML the input name
image_input_names = 'stampImage', # Inform CoreML the input for images
output_names='prediction',
class_labels = class_labels, # From labels_dict
predicted_feature_name='predicted_feature_name',
is_bgr=True)
coreml_model.save('./trained_models_data/stamps' + version + '.mlmodel')
转换运行良好。然后我加载了模型:
model = coremltools.models.MLModel('./trained_models_data/stamps' + version + '.mlmodel')
和图像:
from PIL import Image
image = Image.open('C1243_LND_899FA3F6-B703-4724-9701-F11DAD7CEDFE.jpg')
但是当我试图预测时:
model.predict({'stampImage': image})
我收到一个错误:
Error: value type not convertible:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=714x714 at 0x1127425A8>
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-21-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
我尝试按照 WWDC 中“Core ML in depth”会议的步骤进行操作……有人知道出了什么问题吗?