【发布时间】: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 }
...
}
【问题讨论】:
-
可能是测试版问题。