【问题标题】:A warning "'init()' is deprecated". [Swift, Ios app, learning model]警告“'init()' 已被弃用”。 【Swift、Ios app、学习模型】
【发布时间】:2021-02-15 07:29:00
【问题描述】:

我正在使用 Swift 制作图像分类 iOS 应用。

当我写作时

guard let model = try? VNCoreMLModel(for: SqueezeNet().model) else { return }

我收到了这个警告。

'init()' is deprecated: Use init(configuration:) instead and handle errors appropriately.

你知道如何摆脱它吗?

【问题讨论】:

  • 错误信息很清楚。您应该使用SqueezeNet(configuration:) 而不是SqueezeNet()

标签: swift ios-app-extension


【解决方案1】:

这很奇怪。右键单击SqueezeNet() 并跳转到它的定义。它会带你去上课。

找到类的init() 方法。在你的 SqueezeNet 类中应该是这样的:

/**
    Construct SqueezeNet instance by automatically loading the model from the app's bundle.
*/
@available(*, deprecated, message: "Use init(configuration:) instead and handle errors appropriately.")
convenience init() {
    try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
}

我不确定您是如何设置机器学习的,但看起来好像是:

  • @available(*, deprecated, message: "Use init(configuration:) instead and handle errors appropriately.")

不是为你而过。这可能意味着以下任何一种:

  1. 您的机器学习设置不正确
  2. 您的 iOS 不是最新的

轻松修复:

您所要做的就是将其粘贴到您的项目中:

extension SqueezeNet {
    convenience init(_ foo: Void) {
        try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
    }
}

然后,像这样编辑您的代码:

guard let model = try? VNCoreMLModel(for: SqueezeNet(()).model) else { return }

它应该不能正常工作。如果没有,请告诉我。

【讨论】:

    【解决方案2】:

    如消息所述,init() 已弃用。新的初始化程序采用配置参数。默认配置可能适合您,在这种情况下,您可以将 SqueezeNet() 替换为:

    SqueezeNet(configuration: MLModelConfiguration())
    

    【讨论】:

      【解决方案3】:

      对我来说,Apple 示例项目的代码已制定:

      guard let mlModel = try? YOLOv3(configuration: .init()).model,
                    let model = try? VNCoreMLModel(for: mlModel) else {
                  print("Failed to load detector!")
                  return
      


      这是给我警告的代码:

      guard let model = try? VNCoreMLModel(for: YOLOv3().model) else {
                  print("Could not load model")
                  return
      

      【讨论】:

        猜你喜欢
        • 2017-12-14
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多