
Many applications of machine learning and reinforcement learning approaches require a transfer of the trained neural network to the embedded software framework of the target control unit as a final step. Usually, the training is conducted in a specialized training environment such as PyTorch, Tensorflow, Julia or similar. Each environment has its own representation of the trainable models and as long as the training and evaluation is ongoing, the user does not need to know the exact internal structures. This situation changes, when the training is completed and the trained neural networks needs to be transferred to a testing platform, such as a rapid prototyping system or an embedded control unit.
We developed a python-package for this purpose, that implements an intuitive, layer-based representation of neural networks. This representation can then be saved in a mat- or json-file and then be archived or exchanged with other developers. The most important feature of the package is however a code generator, that generates native C-code to represent the neural network. This C-code serves as a basis for several applications. In simulation tools such as Simulink, the exported neural networks can then be validated inside the embedding control algorithms. Furthermore, the networks as such or within the embedding algorithm can be compiled into a Functional-Mockup-Unit (FMU) , e.g. to share it with users without disclosing the source-code. Finally, Simulink (among others) enables the code-generation for real-time targets, thus completing the transfer to the real-world system.
The graphic below depicts an exemplary application of the described export-toolchain. In this case, the training was conducted using Stable-Baselines3 (Sb3), a reinforcement learning library based on PyTorch. The corresponding Sb3-Adapter transfers the specific representation to the developed simplistic representation. The generated C-code is then automatically compiled into a S-Function and as such ready to use in Simulink.
For the usage of different sources or even standardized representations such as Open Neural Network Exchange Format (ONNX), only the corresponding adapter must be implemented in python, which is usually straightforward.