我目前正在尝试构建使用Google的autoML功能的应用。我已经训练了一个模型并将其发布在google firebase上,并根据文档将必要的代码集成到了我的应用中:
https://firebase.google.com/docs/ml-kit/ios/label-images-with-automl
我使用的是远程模型,而不是本地模型。但是,当我尝试运行代码,然后在模拟器中选择图像时,控制台中将输出一个空的预测列表。
我还打开了调试功能,但这并不能帮助我解决错误。这是我在ViewController中运行的代码:
import UIKit
import CoreML
import Vision
import Firebase
import FirebaseMLCommon
var serverImage: UIImage? = nil
var topResult = ""
class ViewController: UIViewController {
@IBOutlet var skinDiseaseImageView: UIImageView!
@IBOutlet var result1Label: UILabel!
@IBOutlet var result1Confidence: UILabel!
@IBOutlet var result2Label: UILabel!
@IBOutlet var result2Confidence: UILabel!
@IBOutlet var result3Label: UILabel!
@IBOutlet var result3Confidence: UILabel!
override func viewDidLoad() {
let initialConditions = ModelDownloadConditions(allowsCellularAccess: true,
allowsBackgroundDownloading: true)
let updateConditions = ModelDownloadConditions(allowsCellularAccess: false,
allowsBackgroundDownloading: true)
let remoteModel = RemoteModel(
name: "skinDiseaseModel", // The name you assigned in the console.
allowsModelUpdates: true,
initialConditions: initialConditions,
updateConditions: updateConditions
)
ModelManager.modelManager().register(remoteModel)
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController {
@IBAction func selectImage(_ sender: Any) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .savedPhotosAlbum
present(pickerController, animated: true)
}
}
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
dismiss(animated: true)
guard let skinImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Error Retrieving Image Line 95")
}
var skinImageToDiagnose = VisionImage(image: skinImage)
serverImage = skinImage
skinDiseaseImageView.image = skinImage
let labelerOptions = VisionOnDeviceAutoMLImageLabelerOptions(
remoteModelName: "skinDiseaseModel", // Or nil to not use a remote model
localModelName: nil // Or nil to not use a bundled model
)
labelerOptions.confidenceThreshold = 0 // Evaluate your model in the Firebase console
// to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: labelerOptions)
var topThreeResults = [String]()
var topThreeConfidences = [String]()
labeler.process(skinImageToDiagnose) { labels, error in
guard error == nil, let labels = labels
else {
print(error)
return
}
//task succeeded
print("1")
print(labels)
var counter = 0
for label in labels {
topThreeResults.append(String(describing: label))
topThreeConfidences.append(String(describing: label.confidence))
counter = counter + 1
print("counter")
if counter == 3 {
break
}
}
}
result1Label.text = topThreeResults[0]
result1Confidence.text = (topThreeConfidences[0] + "%")
result2Label.text = topThreeResults[1]
result2Confidence.text = (topThreeConfidences[1] + "%")
result3Label.text = topThreeResults[2]
result3Confidence.text = (topThreeConfidences[2] + "%")
}
}
这是我收到的错误:
Fatal error: Index out of range
2019-08-31 19:50:19.763469-0700 medicalAppFinal[13776:2281569]
(lldb)
我认为索引超出范围问题是由于标签列表(输出预测)在打印后为空。因此,我理解了为什么索引超出范围,但是我不知道为什么在将图像传递到labeler.process()后收到一个空列表。如何解决此错误?告诉我您是否需要更多信息
答案 0 :(得分:1)
这是一个逻辑错误。这部分代码:
result1Label.text = topThreeResults[0]
result1Confidence.text = (topThreeConfidences[0] + "%")
result2Label.text = topThreeResults[1]
result2Confidence.text = (topThreeConfidences[1] + "%")
result3Label.text = topThreeResults[2]
result3Confidence.text = (topThreeConfidences[2] + "%")
应该位于labeler.process()函数内部。否则,它将运行以上代码,甚至没有检索到预测列表,从而导致致命错误。通过将其放入其中,我确保它已检索到预测列表,然后才运行上面的代码以在列表中查找特定值。