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 (AveragePo (None, 24, 24, 24)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 16)        3472      
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 9, 9, 24)          3480      
_________________________________________________________________
batch_normalization (BatchNo (None, 9, 9, 24)          96        
_________________________________________________________________
activation (Activation)      (None, 9, 9, 24)          0         
_________________________________________________________________
average_pooling2d_1 (Average (None, 4, 4, 24)          0         
_________________________________________________________________
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
hash: None
date: None

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     | conv_2d         | 96x96x1 (int8)  | 48x48x24 (int8) | Padding:same stride:2x2 activation:relu             |
|       |                 | 3x3x1 (int8)    |                 |                                                     |
|       |                 | 24 (int32)      |                 |                                                     |
| 1     | average_pool_2d | 48x48x24 (int8) | 24x24x24 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 2     | conv_2d         | 24x24x24 (int8) | 11x11x16 (int8) | Padding:valid stride:2x2 activation:relu            |
|       |                 | 3x3x24 (int8)   |                 |                                                     |
|       |                 | 16 (int32)      |                 |                                                     |
| 3     | conv_2d         | 11x11x16 (int8) | 9x9x24 (int8)   | Padding:valid stride:1x1 activation:relu            |
|       |                 | 3x3x16 (int8)   |                 |                                                     |
|       |                 | 24 (int32)      |                 |                                                     |
| 4     | average_pool_2d | 9x9x24 (int8)   | 4x4x24 (int8)   | Padding:valid stride:2x2 filter:2x2 activation:none |
| 5     | reshape         | 4x4x24 (int8)   | 384 (int8)      | BuiltinOptionsType=0                                |
|       |                 | 2 (int32)       |                 |                                                     |
| 6     | fully_connected | 384 (int8)      | 3 (int8)        | Activation:none                                     |
|       |                 | 384 (int8)      |                 |                                                     |
|       |                 | 3 (int32)       |                 |                                                     |
| 7     | softmax         | 3 (int8)        | 3 (int8)        | BuiltinOptionsType=9                                |
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
Total MACs: 1.197 M
Total OPs: 2.524 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor
hash: 8bf54869131182b072b9ac3d65757a11
date: 2021-10-19T18:50:46.802Z
samplewise_norm.rescale: 0
samplewise_norm.mean_and_std: True
.tflite file size: 14.9kB

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) | Multipler: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) | Multipler: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) | Multipler: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) | Multipler: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)      | BuiltinOptionsType=0                                  |
|       |                   | 2 (int32)      |                |                                                       |
| 11    | fully_connected   | 64 (int8)      | 12 (int8)      | Activation:none                                       |
|       |                   | 64 (int8)      |                |                                                       |
|       |                   | 12 (int32)     |                |                                                       |
| 12    | softmax           | 12 (int8)      | 12 (int8)      | BuiltinOptionsType=9                                  |
+-------+-------------------+----------------+----------------+-------------------------------------------------------+
Total MACs: 2.657 M
Total OPs: 5.394 M
Name: summarize_model
Version: 1
Description: Generated by Silicon Lab's MLTK Python package
classes: []
hash: None
date: None
.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
Model: "image_example1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_3 (Conv2D)            (None, 48, 48, 24)        240       
_________________________________________________________________
average_pooling2d_2 (Average (None, 24, 24, 24)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 11, 11, 16)        3472      
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 9, 9, 24)          3480      
_________________________________________________________________
batch_normalization_1 (Batch (None, 9, 9, 24)          96        
_________________________________________________________________
activation_2 (Activation)    (None, 9, 9, 24)          0         
_________________________________________________________________
average_pooling2d_3 (Average (None, 4, 4, 24)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 384)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 1155      
_________________________________________________________________
activation_3 (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
hash: None
date: None
Test mode enabled, forcing max_samples_per_class=3, batch_size=3
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
Forcing epochs=1 since test=true
Class weights:
- rock = 1.00
- paper = 1.00
- scissor = 1.00
Starting model training ...
Epoch 1/1
Generating C:/Users/reed/.mltk/models/image_example1-test/image_example1.test.h5


*** Best training val_accuracy = 0.222


Training complete
Training logs here: C:/Users/reed/.mltk/models/image_example1-test
Trained model files here: c:/users/reed/workspace/silabs/mltk/mltk/models/examples/image_example1-test.mltk.zip
ProcessPoolManager using 7 of 24 CPU cores
NOTE: You may need to adjust the "cores" parameter of the data generator if you're experiencing performance issues
Generating tflite_model
INFO:tensorflow:Assets written to: E:\tmpevbj7v09\assets
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
| Index | OpCode          | Input(s)        | Output(s)       | Config                                              |
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
| 0     | conv_2d         | 96x96x1 (int8)  | 48x48x24 (int8) | Padding:same stride:2x2 activation:relu             |
|       |                 | 3x3x1 (int8)    |                 |                                                     |
|       |                 | 24 (int32)      |                 |                                                     |
| 1     | average_pool_2d | 48x48x24 (int8) | 24x24x24 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 2     | conv_2d         | 24x24x24 (int8) | 11x11x16 (int8) | Padding:valid stride:2x2 activation:relu            |
|       |                 | 3x3x24 (int8)   |                 |                                                     |
|       |                 | 16 (int32)      |                 |                                                     |
| 3     | conv_2d         | 11x11x16 (int8) | 9x9x24 (int8)   | Padding:valid stride:1x1 activation:relu            |
|       |                 | 3x3x16 (int8)   |                 |                                                     |
|       |                 | 24 (int32)      |                 |                                                     |
| 4     | average_pool_2d | 9x9x24 (int8)   | 4x4x24 (int8)   | Padding:valid stride:2x2 filter:2x2 activation:none |
| 5     | reshape         | 4x4x24 (int8)   | 384 (int8)      | BuiltinOptionsType=0                                |
|       |                 | 2 (int32)       |                 |                                                     |
| 6     | fully_connected | 384 (int8)      | 3 (int8)        | Activation:none                                     |
|       |                 | 384 (int8)      |                 |                                                     |
|       |                 | 3 (int32)       |                 |                                                     |
| 7     | softmax         | 3 (int8)        | 3 (int8)        | BuiltinOptionsType=9                                |
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
Total MACs: 1.197 M
Total OPs: 2.524 M
Name: image_example1
Version: 1
Description: Image classifier example for detecting Rock/Paper/Scissors hand gestures in images
Classes: rock, paper, scissor
hash: 0dd2d415f9fedd530c149e0ba870b44d
date: 2021-10-19T18:55:40.561Z
samplewise_norm.rescale: 0
samplewise_norm.mean_and_std: True
.tflite file size: 14.9kB