Generate Code for 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 on printed circuit boards (PCBs).
The example uses a model that detects PCB defects by using the MATLAB Function blocks to implement a YOLOX object detector. The simulation takes advantage of the NVIDIA® TensorRT™ high performance inference library for NVIDIA GPUs. Then you generate plain CUDA code that is independent of third party libraries for the deep learning Simulink® model. Plain CUDA code offers greater portability, easier maintenance, improved adaptability to different environments, and fewer compatibility issues compared to code that depends on libraries.
This example uses the pretrained YOLOX network from Detect Defects on Printed Circuit Boards Using YOLOX Network (Computer Vision Toolbox) example. 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 this example.
CUDA-enabled NVIDIA® GPU.
NVIDIA CUDA toolkit and driver.
NVIDIA TensorRT library.
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);
Examine the YOLOX Object 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 defects: missing holes, mouse bites, open circuits, shorts, spurs, 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:
Defines a persistent variable called
myDetector
. The persistent variable prevents subsequent calls to theyoloXDetect
function from reconstructing and reloading the network object.Loads the detector in the file
trainedYoloxObjectDetector.mat
into themyDetector
variable using thevision.loadYOLOXObjectDetector
(Computer Vision Toolbox) function.Uses the detector to detect object bounding boxes, scores, and labels on the input test image
img
.
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 the 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")
Examine the YOLOX Defect Detection Simulink Model
Open the yolox_pcb_defect_detector
model, which performs PCB defect detection. 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 To Video Display
block displays the input image and the detected defects.
open_system("yolox_pcb_defect_detector");
Customize Configuration Parameters for Simulation
You can use our desktop and video GPU to accelerate the simulation. Open Configuration Parameters dialog box. In Simulation Target pane, select GPU acceleration. In the Deep learning section, set Deep learning library to TensorRT
that takes advantage of the NVIDIA TensorRT high performance deep learning inference optimizer and run-time library. Alternatively, set the parameters using these 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, simulate the model or enter:
out = sim("yolox_pcb_defect_detector");
Customize Configuration Parameters for Code Generation
Open the Configuration Parameters dialog box. In the Code Generation pane, set Language to C++
and enable Generate GPU code. Then, in the Code Generation > Interface pane, in the Deep learning section, set Deep learning library to None
. Generating plain CUDA code that is independent of third party libraries increases portability and flexibility, allowing the code to be adapted to various environments. In the Code interface section, set Multi-instance code error diagnostic to None
.
Alternatively, set the parameters using these 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 section, 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
. Alternatively, set the parameters using these 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 to your hardware board. This example sets Device vendor to Intel
and Device type to x86x64 (Linux 64)
. Then, in the Device details section, enable long long data types for simulation and code generation by selecting Support long long. 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 or by clicking Update Model on the Modeling tab. The code generator places the files in a build folder 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 5, 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)
Topics
- 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
- Getting Started with YOLO v4 (Computer Vision Toolbox)