我从 TensorFlow Hub 获取了一个模型,将其转换为 CoreML 模型,并尝试将其与我的 Xcode 项目完全集成。但是,尝试构建我的项目时出现错误:
无法调用非函数类型“MLModel”的值
这些错误来自自动生成的文件中的这两个函数:`
/** 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)
}
我已经验证我的模型处于正确的构建阶段(即编译源),验证了目标成员资格,已清理构建文件夹,删除派生数据等,但我不断收到此错误。
有人在尝试在 Xcode 项目中使用 CoreML 模型时遇到过这个问题吗?如果是这样,如果可能的话,如果有人能指出我正确的方向,我将非常感激。
您的问题是您将类命名为
model
并且您还有名为 model
的变量。这造成了混乱。将类名称修复为Model
,一切都会更好。
您始终希望类名称、结构名称和枚举名称以大写字母开头。变量名、方法名、大小写名均应以小写字母开头。