Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

YOLOv5-3D: Illumination-Enhanced PCB Defect Detection

A modified YOLOv5 architecture with an illumination-based 3D branch for robust PCB defect detection under inconsistent lighting conditions.

🎯 Key Features

  • 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

πŸ“ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    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)     β”‚                           β”‚
β”‚        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

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

πŸš€ Quick Start

Installation

# Clone or download the project
cd yolov5_3d

# Install dependencies
pip install -r requirements.txt

Data Preparation

Your 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

Training

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

Validation

python val.py --weights runs/train/exp/best.pt --data /path/to/data

Inference

# 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

# 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

βš™οΈ Training Configuration

Key Parameters

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

Memory Optimization

For limited GPU memory (4GB):

  • Use --batch-size 4 or lower
  • Enable --amp for mixed precision
  • Use gradient accumulation: --gradient-accumulation-steps 2

πŸ“Š Performance Metrics

The training generates:

  1. Training Curves (training_curves.png)

    • Loss over epochs
    • mAP@0.5 and mAP@0.5:0.95
    • Precision and Recall
  2. Confusion Matrix (confusion_matrix.png)

    • Normalized detection results per class
  3. Results CSV (results.csv)

    • Complete training history

πŸ”§ Transfer Learning

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

🏭 Edge Deployment

ONNX Export

python export.py --weights best.pt --include onnx --simplify

TensorRT Optimization

# Requires TensorRT installation
python export.py --weights best.pt --include tensorrt --fp16

TFLite for Mobile

python export.py --weights best.pt --include tflite --quantize

πŸ“ˆ Model Variants

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

πŸ”¬ Technical Details

Illumination-Depth Fusion

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

Cross-Attention Fusion

class MultiModalFusion(nn.Module):
    # Cross-attention between RGB and ID features
    # Learnable fusion weights for adaptive combination

πŸ“ Citation

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

πŸ“œ License

This project is licensed under the GPL-3.0 License.

πŸ™ Acknowledgments

About

This repo has the core model code for custom yolo 3D

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages