Patrick Goley
Patrick Goley

Reputation: 5417

Continuously train CoreML model after shipping

In looking over the new CoreML API, I don't see any way to continue training the model after generating the .mlmodel and bundling it in your app. This makes me think that I won't be able to perform machine learning on my user's content or actions because the model must be entirely trained beforehand.

Is there any way to add training data to my trained model after shipping?

EDIT: I just noticed you could initialize a generated model class from a URL, so perhaps I can post new training data to my server, re-generate the trained model and download it into the app? Seems like it would work, but this completely defeats the privacy aspect of being able to use ML without the user's data leaving the device.

Upvotes: 19

Views: 4631

Answers (7)

Andy Jazz
Andy Jazz

Reputation: 58143

As alternative to bundling an mlmodel with application, you can download and then compile models within your CoreML app. For this you just need to download the model definition file onto the user’s device by using, for example, URLSession. And after this you have to compile the model definition by calling a throwing compileModel(at:) type method.

let newCompiledModel = try MLModel.compileModel(at: modelDescriptionURL)

You'll get a new compiled model file with the same name as the model description but its ending will be mlmodelc. Eventually, create a new MLModel instance by passing the compiled model URL to its initialiser.

let model = try MLModel(contentsOf: newCompiledModel)

However, remember that compiling process may be time consuming and it shouldn't be done on main thread.

Upvotes: 1

Marcos Paulo
Marcos Paulo

Reputation: 337

CoreML 3 now supports on-device model personalization. You can improve your model for each user while keeping its data private.

https://developer.apple.com/machine-learning/core-ml/

Upvotes: 4

michaelxing
michaelxing

Reputation: 93

Now with iOS11 beta4, you can compile the model, download from server.

(Details)

Upvotes: 2

Shehroz
Shehroz

Reputation: 171

Apple has recently added a new API for on-device model compilation. Now you can download your model and compile it on device

Upvotes: 10

Alex Brown
Alex Brown

Reputation: 42892

Core ML supports inference but not training on device.


You can update the model by replacing it with a new one from a server, but that deserves its own question.

Upvotes: 2

Satoshi Nakajima
Satoshi Nakajima

Reputation: 2113

In order to dynamically update the model (without updating the whole app), you need to use MPS (Metal Performance Shader) directly instead of relying on .mlmodel, which must be bundled with the app.

It means you need to manually build the neural network by writing some Swift code (instead of using coremltools to converts existing models directly), and feed various weights for each layer, which is a little bit of work, but not a rocket science.

This is a good video to watch if you want to know more about MPS.

https://developer.apple.com/videos/play/wwdc2017/608/

Upvotes: 2

Matthijs Hollemans
Matthijs Hollemans

Reputation: 7892

The .mlmodel file is compiled by Xcode into a .mlmodelc structure (which is actually a folder inside your app bundle).

Your app might be able to download a new .mlmodel from a server but I don't think you can run the Core ML compiler from inside your app.

Maybe it is possible for your app to download the compiled .mlmodelc data from a server, copy it into the app's Documents directory, and instantiate the model from that. Try it out. ;-)

(This assumes the App Store does not do any additional processing on the .mlmodelc data before it packages up your app and ships it to the user.)

Upvotes: 11

Related Questions