CoolFace
Apppublic

mcthebest/PCB_RTDETR

sourceHugging Faceapache-2.0updated 5mo agoView on Hugging Face
1likes
App README

PCB Defect Detection — RT-DETRv4 X on DsPCBSD+

RT-DETRv4 X fine-tuned on the DsPCBSD+ dataset for 9-class copper-layer defect detection.

Live Demo

Try it in your browser without any setup:

[https://huggingface.co/spaces/mcthebest/PCB_RTDETR](https://huggingface.co/spaces/mcthebest/PCB_RTDETR)

Upload your own PCB image or pick from 29 built-in test images. The confidence threshold is adjustable in the sidebar.

Model

Architecture: RT-DETRv4 X

RT-DETRv4 builds on the RT-DETR lineage (v1, v2, v3) with ideas from D-FINE and DEIM. The main addition is a semantic distillation framework that uses a frozen DINOv3 teacher during training only, adding no cost at inference.

ComponentDetail
BackboneHGNetv2-X, multi-scale features at stride 8/16/32
EncoderEfficient Hybrid Encoder: AIFI (global self-attention on S5) + CCFF (CNN cross-scale fusion)
DecoderDFINETransformer, 6 layers, 300 queries, 32-bin probabilistic box regression per edge
Teacher (train only)DINOv3 ViT-B/16 (frozen), trained on LVD-1689M (~1.7B images)
DSI moduleAligns F5 features with DINOv3 semantics via cosine similarity loss
GAM moduleAdjusts DSI loss weight per epoch based on gradient norms

Training loss:

L_total = L_det + λ · L_DSI

L_det includes VFL, L1, GIoU, FGL, DDF, and MAL losses from D-FINE and DEIM. DINOv3 is not loaded at inference.

Training Configuration

ParameterValue
DatasetDsPCBSD+ (8,208 train / 2,051 val, 80-20 split)
Pretrained weightsRT-DETRv4-X COCO + DINOv3 ViT-B/16 (LVD-142M)
Input resolution640 x 640 px
Epochs10 (72 recommended; limited by Colab T4)
Batch size8
OptimizerAdamW (lr encoder/decoder = 2e-4, lr backbone = 1e-5, wd = 1e-4)
LR SchedulerFlatCosine (warmup 500 iter, flat 5 epoch, no-aug 2 epoch)
Mixed precisionAMP FP16/FP32
HardwareNVIDIA Tesla T4 (16 GB VRAM), Google Colab

Mosaic and MixUp augmentation were disabled due to memory constraints. Multi-scale training (480-800 px, 11 levels) was kept.

Results on DsPCBSD+ Validation Set

Overall COCO Metrics

MetricValue
mAP @ IoU=0.500.863
mAP @ IoU=0.50:0.950.522
mAP @ IoU=0.750.551
AP small (<32^2 px)0.439
AP medium0.602
AP large (>96^2 px)0.754
AR @ maxDets=1000.686

Per-Class AP @ IoU=0.50:0.95

ClassFull NameAP
SHShort Circuit0.597
SPSpur (Copper Spike)0.381
SCSpurious Copper0.511
OPOpen Circuit0.555
MBMouse Bite0.408
HBHole Breakout0.807
CSConductor Scratch0.504
CFOConductor Foreign Object0.462
BMFOBase Material Foreign Object0.478
Mean0.522

Per-Class F1 @ Conf>=0.5, IoU>=0.5

ClassPrecisionRecallF1
SH0.910.860.88
SP0.770.690.73
SC0.890.770.83
OP0.850.840.84
MB0.870.690.77
HB0.910.930.92
CS0.820.650.73
CFO0.860.560.68
BMFO0.870.830.85
Mean0.802

Comparison with SOTA on DsPCBSD+

ModelBackbonemAP@50mAP@50:95Notes
YOLOv11-CGLYOLOv11n84.5%51.6%300 epochs, lightweight
PCB-AMYOLOv8s85.7%N/AAttention-guided modules
PCB-FSYOLOv886.2%52.4%Frequency-spatial features
RT-DETRv4 X (ours)HGNetv2-X + DINOv386.3%52.25%10 epochs, T4 only

RT-DETRv4 X matches or exceeds all compared methods at only 10 epochs on a single T4. PCB-FS, for comparison, used 100+ epochs with more resources.

Dataset: DsPCBSD+

DsPCBSD+ is a 2024 open dataset for PCB copper-layer defect detection, captured by a professional AOI system (AGLE'OL AOI-100 V8, 16K camera, controlled LED lighting). Nine defect categories, annotated at instance level:

CodeDefectDescription
SHShortConductive bridge between two traces
SPSpurAnomalous copper spike from a trace
SCSpurious CopperUnwanted copper on board surface
OPOpen CircuitBreak in a conductive trace
MBMouse BiteEdge notch or chip in the trace
HBHole BreakoutDamage to material around a drill hole
CSConductor ScratchScratch mark on a conductive trace
CFOConductor Foreign ObjectForeign particle on a trace
BMFOBase Material Foreign ObjectForeign particle in substrate material
S. Lv et al., "A dataset for deep learning based detection of printed circuit board surface defect," Scientific Data, vol. 11, no. 1, p. 811, 2024. https://doi.org/10.1038/s41597-024-03656-8

Files

FileDescription
last.pthFine-tuned checkpoint (EMA weights)
test_image/29 example PCB images for inference testing

The training code, config, and Streamlit app are in the companion Space: https://huggingface.co/spaces/mcthebest/PCB_RTDETR

Running Locally

Clone the Space and install dependencies:

bash
git clone https://huggingface.co/spaces/mcthebest/PCB_RTDETR
cd PCB_RTDETR
pip install -r requirements.txt

Download the checkpoint:

python
from huggingface_hub import hf_hub_download

ckpt_path = hf_hub_download(
    repo_id="mcthebest/PCB_RTDETR",
    repo_type="model",
    filename="last.pth",
)

Or place it manually at outputs/rtv4_hgnetv2_x_pcb/last.pth, then run:

bash
streamlit run app.py

How Inference Works

The image is resized to 640x640 and passed through RT-DETRv4 X. The decoder outputs 300 candidate queries which the postprocessor filters by confidence threshold (default 0.30). Detections are returned as (labels, boxes, scores) and drawn with labeled bounding boxes. DINOv3 is not loaded at inference.

Known Limitations

  • Small defects (AP=0.439): sub-32px defects like SP (Spur) are the hardest class.
  • Only 10 epochs were run vs. the recommended 72. The learning curve had not plateaued, so more training should meaningfully improve mAP@50:95.
  • The dataset was collected under controlled AOI conditions. Images from different lighting or camera setups may need domain adaptation.
  • AR@100=0.686, meaning roughly 31% of defects are missed. Not production-ready for zero-miss QC pipelines.

References