AI-Powered Manufacturing Defect Detection with Visual Explainability (Grad-CAM)
DefectX AI is a production-ready, explainable AI system for automated manufacturing quality control. It combines state-of-the-art YOLO object detection with Gradient-weighted Class Activation Mapping (Grad-CAM) to not only identify defects in manufacturing products but also provide visual explanations for its decisions, building trust in AI-powered inspection systems.
Unlike traditional defect detection systems that operate as "black boxes," DefectX AI provides Grad-CAM heatmaps that highlight the exact regions of an image that influenced the model's decision. This transparency is critical for:
- Quality Assurance Teams: Understanding why a product was flagged
- Regulatory Compliance: Demonstrating AI decision-making processes
- Continuous Improvement: Identifying patterns in false positives
)
---
| Feature | Description | Impact |
|---|---|---|
| π Real-Time Detection | YOLOv8-based defect detection with 25ms inference on CPU | Real-time production line integration |
| π§ Grad-CAM XAI | Visual heatmaps explaining model attention regions | Trust & transparency in AI decisions |
| π Interactive Dashboard | Streamlit-based analytics with defect trends | Data-driven quality decisions |
| πΉ Video Processing | Frame-by-frame analysis with batch export | Complete production line monitoring |
| π₯ Export Reports | CSV logs + annotated images + summary reports | Audit trails & documentation |
| β‘ CPU Optimized | Efficient inference without GPU requirements | Cost-effective deployment |
- β Good Product - Passes quality standards
- π΄ Crack - Structural damage (Critical)
- π Scratch - Surface damage (High/Medium)
- π‘ Dent - Deformation defects
- π΅ Misalignment - Positioning errors
- π£ Contamination - Foreign particles
- π΅ Discoloration - Color/texture anomalies
- π΄ Missing Component - Assembly errors (Critical)
| Component | Technology | Purpose |
|---|---|---|
| Detection Engine | Ultralytics YOLOv8/v11 | Object detection backbone |
| Explainability | Grad-CAM + PyTorch Hooks | XAI visual explanations |
| Web Framework | Streamlit 1.32+ | Interactive dashboard |
| Computer Vision | OpenCV + Pillow | Image/video processing |
| Analytics | Plotly + Pandas + Seaborn | Data visualization |
| ML Framework | PyTorch 2.2+ | Deep learning operations |
| Export | CSV + JSON + PNG | Report generation |
DefectX AI/
βββ app.py # Main Streamlit application
βββ utils.py # Core detection + XAI utilities
βββ requirements.txt # Python dependencies
βββ README.md # Documentation
βββ models/ # YOLO model storage
β βββ yolov8n.pt # Default nano model (auto-downloaded)
βββ logs/ # Detection history (JSON)
βββ outputs/ # Annotated images & reports
β βββ annotated/ # Detection + Grad-CAM exports
β βββ batch_exports/ # Batch processing results
βββ assets/ # Static assets & sample images
- Python 3.8 or higher
- 4GB+ RAM (8GB recommended for video processing)
- Windows/Linux/macOS
# Clone the repository
git clone https://github.com/shubham333k/DefectX-AI.git
cd DefectX-AI
# Create virtual environment
python -m venv venv
# Activate environment
# Windows:
venv\Scripts\activate
# Linux/macOS:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt# Launch Streamlit dashboard
streamlit run app.py
# The application will open in your browser at:
# http://localhost:8501- Launch the application
- Click "Initialize Model" in sidebar (downloads YOLO on first run)
- Select "Single Image" upload type
- Upload a product image (PNG/JPG)
- View results: Original β Detection β Grad-CAM Explanation
- Download annotated images and comparison view
- Select "Batch Images" upload type
- Upload multiple images
- View summary statistics and individual results
- Export CSV report with all detections
- Select "Video" upload type
- Upload production line footage (MP4/AVI)
- System processes frames at configurable intervals
- Download annotated video and frame-by-frame reports
- YOLO Model: Choose from Nano (fastest) to XLarge (most accurate)
- Confidence Threshold: 0.1 - 0.9 (default: 0.25)
- Grad-CAM Intensity: Adjust heatmap transparency
- Device: CPU or CUDA (if GPU available)
| Model | Input Size | CPU Inference | mAP | Use Case |
|---|---|---|---|---|
| YOLOv8n | 640x640 | ~25ms | 37.3 | Real-time demo |
| YOLOv8s | 640x640 | ~45ms | 44.9 | Balanced |
| YOLOv8m | 640x640 | ~90ms | 50.2 | Higher accuracy |
| YOLOv8l | 640x640 | ~150ms | 52.9 | Quality priority |
Benchmarks measured on Intel i7 / AMD Ryzen 7 processors
Input Image
β
YOLO Backbone (CSPDarknet)
β
Feature Extraction Layers
β
[Hook: Capture Activations] βββββββββ
β β
Detection Head β
β β
Predictions (Class + BBox) β
β β
Backpropagate Class Score β
β β
[Hook: Capture Gradients] βββββββββββ
β
Gradient Γ Activation = Grad-CAM
β
Heatmap Overlay on Image
- Localization Accuracy: Pinpoints exact defect regions, not just general areas
- Model Validation: Confirms model focuses on actual defects, not background noise
- False Positive Analysis: Identifies why good products might be flagged
- Training Insights: Reveals if model learned relevant features
Image: pcb_board_042.jpg
ββββββββββββββββββββββββββββ
β
Quality Check: FAILED
π΄ Defects Found: 2
β±οΈ Processing Time: 0.032s
Detection #1:
Type: Crack
Confidence: 94.2%
Severity: Critical
Location: (124, 256) - (180, 320)
Detection #2:
Type: Scratch
Confidence: 78.5%
Severity: High
Location: (400, 120) - (450, 200)
- Defect rate trends over time
- Severity distribution (Critical/High/Medium/Low)
- Defect type frequency analysis
- Confidence score distributions
- Production quality metrics
from ultralytics import YOLO
# Load pre-trained model
model = YOLO('yolov8n.pt')
# Train on custom defect dataset
model.train(
data='defects_dataset.yaml',
epochs=100,
imgsz=640,
batch=16,
name='custom_defect_model'
)Edit utils.py:
DEFAULT_DEFECT_CLASSES = {
0: 'Good Product',
1: 'Scratch',
2: 'Crack',
# ... add your custom classes
8: 'Your Custom Defect'
}- Computer Vision: Object detection, image processing, video analytics
- Deep Learning: CNN architectures, feature extraction, backpropagation
- Explainable AI: Grad-CAM implementation, model interpretability, trustworthiness
- MLOps: Model deployment, versioning, performance optimization
- Full-Stack ML: Backend (PyTorch) + Frontend (Streamlit) integration
- Domain Application: Manufacturing QA, industrial automation, quality engineering
- π§ Automotive: Detecting dents, scratches on car parts
- π± Electronics: PCB inspection, component verification
- πΎ Food & Beverage: Bottle inspection, contamination detection
- ποΈ Construction: Material defect identification
- π Textile: Fabric quality control, defect classification
detector = DefectDetector(
model_path='path/to/model.pt', # or None for default
model_name='yolov8n.pt', # YOLO variant
conf_threshold=0.25, # Detection threshold
device='cpu' # 'cpu' or 'cuda'
)
# Detect defects
result = detector.detect(image)
# Returns: {detections, annotated_image, original_image, total_defects}
# Generate Grad-CAM
gradcam = detector.generate_gradcam(image, alpha=0.5)
# Process batch
results = detector.process_batch(['img1.jpg', 'img2.jpg'])
# Get analytics
analytics = detector.get_analytics()- Fork the repository
- Create feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Project Maintainer: AI/ML Engineer
Email: shubhamjhanjhot333k@gmail.com
LinkedIn:[https://www.linkedin.com/in/shubhamkumar-aiml/]
GitHub: @shubham333k
- Ultralytics for YOLO implementation
- PyTorch for deep learning framework
- Streamlit for web application framework
- Grad-CAM Paper by Selvaraju et al.
π DefectX AI - Making Manufacturing Quality Control Transparent with Explainable AI
Built with β€οΈ for AI/ML Engineers passionate about industrial applications