如何设置 CoreML 模型?

How to setup a CoreML model?

我在设置 Core ML 模型时遇到一些问题...

Xcode 告诉我:“不推荐使用 init():改为使用 init(configuration:) 并适当地处理错误。”

这是我的代码:

guard let model = try? VNCoreMLModel(for: MobileNetV2().model) else {
        fatalError("Unable to load the model")
    }
    
    let classificationRequest = VNCoreMLRequest(model: model, completionHandler: classificationCompleteHandler)
    classificationRequest.imageCropAndScaleOption = VNImageCropAndScaleOption.centerCrop
    visionRequests = [classificationRequest]
    
    loopCoreMLUpdate()
}

我该如何解决?

非常感谢您的回答!

洛伊克

答案就在错误信息中:不要使用没有参数的 init,使用 init(configuration:) 来实例化模型。

let config = MLModelConfiguration()
guard let coreMLModel = try? MobileNetV2(configuration: config),
      let visionModel = try? VNCoreMLModel(for: coreMLModel.model) else {

此更改的原因是已弃用的 init() 没有办法告诉您加载模型可能失败了。