Use the Fritz Python Library to simplify your mobile machine learning workflow. This Library has the tools you need to effectively train, test, and deploy your models in a mobile environment.
For detailed API documentation, check out the Fritz Python Library Docs.
How to train and deploy your model using Fritz Keras Callbacks¶
Use our custom Keras callback classes to reduce the number of steps needed to test out the latest version of a model in your mobile app. There are 3 Keras callbacks that will convert and and upload the latest mobile-ready version to the webapp during training:
FritzMLModelCheckpoint- For Core ML models.
FritzTFMobileModelCheckpoint- For TensorFlow Mobile models.
FritzTFLiteModelCheckpoint- For TensorFlow Lite models.
The rest of the tutorial will use the
FritzMLModelCheckpoint, but the steps are the same for the other model types.
Setup Training with Fritz Integrated¶
1. Install the Fritz Python Library.
virtualenv, install the Fritz Python Library. This contains a client for interacting with our servers as well as a Keras Callback to easily upload your models during training. Install with the Keras extension to install all necessary dependecies.pip install fritz fritz[keras]
2. Create a Custom Model in the webapp.
Next, create a new custom model in the webapp. Follow the instructions directions on Custom Models. At this point, your model may not be trained. However, the Fritz Python Library provides the most value when you can test the model running inside an app, so you can track progress as the model trains.
Re-training a model used in a production app
If you are re-training a model deployed in a production app, you may want to test out a model as it is being retrained without deploying the model to all users. In this case, we strongly recommend you create a new development version of the model in the webapp. Here are the basic steps:
- Create a new model development model from the Models page using the current version of the model.
- Update the
modelIdentifierin your local development app with the model identifier of the newly created model.
- Create a
FritzMLModelCheckpointcallback and begin training model. During training, your local development app will update, but production apps will not be affected.
- After you have finished trainig and wish to deploy model to all users, download the model version and add as a new version in the webapp.
- To deploy this update to all devices, set the active version to the latest uploaded version from the Model Details page in the webapp.
- If you are done using the development model, archive it from the Model Details page so it does not show up in your active model list.
3. Create a python function to convert your Keras model to Core ML
Include a function to convert the Keras model to a Core ML model. This will most likely use the
coremltoolslibrary from Apple. If you are unsure how to convert the model, the coremltools documentation can help you get started. Here is an example on how to convert the Image Segmentation model available Image Segmentation Model:import coremltools def convert_model_to_coreml(model): return coremltools.converters.keras.convert( model, input_names='image', image_input_names='image', image_scale=1.0 / 255.0, red_bias=-0.5, green_bias=-0.5, blue_bias=-0.5, output_names='output' )
4. Add FritzMLModelCheckpoint to your Keras training code
The easiest way to add the
FritzMLModelCheckpointis to copy pre-configured code from the Model Details page in the webapp:
You will copy a code block that looks similar to this:import fritz.keras API_KEY = "<Account API Key from Account Overview page in webapp>" MODEL_ID = "<Your Model Identifier>" KERAS_MODEL_FILENAME = "/path/to/keras_model.h5" callback = fritz.keras.FritzMLModelCheckpoint( API_KEY, MODEL_ID, KERAS_MODEL_FILENAME, convert_model_to_coreml, period=1, deploy=True, )
Once you have copied the code, there are a few configuration options.
First, include the path of the Keras model. This is useful when downloading the model from the webapp. The basename of the path will be used as the name of the uploaded model. For example, if you download a model with a Keras path of
/path/to/my_great_model.h5the downloaded Core ML model will be
You can optionally configure how often to upload a new version of the model to the webapp. By default, it will upload after every epoch. If you change it to
period=10, a new version will be created every 10 epochs, along with the final model version.
Finally, you can choose whether or not you want to set the latest created version as active or not. If
deploy=True, when that model is initialized in an app, the SDK will download the latest version. If
deploy=False, the version will simply be uploaded to the webapp but not updated in apps with the model installed. We recommend you create a new model in the webapp to train (see tip above regarding re-training) so you do not accidentally push new versions to users while training.