Main Content

Code Generation for Detect Defects on Printed Circuit Boards Using YOLOX Network

Since R2023b

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 generates a MEX function that runs on Intel® CPUs and uses the Intel Math Kernel Library for Deep Neural Networks (MKL-DNN). If you have a GPU Coder™ license, you can also generate CUDA MEX for the YOLOX detector network. The generated CUDA MEX takes advantage of the NVIDIA® TensorRT™ high performance inference library for NVIDIA GPUs.

This example also demonstrates PCB defect detection in Simulink® by using MATLAB Function blocks to implment the YOLOX object detector. Simulation and code generation for deep learning networks in Simulink requires a Simulink Coder™ license.

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.

CPU Deployment

  • Intel processor with support for Intel Advanced Vector Extensions 2 (Intel AVX2) instructions (required).

  • Intel Math Kernel Library for Deep Neural Networks (MKL-DNN) (optional).

  • For information on the supported versions of the compilers and libraries, see Prerequisites for Deep Learning with MATLAB Coder (optional).

GPU Deployment

Note: For non-MEX builds, all optional requirements must be satisfied.

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 the yoloXDetect function.

  • Load the detector in the file trainedYoloxObjectDetector.mat into the myDetector variable using the vision.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")

Generate MEX Function

To generate a MEX function for the yoloXDetect entry-point function, create a code configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a MKL-DNN deep learning configuration object and assign it to the DeepLearningConfig property of the code configuration object.

cfg = coder.config("mex");
cfg.TargetLang = "C++";
cfg.DeepLearningConfig = coder.DeepLearningConfig("mkldnn");
inputArgs = {coder.typeof(uint8(0),[inf inf 3],[1 1 0]),coder.Constant(matFile)};
codegen -config cfg -args inputArgs yoloXDetect -report
Code generation successful: View report

Perform PCB Defect Detection on CPU

Predict the bounding boxes, labels, and class-specific confidence scores in the test image by calling the code generated mex file yoloXDetect_mex.

[bboxes,~,labels] = yoloXDetect_mex(sampleImage,matFile);

Display the results.

labels = cellstr(labels);
defectsImage = insertObjectAnnotation(sampleImage,"rectangle",bboxes,labels);
imshow(defectsImage)
title("Predicted Defects - CPU MEX")

Generate CUDA MEX

To generate CUDA code for the yoloXDetect entry-point function, create a GPU code configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a TensorRT deep learning configuration object and assign it to the DeepLearningConfig property of the GPU code configuration object.

cfg = coder.gpuConfig("mex");
cfg.TargetLang = "C++";
cfg.DeepLearningConfig = coder.DeepLearningConfig("tensorrt");
inputArgs = {coder.typeof(uint8(0),[inf inf 3],[1 1 0]),coder.Constant(matFile)};
codegen -config cfg -args inputArgs yoloXDetect -report
Code generation successful: View report

Perform PCB Defect Detection on GPU

Predict the bounding boxes, labels, and class-specific confidence scores in the test image by calling the code generated MEX-file yoloXDetect_mex.

[bboxes,~,labels] = yoloXDetect_mex(sampleImage,matFile);

Display the results.

labels = cellstr(labels);
defectsImage = insertObjectAnnotation(sampleImage,"rectangle",bboxes,labels);
imshow(defectsImage)
title("Predicted Defects - CUDA MEX")

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.

The Simulink model also requires the Simulink Coder™ product for simulation and code generation.

open_system("yolox_pcb_defect_detector");

To build and simulate the yolox_pcb_defect_detector model, select Run on the Simulation tab or use the MATLAB command:

out = sim("yolox_pcb_defect_detector");

By default, the Simulink model is configured for CPUs using the Intel MKL-DNN deep learning libraries. To use GPU for simulation and code generation, use the following commands:

set_param("yolox_pcb_defect_detector","GPUAcceleration","on");
set_param("yolox_pcb_defect_detector","SimDLTargetLibrary","tensorrt");
set_param("yolox_pcb_defect_detector","GenerateGPUCode","CUDA");
set_param("yolox_pcb_defect_detector", "DLTargetLibrary","tensorrt");

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

Related Examples

More About