In modern electronics manufacturing, ensuring Printed Circuit Board (PCB) quality is crucial for preventing malfunctioning devices and costly recalls. Manual inspection is time-consuming and error-prone, hence automated defect detection using deep learning significantly improves speed, accuracy, and reliability in quality assurance pipelines.
Problem Definition
The goal is to build an object detection model that accurately identifies and classifies six types of PCB defects using YOLOv5.
About the Data
The dataset contains annotated images of PCB boards with six defect types:
missing_hole
mouse_bite
open_circuit
short
spur
spurious_copper
Each image is paired with a .txt file in YOLO format indicating bounding boxes and corresponding class labels. The dataset was split into:
Training Images: 92
Validation Images: 23
To prepare for YOLOv5 training, the annotations were converted into YOLO format and directories were structured accordingly using a custom to_v5_directories() utility function.
Data Preprocessing
Verified and converted all annotations to YOLOv5-compatible format.
Ensured the structure: images/train, images/val, labels/train, labels/val.
Verified class balance and number of annotations per class.
Actionable Insights using EDA
The dataset is imbalanced with varying instances per class.
Defects like missing_hole and short are more frequent than spur or spurious_copper.
Most images contain one or two defects; very few have multiple overlapping defects.
Bounding boxes are relatively small, requiring high-resolution training.
The defect types are visually distinct, making object detection feasible with the right model.
Data Preparation
All images resized to 416x416 pixels.
Real-time augmentations using Albumentations were applied:
Horizontal Flip
CLAHE
Blur
Grayscale
YOLOv5's built-in caching was used to speed up data loading during training.
Model Selection
Chose YOLOv5s (small variant of YOLOv5) due to its balance between speed and accuracy, making it suitable for real-time defect detection on edge devices.
Model Training
Framework: PyTorch with YOLOv5
Pre-trained weights: yolov5s.pt
Custom training on 6 defect classes
Trained for 100 epochs using default hyperparameters
Loss Function: Combined objectness, classification, and bounding box regression losses
Optimizer: SGD (used by default in YOLOv5)
Model Evaluation
Accuracy: 93.7%
mAP@0.5: High across classes
Precision: Up to 99.8% for specific classes
Overall Performance: 47.9% across all metrics
Detection samples were visualized and confirmed using bounding boxes.
Feature Importance
Feature importance in object detection is reflected in how YOLO learns spatial and class-related features. The most dominant classes had better localization and confidence scores. Visual inspection showed that open_circuit and missing_hole were more accurately localized.
Key Results
Successfully trained YOLOv5 model to detect six PCB defect types.
Achieved strong performance with high accuracy and precision.
Validated through visual inspection and inference scripts.
Model saved in .pt format for deployment.
Business Impact
Automated PCB inspection can reduce manual labor by over 70%.
Improves defect detection consistency and reliability.
Saves time in production pipelines, boosting throughput.
Reduces return rates and product failures due to faulty PCBs.
Deployment
CI/CD Pipeline with GitHub Actions and AWS
Environment Setup
Created a Conda environment with Python 3.9.
Installed dependencies using pip install -r requirements.txt.
Dockerization
Docker image built from source code including app.py, models, and utility components.
Dockerfile includes all setup steps to run the inference API.
AWS Services Used
Amazon ECR: Container image registry to store Docker images.
Amazon EC2: Used as a host to run the containerized application.
GitHub Actions Workflow
CI/CD pipeline defined in .github/workflows/main.yml.
On every push to main branch:
Builds the Docker image
Authenticates and pushes it to ECR
SSH into EC2
Pulls and runs the latest Docker image
Self-Hosted Runner
Configured EC2 instance as a self-hosted GitHub runner.
Ensures secure and direct deployment without manual intervention.
Secrets Used
AWS credentials, region, ECR URI, and repository name are securely stored as GitHub secrets.
Future Scope
Expand dataset size for better generalization.
Fine-tune hyperparameters using YOLOv5’s hyperparameter evolution.
Integrate defect severity classification to aid manufacturing decisions.
Deploy model on an edge device (e.g., Jetson Nano) for real-time inspection.