The printed circuit board (PCB) holds immense importance in the electronic industry as a crucial component for the development of electronic products. PCBs are becoming increasingly integrated and smaller due to the excellent craftsmanship, precise wiring, and rapid development of integrated circuits.
The printed circuit board (PCB) holds immense importance in the electronic industry as a crucial component for the development of electronic products. PCBs are becoming increasingly integrated and smaller 
due to the excellent craftsmanship, precise wiring, and rapid development of integrated circuits. However, with the reduction in size, defects in the PCBs are also getting smaller and more challenging to detect. Therefore, it is imperative to conduct a thorough defect detection process during PCB-related production to improve product quality and reduce company costs.
The conventional methods of detecting defects in PCBs are classified into three categories: manual visual inspection, electrical testing, and optical inspection 
. Manual visual inspection involves workers inspecting bare PCBs directly using their eyes and other equipment. However, this method has become inadequate due to the increasing demand for higher precision in PCB development, as it has poor detection stability and low efficiency. On the other hand, electrical testing employs contact testing to detect defects in bare PCBs, which requires complex testing circuits, expensive molds, and fixtures for each batch of PCBs. This method is also limited in detecting multi-layer PCBs and poses a risk of secondary damage. In contrast, automated optical inspection (AOI) is a non-contact inspection method that uses machine vision technology and image processing algorithms 
. Industrial cameras capture images of the PCBs, which are transmitted to a computer that provides feedback on the defect detection results. AOI is more stable and accurate than the previous methods, with a faster detection speed 
, and does not impact the PCB.
The advancement of deep learning has led to the development of contactless automatic detection methods, which have become a popular area of research due to their strong recognition adaptability and generalization ability. Typically, deep learning-based detection networks can be categorized into one-stage and two-stage networks. The one-stage network includes Single Shot Detector (SSD) 
, and You Only Look Once (YOLO) 
. In contrast, the two-stage network includes regions with convolutional neural networks (R-CNN) 
, Fast R-CNN 
, and Faster R-CNN 
, which is an improved version of R-CNN. The primary difference between these networks is that the one-stage network directly predicts the location and category of defects in the network after feature extraction, while the two-stage network first generates proposals that may include defects, then conducts the detection process. Specifically, the two-stage network generates candidate boxes of different sizes that may contain defect features, then performs target detection to predict defect classes and locations. However, the detection speed is slow due to the generation of many candidate frames. On the other hand, the one-stage network performs both training and detection in a single network without the need for explicit region proposals, resulting in faster detection speed. This paper adopts the one-stage network based on YOLOv7 
and improves it to meet real-time performance requirements in the industrial field.
The Swin Transformer v2 
is designed to overcome three significant challenges in large visual model training and application, namely, model instability, the resolution gap problem, and a chronic lack of labeled data. To address these challenges, the Swin Transformer v2 proposes three primary methods. First, it combines cosine attention and post-normalization to enhance model stability. Second, it introduces a logarithmic space continuous location deviation method, which enables the model to be trained on low-resolution images, then transferred to its higher-resolution counterparts. Lastly, it introduces SimMIM, a self-supervised pretraining method that reduces the need for large amounts of labeled data. To improve the global feature extraction and stability of the model, SwinV2_CSPB modules can replace some ELAN modules in the YOLOv7 backbone network.
2. PCB Defect Based on Improved YOLOv7
The conventional approach for detecting visual anomalies in artificial systems has drawbacks, such as high cost, low efficiency, and errors in detection. As an alternative, the electrical properties of components can be leveraged for detecting defects in printed circuit boards (PCBs) through a semi-automatic, manual detection method that includes online and functional testing 
. Researchers have explored various techniques to enhance this method, such as compressing images using wavelet transform to reduce memory and computation requirements 
, using traditional machine learning algorithms for defect detection 
, and designing low-complexity neural network and machine vision schemes to improve defect detection 
. Other approaches include using Fourier image reconstruction to identify small defects 
and ultrasonic laser thermal imaging for real-time defect detection 
. Although these methods can reduce costs compared to manual detection, their limited application is attributed to factors, such as the non-reusability of the test process, the high cost of equipment, and complex writing functions, among others.
Machine vision detection methods have emerged as a viable solution to overcome the shortcomings of traditional artificial detection methods and are increasingly being applied in modern industries 
. There are three primary categories of PCB defect detection methods based on machine vision: reference, non-reference, and hybrid methods. The reference method 
typically involves image segmentation techniques to detect defects. For example, Li et al. compared PCB images with and without defects to identify defects 
. Non-reference methods 
mainly rely on machine learning algorithms for defect detection. For instance, Malge et al. employed an image segmentation algorithm to detect PCB defects 
. The hybrid method 
combines reference and non-reference methods to achieve more accurate defect detection. For example, Ray et al. developed a hybrid detection method by comparing PCB images and using image segmentation techniques 
. Image segmentation techniques include threshold segmentation, edge segmentation, and region segmentation methods. For example, Ardhy et al. 
used the adaptive Gaussian threshold segmentation to achieve rapid detection with minimal parameters, but the detection efficacy varied significantly in different areas with light strips. Baygin et al. 
used Hough transform for edge segmentation and combined it with the Canny operator to enhance detection efficiency. Ma et al. 
improved the region growth algorithm for region segmentation to achieve better detection outcomes. However, these methods require manual tuning of model parameters, which may lead to suboptimal accuracy and efficiency.
Recent studies have demonstrated that the accuracy of automated optical inspection (AOI) is higher compared to other methods. However, due to the system’s high sensitivity, it has very strict parameter-setting rules and may miss some cases, necessitating manual screening after machine screening is complete 
. Meanwhile, deep learning technology has been rapidly advancing. Target defect detection methods based on deep learning have shown to be highly accurate, fast, and do not require manual screening. Thus, they are more cost-effective and efficient. Moreover, the parameter-setting rules are not as strict as those in the AOI system. As a result, deep learning-based methods are being increasingly studied and applied in various industries.
Due to advancements in computing technology, complex operations have become more affordable, resulting in the rapid development of neural networks, including a large number of deep neural networks. In the field of PCB defect detection, many scholars have applied deep learning techniques. DenseNet 
achieved better performance with fewer parameters and computing costs by densely connecting all front and back layers to enable feature reuse. Huang et al. 
improved detection accuracy and efficiency by designing a convolutional neural network that connects each layer in a feedforward manner. Compared to conventional machine vision methods, deep learning algorithms have stronger nonlinear abilities, higher robustness, and are applicable to more complex scenarios. He 
proposed an improvement measure that helped achieve a 96.91% accuracy rate. Geng et al. 
improved the detection accuracy to 96.65% by using focal loss and ResNet50 as the backbone network. Ding et al. 
designed TDD-net, a detection network specifically aimed at tiny PCB defects, which adopted a multi-scale fusion strategy and applied online hard example mining to enhance the certainty of ROI proposals, resulting in a detection accuracy of 98.90%. Sun et al. 
proposed the Inception-ResNet-v2 model, which improved the PCB detection accuracy by adding an SE module to part of the structure. Hu et al. 
presented UF-Net, which retained more defect target information by using the Skip Connect method and achieved a detection accuracy of 98.6%. Li et al. 
improved the mAP
value to 98.71% by replacing the convolution layer in the trunk with the residual structure unit CSP based on the YOLOv4 algorithm. Wang et al. 
proposed a lightweight model that used the ShuffleNetV2 structure in the YOLOv5 backbone and achieved an accuracy of 95%. YOLOv7, as a classic representative of the target detection algorithm, has surpassed the previous YOLO series in detection speed and accuracy.