Skip to content

Hussaincodes01/Yolo-3D-model-Code

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

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors