Casting Defect Detection (ViT)
Fine-tuned a Vision Transformer on casting surface images to classify defective vs. ok parts. Model hosted on HuggingFace Hub.
Goal
Detect surface defects on metal casting parts using a fine-tuned Vision Transformer, with experiment tracking and a serving endpoint.
Approach
- Load the Kaggle casting inspection dataset (6,633 train / 715 test images of pump impellers, ~57% defective).
- Preprocess with the ViT image processor: resize to 224×224, normalize to ImageNet statistics.
- Fine-tune
google/vit-base-patch16-224for 3 epochs on an RTX 3060, tracking everything with MLflow. - Evaluate on the held-out test set. Serve predictions via FastAPI.
Guiding questions
- Can a pretrained ViT reach near-perfect accuracy on a small industrial dataset with minimal tuning?
- How fast does the model converge — is 3 epochs enough, or does it overfit?
- Is the model confident on both classes, or does it hedge on borderline samples?
Data
- Kaggle — Casting Product Image Data for Quality Inspection: 7,348 grayscale images of submersible pump impellers.
Headline numbers
Model architecture
HuggingFace datasetsLoad 6,633 train + 715 test images from folder structure. Labels assigned from directory names (ok_front / def_front).
ViTImageProcessorResize 512×512 casting photos to 224×224, normalize pixel values to ViT input distribution.
HuggingFace TrainerReplace ImageNet classification head (1000 → 2 classes). Fine-tune full model for 3 epochs, lr=2e-5, batch size 16.
MLflowLog hyperparameters, loss curves, and per-epoch F1/accuracy. Compare runs to pick the best checkpoint.
FastAPIREST endpoint accepts image upload, returns defect/ok prediction with confidence score.
HuggingFace HubPush fine-tuned weights to Hub. Anyone can load the model with one line of code.
Live demo
Upload a casting image or use the examples — the model runs on HuggingFace Spaces.
Conclusion
- A pretrained ViT-B/16 reaches 99.5% macro F1 after just 3 epochs of fine-tuning on ~6,600 images — transfer learning makes small industrial datasets practical.
- The full pipeline covers fine-tuning (HuggingFace Trainer), experiment tracking (MLflow), model serving (FastAPI), and model sharing (HuggingFace Hub).