【发布时间】:2020-10-15 04:46:12
【问题描述】:
使用 Apple 的 Create ML 应用程序(Xcode 附带的开发工具),我训练了一个图像分类模型并下载了它。然后,我使用 coremltools 包将模型作为 python 项目的一部分加载:
import coremltools
import PIL.Image
def load_image(path, resize_to=None):
img = PIL.Image.open(path)
if resize_to is not None:
img = img.resize(resize_to, PIL.Image.ANTIALIAS)
r, g, b= img.split()
img = PIL.Image.merge("RGB", (b, g, r))
return img
model = coremltools.models.MLModel('classification1.mlmodel')
img_dir = "img1"
img = load_image(img_dir, resize_to=(299, 299))
result = model.predict({'image': img})
print(result)
此代码打印出预测的classlabel,与我直接在 Create ML 应用程序中预测 img1 的标签时得到的预测结果不同。我相信应用程序在预测图像的类标签之前对输入图像进行了一些调整。当我print(model)时,我得到了一些关于输入的信息:
input {
name: "image"
shortDescription: "Input image to be classified"
type {
imageType {
width: 299
height: 299
colorSpace: BGR
imageSizeRange {
widthRange {
lowerBound: 299
upperBound: -1
}
heightRange {
lowerBound: 299
upperBound: -1
}
}
}
}
}
我相信我已经通过调整图像大小和转换色彩空间进行了所需的调整。为什么代码和应用的预测不一致?
【问题讨论】:
标签: python python-imaging-library coreml coremltools createml