# neeraj-n/wafer_defect architecture

> Architecture diagram of the GitHub repository neeraj-n/wafer_defect, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/neeraj-n/wafer_defect
- Repository: https://github.com/neeraj-n/wafer_defect

## Overview

#### 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: [`src/preprocessing.py`](https://github.com/neeraj-n/wafer_defect/blob/main/src/preprocessing.py) (Resize and lot split), [`src/train.py`](https://github.com/neeraj-n/wafer_defect/blob/main/src/train.py) (CNN training), [`src/evaluate.py`](https://github.com/neeraj-n/wafer_defect/blob/main/src/evaluate.py) (Held-out evaluation), [`scripts/download_data.sh`](https://github.com/neeraj-n/wafer_defect/blob/main/scripts/download_data.sh) (Data download)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_external_data[("WM-811K hosting<br/>external dataset")]
  node_download["Data download<br/>shell entry point<br/>[download_data.sh]"]
  node_raw_data[("LSWMD.pkl<br/>raw dataset")]
  node_loader["Labeled data loader<br/>ingestion module<br/>[data_loader.py]"]
  node_preprocessing["Resize and lot split<br/>preprocessing module<br/>[preprocessing.py]"]
  node_processed_cache[("Processed cache<br/>cached data")]
  node_eda["EDA notebook<br/>exploration notebook<br/>[01_eda.ipynb]"]
end

subgraph group_cnn["CNN path"]
  node_dataset["CNN dataset channels<br/>dataset module<br/>[dataset.py]"]
  node_cnn_model["PyTorch CNN<br/>model definition<br/>[model.py]"]
  node_cnn_train["CNN training<br/>training entry point<br/>[train.py]"]
  node_checkpoints[("Best CNN checkpoint<br/>model artifact")]
end

subgraph group_baseline["Baseline path"]
  node_features["Spatial descriptors<br/>feature module<br/>[features.py]"]
  node_baseline_train["Random Forest training<br/>training entry point<br/>[train_baseline.py]"]
end

subgraph group_evaluation["Evaluation"]
  node_evaluate["Held-out evaluation<br/>evaluation entry point<br/>[evaluate.py]"]
  node_metrics["Metrics and confusion analysis<br/>reporting boundary"]
  node_visualize["Plotting support<br/>visualization module<br/>[visualize.py]"]
end

node_external_data -->|"acquired by"| node_download
node_download -->|"writes"| node_raw_data
node_raw_data -->|"reads"| node_loader
node_loader -->|"labeled wafers"| node_preprocessing
node_preprocessing -.->|"may cache"| node_processed_cache
node_preprocessing -->|"lot-grouped splits"| node_features
node_features -->|"feature vectors"| node_baseline_train
node_preprocessing -->|"resized splits"| node_dataset
node_dataset -->|"channel tensors"| node_cnn_train
node_cnn_model -->|"model"| node_cnn_train
node_cnn_train -->|"selects and writes"| node_checkpoints
node_preprocessing -->|"held-out test split"| node_evaluate
node_checkpoints -->|"loads"| node_evaluate
node_evaluate -->|"produces"| node_metrics
node_visualize -->|"plots"| node_metrics
node_raw_data -.->|"explores"| node_eda

click node_download "https://github.com/neeraj-n/wafer_defect/blob/main/scripts/download_data.sh"
click node_loader "https://github.com/neeraj-n/wafer_defect/blob/main/src/data_loader.py"
click node_preprocessing "https://github.com/neeraj-n/wafer_defect/blob/main/src/preprocessing.py"
click node_features "https://github.com/neeraj-n/wafer_defect/blob/main/src/features.py"
click node_baseline_train "https://github.com/neeraj-n/wafer_defect/blob/main/src/train_baseline.py"
click node_dataset "https://github.com/neeraj-n/wafer_defect/blob/main/src/dataset.py"
click node_cnn_model "https://github.com/neeraj-n/wafer_defect/blob/main/src/model.py"
click node_cnn_train "https://github.com/neeraj-n/wafer_defect/blob/main/src/train.py"
click node_evaluate "https://github.com/neeraj-n/wafer_defect/blob/main/src/evaluate.py"
click node_visualize "https://github.com/neeraj-n/wafer_defect/blob/main/src/visualize.py"
click node_eda "https://github.com/neeraj-n/wafer_defect/blob/main/notebooks/01_eda.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_external_data,node_download,node_raw_data,node_loader,node_preprocessing,node_processed_cache,node_eda toneBlue
class node_dataset,node_cnn_model,node_cnn_train,node_checkpoints toneAmber
class node_features,node_baseline_train toneMint
class node_evaluate,node_metrics,node_visualize toneRose
```

## Components

### Data pipeline

- **WM-811K hosting**: external dataset · Kaggle/MIR Lab supplies raw wafer maps.
- [Data download](https://github.com/neeraj-n/wafer_defect/blob/main/scripts/download_data.sh): shell entry point · `scripts/download_data.sh`
- **LSWMD.pkl**: raw dataset · Externally supplied and absent from version control.
- [Labeled data loader](https://github.com/neeraj-n/wafer_defect/blob/main/src/data_loader.py): ingestion module · Unwraps labels and removes unlabeled wafers. · `src/data_loader.py`
- [Resize and lot split](https://github.com/neeraj-n/wafer_defect/blob/main/src/preprocessing.py): preprocessing module · Creates leakage-safe lot-grouped train, validation, and test partitions. · `src/preprocessing.py`
- **Processed cache**: cached data
- [EDA notebook](https://github.com/neeraj-n/wafer_defect/blob/main/notebooks/01_eda.ipynb): exploration notebook · `notebooks/01_eda.ipynb`

### CNN path

- [CNN dataset channels](https://github.com/neeraj-n/wafer_defect/blob/main/src/dataset.py): dataset module · Converts categorical maps into binary channels. · `src/dataset.py`
- [PyTorch CNN](https://github.com/neeraj-n/wafer_defect/blob/main/src/model.py): model definition · `src/model.py`
- [CNN training](https://github.com/neeraj-n/wafer_defect/blob/main/src/train.py): training entry point · Uses weighted loss, validation macro-F1 selection, scheduling, and early stopping. · `src/train.py`
- **Best CNN checkpoint**: model artifact · Best validation macro-F1 model.

### Baseline path

- [Spatial descriptors](https://github.com/neeraj-n/wafer_defect/blob/main/src/features.py): feature module · `src/features.py`
- [Random Forest training](https://github.com/neeraj-n/wafer_defect/blob/main/src/train_baseline.py): training entry point · Trains a class-balanced benchmark. · `src/train_baseline.py`

### Evaluation

- [Held-out evaluation](https://github.com/neeraj-n/wafer_defect/blob/main/src/evaluate.py): evaluation entry point · Evaluates the checkpoint on the lot-grouped test split. · `src/evaluate.py`
- **Metrics and confusion analysis**: reporting boundary · Reports macro-F1, balanced accuracy, and confusion matrices.
- [Plotting support](https://github.com/neeraj-n/wafer_defect/blob/main/src/visualize.py): visualization module · `src/visualize.py`

## Connections

- 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
