How neeraj-n/wafer_defect works
Purpose and Entry Points
This is a Python ML pipeline for 9-class silicon wafer defect classification using WM-811K wafer maps. Primary executable entry points are src/train.py for CNN training, src/train_baseline.py for the Random Forest benchmark, and src/evaluate.py for held-out checkpoint evaluation. Data acquisition is externalized through scripts/download_data.sh.
Data Ingestion and Dataset Boundary
src/data_loader.py reads the externally supplied LSWMD.pkl from data/raw/, unwraps MATLAB-style nested label fields, and excludes unlabeled wafers. This establishes the supervised labeled subset and normalizes source metadata such as failure type and lot identity before model-specific processing.
Preprocessing and Leakage-Safe Partitioning
src/preprocessing.py resizes variable-shape categorical wafer maps to a fixed image size using nearest-neighbor interpolation. It also creates train/validation/test partitions grouped by lotName, preventing wafers from the same fabrication lot from appearing across splits. Processed resized data may be cached beneath data/processed/.
Two Modeling Paths
The classical path uses handcrafted spatial descriptors from src/features.py, then trains a class-balanced Random Forest via src/train_baseline.py. The deep-learning path transforms maps into categorical binary channels through src/dataset.py and feeds them to the small PyTorch CNN defined in src/model.py. Both paths address the dominant none class with class-aware weighting.
CNN Training and Checkpoint Lifecycle
src/train.py orchestrates reproducible CNN training: dataset construction, weighted loss, Adam optimization, validation-driven learning-rate reduction, early stopping, and best-model selection by validation macro-F1. Selected artifacts are written to checkpoints/, notably the checkpoint consumed by evaluation.
Evaluation and Reporting
src/evaluate.py loads a CNN checkpoint and evaluates against the held-out lot-grouped test partition. Its reporting boundary is metric-centric rather than raw accuracy-centric: macro-F1, balanced accuracy, and confusion-matrix analysis are implemented alongside shared evaluation functionality. src/visualize.py provides plotting support for analysis and result interpretation.
Runtime and External Dependencies
The runtime is Python with PyTorch for the CNN path and scikit-learn-style preprocessing/splitting and Random Forest baseline functionality; dependencies are declared in requirements.txt. Jupyter exploration is available through notebooks/01_eda.ipynb. The only required external data service is Kaggle/MIR Lab hosting for WM-811K; raw data is intentionally absent from version control.
Main entry points: preprocessing.py Resize and lot split · train.py CNN training · evaluate.py Held-out evaluation · download_data.sh Data download
Components 16
Data pipeline
- WM-811K hosting · Kaggle/MIR Lab supplies raw wafer maps.
- Data download
scripts/download_data.sh - LSWMD.pkl · Externally supplied and absent from version control.
- Labeled data loader · Unwraps labels and removes unlabeled wafers.
src/data_loader.py - Resize and lot split · Creates leakage-safe lot-grouped train, validation, and test partitions.
src/preprocessing.py - Processed cache
- EDA notebook
notebooks/01_eda.ipynb
CNN path
- CNN dataset channels · Converts categorical maps into binary channels.
src/dataset.py - PyTorch CNN
src/model.py - CNN training · Uses weighted loss, validation macro-F1 selection, scheduling, and early stopping.
src/train.py - Best CNN checkpoint · Best validation macro-F1 model.
Baseline path
- Spatial descriptors
src/features.py - Random Forest training · Trains a class-balanced benchmark.
src/train_baseline.py
Evaluation
- Held-out evaluation · Evaluates the checkpoint on the lot-grouped test split.
src/evaluate.py - Metrics and confusion analysis · Reports macro-F1, balanced accuracy, and confusion matrices.
- Plotting support
src/visualize.py
Connections 16
- WM-811K hosting → Data download: acquired by
- Data download → LSWMD.pkl: writes
- LSWMD.pkl → Labeled data loader: reads
- Labeled data loader → Resize and lot split: labeled wafers
- Resize and lot split → Processed cache: may cache
- Resize and lot split → Spatial descriptors: lot-grouped splits
- Spatial descriptors → Random Forest training: feature vectors
- Resize and lot split → CNN dataset channels: resized splits
- CNN dataset channels → CNN training: channel tensors
- PyTorch CNN → CNN training: model
- CNN training → Best CNN checkpoint: selects and writes
- Resize and lot split → Held-out evaluation: held-out test split
- Best CNN checkpoint → Held-out evaluation: loads
- Held-out evaluation → Metrics and confusion analysis: produces
- Plotting support → Metrics and confusion analysis: plots
- LSWMD.pkl → EDA notebook: explores