A modified YOLOv5 architecture with an illumination-based 3D branch for robust PCB defect detection under inconsistent lighting conditions.
- Illumination-Depth Fusion: Novel multi-modal architecture combining RGB images with depth and illumination maps
- Edge-Compatible: Designed for deployment on edge devices with limited resources
- Standalone Training: Complete training pipeline without Ultralytics dependency
- Pretrained Transfer: Easy weight transfer from YOLOv5s pretrained models
- Comprehensive Metrics: mAP, precision, recall, F1, and confusion matrix visualization
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β YOLOv5-3D Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββ βββββββββββββ βββββββββββββ β
β β RGB β βIlluminationβ β Depth β β
β β (3 ch) β β (3 ch) β β (1 ch) β β
β βββββββ¬ββββββ βββββββ¬ββββββ βββββββ¬ββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββ βββββββββββββββββββββββββββββ β
β β Backbone β β Illumination-Depth Branch β β
β β(YOLOv5s) β β (Lightweight Encoder) β β
β βββββββ¬ββββββ βββββββββββββββ¬ββββββββββββββ β
β β β β
β ββββββββββββ¬ββββββββββββ β
β βΌ β
β βββββββββββββββββββββββ β
β β Multi-Modal Fusion β β
β β (Cross-Attention) β β
β ββββββββββββ¬βββββββββββ β
β βΌ β
β βββββββββββββββββββββββ β
β β Neck (FPN+PAN) β β
β ββββββββββββ¬βββββββββββ β
β βΌ β
β βββββββββββββββββββββββ β
β β Detection Head β β
β β (Multi-scale) β β
β βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
yolov5_3d/
βββ models/
β βββ common.py # Core building blocks (Conv, C3, SPPF, etc.)
β βββ yolo.py # YOLOv5-3D model definition
β βββ yolo5_3d.yaml # Model architecture config
β
βββ utils/
β βββ datasets.py # Custom PCB dataset loader
β βββ loss.py # Loss functions (CIoU, BCE)
β βββ metrics.py # mAP, precision, recall, F1
β βββ plots.py # Training curves, confusion matrix
β βββ general.py # General utilities
β
βββ data/
β βββ pcb.yaml # Dataset configuration
β βββ hyp.scratch.yaml # Hyperparameters
β
βββ scripts/
β βββ transfer_weights.py # Transfer YOLOv5 weights
β
βββ weights/ # Model weights
βββ runs/ # Training outputs
β
βββ train.py # Main training script
βββ val.py # Validation script
βββ detect.py # Inference script
βββ export.py # Export to ONNX/TFLite
βββ requirements.txt # Dependencies
# Clone or download the project
cd yolov5_3d
# Install dependencies
pip install -r requirements.txtYour dataset should be organized as follows:
data/
βββ train/
β βββ images/ # RGB images (.jpg, .png)
β βββ labels/ # YOLO format labels (.txt)
β βββ depth/ # Depth maps (.npy) [optional]
β βββ illumination/ # Illumination maps (.npy) [optional]
βββ valid/
β βββ images/
β βββ labels/
β βββ depth/
β βββ illumination/
βββ test/
β βββ ...
βββ classes.txt # Class names (one per line)
Label format (YOLO):
class_id x_center y_center width height
Numpy file formats:
- Depth:
(H, W)or(H, W, 1)- single channel - Illumination:
(H, W, 3)or(3, H, W)- three channels
# Basic training
python train.py --data /path/to/data --epochs 100 --batch-size 8 --imgsz 640
# With pretrained weights
python train.py --data /path/to/data --weights yolov5s.pt --pretrained
# With frozen backbone (transfer learning)
python train.py --data /path/to/data --weights yolov5s.pt --freeze-backbonepython val.py --weights runs/train/exp/best.pt --data /path/to/data# Single image
python detect.py --source image.jpg --weights best.pt
# With depth and illumination
python detect.py --source images/ --weights best.pt \
--depth-dir depth/ --illum-dir illumination/
# Directory
python detect.py --source /path/to/images --weights best.pt --save-txt# Export to ONNX
python export.py --weights best.pt --include onnx
# Export to multiple formats
python export.py --weights best.pt --include onnx torchscript tflite| Parameter | Default | Description |
|---|---|---|
--epochs |
100 | Number of training epochs |
--batch-size |
8 | Batch size |
--imgsz |
640 | Image size |
--lr0 |
0.01 | Initial learning rate |
--lrf |
0.01 | Final learning rate multiplier |
--optimizer |
AdamW | Optimizer (SGD or AdamW) |
--freeze-backbone |
True | Freeze backbone layers |
--amp |
True | Automatic mixed precision |
For limited GPU memory (4GB):
- Use
--batch-size 4or lower - Enable
--ampfor mixed precision - Use gradient accumulation:
--gradient-accumulation-steps 2
The training generates:
-
Training Curves (
training_curves.png)- Loss over epochs
- mAP@0.5 and mAP@0.5:0.95
- Precision and Recall
-
Confusion Matrix (
confusion_matrix.png)- Normalized detection results per class
-
Results CSV (
results.csv)- Complete training history
Transfer weights from pretrained YOLOv5s:
# Download YOLOv5s weights
wget https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt
# Transfer to YOLOv5-3D
python scripts/transfer_weights.py --weights yolov5s.pt --output weights/yolov5_3d_transferred.ptpython export.py --weights best.pt --include onnx --simplify# Requires TensorRT installation
python export.py --weights best.pt --include tensorrt --fp16python export.py --weights best.pt --include tflite --quantize| Model | Parameters | mAP@0.5 | FPS (V100) |
|---|---|---|---|
| YOLOv5-3D-S | 7.2M | ~85% | ~120 |
| YOLOv5-3D-M | 21M | ~88% | ~80 |
| YOLOv5-3D-L | 46M | ~90% | ~50 |
Results on PCB defect dataset with illumination-depth data
The model uses a lightweight encoder to process depth and illumination:
class IlluminationDepthEncoder(nn.Module):
# Depth: 1ch -> 16ch -> 32ch
# Illumination: 3ch -> 32ch -> 64ch
# Fusion: Concat + Conv -> 64chclass MultiModalFusion(nn.Module):
# Cross-attention between RGB and ID features
# Learnable fusion weights for adaptive combinationIf you use this code for your research, please cite:
@misc{yolov5-3d,
title={YOLOv5-3D: Illumination-Enhanced PCB Defect Detection},
author={Your Name},
year={2025}
}This project is licensed under the GPL-3.0 License.
- YOLOv5 by Ultralytics: https://github.com/ultralytics/yolov5
- PCB defect detection research community