如何设置CoreML模型



我在设置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()
}

我该如何解决?

非常感谢你的回答!

Loïc

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

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

此更改的原因是不推荐使用的init()无法告诉您加载模型可能失败。

最新更新