Model Summary

This describes how to summarize an ML model using the MLTK’s commands/APIs.

Note

Any .tflite or .h5 model file will work with the model summary generation.
i.e. The model file does not need to be generated by the MLTK to generate a summary of it.

Quick Reference

Overview

The MLTK’s model summary feature generates a text summary of a model containing some or all of the following information:

  • Model layout including each layer’s name/type/output shape

  • Number of trainable parameters

  • Number of multiply-accumulate (MAC) operations

  • Number of operations

  • Any additional model parameters

Keras Model vs Tensorflow-Lite Model Summary

Two types of model summaries may be generated:

  • Keras Model - This provides a summary of the exact model used for training, file extension: .h5

  • Tensorflow-Lite Model - This provides a summary of the converted, quantized model that can execute on an embedded device, file extension: .tflite (see Model Quantization)

Both these models have similar model layouts, however, the Tensorflow-Lite model is slightly modified to optimize it for embedded execution.

The Keras Model summary is generated by KerasModel.summary() API,
while the Tensorflow-Lite Model is generated by the TfliteModel.summary() API.

By default, the summarize command or summarize_model API will generate a summary of the trained .h5 model in the model archive file.
Specify the --tflite option to summarize the trained .tflite model in model archive file.

Alternatively, provide the --build option to summarize the model without training first.
Alternatively, provide the path to a .tflite or .h5 model file to summarize a non-MLTK generated model file.

Command

Model summarization from the command-line is done using the summarize operation.

For more details on the available command-line options, issue the command:

mltk summarize --help

Example 1: Summarize Keras model

In this example, we generate a summary of the trained .h5 model file in the image_classification model’s model archive.

mltk summarize image_classification

Example 2: Summarize Tensorflow-Lite model

In this example, we generate a summary of the trained .tflite model file in the image_classification model’s model archive.

mltk summarize image_classification --tflite

Example 3: Summarize external Tensorflow-Lite model

The given model need not be generated by the MLTK. External models are also supported by the summarize command:

mltk summarize ~/workspace/my_model.tflite

Example 4: Summarize model before training

Training a model can be very time-consuming, and it is useful to know basic information about a model before investing time and energy into training it. For this reason, the MLTK summarize command features a --build flag to build a model and summarize it before the model is fully trained.

In this example, the image_classification model is built at command-execution-time and a summarize is generated. Note that only the model specification script is required, it does not need to be trained first.

mltk summarize image_classification --tflite --build

Python API

Model summarization is accessible via the summarize_model API.

Examples using this API may be found in summarize_model.ipynb