【问题标题】:Attempting to initialize CoreML model results in ambiguous error message尝试初始化 CoreML 模型会导致模棱两可的错误消息
【发布时间】:2020-10-25 23:26:21
【问题描述】:

我正在为涉及 CoreML 的 iOS 14.0 开发消息过滤器扩展。我正在尝试加载使用 Create ML 生成的 mlmodel (TSF_ML_2 1.mlmodel)。该模型在 Xcode/Create ML 的预览部分中使用时表面上可以工作,但是当以编程方式初始化模型时,我收到以下错误,我找不到任何信息:

使用模型数据初始化文本分类器模型失败

完整的追溯:

2020-07-05 15:51:07.965420-0400 SpamFilter[36466:3635584] [coreml] MLModelAsset: load failed with error Error Domain=com.apple.CoreML Code=0 "initialization of text classifier model with model data failed" UserInfo={NSLocalizedDescription=initialization of text classifier model with model data failed}
2020-07-05 15:51:07.967619-0400 SpamFilter[36466:3635584] [coreml] MLModelAsset: modelWithError: load failed with error Error Domain=com.apple.CoreML Code=0 "initialization of text classifier model with model data failed" UserInfo={NSLocalizedDescription=initialization of text classifier model with model data failed}
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.CoreML Code=0 "initialization of text classifier model with model data failed" UserInfo={NSLocalizedDescription=initialization of text classifier model with model data failed}: file /Users/username/Library/Developer/Xcode/DerivedData/SpamApp-fqenpxvawdmzkvdmdxjbatoucwji/Build/Intermediates.noindex/SpamApp.build/Debug-iphoneos/SpamFilter.build/DerivedSources/CoreMLGenerated/TSF_ML_2 1/TSF_ML_2 1.swift, line 63
2020-07-05 15:51:07.970910-0400 SpamFilter[36466:3635584] Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.CoreML Code=0 "initialization of text classifier model with model data failed" UserInfo={NSLocalizedDescription=initialization of text classifier model with model data failed}: file /Users/username/Library/Developer/Xcode/DerivedData/SpamApp-fqenpxvawdmzkvdmdxjbatoucwji/Build/Intermediates.noindex/SpamApp.build/Debug-iphoneos/SpamFilter.build/DerivedSources/CoreMLGenerated/TSF_ML_2 1/TSF_ML_2 1.swift, line 63
(lldb)

我正在使用 Xcode 12.0 beta (12A6159),并使用与此 Xcode 关联的 Create ML(版本 1.1 Beta (41))生成了 mlmodel。

我尝试过的事情:

  • 使用各种参数和数据重新生成 mlmodel。 (主要是我使用迁移学习)
  • 确认目标正确且 mlmodel 类已正确生成/链接
  • 使用contentsOf 手动指定模型 URL
  • 在设备上而不是在 Xcode 中编译 mlmodelc

这是我在MessageFilterExtension.swift 中的初始化逻辑(中断@ line 1):

let model:TSF_ML_2_1! = TSF_ML_2_1()

guard let spamOutput = try? model.prediction(text: messageBody) else {
    fatalError("Unexpected runtime error.")
}

print(spamOutput.label)

这里是自动生成的 Swift 模型类的相关部分,TSF_ML_2 1.swift(错误回溯@第 63 行):

52 class TSF_ML_2_1 {
53     let model: MLModel
54     class var urlOfModelInThisBundle : URL {
55         let bundle = Bundle(for: self)
56         return bundle.url(forResource: "TSF_ML_2 1", withExtension:"mlmodelc")!
57     }
58     init(model: MLModel) {
59         self.model = model
60     }
61     @available(*, deprecated, message: "Use init(configuration:) instead and handle errors appropriately.")
62     convenience init() {
63         try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
64     }
    ...
   }

【问题讨论】:

  • 可能是测试版问题。

标签: ios swift xcode coreml


【解决方案1】:

在定义MLModel 时使用此模型初始化:

var myModel: Resnet50 = try! Resnet50(configuration: MLModelConfiguration.init())

【讨论】:

    【解决方案2】:

    我还发现代码以这种方式运行没有任何错误。

    let myModel: modelName = try! modelName(configuration: MLModelConfiguration.init())
    
    guard let model = try? VNCoreMLModel(for: myModel.model) else {fatalError()}
    

    【讨论】:

      【解决方案3】:

      我仍在学习 Swift,但看起来他们希望您同时掌握模型初始化和预测。以下是我将如何编码您的预测:

      do {
          let model = try TSF_ML_2_1(configuration: .init())
          let spamOutput = try model.prediction(text: messageBody)
          // successful code
      } catch {
          // failure code
      }
      

      这似乎对我有用。

      【讨论】:

        猜你喜欢
        • 2010-09-30
        • 2017-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2013-05-23
        • 2013-11-19
        相关资源
        最近更新 更多