0
votes

I'm working on a Message Filter Extension for iOS 14.0 involving CoreML. I'm trying to load a mlmodel (TSF_ML_2 1.mlmodel) that I generated using Create ML. The model ostensibly works when used in the preview section of Xcode/Create ML, but when the model is initialized programmatically, I get the following error on which I cannot find any information:

initialization of text classifier model with model data failed

Full traceback:

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)

I'm working with Xcode 12.0 beta (12A6159), and generated the mlmodel with the Create ML (Version 1.1 Beta (41)) associated with this Xcode.

Things I've tried:

  • Regenerating the mlmodel using various parameters & data. (Principally I am using transfer learning)
  • Confirm that the target is correct and mlmodel classes are generated/linked appropriately
  • Manually specifying model URL with contentsOf
  • Compiling mlmodelc on the device instead of in Xcode

Here is my initialization logic in MessageFilterExtension.swift (breaks @ 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)

And here is the relevant portion of the autogenerated Swift model class, TSF_ML_2 1.swift (error traceback @ line 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     }
    ...
   }
3
Might be a beta issue. - Matthijs Hollemans

3 Answers

3
votes

Use this model initialization when you define your MLModel:

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

I am still learning Swift, but it looks like they want you to catch both the model init and the prediction. Here is how I would have coded your prediction:

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

That seems to work for me.

0
votes

I also ecountered that Code in this way works without any errors.

let myModel: modelName = try! modelName(configuration: MLModelConfiguration.init())

guard let model = try? VNCoreMLModel(for: myModel.model) else {fatalError()}