Search code examples
swifttensorflowcoreml

Having trouble integrating CoreML model with my Xcode project


I took a model from TensorFlow Hub, converted it to a CoreML model, and I am trying to fully integrate it with my Xcode project. However, trying to build my project gets me an error:

Cannot call value of non-function type 'MLModel'

These errors are from these two functions located in an automatically generated file:`

/** Construct model instance asynchronously with URL of the .mlmodelc directory with optional configuration. Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread.

    - parameters:
      - modelURL: the URL to the model
      - configuration: the desired model configuration
      - handler: the completion handler to be called when the model loading completes successfully or unsuccessfully
*/
class func load(contentsOf modelURL: URL, configuration: MLModelConfiguration = MLModelConfiguration(), completionHandler handler: @escaping (Swift.Result<model, Error>) -> Void) {
    MLModel.load(contentsOf: modelURL, configuration: configuration) { result in
        switch result {
        case .failure(let error):
            handler(.failure(error))
        case .success(let model):
            handler(.success(model(model: model)))
        }
    }
}

/**
    Construct model instance asynchronously with URL of the .mlmodelc directory with optional configuration.

    Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread.

    - parameters:
      - modelURL: the URL to the model
      - configuration: the desired model configuration
*/
class func load(contentsOf modelURL: URL, configuration: MLModelConfiguration = MLModelConfiguration()) async throws -> model {
    let model = try await MLModel.load(contentsOf: modelURL, configuration: configuration)
    return model(model: model)
}

I've verified that my model is in the correct build phase (i.e. Compile Sources), verified target membership, have cleaned out the build folder, deleted Derived Data, etc. and I keep getting this error.

Has anyone gotten this while trying to use a CoreML model in their Xcode project? If so, I'd really appreciate if someone could point me in the right direction if possible.


Solution

  • Your issue is that you named the class as model and you also have variables named model. This is causing confusion. Fix the class name to be Model and everything should work better.

    You always want class names, struct names, and enum names to start with uppercase letters. Variable names, method names, and case names should start with lowercase letters.