【问题标题】:How to access prediction results of an image classifier ML model in an iOS app using Swift, UIkit, and CoreML如何使用 Swift、UIkit 和 CoreML 在 iOS 应用程序中访问图像分类器 ML 模型的预测结果
【发布时间】:2021-12-22 05:05:32
【问题描述】:

我正在尝试开发一个应用程序,该应用程序使用使用 Apple 的 CoreML 训练的模型对从相机拍摄的图像或从图像库中选择的图像进行分类。该模型经过适当的训练和测试。在将它添加到 xcode 项目后,当我使用 Preview 对其进行测试时,它显示没有问题。但是当我尝试使用 Swift 进行预测时,结果是错误的,并且与 Preview 显示的完全不同。感觉模型没有经过训练。

这是我访问模型所做预测的代码:

let pixelImage = buffer(from: (image ?? UIImage(named: "imagePlaceholder"))!)
self.imageView.image = image

guard let result = try? imageClassifier!.prediction(image: pixelImage!) else {
    fatalError("unexpected error happened")
}
        
let className: String = result.classLabel
let confidence: Double = result.classLabelProbs[result.classLabel] ?? 1.0
classifier.text = "\(className)\nWith Confidence:\n\(confidence)"

print("the classification result is: \(className)\nthe confidence is: \(confidence)")

imageClassifier是我在代码段之前使用这行代码创建的模型:

let imageClassifier = try? myImageClassifier(configuration: MLModelConfiguration())

myImageClassifier 是我使用 CoreML 创建的 ML 模型的名称。

图像是正确的,即使我输入相同的图像,它也会显示不同于预览的结果。但它必须转换为 UIImage 类型到 CVPixelBuffer,因为预测只允许输入 CVPixelBuffer 类型。上面代码段中的pixelImage 是更改为CVPixelBuffer 类型后的图像。我使用this stackoverflow question 中的解决方案进行转换。代码在这里,以防出现问题:

func buffer(from image: UIImage) -> CVPixelBuffer? {
    let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
    var pixelBuffer : CVPixelBuffer?
    let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(image.size.width), Int(image.size.height), kCVPixelFormatType_32ARGB, attrs, &pixelBuffer)
    guard (status == kCVReturnSuccess) else {
        return nil
    }
    
    CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
    let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)

    let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
    let context = CGContext(data: pixelData, width: Int(image.size.width), height: Int(image.size.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)

    context?.translateBy(x: 0, y: image.size.height)
    context?.scaleBy(x: 1.0, y: -1.0)

    UIGraphicsPushContext(context!)
    image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
    UIGraphicsPopContext()
    CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))

    return pixelBuffer
}

我不认为模型本身有什么问题,只是我在应用中实现它的方式。

编辑: 我已经从 Apple 的教程中下载了一个示例项目,并将其模型 MobileNet 实现到我的项目中。代码执行没有错误,结果是正确的。我创建的模型可能有问题吗?

【问题讨论】:

    标签: ios swift machine-learning coreml


    【解决方案1】:

    我现在得到了正确的结果。我唯一改变的是在真实设备上运行应用程序(在这种情况下,我的 iPad mini 6,iPadOS 15.0)而不是模拟器,模型和代码保持不变。我只能假设我的代码或模型没有任何问题,只是模拟器存在一些问题导致了这个错误。我不知道为什么会发生这种情况。我的 Xcode 是 13.1 版本,模拟器在 iOS 15 上运行。如果这确实是一个错误,那么苹果需要修复它。这确实让我们开发人员的生活变得更加艰难。

    【讨论】:

    • 请在模拟器上尝试: let configuration = MLModelConfiguration() configuration.computeUnits = .cpuOnly 对于 GPU 和 CPU 得到不同推断的情况。
    • @DmytroHrebeniuk 我试过这个,但即使我多次输入相同的图像,我仍然得到不同和错误的预测。我也尝试了 .cpuAndGpu 和 .all,但没有发生任何不同。当我在真实设备上运行它时仍然没有任何问题。
    • 感谢您的分享,拯救我的一天 Orz
    猜你喜欢
    • 2020-06-30
    • 2020-10-26
    • 2018-11-28
    • 2019-04-03
    • 2020-10-09
    • 2019-08-02
    • 2021-07-14
    • 2018-01-04
    • 2021-06-23
    相关资源
    最近更新 更多