CoreML Error: "Failed to unarchive update parameters. Model should be re-compiled"

I am building an image recognition app for iPhone, using Swift, Vision, CoreML and the pretrained Resnet50 model. When I run MLUpdateTask, it gives me an error that my model (Resnet50) needs to be re-compiled.

I am using Xcode 14.3.1 on Ventura 13.4.1 (c).

Here's the error message:

EXCEPTION from MLUpdateTask: Error Domain=com.apple.CoreML Code=6 "Failed to unarchive update parameters. Model should be re-compiled." UserInfo={NSLocalizedDescription=Failed to unarchive update parameters. Model should be re-compiled.}

Here's the code snippet:

struct ImageCoreML {
    let sourceModelURL = Bundle.main.url(forResource: "Resnet50", withExtension: ".mlmodelc")!
    static let docsURL: URL = {
        return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    }()
    let updatedModelURL = docsURL.appendingPathComponent("ImageID")
                                 .appendingPathExtension("mlmodelc")
    init() {
        if !FileManager.default.fileExists(atPath: updatedModelURL.path) {
          do {
            try FileManager.default.copyItem(at: sourceModelURL, to: updatedModelURL)
            return
          } catch {
            print("copy models Error: \(error)")
          }
        }
    }

    private func completionHandler(_ context: MLUpdateContext) {
        let updatedModel = context.model
        let fileManager = FileManager.default
        do {
            try updatedModel.write(to: self.updatedModelURL)
            print("Updated model saved to:\n\t\(self.updatedModelURL)")
        } catch let error {
            print("Could not save updated model to the file system: \(error)")
            return
        }
    }

    // Update the CNN model with the saved image.
    func updateModel(croppedImage: CVPixelBuffer?, personLabel: String) {
        print("updateModel()")
        guard let pixelBuffer = croppedImage else {
            print("ERROR: cannot convert cropped image to cgImage buffer")
            return
        }
        var featureProviders = [MLFeatureProvider]()
        let imageFeature = MLFeatureValue(pixelBuffer: pixelBuffer)
        let personLabel = MLFeatureValue(string: personLabel)
        let dataPointFeatures: [String: MLFeatureValue] = ["image": imageFeature,
                                                           "personID": personLabel]
        if let provider = try? MLDictionaryFeatureProvider(dictionary: dataPointFeatures) {
            featureProviders.append(provider)
        }

        let trainingData = MLArrayBatchProvider(array: featureProviders)
        do {
            let updateTask = try MLUpdateTask(forModelAt: self.updatedModelURL,
                             trainingData: trainingData,
                             configuration: nil,
                             completionHandler: completionHandler) 
            updateTask.resume()
        } catch {
            print("EXCEPTION from MLUpdateTask:\n\(error)")
            return
        }
    }
}

OK I'm dumb. The Resnet50 model from the Apple website is not updatable. I think the solution is to use coremltools to convert a PyTorch model into CoreML, and make it updatable in the process, perhaps a la https://coremltools.readme.io/docs/updatable-neural-network-classifier-on-mnist-dataset#define-make_updatable.

CoreML Error: "Failed to unarchive update parameters. Model should be re-compiled"
 
 
Q