I’ve created a TensorFlow model which I then convert to a CoreML model using the Apple coremltools
Python package for use in an iOS app. My current workflow is:
- Generate the model and convert to CoreML format using a script
- Manually open the generated model and generate an encryption key by using the UI (see screenshot)
- Add the generated key to Xcode using a script.
- Xcode automatically encrypts the model using the compiler flag
--encrypt "$SRCROOT/App/Resources/Models/MyModel.mlmodelkey"
when building the app
Automate Encryption
I often have to make changes and improvements to my model for each app release and would like to automate the generation of the encryption key, so it can be added to Xcode.
It’s possible to encrypt a CoreML model using the following terminal command:
xcrun coremlcompiler compile <model.{mlmodel,mlpackage}> </desired/path/to/output/directory> --encrypt <path/to/model_key_file>
Using xcrun coremlcompiler --help
I have not yet found a way to generate an encryption key automatically and it appears that it has to be done by the UI.
Is it possible to generate an encryption key for CoreML models using the command line or some other automation method?
Upload Key to Apple’s Servers
In an ideal scenario I would like to also like to be able to encrypt the model in the terminal using the generated key before it gets to Xcode. This can be achieved by the --encrypt
command parameter:
xcrun coremlcompiler compile <model.{mlmodel,mlpackage}> </desired/path/to/output/directory> --encrypt <path/to/model_key_file>
Since the encryption key is downloaded to the user’s device from Apple servers, can this encryption key be somehow uploaded when supplying a pre-encrypted model to Xcode (e.g. as part of a framework)?