Generate Generic C/C++ Code for Deep Learning Networks in Simulink
从R2021A开始,您可以使用Simulink编码器万博1manbetx和嵌入式编码器来生成通用ANSI/ISO兼容C和C ++代码,而不受对第三方深度学习库的依赖,用于包含深度学习网络的模型。将卷积和经常性的神经网络纳入Simulink,并通过深度学习工具箱的块,包括状态预测和状态分类块。万博1manbetx将预处理和后处理应用于时间序列数据,以在Simulink模型中使用。万博1manbetx使用嵌入式编码器的软件(SIL)和处理器(PIL)测试支持,验证生成代码的数值等效性。万博1manbetx将生成的代码部署到可以编译C ++的任何处理器,包括ARM Cortex-M处理器,数字信号处理器,微控制器等。例如,将长期的短期内存(LSTM)网络部署到STM32F7发现板上,并使用嵌入式编码器硬件支持软件包,用于Stmicroelectronics Discovery Boards。万博1manbetx
As of R2021a, you can now use Simulink Coder and Embedded Coder to generate generic C and C++ code from Simulink models containing deep neural networks. The generic C and C++ code does not depend on third-party libraries and, as a result, can be deployed to any embedded processor that compiles C and C++ code, including ARM Cortex-M series processors, DSPs, and microcontrollers from a range of device vendors.
可以为Simulink中的卷积和经常性深学习神经网络生成通用C和C ++代码。万博1manbetx您可以将网络对象与深度学习工具箱中的块合并到Simulink模型中,包括在R2021A中或M万博1manbetxATLAB功能块中引入的状态分类和状态预测块。为通用C和C ++代码生成支持各种网络和层。万博1manbetx完整列表可以在我们的文档中找到。
Now let's deploy a deep learning network from Simulink to an ARM Cortex-M processor. In this example, we'll use an LSTM network to predict the remaining useful life of turbofan engines. The pre-trained network accepts sequence data from 17 engine sensors and outputs a prediction of the engines' remaining useful life, measured in cycles. The time series input data is fed to the model with a firm workspace block and is then sent to the predict neural network block.
使用Deep Network Designer应用程序,我们可以更深入地了解网络,并看到它包含六层,包括LSTM层。让我们在Simulink中模拟该模型。万博1manbetx从模拟中,我们可以看到,预先训练的神经网络预测了此处显示的每个观测值中涡轮扇引擎的剩余使用寿命。预测值非常遵循实际值,均方根误差为20.37。
现在,让我们从模型生成通用C代码,并将其部署到ARM Cortex-M处理器中。在配置参数中,我们将选择STM32F746G - 发现板作为我们的硬件板。在代码生成下,我们将系统目标文件设置为使用嵌入式编码器和目标语言。最后,我们将确保将深度学习目标库设置为无需删除第三方库的任何依赖项。有了这些设置,让我们为包含神经网络的子系统生成代码。
In the code-generation report, we can see the files generated do not include any external deep learning libraries. Let's search for the model step function in the generated code. Looking at the step function, we can see that it contains a predict method. Inside the predict method, we can see a set of weights and biases defined for use in the neural network. Scrolling down, we can see the code used to calculate the outputs of the neural net for the time series inputs passed in at each time step.
通过成功生成的代码,让我们将其部署到我们的目标。我们将使用处理器循环执行以数值验证生成的代码的输出。在第二个模型中,我们将相同的预测块放置在模型参考中的内部,并将其仿真模式设置为pil。在SIL/PIL Manager应用程序中,我们将设置仿真模式以SIL/PIL仅用于收集使用PIL执行的生成代码的结果,并在SIL/PIL模式下选择模型块作为我们的系统测试。
Post-simulation, we'll compare the prediction outputs from running the generic C code on hardware to those from Simulink. Now let's run the generated code. Code is first generated for the model reference block in PIL mode. A connection is established with our Cortex-M Discovery board. And finally, the source code is built and downloaded to it.
左图集显示了在硬件上运行生成的代码的结果,遵循实际的测试数据。为了验证代码的数值准确性,我们在右侧创建了第二组图,以显示预测的差异,同时在Simulink中运行仿真,并在目标硬件上以PIL模式下。万博1manbetx所有10个观察结果都表明,偏差约为500,000分之一,对神经网络的准确性有忽略不计。
Overall, we've seen how it's now possible to generate generic C and C++ code from deep learning networks in Simulink that do not depend on optimization libraries, effectively extending its use to nearly any embedded processor. Additionally, with processor in the loop testing, we verified that the output from the generated code matches that from the Simulink simulation. To learn more about deep learning code generation and MATLAB in Simulink, please click one of the links below or refer to our documentation.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
您还可以从以下列表中选择一个网站:
如何获得最佳网站性能
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- AméricaLatina(Español)
- Canada(English)
- United States(English)
欧洲
Asia Pacific
- Australia(English)
- India(English)
- 新西兰(English)
- 中国
- 日本Japanese(日本语)
- 한국Korean(한국어)