Deep learning pipeline for industrial surface defect segmentation with uncertainty-aware risk scoring and automated accept/reject decision-making.
This project implements a production-grade, end-to-end system for detecting and segmenting surface defects in steel manufacturing images, specifically targeting the Severstal Steel Defect Detection dataset. Beyond simple segmentation, it layers Monte Carlo Dropout uncertainty estimation and a risk engine on top of model predictions, culminating in an automated decision system that can classify each sample as ACCEPT, AUTO_REJECT, or HUMAN_REVIEW.
- π¬ Multi-class Segmentation β Simultaneously detects 4 defect classes using pixel-wise sigmoid outputs
- π² Uncertainty Quantification β Monte Carlo Dropout with configurable forward passes quantifies prediction uncertainty
- βοΈ Risk Engine β Combines defect area, class severity, and uncertainty into a composite risk score
- π€ Automated Decisions β Rule-based decision logic classifies samples without human intervention
- ποΈ Dual Architecture β Supports both U-Net (ResNet-34 encoder) and SegFormer (MiT-B2 backbone)
- π¦ Kaggle-Ready β Auto-detects Kaggle environment, adapts paths and workers accordingly
- π Analytics Suite β Calibration analysis, uncertainty diagnostics, and decision reporting
βββ main.py # CLI entry point (train / evaluate / predict / analyze)
βββ config.py # Centralized dataclass-based configuration
βββ kaggle_train.py # Kaggle notebook-compatible training script
βββ requirements.txt # Python dependencies
β
βββ data/
β βββ dataset.py # PyTorch Dataset with patch extraction and augmentation
β βββ dataloader.py # DataLoader factory with stratified splits
β βββ patching.py # Patch-based tiling and reconstruction utilities
β βββ rle.py # Run-Length Encoding / decoding helpers
β
βββ models/
β βββ unet.py # U-Net with segmentation-models-pytorch (ResNet encoder)
β βββ segformer.py # SegFormer with HuggingFace Transformers
β
βββ training/
β βββ train.py # Two-stage training loop (warmup β full)
β βββ losses.py # Dice-BCE and Dice-Focal combined losses
β βββ metrics.py # Per-class Dice, IoU, pixel accuracy
β βββ evaluate.py # Validation and test evaluation harness
β
βββ uncertainty/
β βββ mc_dropout.py # Monte Carlo Dropout inference engine
β
βββ decision/
β βββ risk_engine.py # Risk score computation (area + severity + uncertainty)
β βββ decision_logic.py # accept / reject / human-review thresholding
β
βββ analytics/
β βββ calibration.py # Reliability diagrams and ECE/ACE calibration metrics
β
βββ visualization/
β βββ visualize.py # Overlay masks, uncertainty maps, and decision summaries
β
βββ scripts/
β βββ analyze_dataset.py # EDA: class distribution, defect statistics, sample viz
β βββ test_training.py # Quick smoke test for the training pipeline
β βββ test_kaggle.py # End-to-end Kaggle compatibility test
β
βββ checkpoints/ # Saved model weights (git-ignored)
βββ logs/ # TensorBoard logs (git-ignored)
βββ outputs/ # Predictions, reports, visualizations (git-ignored)
| Model | Encoder | Parameters | Notes |
|---|---|---|---|
| U-Net | ResNet-34 | ~25M | Fast, reliable baseline |
| SegFormer | MiT-B2 | ~25M | Transformer-based, stronger on texture |
Both models output 4-channel sigmoid maps (one per defect class), enabling multi-label pixel classification.
- Warmup Stage (10 epochs) β Trains on a defect-heavy subset (90% defect : 10% clean) to teach the model defect patterns before class imbalance dominates
- Full Training Stage (40 epochs) β Fine-tunes on the full dataset with cosine LR scheduling and early stopping
Monte Carlo Dropout is applied at inference time by keeping dropout layers active over N stochastic forward passes. The pixel-wise variance across runs serves as an uncertainty map, surfacing regions where the model is unsure.
Uncertainty = ΟΒ²(predictions over N forward passes)
Each prediction is scored by a weighted combination of:
| Factor | Weight | Description |
|---|---|---|
| Defect Area | 0.4 | Percentage of pixels flagged as defective |
| Uncertainty | 0.3 | Mean uncertainty across defect regions |
| Class Severity | 0.3 | Per-class severity multipliers [1.0, 2.0, 0.8, 1.5] |
Risk Score β ACCEPT if risk < 0.20
Risk Score β AUTO_REJECT if defect_area > 5% AND uncertainty < 0.30
Risk Score β HUMAN_REVIEW if uncertainty > 0.30
All hyperparameters are centralized in config.py via Python dataclasses β no magic numbers anywhere else.
from config import get_config, ModelConfig
# Default config (auto-detects Kaggle vs local)
config = get_config()
# Switch to SegFormer
config = get_config(model=ModelConfig(model_type="segformer"))Key config groups:
| Config Class | Controls |
|---|---|
DataConfig |
Paths, patch size, augmentation, class balance |
ModelConfig |
Architecture, encoder, dropout |
TrainingConfig |
LR, scheduler, loss type, AMP, early stopping |
UncertaintyConfig |
MC passes, uncertainty threshold |
RiskConfig |
Area/severity/uncertainty weights |
DecisionConfig |
Accept/reject/review thresholds |
Environment variable overrides are supported for all paths:
STEEL_DATA_ROOT=/path/to/data
STEEL_OUTPUT_DIR=/path/to/outputs
STEEL_CHECKPOINT_DIR=/path/to/checkpoints
STEEL_LOG_DIR=/path/to/logspip install -r requirements.txtDownload the Severstal Steel Defect Detection dataset and place it as:
data/raw/
train.csv
train_images/
test_images/
Or set STEEL_DATA_ROOT to point to your data directory.
python scripts/analyze_dataset.py# Train U-Net (both stages)
python main.py train --model unet --stage both
# Train SegFormer (full stage only)
python main.py train --model segformer --stage full
# Resume from checkpoint
python main.py train --model unet --resume checkpoints/best.ptpython main.py evaluate --model unet --checkpoint checkpoints/best.ptpython main.py predict --model unet --checkpoint checkpoints/best.pt --mc-passes 20python main.py analyze --checkpoint checkpoints/best.ptThe system auto-detects the Kaggle environment. Simply run kaggle_train.py as your kernel script β paths and worker counts are adjusted automatically.
# In Kaggle notebook
!python kaggle_train.pySmoke test before full training:
python scripts/test_kaggle.py| Package | Purpose |
|---|---|
torch >= 2.0 |
Core deep learning framework |
segmentation-models-pytorch |
U-Net encoder-decoder |
transformers >= 4.30 |
SegFormer backbone |
albumentations |
Image augmentation |
scikit-learn |
Stratified splitting, metrics |
numpy, pandas |
Data manipulation |
opencv-python |
Image I/O and processing |
matplotlib, seaborn |
Visualization |
tensorboard |
Training monitoring |
Note: This is an active research project. Results will be updated as training progresses.
Evaluation metrics tracked per defect class:
- Dice Coefficient (primary metric)
- IoU (Jaccard Score)
- Pixel Accuracy
Severstal Steel Defect Detection β Kaggle Competition
Images: 1600Γ256 grayscale steel strip scans
Labels: 4 defect classes encoded as Run-Length Encoding (RLE) masks
Train set: ~12,568 images | Test set: ~1,801 images
This project is open-source and released under the MIT License.
sharksurfauto-byte
GitHub: @sharksurfauto-byte