Deep Learning in Simulink
With MATLAB® R2020b, you can use the Deep Learning Toolbox™ block library as well as MATLAB Function block to simulate and generate code from trained deep learning models in Simulink®.
See how you can simulate deep learning networks in Simulink with control, signal processing, and sensor fusion components to assess the impact of your deep learning model on system-level performance. Learn more about using NVIDIA® GPUs to speed up the execution of your deep learning network model in Simulink.
Published: 3 Sep 2020
As of R2020b release of MATLAB®, you can use the MATLAB Function block as well as the Deep Learning Toolbox™ block library to simulate and generate code from trained deep learning models in Simulink®. For example, to design a highway lane following system, you can use the deep learning blocks to create a Simulink subsystem that performs lane and vehicle detection, integrate this subsystem with a larger Simulink model that includes additional components such as the vehicle dynamics model, the lane following controller, sensor fusion and 3D visualization, and verify performance of the overall design through system-level simulation before deployment. Let’s see how we can create a subsystem that performs vehicle and lane detection in Simulink. The first thing we will need is a C++ compiler. We will also need the support packages that provide interfaces from MATLAB Coder™ and GPU coder™ to target-specific deep learning libraries. Finally, we assume that we have a pretrained lane detection network as well as a pretrained yolov2 vehicle detector stored in MATLAB files. In the Simulink model, we are reading from a traffic video file, and after the vehicle and lane detection parts we are displaying the traffic video again with lane and vehicle annotations. For lane detection, first we are resizing the video frames to match the input expected by the trained lane detection network. Next we will use the ‘Predict’ block from the deep learning block library to perform inference on the trained network. To link the block to the lane detector object we are providing the path to the appropriate MATLAB file. Another option here would be to use a MATLAB function. The predict block will output two lane boundaries represented by a parabolic equation with 3 parameters that are then transformed into lanes in image coordinates. For vehicle detection, we will use a MATLAB Function block to perform inference on the pretrained yolov2 vehicle detector. Inside the MATLAB Function block we will load the pretrained network, and call the detect method to get the bounding boxes and associated confidence scores. Next, we will specify the dimensions of the block outputs. The MATLAB Function block can also be used with other types of networks like LSTMs, and you can also use it to output activations from specific layers of the network. The last thing we need to do before we run the simulation, is to set the simulation target language to C++ in the model settings. Clicking the Run button will under the hood generate code from the Predict and MATLAB Function blocks and compile it for CPU-accelerated simulation. The output of the simulation displays the annotated traffic video as expected. Finally, you can also use GPU Coder with NVIDIA® GPUs to accelerate simulation of deep learning models in Simulink. To configure the model for GPU Acceleration, check the ‘GPU Acceleration’ box under Simulation Target in the Model Settings. Once the system design is verified in simulation, you can generate code from the Simulink model for deployment. You can use Simulink Coder™, Embedded Coder® or GPU Coder to generate C++ or CUDA code and deploy deep learning networks on Intel®, ARM®, or NVIDIA platforms.