【发布时间】:2021-01-04 10:41:03
【问题描述】:
我正在开发一个需要使用 CoreML 模型来执行图像分类的 iOS 应用程序。
我使用 Google Cloud Platform AutoML Vision 来训练模型。 Google 提供了该模型的 CoreML 版本,我下载了它以在我的应用程序中使用。
我按照 Google 的教程进行操作,一切似乎都很顺利。然而,当它使用时间开始使用模型时,得到了非常奇怪的预测。我得到了预测的信心,然后我得到了一个非常奇怪的字符串,我不知道它是什么。
<VNClassificationObservation: 0x600002091d40> A7DBD70C-541C-4112-84A4-C6B4ED2EB7E2 requestRevision=1 confidence=0.332127 "CICAgICAwPmveRIJQWdsYWlzX2lv"
我指的字符串是CICAgICAwPmveRIJQWdsYWlzX2lv。
经过一番研究和调试,我发现这是一个 NSCFString。
https://developer.apple.com/documentation/foundation/1395135-nsclassfromstring
显然这是基础 API 的一部分。有人有这方面的经验吗?
CoreML 文件还附带一个带有正确标签的 dict.txt 文件。我必须将此字符串转换为标签吗?我该怎么做。
这是我到目前为止的代码。
//
// Classification.swift
// Lepidoptera
//
// Created by Tomás Mamede on 15/09/2020.
// Copyright © 2020 Tomás Santiago. All rights reserved.
//
import Foundation
import SwiftUI
import Vision
import CoreML
import ImageIO
class Classification {
private lazy var classificationRequest: VNCoreMLRequest = {
do {
let model = try VNCoreMLModel(for: AutoML().model)
let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in
if let classifications = request.results as? [VNClassificationObservation] {
print(classifications.first ?? "No classification!")
}
})
request.imageCropAndScaleOption = .scaleFit
return request
}
catch {
fatalError("Error! Can't use Model.")
}
}()
func classifyImage(receivedImage: UIImage) {
let orientation = CGImagePropertyOrientation(rawValue: UInt32(receivedImage.imageOrientation.rawValue))
if let image = CIImage(image: receivedImage) {
DispatchQueue.global(qos: .userInitiated).async {
let handler = VNImageRequestHandler(ciImage: image, orientation: orientation!)
do {
try handler.perform([self.classificationRequest])
}
catch {
fatalError("Error classifying image!")
}
}
}
}
}
【问题讨论】:
-
将
NSCFString视为NSString,它只是一个内部类。由于它是一个 NSString,它应该可以毫无问题地桥接到String。 -
感谢您的回复。问题是我不想要那个字符串。我想要正确的标签,这不是模型应该返回的标签...:/
标签: ios swift machine-learning computer-vision coreml