# junghwamin/nmfc-plan1-v1-reproduction architecture

> Architecture diagram of the GitHub repository junghwamin/nmfc-plan1-v1-reproduction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/junghwamin/nmfc-plan1-v1-reproduction
- Repository: https://github.com/junghwamin/nmfc-plan1-v1-reproduction

## Overview

#### 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: [`src/train.py`](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/train.py) (Training entry point), [`src/models/heads.py`](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/heads.py) (NMFC head), [`src/models/encoders.py`](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/encoders.py) (Embedding encoders), [`src/utils/experiments.py`](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/utils/experiments.py) (Experiment cache)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data &amp; batches"]
  node_datasets["Synthetic &amp; image datasets<br/>dataset loaders<br/>[image.py]"]
  node_synthetic["Synthetic manifolds<br/>dataset loader<br/>[synthetic.py]"]
  node_pk_sampler["PK batch sampler<br/>balanced sampler<br/>[samplers.py]"]
end

subgraph group_models["Models &amp; NMFC"]
  node_encoders["Embedding encoders<br/>MLP / ResNet-50<br/>[encoders.py]"]
  node_baselines["Baseline branch<br/>comparison heads<br/>[baselines.py]"]
  node_nmfc_head["NMFC head<br/>stateful geometry head<br/>[heads.py]"]
  node_affinity["Affinity weights<br/>NMFC math<br/>[affinity.py]"]
  node_energy["Local energies<br/>NMFC math<br/>[energy.py]"]
  node_losses["Geometric objectives<br/>NMFC losses<br/>[losses.py]"]
  node_variants["Variant dispatch<br/>extension boundary<br/>[variants.py]"]
  node_v3_head["v3 head extension<br/>future variant<br/>[heads_v3.py]"]
  node_softmin["Softmin primitive<br/>future math module<br/>[softmin.py]"]
end

subgraph group_training["Training control"]
  node_configs["Experiment configs<br/>YAML configs<br/>[base.yaml]"]
  node_train["Training entry point<br/>PyTorch runner<br/>[train.py]"]
  node_apt{{"APT phase controller<br/>torch-free state machine<br/>[apt.py]"}}
end

subgraph group_evaluation["Inference &amp; evaluation"]
  node_reference_bank[("Reference bank<br/>labeled embedding store<br/>[banks.py]")]
  node_evaluate["Evaluation utilities<br/>metrics runner<br/>[evaluate.py]"]
  node_experiment_cache[("Experiment cache<br/>results sentinel<br/>[experiments.py]")]
  node_metrics["Completed metrics<br/>JSON result<br/>[metrics.json]"]
  node_notebook["Analysis notebook"]
end

node_configs -->|"configures"| node_train
node_datasets -->|"examples"| node_pk_sampler
node_synthetic -->|"examples"| node_pk_sampler
node_pk_sampler -->|"PK batches"| node_train
node_train -->|"trains"| node_encoders
node_train -.->|"optional comparison"| node_baselines
node_encoders -->|"embeddings"| node_nmfc_head
node_nmfc_head -->|"pair geometry"| node_affinity
node_affinity -->|"weights"| node_energy
node_energy -->|"energies"| node_losses
node_losses -->|"optimization loss"| node_train
node_train -->|"Fisher ratio per epoch"| node_apt
node_apt -->|"Phase I → Phase II"| node_losses
node_train -->|"builds after training"| node_reference_bank
node_encoders -->|"training embeddings"| node_reference_bank
node_reference_bank -->|"persistent references"| node_nmfc_head
node_nmfc_head -->|"bank-backed logits &amp; energy"| node_evaluate
node_evaluate -->|"caches results"| node_experiment_cache
node_experiment_cache -->|"completion sentinel"| node_metrics
node_experiment_cache -->|"histories"| node_notebook
node_variants -->|"v1 default"| node_nmfc_head
node_variants -.->|"future dispatch"| node_v3_head
node_v3_head -.->|"uses"| node_softmin

click node_configs "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/configs/base.yaml"
click node_train "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/train.py"
click node_datasets "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/image.py"
click node_synthetic "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/synthetic.py"
click node_pk_sampler "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/samplers.py"
click node_encoders "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/encoders.py"
click node_baselines "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/baselines.py"
click node_nmfc_head "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/heads.py"
click node_affinity "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/affinity.py"
click node_energy "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/energy.py"
click node_losses "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/losses.py"
click node_apt "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/apt.py"
click node_reference_bank "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/banks.py"
click node_evaluate "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/evaluate.py"
click node_experiment_cache "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/utils/experiments.py"
click node_metrics "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/cifar100_nmfc/metrics.json"
click node_variants "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/variants.py"
click node_v3_head "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/heads_v3.py"
click node_softmin "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/softmin.py"
click node_notebook "https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/notebooks/1.NMFC_version1.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_datasets,node_synthetic,node_pk_sampler toneBlue
class node_encoders,node_baselines,node_nmfc_head,node_affinity,node_energy,node_losses,node_variants,node_v3_head,node_softmin toneAmber
class node_configs,node_train,node_apt toneMint
class node_reference_bank,node_evaluate,node_experiment_cache,node_metrics,node_notebook toneRose
```

## Components

### Data & batches

- [Synthetic & image datasets](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/image.py): dataset loaders · Provides manifolds, CIFAR, and OOD image data. · `src/datasets/image.py`
- [Synthetic manifolds](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/synthetic.py): dataset loader · `src/datasets/synthetic.py`
- [PK batch sampler](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/datasets/samplers.py): balanced sampler · Ensures valid positive and negative neighborhood pairs. · `src/datasets/samplers.py`

### Models & NMFC

- [Embedding encoders](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/encoders.py): MLP / ResNet-50 · Maps inputs to configured Euclidean embeddings. · `src/models/encoders.py`
- [Baseline branch](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/baselines.py): comparison heads · Implements softmax, ArcFace, prototype, triplet, and Deep-MFA paths. · `src/models/baselines.py`
- [NMFC head](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/heads.py): stateful geometry head · Uses batch-local logits in training and bank-backed logits in inference. · `src/models/heads.py`
- [Affinity weights](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/affinity.py): NMFC math · Computes pairwise distances and auto-tuned neighborhood affinities. · `src/nmfc/affinity.py`
- [Local energies](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/energy.py): NMFC math · Computes positive and negative neighborhood energies. · `src/nmfc/energy.py`
- [Geometric objectives](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/losses.py): NMFC losses · Applies linear-MFA warm-up or NMFC NLL. · `src/nmfc/losses.py`
- [Variant dispatch](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/variants.py): extension boundary · v1 defaults to NMFCHead; later variants are not active in v1 configs. · `src/variants.py`
- [v3 head extension](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/heads_v3.py): future variant · `src/models/heads_v3.py`
- [Softmin primitive](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/softmin.py): future math module · `src/nmfc/softmin.py`

### Training control

- [Experiment configs](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/configs/base.yaml): YAML configs · Select data, model, controller, and run type. · `configs/base.yaml`
- [Training entry point](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/train.py): PyTorch runner · Runs warm-up, switching, optimization, and final bank construction. · `src/train.py`
- [APT phase controller](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/nmfc/apt.py): torch-free state machine · Switches one-way after Fisher-ratio plateau and warm-up guards. · `src/nmfc/apt.py`

### Inference & evaluation

- [Reference bank](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/models/banks.py): labeled embedding store · Persists training embeddings and frozen geometry scales. · `src/models/banks.py`
- [Evaluation utilities](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/evaluate.py): metrics runner · Computes accuracy, silhouette, and OOD AUC from bank-backed inference. · `src/evaluate.py`
- [Experiment cache](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/src/utils/experiments.py): results sentinel · Completed metrics files gate repeated runs and retain histories. · `src/utils/experiments.py`
- [Completed metrics](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/cifar100_nmfc/metrics.json): JSON result · `산출물/cifar100_nmfc/metrics.json`
- [Analysis notebook](https://github.com/junghwamin/nmfc-plan1-v1-reproduction/blob/main/notebooks/1.NMFC_version1.ipynb): notebook · Regenerates plots from retained experiment histories. · `notebooks/1.NMFC_version1.ipynb`

## Connections

- 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
