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

11 次查看(过去 30 天)
Hello,
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!
Marisa

采纳的回答

Sayan Saha
Sayan Saha 2022-5-12
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!
~Sayan
  1 个评论
Marisa Ehemann
Marisa Ehemann 2022-5-15
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by