简体   繁体   English

更新 CoreML model 时 MLUpdateContext 为空

[英]MLUpdateContext is empty when updating CoreML model

My problem is the following - In the method below the variable finalContext seem to not contain anything.我的问题如下 - 在变量finalContext下面的方法中似乎不包含任何内容。 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.) Error: The operation couldn't be completed. (Foundation._GenericObjCError error 0.) when calling the function. Error: The operation couldn't be completed. (Foundation._GenericObjCError error 0.)调用 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.编辑 - finalContext不包含我试图访问的 model 。

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.我没有正确配置 MLArrayBatchProvider,因此 updateTask 没有正确完成。

Es domaj, ka vajag panemt iepist al un paprovet velreiz 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对我来说,通过放弃使用符合 id<MLFeatureProvider> 的 UpdatableTrainingInput class 解决了这个问题,而是创建了一个 MLDictionaryFeatureProvider,如下所示: https://developer.apple.com/documentalupdates_personizing? =对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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