{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Update Model Parameters API Examples\n", "\n", "This demonstrates how to use the [update_model_parameters](../../docs/python_api/operations/update_model_parameters.md) API.\n", "\n", "Refer to the [Model Parameters](../../docs/guides/model_parameters.md) guide for more details.\n", "\n", "__NOTES:__ \n", "\n", "- 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/update_params.ipynb) to run this example interactively in your browser \n", "- Refer to the [Notebook Examples Guide](../../docs/guides/notebook_examples_guide.md) for how to run this example locally in VSCode " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install MLTK Python Package" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Install the MLTK Python package (if necessary)\n", "!pip install --upgrade silabs-mltk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import Python Packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import the necessary MLTK APIs\n", "from mltk.core import update_model_parameters, summarize_model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1: Update model specification\n", "\n", "The most common use case of the [update_model_parameters](../../docs/python_api/operations/update_model_parameters.md) API is: \n", "1. Fully [train](../../docs/guides/model_training.md) a model \n", "2. Later modify the [model specification](../../docs/guides/model_specification.md) script with additional parameters \n", "3. Run the [update_model_parameters](../../docs/python_api/operations/update_model_parameters.md) API to update the `.tflite` model file in the [model archive](../../docs/guides/model_archive.md).\n", "\n", "In this example, it's assumed that the [MltkModel.model_parameters](https://siliconlabs.github.io/mltk/docs/python_api/mltk_model/model.html#mltk.core.MltkModel.model_parameters) settings in\n", "the [tflite_micro_speech](../../docs/python_api/models/tflite_micro/tflite_micro_speech.md) model specification script have been modified _after_ the model have been\n", "trained.\n", "\n", "```python\n", "my_model.model_parameters['average_window_duration_ms'] = 1000\n", "my_model.model_parameters['detection_threshold'] = 185\n", "my_model.model_parameters['suppression_ms'] = 1500\n", "my_model.model_parameters['minimum_count'] = 3\n", "my_model.model_parameters['volume_db'] = 5.0\n", "my_model.model_parameters['latency_ms'] = 0\n", "my_model.model_parameters['log_level'] = 'info'\n", "```\n", "\n", "After this API completes, the `tflite_micro_speech.mltk.zip` model archive is updated with a new `tflite_micro_speech.tflite` model file. \n", "Note that _only_ the parameters in the `.tflite`'s metadata section are modified. The model weights and layers are untouched." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using Tensorflow-Lite Micro version: b13b48c (2022-06-08)\n", "Searching for optimal runtime memory size ...\n", "Determined optimal runtime memory size to be 9600\n", "Updating c:/users/reed/workspace/silabs/mltk/mltk/models/tflite_micro/tflite_micro_speech.mltk.zip\n", "Model: \"tflite_micro_speech\"\n", "_________________________________________________________________\n", " Layer (type) Output Shape Param # \n", "=================================================================\n", " conv2d (Conv2D) (None, 25, 20, 8) 648 \n", " \n", " batch_normalization (BatchN (None, 25, 20, 8) 32 \n", " ormalization) \n", " \n", " activation (Activation) (None, 25, 20, 8) 0 \n", " \n", " dropout (Dropout) (None, 25, 20, 8) 0 \n", " \n", " flatten (Flatten) (None, 4000) 0 \n", " \n", " dense (Dense) (None, 4) 16004 \n", " \n", "=================================================================\n", "Total params: 16,684\n", "Trainable params: 16,668\n", "Non-trainable params: 16\n", "_________________________________________________________________\n", "\n", "Total MACs: 336.000 k\n", "Total OPs: 684.004 k\n", "Name: tflite_micro_speech\n", "Version: 1\n", "Description: TFLite-Micro speech\n", "Classes: yes, no, _unknown_, _silence_\n", "average_window_duration_ms: 1000\n", "detection_threshold: 185\n", "suppression_ms: 750\n", "minimum_count: 3\n", "volume_gain: 2\n", "latency_ms: 100\n", "verbose_model_output_logs: False\n" ] } ], "source": [ "# Update the model parameters\n", "update_model_parameters('tflite_micro_speech')\n", "\n", "# Generate a summary of the updated model with new parameters\n", "print(summarize_model('tflite_micro_speech'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2: Update with argument\n", "\n", "The [update_model_parameters](../../docs/python_api/operations/update_model_parameters.md) API also works with `.tflite` model files generated outside of the MLTK. \n", "In this mode, model parameters can be supplied via API argument, e.g.:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os \n", "import tempfile\n", "import urllib\n", "import shutil\n", "\n", "# Use .tflite mode found here:\n", "# https://github.com/mlcommons/tiny/tree/master/benchmark/training/keyword_spotting/trained_models\n", "# NOTE: Update this URL to point to your model if necessary\n", "TFLITE_MODEL_URL = 'https://github.com/mlcommons/tiny/raw/master/benchmark/training/keyword_spotting/trained_models/kws_ref_model.tflite'\n", "\n", "# Download the .tflite file and save to the temp dir\n", "external_tflite_path = os.path.normpath(f'{tempfile.gettempdir()}/kws_ref_model.tflite')\n", "with open(external_tflite_path, 'wb') as dst:\n", " with urllib.request.urlopen(TFLITE_MODEL_URL) as src:\n", " shutil.copyfileobj(src, dst)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using Tensorflow-Lite Micro version: b13b48c (2022-06-08)\n", "Searching for optimal runtime memory size ...\n", "Determined optimal runtime memory size to be 25472\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "| Index | OpCode | Input(s) | Output(s) | Config |\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "| 0 | conv_2d | 49x10x1 (int8) | 25x5x64 (int8) | Padding:same stride:2x2 activation:relu |\n", "| | | 10x4x1 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 1 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 2 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 3 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 4 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 5 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 6 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 7 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 8 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 9 | average_pool_2d | 25x5x64 (int8) | 1x1x64 (int8) | Padding:valid stride:5x25 filter:5x25 activation:none |\n", "| 10 | reshape | 1x1x64 (int8) | 64 (int8) | Type=none |\n", "| | | 2 (int32) | | |\n", "| 11 | fully_connected | 64 (int8) | 12 (int8) | Activation:none |\n", "| | | 64 (int8) | | |\n", "| | | 12 (int32) | | |\n", "| 12 | softmax | 12 (int8) | 12 (int8) | Type=softmaxoptions |\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "Total MACs: 2.657 M\n", "Total OPs: 5.394 M\n", "Name: summarize_model\n", "Version: 1\n", "Description: Generated by Silicon Lab's MLTK Python package\n", "Runtime memory size (RAM): 24.384 k\n", "hash: a4fadcb07cc13bf4ce4249e7349cadf3\n", "date: 2022-10-06T16:36:18.836Z\n", "volume: 10.0\n", "log_level: debug\n", "threshold: 43\n", ".tflite file size: 54.1kB\n" ] } ], "source": [ "# Set the parameters in a Python dictionary\n", "parameters = {\n", " \"volume\": 10.0,\n", " \"log_level\": \"debug\",\n", " \"threshold\": 43\n", "}\n", "# Update the model parameters\n", "update_model_parameters(external_tflite_path, params=parameters)\n", "\n", "# Generate a summary of the updated model with new parameters\n", "print(summarize_model(external_tflite_path))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3: Return TfliteModel instance\n", "\n", "Rather than update the given model, the [update_model_parameters](../../docs/python_api/operations/update_model_parameters.md) API can also return a [TfliteModel](../../docs/python_api/tflite_model/index.md) instance with the updated parameters.\n", "\n", "This is done by specifying the `output='tflite_model'` API argument:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using Tensorflow-Lite Micro version: b13b48c (2022-06-08)\n", "Searching for optimal runtime memory size ...\n", "Determined optimal runtime memory size to be 25472\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "| Index | OpCode | Input(s) | Output(s) | Config |\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "| 0 | conv_2d | 49x10x1 (int8) | 25x5x64 (int8) | Padding:same stride:2x2 activation:relu |\n", "| | | 10x4x1 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 1 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 2 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 3 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 4 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 5 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 6 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 7 | depthwise_conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Multiplier:1 padding:same stride:1x1 activation:relu |\n", "| | | 3x3x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 8 | conv_2d | 25x5x64 (int8) | 25x5x64 (int8) | Padding:same stride:1x1 activation:relu |\n", "| | | 1x1x64 (int8) | | |\n", "| | | 64 (int32) | | |\n", "| 9 | average_pool_2d | 25x5x64 (int8) | 1x1x64 (int8) | Padding:valid stride:5x25 filter:5x25 activation:none |\n", "| 10 | reshape | 1x1x64 (int8) | 64 (int8) | Type=none |\n", "| | | 2 (int32) | | |\n", "| 11 | fully_connected | 64 (int8) | 12 (int8) | Activation:none |\n", "| | | 64 (int8) | | |\n", "| | | 12 (int32) | | |\n", "| 12 | softmax | 12 (int8) | 12 (int8) | Type=softmaxoptions |\n", "+-------+-------------------+----------------+----------------+-------------------------------------------------------+\n", "Total MACs: 2.657 M\n", "Total OPs: 5.394 M\n", "Name: summarize_model\n", "Version: 1\n", "Description: Generated by Silicon Lab's MLTK Python package\n", "Runtime memory size (RAM): 24.384 k\n", "hash: a4fadcb07cc13bf4ce4249e7349cadf3\n", "date: 2022-10-06T16:36:26.831Z\n", "volume: 10.0\n", "log_level: debug\n", "threshold: 43\n", ".tflite file size: 54.1kB\n" ] } ], "source": [ "# Set the parameters in a Python dictionary\n", "parameters = {\n", " \"volume\": 10.0,\n", " \"log_level\": \"debug\",\n", " \"threshold\": 43\n", "}\n", "# Generate a TfliteModel instance with the given parameters\n", "# NOTE: The input external_tflite_path file is NOT modified\n", "tflite_model = update_model_parameters(external_tflite_path, params=parameters, output='tflite_model')\n", "\n", "# Generate a summary of the returned TfliteModel instance\n", "print(summarize_model(tflite_model))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('.venv': venv)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "600e22ae316f8c315f552eaf99bb679bc9438a443c93affde9ac001991b79c8f" } } }, "nbformat": 4, "nbformat_minor": 2 }