Home About Expertise Projects Blogs Contact
UpcomingComputer VisionOriginal Project

Manufacturing Defect
Detector

A Gradio app that classifies product images as defective or non-defective using transfer learning, upload any product image and get instant defect classification with a confidence score.

StatusStarting Soon
TypeComputer Vision · Transfer Learning · Gradio App
DatasetKaggle Casting Defect Dataset
StackPython · ResNet / MobileNet · Gradio · PyTorch
DomainQuality Control · Manufacturing
CategoryOriginal Project
👁
Upcoming Project
Development starting soon. Full technical plan and approach documented here.
ResNet
Base Architecture
Transfer
Learning Approach
Gradio
Deployment Interface
Kaggle
Casting Defect Dataset
01, The Problem

Quality control that never blinks.

In manufacturing, product defects that slip through quality control are expensive, rework costs, warranty claims, customer complaints, and in safety-critical industries, regulatory consequences. Traditional quality control relies on human visual inspection, slow, inconsistent, and not scalable.

Computer vision changes this. A trained model can inspect thousands of parts per minute, 24/7, with consistent accuracy that doesn't degrade after an 8-hour shift. This project builds exactly that, a visual defect classifier that works from a single uploaded image.

🏭
Manufacturing + AI, the intersection I work in
This project isn't just a computer vision exercise. It's a direct application of AI to the manufacturing domain I've spent 2.5 years working in. Quality management is a core PLM module, I've configured it in ENOVIA. Now I'm building the AI layer that sits on top of it.
02, The Dataset

Kaggle Casting Defect Dataset

The dataset contains images of casting products (pump impellers) in two categories: defective and non-defective. Casting defects include roughness, blowholes, cracks, and incomplete fills, all visible in the product surface image.

SplitOK (Non-Defective)DefectiveTotal
Training2,875 images781 images3,656
Test453 images267 images720
Total3,3281,0484,376
⚖️
Class imbalance note
The dataset has more OK images than defective ones (76:24). Will apply class weights or oversampling to prevent the model from being biased toward predicting OK for everything.
03, Why Transfer Learning

Not training from scratch

Training a deep CNN from scratch on 4,376 images would produce a mediocre model. Transfer learning takes a model already trained on millions of images (ImageNet) and fine-tunes it on the specific defect classification task. The result: high accuracy with limited data and compute.

🧠
ResNet-50 (Primary)
Deep residual network with skip connections, strong baseline for image classification. Well-documented, widely used in industrial vision applications.
📱
MobileNetV2 (Alternative)
Lightweight architecture designed for edge deployment. If the app needs to run on a mobile device or embedded system in a factory, MobileNet is the answer.
🔄
Fine-tuning Strategy
Freeze all layers except the final classification head. Train on the casting dataset. If accuracy is insufficient, unfreeze the last few convolutional blocks and train end-to-end with a lower learning rate.
📊
Data Augmentation
Random horizontal flip, rotation (±15°), brightness and contrast jitter. Prevents overfitting on the small dataset and improves model robustness to lighting variation in real factory conditions.
Python, transfer_learning.py
import torchvision.models as models
import torch.nn as nn

# Load pre-trained ResNet-50
model = models.resnet50(pretrained=True)

# Freeze all layers
for param in model.parameters():
    param.requires_grad = False

# Replace final layer with binary classifier
num_features = model.fc.in_features
model.fc = nn.Sequential(
    nn.Linear(num_features, 256),
    nn.ReLU(),
    nn.Dropout(0.4),
    nn.Linear(256, 2)  # defective / non-defective
)

# Data augmentation for training
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(15),
    transforms.ColorJitter(brightness=0.2, contrast=0.2),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
])
04, The Gradio Interface

One image in. Clear answer out.

The app is built with Gradio, the fastest way to turn a PyTorch model into an interactive web demo. No frontend code required. The interface is intentionally simple: upload an image, get a result.

🖼️
Image Upload
Drag-and-drop or click to upload any product image. Accepts JPG, PNG, WebP.
✅❌
Defective / Non-Defective
Clear binary classification result, no ambiguous probability ranges that confuse the user.
📊
Confidence Score
Softmax probability displayed as a percentage, e.g. "97.3% confident: Non-Defective".
🔥
Grad-CAM Heatmap (planned)
Visual explanation showing which parts of the image triggered the classification, builds trust and helps identify defect location.
Python, gradio_app.py
import gradio as gr
import torch
from PIL import Image

def classify_image(image):
    # Preprocess
    tensor = preprocess(image).unsqueeze(0)

    # Predict
    with torch.no_grad():
        output = model(tensor)
        probs = torch.nn.functional.softmax(output[0], dim=0)

    confidence = probs.max().item()
    label = "Non-Defective ✅" if probs.argmax() == 0 else "Defective ❌"

    return f"{label}, {confidence*100:.1f}% confident"

demo = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil", label="Upload Product Image"),
    outputs=gr.Textbox(label="Classification Result"),
    title="Manufacturing Defect Detector",
    description="Upload a product image to check for manufacturing defects."
)
demo.launch()
05, Build Roadmap

The plan

Week 1
Data & Baseline
▸Download Kaggle casting defect dataset
▸EDA, class distribution, image size, quality check
▸Train MobileNetV2 baseline (fast to iterate)
▸Measure accuracy, precision, recall on test set
Week 2
ResNet & Fine-tuning
▸Switch to ResNet-50 backbone
▸Experiment with unfreezing last conv blocks
▸Add data augmentation pipeline
▸Optimise for recall on defective class (false negatives are costly)
Week 3
Gradio App
▸Build Gradio interface
▸Add confidence score display
▸Test with out-of-distribution images
▸Deploy to Hugging Face Spaces (free hosting)
Week 3+
Enhancements
▸Add Grad-CAM heatmap visualisation
▸Batch processing for multiple images
▸Add example images to the Gradio demo
▸Write full project case study
06, Tech Stack
Python 3PyTorchtorchvisionResNet-50MobileNetV2GradioTransfer LearningGrad-CAMKaggle Dataset
← Back to Projects View on GitHub ↗