- iOS 13.0+
- Xcode 11.0+
- Core ML
With the Core ML framework, you can customize an updatable model at runtime on the user’s device. Using this technique, you can create a personalized experience for the user while keeping their data private.
This sample demonstrates how to update the drawing classifier with an
MLUpdate. The app initiates an update task with the user’s drawings paired with a string label. Once the update is complete, the app uses the updated drawing classifier to recognize similar drawings from the user and convert each into its associated string label.
Prepare Your Model Update Data
Gather your training data:
Wrap each value of a datapoint in an
MLFeature, one for each model input and output.
Group all the feature values for a datapoint in an
Group all the feature providers in an
Each time the user adds a new emoji sticker, the app prompts the user to make three drawings, and uses those drawings to update the drawing classifier. It does this by first creating an
MLDictionary that contains the feature values for a drawing and its label. The app appends each feature provider to an array, which it uses to create an
MLArray at the end of the function.
The sample makes each
MLDictionary by initializing it with a dictionary of two
MLFeature instances keyed by strings. The feature values are:
The underlying image of the drawing keyed by
The emoji character as a string keyed by
The sample creates a feature value for the emoji string by using
feature. However, to convert the drawing’s underlying
CGImage into a feature value, the sample acquires the image constraint of the model’s image input feature.
Create an Update Task
You create an
MLUpdate by passing the following to an initializer:
MLBatchthat contains your update data
The location of the compiled model youʼd like to update (ModelName
MLModel, if applicable
A completion handler with a single
The sample updates the drawing classifier model it’s currently using, which could be the original drawing classifier model or a previously updated model.
Run the Update Task
You begin an update task by calling its
Core ML updates the model on a separate thread and calls your completion handler when it finishes the update process.
Save the Updated Model
Use your completion handler to save the updated model in the
MLUpdate to disk. The sample saves the updated model to the file system by first writing the model to a temporary location. Next, the sample moves the updated model to a permanent location, replacing any previously saved updated model.