Search code examples
iosxcodemachine-learningios11coreml

Multiple and dynamically loaded CoreML models on demand


I'm working on an iOS 11 app to leverage the new CoreML framework. The idea is to have different .mlmodel files to handle a video input and provide a user with an ability to choose the desired model to classify it.

I have two issues here:

  • unable to add more than one model per xcode project. The xcode stops generating strongly typed models and I'm unable to instantiate them
  • unable to load .mlmodel dynamically (let's say I downloaded it on demand). Is there a way to load .mlmodel dynamically?

Solution

  • There is a bug in Xcode that sometimes prevents code generation when multiple models are present. As a work-around you can set the "CoreML Code Generation Language" in your project settings explicitly (instead of Automatic).

    As for dynamically loading models: They just added a new API with iOS 11 Beta 4 that allows to load models over-the-air and compile them on device now, see here.

    You don't need the generated classes to execute models, you can initialize them from the compiled model file yourself with some minor inconvenience. Just inspect the generated classes to see how it's done.