【问题标题】:CreateML MLModel works on playground UI but not in appCreateML MLModel 适用于 Playground UI,但不适用于应用程序
【发布时间】:2022-04-15 21:55:05
【问题描述】:

我正在开发一个机器学习应用程序,用于对手绘数字进行分类。我使用 CreateML 制作了一个模型,该模型据称具有 100% 的准确度(我承认我的样本量仅为每个数字大约 50 张图像)。但是,在我的应用程序上运行它时,它不起作用。为了查看这是否是我的应用程序的问题,我下载了Apple Vision+CoreML Example Xcode 项目并用我自己的替换了 MobileNet 分类器。我从自己的应用程序加载了保存在手机上的图像,但分类仍然不准确。有趣的是,我尝试在操场上的 CreateML UI 空间中测试完全相同的图像,您可以在其中测试图像和分类工作。

TL/DR:图像分类适用于 Playground 上的 CreateML Live View,但不适用于 Apple 的 vision+coreML 示例项目的精确副本。

Here is an example of an image that I tried to classify

Here is what shows up on the app for 7, Here is what shows up on the app for 5

Here is what shows up on the playground for 7, Here is what shows up on the playground for 5

【问题讨论】:

  • 在 Playground 中构建模型时,您是否将数据分成大约 2/3 的训练数据和 1/3 的测试数据?
  • @dktaylor 是的,我做到了,该模型在 UI 上也适用于训练/测试中未使用的其他新图像
  • 我最好的猜测是图像的裁剪不同。我很确定 Apple 视觉示例对传入的图像进行了中心裁剪
  • @dktaylor Apple 确实运行了这行代码request.imageCropAndScaleOption = .centerCrop,但我尝试将其注释掉,结果相同。
  • 您训练模型的图像与您在应用程序中使用的图像(图像大小、颜色等)有何不同?如果您在应用程序中使用其中一张训练图像,它会起作用吗?

标签: ios swift coreml apple-vision createml


【解决方案1】:

几天来我遇到了类似的问题,问题是 CreateML 可能会为 BGR 格式创建神经网络,并且在 Xcode 项目中 colorSpace 以 RGB 工作。您可以使用 coremltools 和 PIL 库在 Python 上测试您的模型。

诊断问题

获取模型的元数据##

import coremltools
from PIL import Image

#Import your model.
mlmodel = coremltools.models.MLModel('Path/To/Your/Model.mlmodel')
    
#print your metadata of your model, you will see input colorSpace.
print(mlmodel)

输入可能如下所示

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
        }
      }
    }
  }
}

转换输入的颜色空间

img = Image.open("Path/To/Your/Image")
img = img.convert("RGBA").   
data = np.array(img) 
red, green, blue, alpha = data.T
data = np.array([blue, green, red, alpha])
data = data.transpose()
img = Image.fromarray(data)
PIL_image = Image.fromarray(np.array(image))

使用转换后的图像根据您的模型进行预测

print(str(mlmodel.predict({'image': PIL_image})) + '\n')

这次你的预测应该是正确的。

我的解决方案

不幸的是,我不得不放弃 CreateML,在 App 端,我尝试在 PixelBuffer 中转换颜色空间,甚至通过将 UIImage 转换为 cv::Mat 和将 cv::Mat 转换为 UIImage 来导入 OpenCV 库来转换颜色空间,但没有其中为我工作。我使用 Apple 的另一个名为 Turi Create 的简单 ML 创建平台解决了我的问题。您必须使用 python 与此 API 交互,但文档非常清晰,ML 模板与 CreatML 相同。这个 API 比 CreateML 更好,因为您可以在训练前后与模型进行交互,而 CreateML 可以是非常封闭的盒子,即使使用 coremltools 您也无法与它进行很多交互。这个 API 对每个人来说都非常容易访问和简单,它的文档中有非常好的代码示例和场景。

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2020-06-28
    相关资源
    最近更新 更多