Not Retriveing Output Prediction List from Remote Firebase Automl custom model

十年热恋 提交于 2019-12-25 00:25:55

问题


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.

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] + "%")





}

}

This is the error I recieved:

Fatal error: Index out of range
2019-08-31 19:50:19.763469-0700 medicalAppFinal[13776:2281569]            
(lldb) 

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


回答1:


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] + "%")

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.



来源:https://stackoverflow.com/questions/57747464/not-retriveing-output-prediction-list-from-remote-firebase-automl-custom-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!