Improvements in machine learning
Improvements to Core ML presented during WWDC2020 will help developers with the development of their machine learning apps, with improvements that upgrade your app models, secure them, and group them in targeted collections. We are going to cover in this section the new Core ML Model Deployment, the new model collections with targeted deployments, and the new model encryption.
Core ML Model Deployment, collections, and targeted deployments
One of the most significant features introduced in WWDC 2020 for Core ML is Core ML Model Deployment. To describe it in simple words, it lets developers update their models on the fly. Developers are no longer required to update the whole app in the AppStore to make changes to their machine learning models. Apps can just download a new mlmodel
file from the cloud.
Developers will be able to create machine learning model collections on the cloud and update them on CloudKit. Apps will download those collections and stay up to date, with no version upgrading process in the middle. However, developers don't control the download process. The app will detect that there is a new model version available and will download it when the system decides it's appropriate (for example, in the background while the phone is locked and charging on a Wi-Fi connection). So, developers should take into account that the model update may or may not be fast or in real time. The operating system will have the last word.
A useful feature of model collections is that they can be targeted to different users (for example, users on devices with varying capabilities, such as iPhones vs iPads). Assigning different models to different users can be done with targeted deployments applied to collections. There are six options available to configure and target the model that the device will deploy: language code, device class, operating system, operating system version, region code, and app version.
Model encryption
Starting on iOS 14 and macOS 11, Core ML can automatically encrypt the Core ML models.
Xcode will encrypt the compiled model, mlmodelc
(not the original mlmodel
). The decryption happens when the app is instantiated and occurs on the device. Moreover, the decryption result is not stored anywhere; it is just loaded into memory.
More good news on this: Xcode will help you to create an encryption key, associate it with your developer account, and it will be stored in the Apple servers automatically. You can always download a local copy for yourself, but the process is not seamless.
When the encryption key is stored in Apple servers, the file is .mlmodelkey.
When you want to encrypt your model, you just need to add --encrypt {YourModel}.mlmodelkey
to the compiler flags. If you prefer using CloudKit, you just need to provide the encryption key when creating the model archive.
The drawback of this process is this: when the app instantiates, it needs to have an internet connection with the Apple servers to download the encryption key and decrypt your model. If for any reason there is no connectivity, you need to implement your fallback process inside the completion errors of the new {YourModel}.load()
method. The completion handler will throw a modelKeyFetch
error if the encryption key is not available, and you can act accordingly.
Important note
You should not include the encryption key in your app bundle. It is not necessary, and it can compromise your data.
In this section, we have discovered how we can upgrade our machine learning models without updating our apps, how we can group models into collections and assign them to a different type of users/devices, and how we can have our models encrypted and keep our machine learning data safe with no effort. In the next section, we are going to cover the additions to user privacy.