# junghwamin/nmfc-plan3-v4-sphere-negj architecture

> Architecture diagram of the GitHub repository junghwamin/nmfc-plan3-v4-sphere-negj, 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-plan3-v4-sphere-negj
- Repository: https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj

## Overview

#### Purpose and Entry Points
This is a PyTorch NMFC experiment repository for validating a v4 change to Phase-I Fisher discriminant warmup: compute `−J` on unit-normalized embeddings to prevent scale-driven embedding expansion. Primary execution is `python -m src.train --config ...`; evaluation is invoked from training and implemented in `src/evaluate.py`. Experiment definitions live in `configs/cifar100_variants_wave3.yaml` and `configs/ood_cifar10_variants_wave3.yaml`.

#### Configuration and Variant Assembly
`src/variants.py` centralizes a four-axis experiment vocabulary: energy, logit, APT/warmup, and bank behavior. Its APT axis selects reciprocal, raw `negJ`, sphere `negJ`, or trace-ratio variants and uses `is_negj_family()` to keep Phase-I behavior consistent. Config files define named runs such as sphere/reference-bank and sphere/online-bank variants.
Paths: `src/variants.py`, `configs/base.yaml`, `configs/cifar100_variants_wave3.yaml`.

#### Data and Representation Pipeline
Image datasets—including CIFAR-10/100 and SVHN for OOD—are loaded through the dataset layer; class-balanced PK sampling supports scatter/Fisher estimates. Batches flow into a ResNet50-based encoder that emits embedding `z`; this raw embedding remains the model and inference representation.
Paths: `src/datasets/image.py`, `src/datasets/samplers.py`, `src/models/encoders.py`.

#### NMFC Model and Memory Banks
`VariantNMFCHead` composes the selected geometry/logit/bank behaviors over encoder embeddings. Reference and online class banks supply class prototypes or dynamic representation state. This boundary separates learned visual representation from NMFC class-geometry computation.
Paths: `src/models/heads_v3.py`, `src/models/banks.py`, `src/models/heads.py`.

#### Two-Phase Training Control
`src/train.py` owns the stateful training loop. Phase I applies discriminative warmup; for negJ-family variants it routes batch embeddings to Fisher monitoring/loss and, for trace-ratio mode, establishes κ from the median observed Phase-I J values. `ACSController` detects Fisher-progress stagnation and latches a one-way transition to Phase II. Training logs metrics, history, and embedding-norm diagnostics under experiment output directories.
Paths: `src/train.py`, `src/nmfc/acs.py`, `산출물/c100v_shnb/history.json`.

#### Phase-I Fisher Objective: v4 Core Change
`fisher_j` computes within/between-class scatter and the Fisher trace criterion. The sphere variant normalizes only inside this calculation (`z/||z||`), making its J scale-invariant; it does not normalize stored embeddings, Phase-II inputs, or inference inputs. `neg_j_loss` supports raw `−J` and bounded trace-ratio `−J/(J+κ)`.
Paths: `src/nmfc/acs.py`, `src/nmfc/losses.py`, `tests/test_acs.py`.

#### Phase-II NMFC Objective and Evaluation
After ACS transition, the head’s affinity, softmin energy, geometric logits, and NMFC NLL drive optimization. Evaluation reports classification Top-1/Top-5, embedding silhouette, and CIFAR-vs-SVHN OOD AUC; results are persisted as JSON. The architecture is local Python/PyTorch execution with dataset downloads/storage and no indicated deployed service infrastructure.
Paths: `src/nmfc/affinity.py`, `src/nmfc/softmin.py`, `src/nmfc/energy.py`, `src/evaluate.py`, `산출물/ood10v_shnb/metrics.json`.

Main entry points: [`src/train.py`](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/train.py) (Training loop), [`src/models/heads_v3.py`](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/heads_v3.py) (Variant NMFC head), [`configs/cifar100_variants_wave3.yaml`](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/configs/cifar100_variants_wave3.yaml) (Experiment configs), [`src/nmfc/energy.py`](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/energy.py) (NMFC NLL)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experiment["Experiment Assembly"]
  node_configs["Experiment configs<br/>YAML variants"]
  node_base_config["Base configuration<br/>YAML defaults<br/>[base.yaml]"]
  node_variants["Variant vocabulary<br/>variant assembler<br/>[variants.py]"]
end

subgraph group_representation["Data &amp; Representation"]
  node_datasets["Image datasets<br/>data loader<br/>[image.py]"]
  node_sampler["PK sampler<br/>balanced sampler<br/>[samplers.py]"]
  node_encoder["ResNet50 encoder<br/>PyTorch encoder<br/>[encoders.py]"]
end

subgraph group_training["Training &amp; Objectives"]
  node_train["Training loop<br/>CLI trainer<br/>[train.py]"]
  node_acs["ACS controller<br/>phase controller<br/>[acs.py]"]
  node_fisher["Fisher J<br/>scatter criterion<br/>[acs.py]"]
  node_negj_loss["neg-J loss<br/>Phase-I loss<br/>[losses.py]"]
  node_head["Variant NMFC head<br/>NMFC geometry head<br/>[heads_v3.py]"]
  node_banks[("Class memory banks<br/>prototype state<br/>[banks.py]")]
  node_affinity["Affinity<br/>NMFC primitive<br/>[affinity.py]"]
  node_softmin["Softmin energy<br/>NMFC primitive<br/>[softmin.py]"]
  node_energy["NMFC NLL<br/>Phase-II objective<br/>[energy.py]"]
end

subgraph group_outputs["Evaluation &amp; Outputs"]
  node_evaluate["Evaluation<br/>metrics runner<br/>[evaluate.py]"]
  node_history["Training history<br/>experiment artifact<br/>[history.json]"]
  node_ood_metrics["OOD metrics<br/>result artifact<br/>[metrics.json]"]
end

node_base_config -->|"defaults"| node_configs
node_configs -->|"selects axes"| node_variants
node_configs -->|"run config"| node_train
node_variants -->|"configures"| node_head
node_datasets -->|"examples"| node_sampler
node_sampler -->|"balanced batches"| node_encoder
node_encoder -->|"raw z"| node_head
node_train -->|"monitors Phase I"| node_acs
node_train -->|"negJ-family z"| node_fisher
node_fisher -->|"J and κ"| node_negj_loss
node_negj_loss -->|"Phase-I optimization"| node_train
node_acs -->|"enables Phase II"| node_energy
node_head -->|"reads and updates"| node_banks
node_head -->|"class geometry"| node_affinity
node_affinity -->|"affinities"| node_softmin
node_softmin -->|"energy"| node_energy
node_energy -->|"Phase-II optimization"| node_train
node_train -->|"invokes"| node_evaluate
node_train -->|"writes"| node_history
node_evaluate -->|"writes JSON"| node_ood_metrics

click node_configs "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/configs/cifar100_variants_wave3.yaml"
click node_base_config "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/configs/base.yaml"
click node_variants "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/variants.py"
click node_datasets "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/datasets/image.py"
click node_sampler "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/datasets/samplers.py"
click node_encoder "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/encoders.py"
click node_train "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/train.py"
click node_acs "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/acs.py"
click node_fisher "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/acs.py"
click node_negj_loss "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/losses.py"
click node_head "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/heads_v3.py"
click node_banks "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/banks.py"
click node_affinity "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/affinity.py"
click node_softmin "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/softmin.py"
click node_energy "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/energy.py"
click node_evaluate "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/evaluate.py"
click node_history "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100v_shnb/history.json"
click node_ood_metrics "https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/ood10v_shnb/metrics.json"

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_configs,node_base_config,node_variants toneBlue
class node_datasets,node_sampler,node_encoder toneAmber
class node_train,node_acs,node_fisher,node_negj_loss,node_head,node_banks,node_affinity,node_softmin,node_energy toneMint
class node_evaluate,node_history,node_ood_metrics toneRose
```

## Components

### Experiment Assembly

- [Experiment configs](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/configs/cifar100_variants_wave3.yaml): YAML variants · Named CIFAR-100 and OOD variant runs. · `configs/cifar100_variants_wave3.yaml`
- [Base configuration](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/configs/base.yaml): YAML defaults · `configs/base.yaml`
- [Variant vocabulary](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/variants.py): variant assembler · Combines energy, logit, APT, and bank axes. · `src/variants.py`

### Data & Representation

- [Image datasets](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/datasets/image.py): data loader · Loads CIFAR and SVHN image data. · `src/datasets/image.py`
- [PK sampler](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/datasets/samplers.py): balanced sampler · Builds class-balanced batches for Fisher estimates. · `src/datasets/samplers.py`
- [ResNet50 encoder](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/encoders.py): PyTorch encoder · Produces raw embedding z used by model and inference. · `src/models/encoders.py`

### Training & Objectives

- [Training loop](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/train.py): CLI trainer · Primary python -m src.train execution and phase control. · `src/train.py`
- [ACS controller](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/acs.py): phase controller · Latches a one-way transition after Fisher-progress stagnation. · `src/nmfc/acs.py`
- [Fisher J](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/acs.py): scatter criterion · Computes within/between scatter; sphere mode normalizes only here. · `src/nmfc/acs.py`
- [neg-J loss](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/losses.py): Phase-I loss · Supports raw −J and bounded trace-ratio loss. · `src/nmfc/losses.py`
- [Variant NMFC head](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/heads_v3.py): NMFC geometry head · Applies selected geometry, logits, and bank behavior to z. · `src/models/heads_v3.py`
- [Class memory banks](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/models/banks.py): prototype state · Reference and online class representation state. · `src/models/banks.py`
- [Affinity](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/affinity.py): NMFC primitive · `src/nmfc/affinity.py`
- [Softmin energy](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/softmin.py): NMFC primitive · `src/nmfc/softmin.py`
- [NMFC NLL](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/nmfc/energy.py): Phase-II objective · Optimizes geometric logits and softmin energy. · `src/nmfc/energy.py`

### Evaluation & Outputs

- [Evaluation](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/src/evaluate.py): metrics runner · Reports Top-1/5, silhouette, and CIFAR-vs-SVHN OOD AUC. · `src/evaluate.py`
- [Training history](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100v_shnb/history.json): experiment artifact · Metrics and embedding-norm diagnostics. · `산출물/c100v_shnb/history.json`
- [OOD metrics](https://github.com/junghwamin/nmfc-plan3-v4-sphere-negj/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/ood10v_shnb/metrics.json): result artifact · Persisted OOD evaluation results. · `산출물/ood10v_shnb/metrics.json`

## Connections

- Base configuration → Experiment configs: defaults
- Experiment configs → Variant vocabulary: selects axes
- Experiment configs → Training loop: run config
- Variant vocabulary → Variant NMFC head: configures
- Image datasets → PK sampler: examples
- PK sampler → ResNet50 encoder: balanced batches
- ResNet50 encoder → Variant NMFC head: raw z
- Training loop → ACS controller: monitors Phase I
- Training loop → Fisher J: negJ-family z
- Fisher J → neg-J loss: J and κ
- neg-J loss → Training loop: Phase-I optimization
- ACS controller → NMFC NLL: enables Phase II
- Variant NMFC head → Class memory banks: reads and updates
- Variant NMFC head → Affinity: class geometry
- Affinity → Softmin energy: affinities
- Softmin energy → NMFC NLL: energy
- NMFC NLL → Training loop: Phase-II optimization
- Training loop → Evaluation: invokes
- Training loop → Training history: writes
- Evaluation → OOD metrics: writes JSON
