Train and Deploy Models

Use our custom Keras callback class to reduce the number of steps needed to test out the latest version of a model in your mobile app. The FritzSnapshotCallback helps upload and convert mobile models as you train your model.

Create converter functions to convert Keras model to Mobile Frameworks

Build converter functions for each mobile framework you wish to use. Each converter function should return an instance of that framework’s model:

You will most likely use the coremltools library 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'
    )

Configure the FritzSnapshotCallback

Next, you will configure the FritzSnapshotCallback with the converters you wish to use for training.

import fritz
import fritz.keras

fritz.configure()

def keras_to_core_ml(keras_model):
    """Function that converts converted MLModel"""

converters = {
    fritz.frameworks.CORE_ML: keras_to_core_ml
}
KERAS_MODEL_FILENAME = "/path/to/keras_model.h5"

callback = fritz.keras.FritzSnapshotCallback(
    converters_by_framework=converters,
    output_file_name=KERAS_MODEL_FILENAME,
    period=5,
    deploy=True,
)

You can configure the following parameters for the FritzSnapshotCallback:

FritzSnapshotCallback options
output_file_name

(Required)

Output file name of saved Keras model.

model_uids_by_framework

None (default)

Dictionary mapping model framework to model uids. If model_uid not set

for a given platform, a new model will be created.

converters_by_framework

None (default)

Dictionary mapping model framework to conversion function.

period

10 (default)

Interval (number of epochs) between uploading new versions of models.

deploy

False (default)

If True, will update the active version to the newly uploaded models.

api_key

None (default)

Optional API Key which overrides the globally set value

project_uid

None (default)

Optional Project UID which overrides the globally set value.

Connecting to existing Fritz AI models

Specify the Model ID

If you already have a model in Fritz AI that you wish to update you can specify which model to update:

model_ids = {
    fritz.frameworks.CORE_ML: "<Model UID for Core ML Model>"
}

callback = fritz.keras.FritzSnapshotCallback(
    ...
    model_ids_by_framework=model_ids,
    ...
)

When the callback runs, it will upload models to the specified model ID.

Use an existing model name

Alternatively, if you do not specify a Model ID, Fritz AI will look for models with the same name as the filename.

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:

  1. Create a new model development model from the Models page using the current version of the model.
  2. Update the modelIdentifier in your local development app with the model identifier of the newly created model.
  3. Create a FritzSnapshotCallback callback specifying the Model ID:
model_ids = {
    fritz.frameworks.CORE_ML: "<Model ID of development model>"
}

callback = fritz.keras.FritzSnapshotCallback(
    ...
    model_ids_by_framework=model_ids,
    deploy=True
)
  1. Begin training the model. During training, your local development app will update, but production apps will not be affected.
  2. After you have finished training and wish to deploy model to all users, download the model version and add as a new version in the webapp.
  3. To deploy this update to all devices, set the active version to the latest uploaded version from the Model Details page in the webapp.
  4. 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.