是否可以使用 coremltools mlmodel 预测一批图像?

问题描述

是否可以在 mlmodel 中预测批次?如果是,如何?

我将 keras 模型转换为 mlmodel,如in the documentation 所示:

import coremltools as ct 
image_input = ct.ImageType(name='input',shape=(1,224,3))
model = ct.convert(keras_model,inputs=[image_input])

接下来,我加载一个图像,将其大小调整为 (224,224),将其转换为 PIL,并将其用于预测:

img_resized = cv2.resize(img_read,(224,224))
PIL_image = Image.fromarray(np.uint8(img_resized))
img_heatmap = self.model.predict({'input': PIL_image})

是否可以使用批量图像而不是单个图像?

我尝试在转换中定义一批输入类型:

BATCH_SIZE = 8
image_input = ct.ImageType(name='input',shape=(BATCH_SIZE,inputs=[image_input])

但它似乎并没有改变模型的输入。

  1. 当我尝试为它提供 BATCH_SIZE 图像 ((8,3)) 的 numpy 数组时,我收到:
    return self.__proxy__.predict(data,usecpuOnly)
RuntimeError: value type not convertible
  1. 当我尝试向它提供BATCH_SIZE PIL 图像列表时,我收到:
RuntimeError: {
    NSLocalizedDescription = "required input feature not passed to neural network.";
}
  1. 当我使用单个图像进行预测时,输出与带有 BATCH_SIZE=1 的模型相同。

是否可以预测批次?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)