How junghwamin/nmfc-plan1-v1-reproduction works
Purpose and Entry Points
This is a PyTorch reproduction of v1 NMFC: local-neighborhood geometric classification, automatic warm-up transition, and energy-based OOD detection. Primary entry point is python -m src.train --config ... via src/train.py; evaluation utilities live in src/evaluate.py. Experiment configurations select synthetic, CIFAR, rehearsal, diagnostic, and OOD runs under configs/.
Data and Batch Geometry
Datasets provide synthetic manifolds and image/OOD data, then enforce class-balanced PK batches required by neighborhood losses. PKBatchSampler guarantees at least two classes and two examples per class so positive/negative neighborhood affinities are valid.
• src/datasets/synthetic.py
• src/datasets/image.py
• src/datasets/samplers.py
Embedding Models and Baseline Branch
Inputs become Euclidean embeddings through either a 2D MLP for synthetic experiments or ImageNet-pretrained ResNet-50 projected to the configured embedding dimension. A parallel baseline branch supports softmax, ArcFace, prototype, triplet, and Deep-MFA comparisons; it bypasses NMFC-specific geometric logits.
• src/models/encoders.py
• src/models/baselines.py
NMFC Mathematical Pipeline
The stateless NMFC core transforms embeddings and labels into class logits: pairwise squared distances feed auto-tuned affinity weights; local positive/negative energies are computed; detached empirical scales normalize them into geometric logits; NLL or linear-MFA objectives optimize the encoder.
• src/nmfc/affinity.py
• src/nmfc/energy.py
• src/nmfc/losses.py
Two-Phase Training Control
src/train.py orchestrates Phase I linear-MFA embedding warm-up, monitors Fisher ratio each epoch, and delegates phase switching to a torch-free APT state machine. Once plateau criteria and warm-up guards are met, training moves one-way to Phase II NMFC NLL. Fixed/no-warmup controllers support ablations.
• src/train.py
• src/nmfc/apt.py
• configs/base.yaml
Training vs. Inference Boundary
Training uses batch-local neighborhoods through NMFCHead.forward_batch, effectively sampled local geometry. After training, the head builds a persistent labeled ReferenceBank from training embeddings and freezes affinity/energy scales. Evaluation then uses bank-backed logits, predictions, and energy-based OOD scores rather than batch statistics.
• src/models/heads.py
• src/models/banks.py
• src/evaluate.py
Evaluation, Outputs, and Runtime
Evaluation computes top-k accuracy, embedding silhouette, and OOD AUC. Results are cached by experiment name: completed metrics.json files under 산출물/ act as completion sentinels, with histories retained for plotting/notebook regeneration. Runtime is Python/PyTorch, using CUDA-capable Torch where available; image experiments depend on CIFAR and SVHN data services/downloads and ResNet ImageNet pretrained weights.
• src/utils/experiments.py
• 산출물/cifar100_nmfc/metrics.json
• notebooks/1.NMFC_version1.ipynb
Extension Boundary
The repository is the foundational v1 tree for later plans. Variant dispatch exists, but v1 configurations do not activate the v3/v4 modules; the default path remains NMFCHead. Future changes should preserve the clear separation between reusable mathematical modules, stateful head/bank inference, and training orchestration.
• src/variants.py
• src/models/heads_v3.py
• src/nmfc/softmin.py
Main entry points: train.py Training entry point · heads.py NMFC head · encoders.py Embedding encoders · experiments.py Experiment cache
Components 20
Data & batches
- Synthetic & image datasets · Provides manifolds, CIFAR, and OOD image data.
src/datasets/image.py - Synthetic manifolds
src/datasets/synthetic.py - PK batch sampler · Ensures valid positive and negative neighborhood pairs.
src/datasets/samplers.py
Models & NMFC
- Embedding encoders · Maps inputs to configured Euclidean embeddings.
src/models/encoders.py - Baseline branch · Implements softmax, ArcFace, prototype, triplet, and Deep-MFA paths.
src/models/baselines.py - NMFC head · Uses batch-local logits in training and bank-backed logits in inference.
src/models/heads.py - Affinity weights · Computes pairwise distances and auto-tuned neighborhood affinities.
src/nmfc/affinity.py - Local energies · Computes positive and negative neighborhood energies.
src/nmfc/energy.py - Geometric objectives · Applies linear-MFA warm-up or NMFC NLL.
src/nmfc/losses.py - Variant dispatch · v1 defaults to NMFCHead; later variants are not active in v1 configs.
src/variants.py - v3 head extension
src/models/heads_v3.py - Softmin primitive
src/nmfc/softmin.py
Training control
- Experiment configs · Select data, model, controller, and run type.
configs/base.yaml - Training entry point · Runs warm-up, switching, optimization, and final bank construction.
src/train.py - APT phase controller · Switches one-way after Fisher-ratio plateau and warm-up guards.
src/nmfc/apt.py
Inference & evaluation
- Reference bank · Persists training embeddings and frozen geometry scales.
src/models/banks.py - Evaluation utilities · Computes accuracy, silhouette, and OOD AUC from bank-backed inference.
src/evaluate.py - Experiment cache · Completed metrics files gate repeated runs and retain histories.
src/utils/experiments.py - Completed metrics
산출물/cifar100_nmfc/metrics.json - Analysis notebook · Regenerates plots from retained experiment histories.
notebooks/1.NMFC_version1.ipynb
Connections 23
- Experiment configs → Training entry point: configures
- Synthetic & image datasets → PK batch sampler: examples
- Synthetic manifolds → PK batch sampler: examples
- PK batch sampler → Training entry point: PK batches
- Training entry point → Embedding encoders: trains
- Training entry point → Baseline branch: optional comparison
- Embedding encoders → NMFC head: embeddings
- NMFC head → Affinity weights: pair geometry
- Affinity weights → Local energies: weights
- Local energies → Geometric objectives: energies
- Geometric objectives → Training entry point: optimization loss
- Training entry point → APT phase controller: Fisher ratio per epoch
- APT phase controller → Geometric objectives: Phase I → Phase II
- Training entry point → Reference bank: builds after training
- Embedding encoders → Reference bank: training embeddings
- Reference bank → NMFC head: persistent references
- NMFC head → Evaluation utilities: bank-backed logits & energy
- Evaluation utilities → Experiment cache: caches results
- Experiment cache → Completed metrics: completion sentinel
- Experiment cache → Analysis notebook: histories
- Variant dispatch → NMFC head: v1 default
- Variant dispatch → v3 head extension: future dispatch
- v3 head extension → Softmin primitive: uses