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