This code example demonstrates how to run through the ModusToolbox™ machine learning (ModusToolbox™-ML) development flow with PSoC™ 6 MCU, where the end user has a pre-trained neural network (NN) model, which can be profiled and validated at the PC and target device.
You can import a pre-trained model using the ModusToolbox™-ML configurator tool, create an embedded, optimized version of this model, and validate the performance on the PC. After this, the validation data files can be integrated with this code example or streamed to the device (default option), so you can run the same validation data and profile performance when the ML model is deployed on the PSoC™ 6 MCU.
Figure 1. ModusToolbox™-ML development flow
For details about the ModusToolbox™ machine learning solution, see this link.
Provide feedback on this code example.
GCC_ARM
) - Default value of TOOLCHAIN
ARM
)IAR
)Note: TensorFlow Lite for Microcontrollers (TFLM) does not support IAR compiler.
CY8CPROTO-062-4343W
) - Default value of TARGET
CY8CKIT-062-WIFI-BT
)CY8CKIT-062S2-43012
)CYW9P62S1-43438EVB-01
)CYW9P62S1-43012EVB-01
)CY8CPROTO-062S3-4343W
)CY8CKIT-064B0S2-4343W
)CY8CEVAL-062S2
, CY8CEVAL-062S2-LAI-4373M2
, CY8CEVAL-062S2-MUR-43439M2
)This example uses the board's default configuration. See the kit user guide to ensure that the board is configured correctly.
Note: The PSoC™ 6 Bluetooth® LE pioneer kit (CY8CKIT-062-BLE) and the PSoC™ 6 Wi-Fi Bluetooth® pioneer kit (CY8CKIT-062-WIFI-BT) ship with KitProg2 installed. The ModusToolbox™ software requires KitProg3. Before using this code example, make sure that the board is upgraded to KitProg3. The tool and instructions are available in the Firmware Loader GitHub repository. If you do not upgrade, you will see an error like "unable to find CMSIS-DAP device" or "KitProg firmware is out of date".
Install a terminal emulator if you do not have one. Instructions in this document use Tera Term.
Install the Machine Learning pack using this link. Alternatively, use the Infineon Developer Center (IDC) launcher and search for ModusToolbox Machine Learning Pack
and install it.
Use the ModusToolbox™-ML configurator tool (from {ModusToolbox™ software install directory}/packs/ModusToolbox-Machine-Learning-Pack/tools/ml-configurator/) to load a pre-trained NN model and generate C files to be used with this code example. Alternatively, you can launch the ML configurator tool in Eclipse IDE for ModusToolbox™ from the Quick Launch window; or using the make ml-configurator
command in the console. For more information, see the ModusToolbox™ machine learning user guide.
By default, the Makefile uses a model that comes with the code example. The pre-trained NN model is located in the pretrained_models folder. You can use the ModusToolbox™-ML configurator to link to this file, or load another model file and generate C files for the target device.
By default, the output files location is set to mtb_ml_gen. The project name is set to TEST_MODEL. If you change any of these default settings, edit the following Makefile parameters of this code example:
Makefile parameter | Description |
---|---|
NN_TYPE= |
Defines the input data format and NN weights. It can be float , int16x16 , int16x8 , or int8x8 . |
NN_MODEL_NAME= |
Defines the name of the model. The name comes from the project name defined in the ML configurator tool. No quotes are used when changing the name of the model. |
NN_MODEL_FOLDER= |
Sets the name where the model files will be placed. The name comes from the output file location defined in the ModusToolbox™-ML configurator tool. |
NN_INFERENCE_ENGINE |
Defines the inference engine to run. It has three options: tflm , tflm_less and ifx |
Note: The tflm
and tflm_less
inference engines only support float
and int8x8
.
To validate the given model with the ModusToolbox™-ML configurator tool, click the Validate in Desktop tab. By default, the project uses testing data stored in the file located at test_data/test_data.csv.
Alternatively, you can use a random dataset structure to validate the model. You can click the Validate button to initiate the validation. Note that this will generate the regression data files, which can be stored in the internal memory of the target device. Because of that, limit the sample count to 100 in the ModusToolbox™-ML configurator tool.
You can also stream the test data to the device by clicking the Validate on Target. In this scheme, the regression data files are not stored in the internal memory, so you can work with a larger dataset. It uses the UART to stream the data.
This code example sets the UART baudrate based on the operating system used to build the firmware. It sets to 115.2 Kbps for MacOS and 1 Mbps for Windows or Linux. ModusToolbox™-ML configurator tool also sets the baudrate based on the operating system. If the code example is built on a different operating system then the one running the tool, check if the baudrate matches. If not, rebuild the firmware by setting the appropriate value in the main.c when calling cy_retarget_io_init()
;
Create the project and open it using one of the following:
If using a PSoC™ 64 "Secure" MCU kit (like CY8CKIT-064B0S2-4343W), the PSoC™ 64 device must be provisioned with keys and policies before being programmed. Follow the instructions in the "Secure Boot" SDK user guide to provision the device. If the kit is already provisioned, copy-paste the keys and policy folder to the application folder.
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
If using local regression data, in main.c, set the REGRESSION_DATA_SOURCE
to USE_LOCAL_DATA
. Then open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
Program the board using one of the following:
After programming, the application starts automatically. If using regression local data, confirm that "Neural Network Profiler", model information, profiling data and accuracy results are printed on the UART terminal.
If using streaming regression data (default option), open the ModusToolbox™-ML configurator tool. Open the design.mtbml file and click Validate on Target. Select the KitProg COM port (ensure that no other software has the COM port open) and check the Quantization that matches the NN_TYPE set in the Makefile. Click Validate.
If using the ModusToolbox™-ML configurator tool, the results of the validation are printed on the tool's console. Log files for the profiling are stored in the following files (located at NN_MODEL_FOLDER
/info_target/ folder):
You can choose the type of profiling/debugging data to be printed by setting the PROFILE_CONFIGURATION
macro in main.c.
Note: Depending on the chosen quantization, validation results might return a failure if the accuracy is not greater than or equal to 98%.
You can debug the example to step through the code. In the IDE, use the \
Note: (Only while debugging) On the CM4 CPU, some code in main()
may execute before the debugger halts at the beginning of main()
. This means that some code executes twice – once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main()
. See KBA231071 to learn about this and for the workaround.
In this example, you must provide a pre-trained NN model with the weights, regression data, and the model parameters. The ModusToolbox™-ML configurator tool can generate such data based on the standard Keras H5 format or TFLite format. By default, these files are located in the ./mtb_ml_gen folder. The code examples also provide an ModusToolbox™-ML configurator tool project file - design.mtbml, which points to the pre-trained NN model available in the pretrained_models folder.
If you change the output file location in the ModusToolbox™-ML configurator tool, you must also reflect the change in the Makefile (the NN_MODEL_FOLDER
parameter). The model data is stored as a header file or as a binary file (used for filesystem applications). This example uses header files. Depending the type of the NN model chosen in the Makefile (the NN_TYPE
parameter), the application uses the files and variables from the following table, where (KEY is the output file prefix defined by the ModusToolbox™-ML configurator tool):
Table 1. Inference Engine: ifx
Folder name | File name | Variable name | Description |
---|---|---|---|
mtb_ml_models | KEY_ifx_model_int16x16.h/c KEY_ifx_model_int16x8.h/c KEY_ifx_model_int8x8.h/c KEY_ifx_model_float.h/c |
KEY_model_prms_bin KEY_model_bin |
Contains the NN parameters, weights and bias |
mtb_ml_regression_data | KEY_ifx_x_data_int16x16.h/c KEY_ifx_y_data_int16x16.h/c KEY_ifx_x_data_int16x8.h/c KEY_ifx_y_data_int16x8.h/c KEY_ifx_x_data_int8x8.h/c KEY_ifx_y_data_int8x8.h/c KEY_ifx_x_data_float.h/c KEY_ifx_y_data_float.h/c |
KEY_x_data_bin KEY_y_data_bin |
Contains the input (x) and output (y) regression data |
Table 2. Inference Engine: tflm
Folder name | File name | Variable name | Description |
---|---|---|---|
mtb_ml_models | KEY_tflm_model_int8x8.h/c KEY_tflm_model_float.h/c |
KEY_model_bin |
Contains the NN weights and bias |
mtb_ml_regression_data | KEY_tflm_x_data_int8x8.h/c KEY_tflm_y_data_int8x8.h/c KEY_tflm_x_data_float.h/c KEY_tflm_y_data_float.h/c |
KEY_x_data_bin KEY_y_data_bin |
Contains the input (x) and output (y) regression data |
Table 3. Inference Engine: tflm_less
Folder name | File name | Variable name | Description |
---|---|---|---|
mtb_ml_models | KEY_tflm_less_model_int8x8.h/cpp KEY_tflm_less_model_float.h/cpp |
No variables, only functions | Contains the TFLM functions implementation |
mtb_ml_regression_data | KEY_tflm_less_x_data_int8x8.h/c KEY_tflm_less_y_data_int8x8.h/c KEY_tflm_less_x_data_float.h/c KEY_tflm_less_y_data_float.h/c |
KEY_x_data_bin KEY_y_data_bin |
Contains the input (x) and output (y) regression data |
In the Makefile, set the NN_MODEL_NAME
parameter based on the output file prefix chosen in the ModusToolbox™-ML configurator tool.
You can also change the type of inference engine to run by setting the NN_INFERENCE_ENGINE
in the Makefile. We have three options:
1) ifx
: Infineon inference engine.
2) tflm
: TensorFlow Lite Micro inference engine with runtime interpreter
3) tflm_less
: TensorFlow Lite Micro inference engine without interpreter (interpreter-less)
Note: If you are using TensorFlow Lite Micro, only int8x8 and float NN_TYPE
are supported.
After updating the Makefile, all the model files are built into the application automatically, allowing the NN inference engine to be initialized and fed with the regression data.
This application has an option to choose the source of the regression data in the main.c file. You can set the REGRESSION_DATA_SOURCE
to one of the following:
USE_STREAM_DATA
– Uses the ModusToolbox™-ML Configurator tool to stream the regression dataUSE_LOCAL_DATA
– Uses the files located in mtb_ml_gen/mtb_ml_regression_data for the regression dataNote: Some devices from the supported kits might not have enough memory to run some of the configurations above, specially if using local regression data. If that occurs, pick another kit with larger memory device, or refer to the ML user guide on how to define the CY_ML_MODEL_MEM
macro.
Note: When using a TFLM int8x8 model with local regression data, the output of the model is compared to quantized reference int8x8 model results. If using the streamed data, the output of the model is compared to a float reference model results. That means the accuracy results might differ between using local data and streamed data.
The application also has an option to choose what type of profiling/debugging data to print/stream. You can set the PROFILE_CONFIGURATION
to one of the following:
Configuration | Description | Support |
---|---|---|
MTB_ML_PROFILE_DISABLE |
Disable profiling | IFX, TFLM |
MTB_ML_PROFILE_ENABLE_MODEL (default) |
Enables model profiling | IFX, TFLM |
MTB_ML_PROFILE_ENABLE_LAYER |
Enables per-layer profiling | IFX |
MTB_ML_PROFILE_ENABLE_MODEL_PER_FRAME |
Enables per-frame model profiling | IFX |
MTB_ML_PROFILE_ENABLE_LAYER_PER_FRAME |
Enables per-frame layer profiling | IFX |
MTB_ML_LOG_ENABLE_MODEL_LOG |
Enables model output | IFX, TFLM |
If using the local regression data, the application automatically loads the regression data generated by the ML configurator tool. The regression data consists of a collection of inputs (X) and a collection of outputs (Y). Once the inference engine processes X, it outputs the result. Then, the firmware compares the result with the desired value, Y. If these match, the firmware contributes to the accuracy calculation.
If using the ModusToolbox™-ML configurator tool, the same regression data is streamed over the UART. The following figure shows the communication sequence diagram between the tool and the device.
Figure 2. Communication sequence diagram
|-- mtb_ml_gen/ # Contains the model and regression files
|-- pretrained_models/ # Contains the Keras-H5 and TFlite models (used by the ML configurator tool)
|-- test_data/ # Contains a CSV file with the test data
|-- source # Contains the source code files for this example
|- elapsed_timer.c/h # Implements a system tick timer
|- ml_local_regression.c/h # Implements a local regression flow
|-- design.mtbml # ModusToolbox™-ML configurator tool project file
Table 4. Application resources
Resource | Alias/object | Purpose |
---|---|---|
UART (HAL) | cy_retarget_io_uart_obj | UART HAL object used by retarget-io for debug UART port |
Resources | Links |
---|---|
Application notes | AN228571 – Getting started with PSoC™ 6 MCU on ModusToolbox™ software AN221774 – Getting started with PSoC™ 6 MCU on PSoC™ Creator AN210781 – Getting started with PSoC™ 6 MCU with Bluetooth® Low Energy connectivity on PSoC™ Creator AN215656 – PSoC™ 6 MCU: Dual-CPU system design |
Code examples | Using ModusToolbox™ software on GitHub |
Device documentation | PSoC™ 6 MCU datasheets PSoC™ 6 technical reference manuals |
User guides | Machine learning user guide Machine learning configurator guide |
Development kits | Visit https://www.infineon.com/cms/en/design-support/finder-selection-tools/product-finder/evaluation-board/ and use the options in the Select your kit section to filter kits by Product family or Features. |
Libraries on GitHub | mtb-pdl-cat1 – PSoC™ 6 peripheral driver library (PDL) and docs mtb-hal-cat1 – Hardware abstraction layer (HAL) Library and docs retarget-io – Utility library to retarget STDIO messages to a UART port |
Middleware on GitHub | capsense – CAPSENSE™ library and docs psoc6-middleware – Links to all PSoC™ 6 MCU middleware |
Tools | Eclipse IDE for ModusToolbox™ software – ModusToolbox™ software is a collection of easy-to-use software and tools enabling rapid development with Infineon MCUs, covering applications from embedded sense and control to wireless and cloud-connected systems using AIROC™ Wi-Fi and Bluetooth® connectivity devices. |
Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.
For PSoC™ 6 MCU devices, see How to design with PSoC™ 6 MCU - KBA223067 in the Infineon community.
Document title: CE232551 - Machine learning: Neural network profiler
Version | Description of change |
---|---|
1.0.0 | New code example |
1.1.0 | Added test_data.csv file for regression |
2.0.0 | Updated code to use ml-middleware library Added regression data streaming capability |
3.0.0 | Major update to support ModusToolbox™ v3.0. This version is not backward compatible with previous versions of ModusToolbox™. Updated to support TFLM |
3.1.0 | Added more information about profile configuration Added IAR compiler limitation note Added memory limitation note for some devices |
© Cypress Semiconductor Corporation, 2020-2022. This document is the property of Cypress Semiconductor Corporation, an Infineon Technologies company, and its affiliates ("Cypress"). This document, including any software or firmware included or referenced in this document ("Software"), is owned by Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with Cypress governing the use of the Software, then Cypress hereby grants you a personal, non-exclusive, nontransferable license (without the right to sublicense) (1) under its copyright rights in the Software (a) for Software provided in source code form, to modify and reproduce the Software solely for use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of Cypress’s patents that are infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for use with Cypress hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. No computing device can be absolutely secure. Therefore, despite security measures implemented in Cypress hardware or software products, Cypress shall have no liability arising out of any security breach, such as unauthorized access to or use of a Cypress product. CYPRESS DOES NOT REPRESENT, WARRANT, OR GUARANTEE THAT CYPRESS PRODUCTS, OR SYSTEMS CREATED USING CYPRESS PRODUCTS, WILL BE FREE FROM CORRUPTION, ATTACK, VIRUSES, INTERFERENCE, HACKING, DATA LOSS OR THEFT, OR OTHER SECURITY INTRUSION (collectively, "Security Breach"). Cypress disclaims any liability relating to any Security Breach, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any Security Breach. In addition, the products described in these materials may contain design defects or errors known as errata which may cause the product to deviate from published specifications. To the extent permitted by applicable law, Cypress reserves the right to make changes to this document without further notice. Cypress does not assume any liability arising out of the application or use of any product or circuit described in this document. Any information provided in this document, including any sample design information or programming code, is provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality and safety of any application made of this information and any resulting product. "High-Risk Device" means any device or system whose failure could cause personal injury, death, or property damage. Examples of High-Risk Devices are weapons, nuclear installations, surgical implants, and other medical devices. "Critical Component" means any component of a High-Risk Device whose failure to perform can be reasonably expected to cause, directly or indirectly, the failure of the High-Risk Device, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any use of a Cypress product as a Critical Component in a High-Risk Device. You shall indemnify and hold Cypress, including its affiliates, and its directors, officers, employees, agents, distributors, and assigns harmless from and against all claims, costs, damages, and expenses, arising out of any claim, including claims for product liability, personal injury or death, or property damage arising from any use of a Cypress product as a Critical Component in a High-Risk Device. Cypress products are not intended or authorized for use as a Critical Component in any High-Risk Device except to the limited extent that (i) Cypress’s published data sheet for the product explicitly states Cypress has qualified the product for use in a specific High-Risk Device, or (ii) Cypress has given you advance written authorization to use the product as a Critical Component in the specific High-Risk Device and you have signed a separate indemnification agreement.
Cypress, the Cypress logo, and combinations thereof, WICED, ModusToolbox, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are trademarks or registered trademarks of Cypress or a subsidiary of Cypress in the United States or in other countries. For a more complete list of Cypress trademarks, visit cypress.com. Other names and brands may be claimed as property of their respective owners.