更新 CoreML 模型时 MLUpdateContext 为空

MLUpdateContext is empty when updating CoreML model

我的问题如下 - 在下面的方法中,变量 finalContext 似乎不包含任何内容。调用函数时收到错误消息:Error: The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)。我需要帮助如何调试此问题或可能的原因是什么。编辑 - finalContext 不包含我尝试访问的模型。

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)")
    }
}

Es dôme, ka vajag panemt iepist al un paprovet velreiz

我在这里发现了问题。我没有正确配置 MLArrayBatchProvider,所以 updateTask 没有正确完成。