问题描述
我目前正在尝试构建一个使用Google的autoML功能的应用.我已经训练了一个模型并将其发布在google firebase上,并根据文档将必要的代码集成到了我的应用中:
I am currently trying to build an app that uses google's autoML feature. I have trained a model and published it on google firebase and have integrated the necessary code into my app following the documentation:
https://firebase.google.com/docs/ml-kit/ios/label-images-with-automl
我使用的是远程模型,而不是本地模型.但是,当我尝试运行代码,然后在模拟器中选择图像时,控制台中将输出一个空的预测列表.
I am using a remote model instead of making it local. However when I try running the code, then choose an image in the simulator, an empty list of predictions is output in the console.
我还打开了调试功能,但这并不能帮助我解决错误.这是我在ViewController中运行的代码:
I have also turned on the debugging feature, but this has not helped me fix my error. This is the code I am running in 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()后收到一个空列表.我该如何解决此错误?告诉我您是否需要更多信息
I reasoned that the index out of range problem is due to the list of labels(output predictions) being empty after having printed it. Thus I understand why it is index out of range, but I do not know why I am recieving an empty list after passing in the image into labeler.process() How do I solve this error? Tell me if you need more information
推荐答案
这是一个逻辑错误.这部分代码:
It was a logic error. This part of the code:
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()函数内部.否则,它将运行以上代码,甚至没有检索到预测列表,从而导致致命错误.通过将其放入其中,我确保它已检索到预测列表,然后才运行上面的代码以在列表中查找特定值.
should have been inside the labeler.process() function. Otherwise it was running the above code without having even retrieved a list of predictions, thus causing the fatal error. By placing it inside, I ensure it has retrieved the list of predictions and only then runs the above code to find specific values in the list.
这篇关于不从远程Firebase Automl自定义模型检索输出预测列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!