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 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

  • CUDA-enabled NVIDIA® GPU (required).

  • NVIDIA CUDA toolkit and driver (optional).

  • NVIDIA TensorRT library (optional).

  • Environment variables for the compilers and libraries. For more information, see Third-Party Hardware (GPU Coder) and Setting Up the Prerequisite Products (GPU Coder) (optional).

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);
Downloading pretrained network.
This can take several minutes to download...
Done.
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 (GPU Coder) 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")

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