<jupyter_start><jupyter_text>Model Summary API ExamplesThis demonstrates how to use the [summarize_model](https://siliconlabs.github.io/mltk/docs/python_api/operations.htmlsummarize_model) API.Refer to the [Model Summary](https://siliconlabs.github.io/mltk/docs/guides/model_summary.html) guide for more details.__NOTES:__  - Click here: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/siliconlabs/mltk/blob/master/mltk/examples/summarize_model.ipynb) to run this example interactively in your browser  - Refer to the [Notebook Examples Guide](https://siliconlabs.github.io/mltk/docs/guides/notebook_examples_guide.html) for how to run this example locally in VSCode    Install MLTK Python Package<jupyter_code># Install the MLTK Python package (if necessary)
!pip install --upgrade silabs-mltk<jupyter_output><empty_output><jupyter_text>Import Python Packages<jupyter_code># Import the necessary MLTK APIs
from mltk.core import summarize_model<jupyter_output><empty_output><jupyter_text>Example 1: Summarize Keras modelIn this example, we generate a summary of the trained `.h5` model file in the [image_example1](https://siliconlabs.github.io/mltk/docs/python_api/models/examples/image_example1.html) model's [model archive](https://siliconlabs.github.io/mltk/docs/guides/model_archive.html).<jupyter_code>summary = summarize_model('image_example1')
print(summary)<jupyter_output>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         
____________________________________________________[...]<jupyter_text>Example 2: Summarize Tensorflow-Lite modelIn this example, we generate a summary of the trained `.tflite` model file in the [image_example1](https://siliconlabs.github.io/mltk/docs/python_api/models/examples/image_example1.html) model's [model archive](https://siliconlabs.github.io/mltk/docs/guides/model_archive.html).<jupyter_code>summary = summarize_model('image_example1', tflite=True)
print(summary)<jupyter_output>+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
| 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)   |           [...]<jupyter_text>Example 3: Summarize external Tensorflow-Lite modelThe given model need _not_ be generated by the MLTK. External `.tflite` or `.h5` model files are also supported by the summarize API.<jupyter_code>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)<jupyter_output>+-------+-------------------+----------------+----------------+-------------------------------------------------------+
| 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)[...]<jupyter_text>Example 4: Summarize model before trainingTraining 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 modeland summarize it _before_ the model is fully trained.In this example, the [image_example1](https://siliconlabs.github.io/mltk/docs/python_api/models/examples/image_example1.html) model is builtat API execution time and a summary is generated.  Note that _only_ the [model specification](https://siliconlabs.github.io/mltk/docs/guides/model_specification.html) script is required, it does _not_ need to be trained first.<jupyter_code>summary = summarize_model('image_example1', tflite=True, build=True)
print(summary)<jupyter_output>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         
_________________________________[...]
{'fraction_non_alphanumeric': 0.15754582087612115, 'fraction_numerical': 0.0322370986611205, 'mean_word_length': 2.345217391304348, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 13, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '428258', 'license': 'Zlib', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1953, 'n_words': 462, 'path': 'mltk/examples/summarize_model.ipynb', 'repo_name': 'SiliconLabs/mltk', 'size': '7693'}