- Performance - MATLAB & Simulink - MathWorks India
- Optimization Tools and Techniques - MATLAB & Simulink - MathWorks India
- Select objective / Prioritized objectives - MATLAB & Simulink - MathWorks India
- Minimize Computations and Storage for Intermediate Results at Block Outputs - MATLAB & Simulink - MathWorks India
- Enable and Reuse Local Block Outputs in Generated Code - MATLAB & Simulink - MathWorks India
- Inline Numeric Values of Block Parameters - MATLAB & Simulink - MathWorks India
- Inline Invariant Signals - MATLAB & Simulink - MathWorks India
- Vector Operation Optimization - MATLAB & Simulink - MathWorks India
Deploying Neural Network with Simulink Embedded Coder: Reducing Code Size and Improving Execution Speed
21 views (last 30 days)
Show older comments
My goal is to deploy a fully connected neural network model to a STM32MP1 microcontroller using the Embedded Coder.
To this end, I've adapted the following workflow (any suggestions appreciated):
- Transform the neural network from .pth to .onnx file format (via torch.onnx.export),
- Bring the network to Matlab using importONNXNetwork(),
- Load this .mat file inside a Simulink Predict block (see image),
- Generate C-Code via the Embedded Coder App, and
- Build and run the model on the STM32MP1 microcontroller.
The constraints of the microcontroller (RAM) and the task (frequency) are the following:
- Network Size: Given the training results the minimum size is a MLP with three hidden layers (256x128x64), I/O = 10/2
- Code Size: The generated Code needs to be < 200kb in order to fit into the RAM
- Network Speed: The generated code needs to run within < 1ms
Currently, with this workflow, I am able to generate and build C-Code for a smaller network (128x64x32) that is now running on the STM32MP1 with an execution time of around 0.9ms, while the actual network (256x128x64) fails to build on the microcontroller due to an overflow of 76kB.
Hence, the two related questions are (of this priority):
- How to reduce the size of the generated C-Code (from 276kb to <200kb)
- How to improve the execution speed for the generated C-Code
0 Comments
Answers (1)
Prateek
on 6 Jan 2023
Hello Julian,
Optimizing code generation for memory is broad topic, and I can point you to some pages that contain relevant information about this:
These might help you figure out a good approach to optimizing your model.
Hope this helps.
Regards,
Prateek
2 Comments
Prateek
on 6 Jan 2023
Hi Julian,
I would suggest contacting MathWorks Technical Support for this. Here's the link to it : Contact Us - MATLAB & Simulink (mathworks.com)
Regards,
Prateek
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!