Initialization of CoreML model failed: Domain error = com.apple.CoreML Code = 0 "Error declaring network."

I have an app in the App Store and I get its error logs from Crashlytics. One of the most common errors that users get (and one that I lost badly to reproduce) occurs when initializing the CoreML model in my project. This is how I initialize the model:

class VisionManager: NSObject { /// Prediction model private static let model = MobileNet() ... override init() { super.init() guard let visionModel = try? VNCoreMLModel(for: VisionManager.model.model) else { // this case should never happen as we know for sure that the model we are using is an image classification model fatalError("The CoreML model being used is not compatible with the Vision framework.") } ... } ... } 

The error, as seen from Crashlytics, is as follows:

fatal error: 'try!' the expression unexpectedly caused an error: Error Domain = com.apple.CoreML Code = 0 "Error declaring the network." UserInfo = {NSLocalizedDescription = error during network declaration.}: File /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65.2/src/swift/stdlib/public/core/ErrorType.swift, line 181

And the stack trace shows that an error occurs when the guard block is executed. In fact, it goes deeper and shows that the error was thrown inside the static initialization at the top when the initializer was called. The initializer, along with the entire MobileNet.swift class, is automatically created and looks like this:

 init(contentsOf url: URL) throws { self.model = try MLModel(contentsOf: url) } /// Construct a model that automatically loads the model from the app bundle convenience init() { let bundle = Bundle(for: MobileNet.self) let assetPath = bundle.url(forResource: "MobileNet", withExtension:"mlmodelc") try! self.init(contentsOf: assetPath!) } 

It seems obvious that the error was caused by the init(contentsOf url: URL) method init(contentsOf url: URL) . However, since this is a generated file, I believe that I cannot do this to fix this error.

One of the possibilities is that the compiled .mlmodelc file is somehow not copied to the package, and when we try to initialize the MobileNet object using this URL , we get a non-empty error. Is it possible?

Any ideas or pointers to this issue are welcome.

+9
source share
2 answers

It seems obvious that the error was caused by calling the init method (contentOf url: URL). However, since this is a generated file, I believe that I cannot do this to fix this error.

FYI, you can copy this generated file to a new file and use it instead of initializing the model (just rename the classes inside the new file). Then try changing this line in a new file:

 let bundle = Bundle(for: MobileNet.self) 

in

 let bundle = Bundle.main 

I'm not sure if this will fix your specific problem, but that was for me when I moved the generated file to Cocoapod

+1
source

It is better to read the errors at the top of the output panel in Xcode. There should be an error like this that shows the actual error: "The core ML neural network layer requires an implementation called" scaling "that was not found in the global namespace.

In my case, I had an unsupported layer in the model I used, so I need to write MLCustomLayer. https://developer.apple.com/documentation/coreml/core_ml_api/creating_a_custom_layer

0
source

Source: https://habr.com/ru/post/1273450/


All Articles