Updating a Core ML Model
There are few resources and examples provided by Apple and the community on how to create and convert a Core ML model to an updatable model. However, documentation and examples on how to use the updatable model in Xcode are limited. Here are the gist.
There are three steps to the process of updating a Core ML Model
- Get a bundle reference to the model
- Prepare the training data
- Perform the training task
Step 1: Get a Bundle Reference to the Model
Consider the model UpdatableKNNModel.mlmodel
as our updatable model. Grab the bundle URL just like how you would with any file within your Xcode project.
Step 2: Prepare the Training Data
Prepping the data means to create something called an MLArrayBatchProvider
. The required parameter is an array of MLFeatureProvider
. The feature provider can be obtain from the updatable model itself.
An updatable model will have a built-in class with the suffix TrainingInput
. Here, you will pass the training input and the associated true output. The training input will depend on the features you have set as a required input for your updatable model. This can be obtained by opening your core ml file and looking at the Update section. In our case, we are simply passing in a vector with a string as an output.
Step 3: Start the Update Task
Updating will require importing the CoreML module and calling the MLUpdateTask
. There are three required parameters and a completion handler.
First, pass in the model url we’ve obtained from step 1. Second, pass in the batch provider we’ve obtained from step 2. Third, pass in an MLModelConfiguration
object
The completion handler returns an MLUpdateContext
object. We use this to override our existing instance of our updatable model.
That’s all there is to it!