Model Summary API Examples

This demonstrates how to use the summarize_model API.

Refer to the Model Summary 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 summarize_model

Example 1: Summarize Keras model

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

summary = summarize_model('image_example1')
print(summary)
Model: "image_example1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 48, 48, 24)        240       
                                                                 
 average_pooling2d (AverageP  (None, 24, 24, 24)       0         
 ooling2D)                                                       
                                                                 
 conv2d_1 (Conv2D)           (None, 11, 11, 16)        3472      
                                                                 
 conv2d_2 (Conv2D)           (None, 9, 9, 24)          3480      
                                                                 
 batch_normalization (BatchN  (None, 9, 9, 24)         96        
 ormalization)                                                   
                                                                 
 activation (Activation)     (None, 9, 9, 24)          0         
                                                                 
 average_pooling2d_1 (Averag  (None, 4, 4, 24)         0         
 ePooling2D)                                                     
                                                                 
 flatten (Flatten)           (None, 384)               0         
                                                                 
 dense (Dense)               (None, 3)                 1155      
                                                                 
 activation_1 (Activation)   (None, 3)                 0         
                                                                 
=================================================================
Total params: 8,443
Trainable params: 8,395
Non-trainable params: 48
_________________________________________________________________

Total MACs: 1.197 M
Total OPs: 2.528 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor

Example 2: Summarize Tensorflow-Lite model

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

summary = summarize_model('image_example1', tflite=True)
print(summary)
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
| Index | OpCode          | Input(s)          | Output(s)       | Config                                              |
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
| 0     | quantize        | 96x96x1 (float32) | 96x96x1 (int8)  | Type=none                                           |
| 1     | conv_2d         | 96x96x1 (int8)    | 48x48x24 (int8) | Padding:same stride:2x2 activation:relu             |
|       |                 | 3x3x1 (int8)      |                 |                                                     |
|       |                 | 24 (int32)        |                 |                                                     |
| 2     | average_pool_2d | 48x48x24 (int8)   | 24x24x24 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 3     | conv_2d         | 24x24x24 (int8)   | 11x11x16 (int8) | Padding:valid stride:2x2 activation:relu            |
|       |                 | 3x3x24 (int8)     |                 |                                                     |
|       |                 | 16 (int32)        |                 |                                                     |
| 4     | conv_2d         | 11x11x16 (int8)   | 9x9x24 (int8)   | Padding:valid stride:1x1 activation:relu            |
|       |                 | 3x3x16 (int8)     |                 |                                                     |
|       |                 | 24 (int32)        |                 |                                                     |
| 5     | average_pool_2d | 9x9x24 (int8)     | 4x4x24 (int8)   | Padding:valid stride:2x2 filter:2x2 activation:none |
| 6     | reshape         | 4x4x24 (int8)     | 384 (int8)      | Type=none                                           |
|       |                 | 2 (int32)         |                 |                                                     |
| 7     | fully_connected | 384 (int8)        | 3 (int8)        | Activation:none                                     |
|       |                 | 384 (int8)        |                 |                                                     |
|       |                 | 3 (int32)         |                 |                                                     |
| 8     | softmax         | 3 (int8)          | 3 (int8)        | Type=softmaxoptions                                 |
| 9     | dequantize      | 3 (int8)          | 3 (float32)     | Type=none                                           |
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
Total MACs: 1.197 M
Total OPs: 2.561 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor
Runtime memory size (RAM): 71.512 k
hash: 0242764704ebb6643ae7df4a6536bb83
date: 2022-10-06T16:32:48.472Z
samplewise_norm.rescale: 0.0
samplewise_norm.mean_and_std: True
.tflite file size: 15.4kB

Example 3: Summarize external Tensorflow-Lite model

The given model need not be generated by the MLTK. External .tflite or .h5 model files are also supported by the summarize API.

import os 
import tempfile
import urllib
import shutil

# Use .tflite mode found here:
# https://github.com/mlcommons/tiny/tree/master/benchmark/training/keyword_spotting/trained_models
# NOTE: Update this URL to point to your model if necessary
TFLITE_MODEL_URL = 'https://github.com/mlcommons/tiny/raw/master/benchmark/training/keyword_spotting/trained_models/kws_ref_model.tflite'

# Download the .tflite file and save to the temp dir
external_tflite_path = os.path.normpath(f'{tempfile.gettempdir()}/kws_ref_model.tflite')
with open(external_tflite_path, 'wb') as dst:
    with urllib.request.urlopen(TFLITE_MODEL_URL) as src:
        shutil.copyfileobj(src, dst)
summary = summarize_model(external_tflite_path)
print(summary)
+-------+-------------------+----------------+----------------+-------------------------------------------------------+
| Index | OpCode            | Input(s)       | Output(s)      | Config                                                |
+-------+-------------------+----------------+----------------+-------------------------------------------------------+
| 0     | conv_2d           | 49x10x1 (int8) | 25x5x64 (int8) | Padding:same stride:2x2 activation:relu               |
|       |                   | 10x4x1 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 1     | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu  |
|       |                   | 3x3x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 2     | conv_2d           | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu               |
|       |                   | 1x1x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 3     | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu  |
|       |                   | 3x3x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 4     | conv_2d           | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu               |
|       |                   | 1x1x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 5     | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu  |
|       |                   | 3x3x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 6     | conv_2d           | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu               |
|       |                   | 1x1x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 7     | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu  |
|       |                   | 3x3x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 8     | conv_2d           | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu               |
|       |                   | 1x1x64 (int8)  |                |                                                       |
|       |                   | 64 (int32)     |                |                                                       |
| 9     | average_pool_2d   | 25x5x64 (int8) | 1x1x64 (int8)  | Padding:valid stride:5x25 filter:5x25 activation:none |
| 10    | reshape           | 1x1x64 (int8)  | 64 (int8)      | Type=none                                             |
|       |                   | 2 (int32)      |                |                                                       |
| 11    | fully_connected   | 64 (int8)      | 12 (int8)      | Activation:none                                       |
|       |                   | 64 (int8)      |                |                                                       |
|       |                   | 12 (int32)     |                |                                                       |
| 12    | softmax           | 12 (int8)      | 12 (int8)      | Type=softmaxoptions                                   |
+-------+-------------------+----------------+----------------+-------------------------------------------------------+
Total MACs: 2.657 M
Total OPs: 5.394 M
Name: summarize_model
Version: 1
Description: Generated by Silicon Lab's MLTK Python package
.tflite file size: 53.9kB

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 API features a build argument to build a model and summarize it before the model is fully trained.

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

summary = summarize_model('image_example1', tflite=True, build=True)
print(summary)
Enabling test mode
training is using 1 subprocesses
validation is using 1 subprocesses
Model: "image_example1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 48, 48, 24)        240       
                                                                 
 average_pooling2d (AverageP  (None, 24, 24, 24)       0         
 ooling2D)                                                       
                                                                 
 conv2d_1 (Conv2D)           (None, 11, 11, 16)        3472      
                                                                 
 conv2d_2 (Conv2D)           (None, 9, 9, 24)          3480      
                                                                 
 batch_normalization (BatchN  (None, 9, 9, 24)         96        
 ormalization)                                                   
                                                                 
 activation (Activation)     (None, 9, 9, 24)          0         
                                                                 
 average_pooling2d_1 (Averag  (None, 4, 4, 24)         0         
 ePooling2D)                                                     
                                                                 
 flatten (Flatten)           (None, 384)               0         
                                                                 
 dense (Dense)               (None, 3)                 1155      
                                                                 
 activation_1 (Activation)   (None, 3)                 0         
                                                                 
=================================================================
Total params: 8,443
Trainable params: 8,395
Non-trainable params: 48
_________________________________________________________________

Total MACs: 1.197 M
Total OPs: 2.528 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor
Training dataset: Found 9 samples belonging to 3 classes:
      rock = 3
     paper = 3
   scissor = 3
Validation dataset: Found 9 samples belonging to 3 classes:
      rock = 3
     paper = 3
   scissor = 3
Running cmd: c:\Users\reed\workspace\silabs\mltk\.venv\Scripts\python.exe -m pip install -U tensorflow-addons
(This may take awhile, please be patient ...)
Requirement already satisfied: tensorflow-addons in c:\users\reed\workspace\silabs\mltk\.venv\lib\site-packages (0.18.0)

Requirement already satisfied: packaging in c:\users\reed\workspace\silabs\mltk\.venv\lib\site-packages (from tensorflow-addons) (21.3)

Requirement already satisfied: typeguard>=2.7 in c:\users\reed\workspace\silabs\mltk\.venv\lib\site-packages (from tensorflow-addons) (2.13.3)

Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\reed\workspace\silabs\mltk\.venv\lib\site-packages (from packaging->tensorflow-addons) (3.0.9)

WARNING: You are using pip version 21.2.3; however, version 22.2.2 is available.

You should consider upgrading via the 'c:\Users\reed\workspace\silabs\mltk\.venv\Scripts\python.exe -m pip install --upgrade pip' command.

Forcing epochs=3 since test=true
Class weights:
   rock = 1.00
  paper = 1.00
scissor = 1.00
Starting model training ...
Epoch 1/3
Epoch 2/3
Epoch 3/3
Generating C:/Users/reed/.mltk/models/image_example1-test/image_example1.test.h5


*** Best training val_accuracy = 0.333


Training complete
Training logs here: C:/Users/reed/.mltk/models/image_example1-test
validation is using 1 subprocesses
Generating tflite_model
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:\tmpv8h9r1ze\assets
Using Tensorflow-Lite Micro version: b13b48c (2022-06-08)
Searching for optimal runtime memory size ...
Determined optimal runtime memory size to be 72320
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
| Index | OpCode          | Input(s)          | Output(s)       | Config                                              |
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
| 0     | quantize        | 96x96x1 (float32) | 96x96x1 (int8)  | Type=none                                           |
| 1     | conv_2d         | 96x96x1 (int8)    | 48x48x24 (int8) | Padding:same stride:2x2 activation:relu             |
|       |                 | 3x3x1 (int8)      |                 |                                                     |
|       |                 | 24 (int32)        |                 |                                                     |
| 2     | average_pool_2d | 48x48x24 (int8)   | 24x24x24 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 3     | conv_2d         | 24x24x24 (int8)   | 11x11x16 (int8) | Padding:valid stride:2x2 activation:relu            |
|       |                 | 3x3x24 (int8)     |                 |                                                     |
|       |                 | 16 (int32)        |                 |                                                     |
| 4     | conv_2d         | 11x11x16 (int8)   | 9x9x24 (int8)   | Padding:valid stride:1x1 activation:relu            |
|       |                 | 3x3x16 (int8)     |                 |                                                     |
|       |                 | 24 (int32)        |                 |                                                     |
| 5     | average_pool_2d | 9x9x24 (int8)     | 4x4x24 (int8)   | Padding:valid stride:2x2 filter:2x2 activation:none |
| 6     | reshape         | 4x4x24 (int8)     | 384 (int8)      | Type=none                                           |
|       |                 | 2 (int32)         |                 |                                                     |
| 7     | fully_connected | 384 (int8)        | 3 (int8)        | Activation:none                                     |
|       |                 | 384 (int8)        |                 |                                                     |
|       |                 | 3 (int32)         |                 |                                                     |
| 8     | softmax         | 3 (int8)          | 3 (int8)        | Type=softmaxoptions                                 |
| 9     | dequantize      | 3 (int8)          | 3 (float32)     | Type=none                                           |
+-------+-----------------+-------------------+-----------------+-----------------------------------------------------+
Total MACs: 1.197 M
Total OPs: 2.561 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor
Runtime memory size (RAM): 71.512 k
hash: f84b0517005c8392d9746f6c6dae1f50
date: 2022-10-06T16:34:05.327Z
samplewise_norm.rescale: 0.0
samplewise_norm.mean_and_std: True
.tflite file size: 15.3kB
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 "