Code Generation for a Deep Learning Simulink Model that Detect Defects on Printed Circuit Boards Using YOLOX Network
This example shows how to generate code for a You Only Look Once X (YOLOX) object detector that can detect, localize, and classify defects in printed circuit boards (PCBs).
The example first simulates the PCB defect detection model in Simulink® by using MATLAB Function blocks to implement the YOLOX object detector. The simulation takes advantage of the NVIDIA® TensorRT™ high performance inference library for NVIDIA GPUs. Then the example demostrates how to generate plain CUDA code that is independent of third party libraries for the deep learning Simulink model. The generated code offers greater portability, easier maintenance, improved adaptability to different environments, and fewer compatibility issues.
This example uses the pretrained YOLOX network from the Detect Defects on Printed Circuit Boards Using YOLOX Network example from the Computer Vision Toolbox™. For more information, see Detect Defects on Printed Circuit Boards Using YOLOX Network (Computer Vision Toolbox). To learn more about YOLOX, see Getting Started with YOLOX for Object Detection (Computer Vision Toolbox).
Third-Party Prerequisites
The list shows the hardware and software prerequisites for the MEX function generated in this example. For non-MEX builds such as static, dynamic libraries or executables, this example has additional requirements.
CUDA-enabled NVIDIA® GPU.
NVIDIA CUDA toolkit and driver.
Environment variables for the compilers and libraries. For more information, see Third-Party Hardware and Setting Up the Prerequisite Products (optional).
Download Pretrained YOLOX Detector
This example uses a pretrained version of the YOLOX object detector. The file is approximately 19MB in size. Download the files from the MathWorks website.
trainedPCBDefectDetectorNet_url = "https://ssd.mathworks.com/supportfiles/"+ ... "vision/data/trainedPCBDefectDetectorYOLOX.zip"; downloadTrainedNetwork(trainedPCBDefectDetectorNet_url,pwd); matFile = "trainedPCBDefectDetectorYOLOX.mat"; load(matFile);
YOLOX Detector
The YOLOX object detector [1] uses CSP-DarkNet-53 as the base network and is retrained using the PCB defect data set [2][3]. The YOLOX network can detect six types of defect: missing hole, mouse bite, open circuit, short, spur, and spurious copper.
disp(detector);
yoloxObjectDetector with properties: ClassNames: {6×1 cell} InputSize: [800 800 3] NormalizationStatistics: [1×1 struct] ModelName: 'tiny-coco'
The yoloXDetect Entry-Point Function
The yoloXDetect
entry-point function runs the detector on an input image by using the deep learning network in the trainedPCBDefectDetectorYOLOX.mat
file. The entry-point function performs these operations:
Define a persistent variable called
myDetector
. The persistent variable prevents reconstructing and reloading the network object during subsequent calls to theyoloXDetect
function.Load the detector in the file
trainedYoloxObjectDetector.mat
into themyDetector
variable using thevision.loadYOLOXObjectDetector
(Computer Vision Toolbox) function.Detect object bounding boxes, scores, and labels on the input test image
img
, using the detector.
type("yoloXDetect.m")
function [bboxes,scores,labels] = yoloXDetect(image,matFile) % Copyright 2023 The MathWorks, Inc. persistent myDetector; if isempty(myDetector) myDetector = vision.loadYOLOXObjectDetector(matFile); end % Call to detect method [bboxes,scores,labels] = detect(myDetector,image,Threshold = 0.6, ... AutoResize = 1);
Evaluate yoloXDetect Entry-Point Function
Evaluate the yoloXDetect
entry-point function and display the results. By default, the example uses the 01_missing_hole_01.jpg
as the sample image. This image has missing holes on the PCB.
sampleImage = imread("01_missing_hole_01.jpg"); sampleImage = imresize(sampleImage,[400 800]); [bboxes,~,labels] = yoloXDetect(sampleImage,matFile); labels = cellstr(labels); defectsImage = insertObjectAnnotation(sampleImage,"rectangle",bboxes,labels); imshow(defectsImage) title("Predicted Defects - MATLAB Simulation")
YOLOX Defect Detection Simulink Model
The Simulink® model for performing PCB defect detection is shown. The YOLOX PCB Defect Detector
MATLAB Function block uses the vision.loadYOLOXObjectDetector
(Computer Vision Toolbox) function to load the pretrained YOLOX deep learning network. The MATLAB Function block calls the detect method to predict the bounding boxes, labels, and class-specific confidence scores. When the model runs, the Video Viewer
block displays the input image along with the detected defects.
open_system("yolox_pcb_defect_detector");
Customize Configuration Parameters for Simulation
Open Configuration Parameters dialog box. In Simulation Target pane, select GPU acceleration. In the Deep Learning group, select the target library as TensorRT
. You can also use the MATLAB commands:
set_param("yolox_pcb_defect_detector","GPUAcceleration","on"); set_param("yolox_pcb_defect_detector","SimDLTargetLibrary","tensorrt");
Run the Simulation
To build and simulate the yolox_pcb_defect_detector
model, select Run on the Simulate tab or use the MATLAB command:
out = sim("yolox_pcb_defect_detector");
Customize Configuration Parameters for Code Generation
For code generation, open Configuration Parameters dialog box, in Code Generation pane, select the Language as C++ and enable Generate GPU code. Then, in the Interface settings, in the Deep Learning group, set Deep learning library to None
. In the Code interface group, set Multi-instance code error diagnostic to None
. You can also use the MATLAB commands:
set_param("yolox_pcb_defect_detector","GenerateGPUCode","CUDA"); set_param("yolox_pcb_defect_detector", "DLTargetLibrary","none"); set_param("yolox_pcb_defect_detector","MultiInstanceErrorCode","None")
You can write deep learning constants to their own data files during code generation and control the minimum size of the deep learning constants. In Code Generation pane, in the Advanced parameters group, set Large constant generation in MATLAB functions to Write only deep learning constatns to data files
, and Large constant threshold in MATLAB functions (bytes) to 64
. You can also use the MATLAB commands:
set_param("yolox_pcb_defect_detector","LargeConstantGeneration","WriteOnlyDNNConstantsToDataFiles"); set_param("yolox_pcb_defect_detector","LargeConstantThreshold",64);
In Hardware Implementation pane, set the production hardware device type. Then select Support long long to enable long long data type for simulation and code generation on the hardware. You can also use the MATLAB commands:
if ispc set_param("yolox_pcb_defect_detector","ProdHWDeviceType","Intel->x86-64 (Windows64)"); else set_param("yolox_pcb_defect_detector","ProdHWDeviceType","Intel->x86-64 (Linux 64)"); end set_param("yolox_pcb_defect_detector","ProdLongLongMode","on");
Build the Simulink Model
Build the Simulink model on the host GPU by using the slbuild
command or by pressing Ctrl+B. The code generator places the files in a build folder, a subfolder named yolox_pcb_defect_detector_grt_rtw
under your current working folder.
slbuild('yolox_pcb_defect_detector');
References
[1] Ge, Zheng, Songtao Liu, Feng Wang, Zeming Li, and Jian Sun. "YOLOX: Exceeding YOLO Series in 2021", arXiv, August 6, 2021. https://arxiv.org/abs/2107.08430.
[2] Huang, Weibo, and Peng Wei. "A PCB Dataset for Defects Detection and Classification." Preprint, submitted January 23, 2019. https://arxiv.org/abs/1901.08204.
[3] PCB-DATASET. Accessed December 20, 2022. https://github.com/Ironbrotherstyle/PCB-DATASET.
See Also
Functions
Objects
coder.gpuConfig
|coder.gpuEnvConfig
|coder.CuDNNConfig
|vision.VideoFileReader
(Computer Vision Toolbox) |vision.DeployableVideoPlayer
(Computer Vision Toolbox) |yolov4ObjectDetector
(Computer Vision Toolbox)
Related Examples
- Object Detection Using YOLO v4 Deep Learning (Computer Vision Toolbox)
- Code Generation for Object Detection by Using YOLO v2
- Code Generation for Object Detection Using YOLO v3 Deep Learning Network
More About
- Getting Started with YOLO v4 (Computer Vision Toolbox)