How hwaminclaude/nmfc works
Purpose and Entry Points
NMFC is a PyTorch reproduction and experimental framework for Nonlinear Marginal Fisher Classification: local geometric energy logits, automatic phase transition (APT), reference-bank inference, baselines, and OOD evaluation. Primary CLI entry point: src/train.py, driven by YAML experiment definitions such as configs/synthetic.yaml, configs/cifar100.yaml, and configs/ood_cifar10.yaml. Main user-facing artifact is generated/curated notebooks under notebooks/.
Experiment Orchestration and Lifecycle
src/train.py selects synthetic, image benchmark, or OOD runners; constructs datasets, encoder/head or baseline, optimizer, and executes two phases: linear MFA warm-up followed by APT-controlled NMFC training. It writes checkpoints, epoch history, test embeddings, and final metrics. src/utils/experiments.py owns the cache contract: a final results/<exp>/metrics.json is the completion sentinel, so completed runs load rather than retrain.
NMFC Mathematical Core
The stateless formulation is concentrated in src/nmfc/:
• src/nmfc/affinity.py: pairwise distances, adaptive variance, and masked-softmax neighborhood affinity.
• src/nmfc/energy.py: within-class and cross-class energies, scale normalization, and geometric logits.
• src/nmfc/losses.py: NMFC likelihood, linear MFA warm-up loss, scatter matrices, and Fisher ratio.
• src/nmfc/apt.py: pure-Python controller that watches Fisher-ratio convergence and switches phases.
Variant-era extensions reside in src/nmfc/acs.py and src/nmfc/softmin.py.
Model and Inference Boundary
src/models/encoders.py maps synthetic vectors or images to embeddings (MLP or ResNet-50). src/models/heads.py provides NMFCHead, separating batch-local training energy calculations from inference through a per-class ReferenceBank. src/models/banks.py maintains the bounded train-embedding bank used to freeze inference statistics. src/models/heads_v3.py and src/variants.py allow config-selected v1/v3 energy, logit, APT, and bank combinations without changing the standard path.
Data and Batch Structure
src/datasets/synthetic.py generates and splits moons, circles, and Swiss-roll manifolds. src/datasets/image.py loads CIFAR-10, CIFAR-100, and SVHN, including Windows-safe transform construction. src/datasets/samplers.py supplies PK-balanced batches; this is a critical data boundary because NMFC, triplet, and Deep-MFA require meaningful same-class and cross-class in-batch neighborhoods.
Evaluation and Comparative Experiments
src/models/baselines.py implements Softmax CE, ArcFace, batch-hard Triplet, Prototype, and Deep-MFA comparators under shared orchestration. src/evaluate.py computes classification Top-1/Top-5, embedding silhouette, and OOD AUC. NMFC OOD scoring is derived from total geometric energy rather than softmax confidence.
Runtime, Infrastructure, and Outputs
The runtime is Python/PyTorch with CUDA-capable NVIDIA GPUs recommended; dependencies are declared in requirements.txt, and environment bootstrap is scripts/setup_env.ps1. External datasets/services are torchvision-accessible CIFAR-10/100 and SVHN, with a documented BrainChip mirror fallback. Results form a durable three-part interface: checkpoint weights, logs/<exp>/history.json, and results/<exp>/metrics.json plus embeddings for visualization. scripts/run_all.ps1 executes configured experiments sequentially; notebook builders such as scripts/build_notebook.py regenerate presentation artifacts from cached outputs.
Main entry points: train.py Training CLI · heads.py NMFC Head · samplers.py PK Batch Sampler · encoders.py Embedding Encoders
Components 19
Experiment Runtime
- Training CLI · Selects runners and executes warm-up plus APT training.
src/train.py - Experiment Cache · Final metrics file is the completed-run sentinel.
src/utils/experiments.py - Experiment YAML
configs/synthetic.yaml - Evaluation · Computes Top-1/5, silhouette, and energy-based OOD AUC.
src/evaluate.py
Data Pipeline
- Synthetic Datasets
src/datasets/synthetic.py - Image Datasets · Loads CIFAR-10/100 and SVHN.
src/datasets/image.py - PK Batch Sampler · Provides meaningful intra- and cross-class neighborhoods.
src/datasets/samplers.py
NMFC Core
- Neighborhood Affinity · Computes distances, adaptive variance, and masked affinities.
src/nmfc/affinity.py - Geometric Energy · Computes class energies and normalized geometric logits.
src/nmfc/energy.py - NMFC Losses · Provides likelihood, MFA warm-up, and Fisher-ratio calculations.
src/nmfc/losses.py - APT Controller · Switches from linear MFA warm-up on Fisher convergence.
src/nmfc/apt.py
Models & Inference
- Embedding Encoders · MLP or ResNet-50 maps inputs to embeddings.
src/models/encoders.py - NMFC Head · Separates batch-local energy training from bank-based inference.
src/models/heads.py - Reference Bank · Bounded per-class train embeddings freeze inference statistics.
src/models/banks.py - Baseline Models · CE, ArcFace, Triplet, Prototype, and Deep-MFA alternatives.
src/models/baselines.py - Variant Selection · Selects v1/v3 energy, logits, APT, and bank combinations.
src/variants.py
Results & Presentation
- Cached Results · Checkpoints, history, embeddings, and final metrics.
- Generated Notebooks · Curated visualizations are regenerated from cached outputs.
notebooks/1.NMFC_version1.ipynb - Notebook Builder
scripts/build_notebook.py
Connections 22
- Experiment YAML → Training CLI: drives
- Training CLI → Experiment Cache: checks cache
- Experiment Cache → Cached Results: loads or writes
- Training CLI → Synthetic Datasets: builds
- Training CLI → Image Datasets: builds
- Synthetic Datasets → PK Batch Sampler: examples
- Image Datasets → PK Batch Sampler: examples
- PK Batch Sampler → Embedding Encoders: balanced batches
- Training CLI → Embedding Encoders: constructs
- Embedding Encoders → NMFC Head: embeddings
- Neighborhood Affinity → Geometric Energy: neighborhood weights
- Geometric Energy → NMFC Head: geometric logits
- NMFC Losses → APT Controller: Fisher ratio
- APT Controller → Training CLI: phase transition
- NMFC Head → Reference Bank: updates train embeddings
- Reference Bank → NMFC Head: inference statistics
- Training CLI → Baseline Models: alternative model
- Variant Selection → NMFC Head: config-selected implementation
- Training CLI → Evaluation: test embeddings and logits
- Evaluation → Cached Results: final metrics
- Cached Results → Notebook Builder: cached outputs
- Notebook Builder → Generated Notebooks: regenerates