Model Quantization API Examples

This demonstrates how to use the quantize_model API.

Refer to the Model Quantization guide for more details.

NOTES:

  • Click here: Open In Colab to run this example interactively in your browser

  • Refer to the Notebook Examples Guide for how to run this example locally in VSCode

Install MLTK Python Package

# Install the MLTK Python package (if necessary)
!pip install --upgrade silabs-mltk

Import Python Packages

# Import the necessary MLTK APIs
from mltk.core import quantize_model

Example 1: Update .tflite in model archive

The most common use case of the quantize_model API is:

  1. Fully train a model

  2. Later modify the TrainMixin.tflite_converter settings in the model specification script

  3. Run the quantize_model API to update the .tflite model file in the model archive.

In this example, it’s assumed that the TrainMixin.tflite_converter settings in the image_example1 model specification script have been modified after the model have been trained.

After this API completes, the image_example1.mltk.zip model archive is updated with a new image_example1.tflite model file.

tflite_path = quantize_model('image_example1')
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 3 of 3). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: E:\tmpvpnr_8op\assets
INFO:tensorflow:Assets written to: E:\tmpvpnr_8op\assets
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 3 of 3). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: E:\tmp6fs6_29o\assets
INFO:tensorflow:Assets written to: E:\tmp6fs6_29o\assets
c:\Users\reed\workspace\silabs\mltk\.venv\lib\site-packages\tensorflow\lite\python\convert.py:766: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.
  warnings.warn("Statistics for quantized inputs were expected, but not "