C Code Generation for 1D CNN networks using CMSIS-NN

11 views (last 30 days)
since R2022a the CMSIS-NN library is available for C code generation. I am currently trying to generate C code from 1D CNNs for timeseries classification with the Matlab Coder App and run the generated code on a STM32F427. However, the CMSIS Library seems to support only LSTM Networks (tested with the coder.getDeepLearningLayers(TargetLibrary = 'cmsis-nn') command) but neither 1D nor 2D Convolution Layers.
Also i did find this instruction on how to build the CMSIS-NN library. I was able to follow all steps, but i don't really know how to proceed further with the resulting files. Since the title refers to C++ and the instruction itself only refers to C Code i am not sure whether this is the right solution to my problem.
So my questions are:
  • Is it currently possible to generate C-Code from 1D CNNs (with or without CMSIS-NN)?
  • If not, how should a workaorund with 2D CNN (with 1D sequence input) look like?
  • If no solution is available for C, would it help to switch to C++ and embed the generated C++ code into the main C project?
Thank you very much!

Accepted Answer

Sayan Saha
Sayan Saha on 12 May 2022
Hi Marisa,
Please find the answers to your questions below:
  1. It is not possible to generate code for 1d CNNs or any other 1d layers currently with the coder products.
  2. In terms of a workaround to use 2d CNNs with 1d sequence data, we have an example that showcases that workflow for audio signals: https://www.mathworks.com/help/deeplearning/ug/deep-learning-speech-recognition.html The main idea is to convert the audio signal to an auditory spectogram image, so that the 2d CNNs can process that image representing the original 1d signal data. Depending on what data you have, a similar approach can be taken to convert the 1d data to equivalent 2d data to feed to 2d CNNs.
I am however curious to know why you want to use 1d CNNs? Does using lstmLayer/bilstmLayer/gruLayer not provide good results? While CMSIS-NN library only supports lstmLayer currently amongst the three mentioned above, you can generate plain C-code without using any libraries utilizing the "none" targetLibrary and deploy the generated code to STM32F427 board. Here is an example for that workflow: https://www.mathworks.com/help/coder/ug/generate-code-for-lstm-network-and-deploy-on-cortex-m.html Library-free codegeneration supports a wider range of layers as compared to CMSIS-NN library. You can invoke coder.getDeepLearningLayers("TargetLibrary", "none") to get the list of all supported layers.
3. It is a typo that mentions "C++" in the build steps of "CMSIS-NN" library. The library only supports "C" code. We will fix the post soon. Thank you for bringing this issue to our attention.
Hope this helps!
  1 Comment
Marisa Ehemann
Marisa Ehemann on 15 May 2022
Tank You very much! Your answer helped me a lot
I am testing different approaches of timeseries classifications suitable for Cortex-M code generation. My goal is to gain a better understanding of the current possibilitys and limitations. The LSTMs actually did show good results after training and were easy to deploy. However, one of the trained 1D networks did outperform the LSTMs in some usecases. I was curious if i could make it run on the STM somehow to improve the performance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!