简体   繁体   中英

MLUpdateContext is empty when updating CoreML model

My problem is the following - In the method below the variable finalContext seem to not contain anything. I get error message: Error: The operation couldn't be completed. (Foundation._GenericObjCError error 0.) Error: The operation couldn't be completed. (Foundation._GenericObjCError error 0.) when calling the function. I need help how to debug this issue or what could be the possible cause for this. EDIT - finalContext does not contain the model that I am trying to access.

func updateModel(){
    //Configuration for when update is performed
    let modelConfig = MLModelConfiguration()
    modelConfig.computeUnits = .cpuAndGPU
    let fileManager = FileManager.default
    //Image batch for updating the model
    //Might need to change from a batch to a single image
    let updateImages: [UIImage] = [theImage!]
    let imageBatch = createTrainingData(imageArray: updateImages, outputLabel: "dog") // temp outputLabel
    do {
        let updateTask = try MLUpdateTask(forModelAt: globalCompiledModel!, trainingData: imageBatch, configuration: modelConfig,
                         progressHandlers: MLUpdateProgressHandlers(forEvents: [.trainingBegin,.epochEnd],
                          progressHandler: { (contextProgress) in
                            print(contextProgress.event)
                            // you can check the progress here, after each epoch
                         }) { (finalContext) in
                                do {
                                    // Save the updated model to temporary filename.
                                    let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:true)
                                    let fileURL = documentDirectory.appendingPathComponent("CatDog.mlmodelc")
                                    print("Updated temp model URL: \(fileURL)")
                                    try finalContext.model.write(to: fileURL)
                                } catch(let error) {
                                    print("Error: \(error.localizedDescription)")
                                }
        })
        updateTask.resume()
    } catch {
        print("Error while updating: \(error.localizedDescription)")
    }
}

I found the issue here. MLArrayBatchProvider was not properly configured by me so the updateTask was not properly completed.

Es domaj, ka vajag panemt iepist al un paprovet velreiz

For me the issue was resolved by abandoning using a UpdatableTrainingInput class that conformed to id<MLFeatureProvider>, but instead creating a MLDictionaryFeatureProvider as shown here: https://developer.apple.com/documentation/coreml/model_personalization/personalizing_a_model_with_on-device_updates?language=objc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM