# junghwamin/nmfc-plan5-from-scratch-ablation architecture

> Architecture diagram of the GitHub repository junghwamin/nmfc-plan5-from-scratch-ablation, 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-plan5-from-scratch-ablation
- Repository: https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation

## Overview

#### Purpose and Entrypoints
This is a PyTorch NMFC ablation repo testing whether the `shrb` score/energy/reference-bank variant depends on ImageNet-pretrained features. It compares random-init ResNet-50 NMFC against softmax on closed-set CIFAR-100 and open-set 80/100 CIFAR-100.

Primary CLI entry point: `python -m src.train --config ...`, implemented in `src/train.py`. Main experiment specs are `configs/cifar100_scratch.yaml` and `configs/cifar100_openset_scratch.yaml`.

#### Experiment Orchestration
`src/train.py` owns runner dispatch, shared image training setup, two-phase optimization, reference-bank fitting, metric collection, and result persistence. `run_image` handles 100-class closed-set evaluation; `run_openset` reuses the same image core for train-on-80/evaluate-on-100.

The defining plan5 boundary is encoder initialization: `_run_image_core` passes config `pretrained` into `ResNet50Encoder`, defaulting to `True` for compatibility. Scratch configs set `pretrained: false`.
Paths: `src/train.py`, `configs/cifar100_scratch.yaml`, `configs/cifar100_openset_scratch.yaml`.

#### Data and Batch Construction
Image data flows through CIFAR-100 loading and resize/augmentation logic, then PK sampling for metric/energy-oriented batches. Open-set training filters the training split to 80 seen classes while retaining the full evaluation label space; this is the key train/evaluation distribution boundary.
Paths: `src/datasets/image.py`, `src/datasets/samplers.py`, `configs/cifar100_openset_scratch.yaml`.

#### Encoder and Method Assembly
`ResNet50Encoder` wraps torchvision ResNet-50, selecting ImageNet weights only when `pretrained=True`, replacing the classifier with a 128-dimensional embedding projection, and leaving embeddings unnormalized for Euclidean energy calculations.

`src/variants.py` maps the configured four-axis variant specification into the NMFC head. `shrb` selects softmin energy, temperature-hinge logits, reciprocal APT behavior, and minibatch bank behavior; softmax is the control path.
Paths: `src/models/encoders.py`, `src/variants.py`, `src/models/heads_v3.py`.

#### Two-Phase NMFC Training
Training begins with linear MFA warmup, then transitions automatically to full NMFC loss. `APTController` detects a Fisher-ratio plateau subject to configured warmup bounds; scratch runs permit up to 20 warmup epochs because the embedding geometry must form from random initialization.

The NMFC mathematical layer computes affinity, local positive/negative energies, softmin aggregation, geometric logits, and losses. This layer is reused rather than modified by plan5.
Paths: `src/nmfc/losses.py`, `src/nmfc/apt.py`, `src/nmfc/energy.py`.

#### Reference Banks and Evaluation
After training, embeddings populate reference/core-set banks used for inference and open-set transfer. The open-set path reconstructs evaluation behavior over seen and novel classes, allowing `shrb` to assign novel classes through reference-bank geometry while softmax remains restricted to trained output classes.

Evaluation reports classification accuracy, embedding silhouette, and open-set/OOD AUC.
Paths: `src/models/banks.py`, `src/evaluate.py`, `src/train.py`.

#### Runtime, Dependencies, and Outputs
The material runtime is Python/PyTorch with torchvision ResNet-50 and CIFAR-100 data access; pretrained mode may require ImageNet weight download, while scratch mode explicitly avoids it. Runs are GPU-oriented and configuration-driven; no orchestration scripts or web service exist.

Cached histories and metrics are stored under `산출물/`, including closed and open scratch result directories. Checkpoints are intentionally excluded from the repository.
Paths: `산출물/c100s_shrb/metrics.json`, `산출물/c100s_shrb_open/metrics.json`, `tests/test_encoder_pretrained.py`.

Main entry points: [`src/train.py`](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/train.py) (Training CLI), [`src/evaluate.py`](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/evaluate.py) (Closed and open evaluation), [`src/nmfc/losses.py`](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/losses.py) (NMFC energy losses), [`configs/cifar100_openset_scratch.yaml`](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_openset_scratch.yaml) (80-seen-class split)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experiments["Experiment specs"]
  node_cli["Training CLI<br/>Python module<br/>[train.py]"]
  node_closed_config["Closed-set scratch config<br/>YAML config"]
  node_open_config["Open-set scratch config<br/>YAML config"]
end

subgraph group_data["Data pipeline"]
  node_cifar_data["CIFAR-100 loader<br/>dataset pipeline<br/>[image.py]"]
  node_pk_sampler["PK batch sampler<br/>[samplers.py]"]
  node_openset_split{{"80-seen-class split<br/>distribution boundary"}}
end

subgraph group_model["Model assembly"]
  node_encoder["ResNet-50 encoder<br/>embedding model<br/>[encoders.py]"]
  node_variant_factory["Variant mapping<br/>method factory<br/>[variants.py]"]
  node_nmfc_head["NMFC head<br/>classification head<br/>[heads_v3.py]"]
end

subgraph group_training["Training core"]
  node_warmup["Linear MFA warmup<br/>training phase<br/>[train.py]"]
  node_apt{{"APT controller<br/>phase controller<br/>[apt.py]"}}
  node_nmfc_loss["NMFC energy losses<br/>math layer<br/>[losses.py]"]
  node_energy["Energy functions<br/>math layer<br/>[energy.py]"]
end

subgraph group_evaluation["Evaluation and outputs"]
  node_reference_banks[("Reference/core-set banks<br/>embedding bank<br/>[banks.py]")]
  node_evaluation["Closed and open evaluation<br/>evaluator<br/>[evaluate.py]"]
  node_closed_metrics["Closed scratch metrics<br/>result artifact<br/>[metrics.json]"]
  node_open_metrics["Open scratch metrics<br/>result artifact<br/>[metrics.json]"]
end

node_closed_config -->|"configures"| node_cli
node_open_config -->|"configures"| node_cli
node_open_config -->|"defines"| node_openset_split
node_cli -->|"loads images"| node_cifar_data
node_cifar_data -->|"feeds batches"| node_pk_sampler
node_openset_split -->|"filters training"| node_pk_sampler
node_cli -->|"sets pretrained flag"| node_encoder
node_cli -->|"selects variant"| node_variant_factory
node_variant_factory -->|"constructs"| node_nmfc_head
node_encoder -->|"produces embeddings"| node_warmup
node_pk_sampler -->|"supplies batches"| node_warmup
node_warmup -->|"monitors progress"| node_apt
node_apt -->|"transitions to full loss"| node_nmfc_loss
node_nmfc_head -->|"provides head behavior"| node_nmfc_loss
node_energy -->|"supports"| node_nmfc_loss
node_nmfc_loss -->|"trained embeddings fit"| node_reference_banks
node_reference_banks -->|"reference inference"| node_evaluation
node_openset_split -->|"seen/novel evaluation"| node_evaluation
node_evaluation -->|"persists closed results"| node_closed_metrics
node_evaluation -->|"persists open results"| node_open_metrics

click node_cli "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/train.py"
click node_closed_config "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_scratch.yaml"
click node_open_config "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_openset_scratch.yaml"
click node_cifar_data "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/datasets/image.py"
click node_pk_sampler "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/datasets/samplers.py"
click node_openset_split "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_openset_scratch.yaml"
click node_encoder "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/encoders.py"
click node_variant_factory "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/variants.py"
click node_nmfc_head "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/heads_v3.py"
click node_warmup "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/train.py"
click node_apt "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/apt.py"
click node_nmfc_loss "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/losses.py"
click node_energy "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/energy.py"
click node_reference_banks "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/banks.py"
click node_evaluation "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/evaluate.py"
click node_closed_metrics "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100s_shrb/metrics.json"
click node_open_metrics "https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100s_shrb_open/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_cli,node_closed_config,node_open_config toneBlue
class node_cifar_data,node_pk_sampler,node_openset_split toneAmber
class node_encoder,node_variant_factory,node_nmfc_head toneMint
class node_warmup,node_apt,node_nmfc_loss,node_energy toneRose
class node_reference_banks,node_evaluation,node_closed_metrics,node_open_metrics toneIndigo
```

## Components

### Experiment specs

- [Training CLI](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/train.py): Python module · Primary config-driven experiment entry point. · `src/train.py`
- [Closed-set scratch config](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_scratch.yaml): YAML config · Sets pretrained false for CIFAR-100 scratch ablations. · `configs/cifar100_scratch.yaml`
- [Open-set scratch config](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_openset_scratch.yaml): YAML config · Trains on 80 classes and evaluates across 100. · `configs/cifar100_openset_scratch.yaml`

### Data pipeline

- [CIFAR-100 loader](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/datasets/image.py): dataset pipeline · Loads, resizes, and augments image splits. · `src/datasets/image.py`
- [PK batch sampler](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/datasets/samplers.py): sampler · Builds metric and energy-oriented training batches. · `src/datasets/samplers.py`
- [80-seen-class split](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/configs/cifar100_openset_scratch.yaml): distribution boundary · Filters training classes while retaining the full evaluation label space. · `configs/cifar100_openset_scratch.yaml`

### Model assembly

- [ResNet-50 encoder](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/encoders.py): embedding model · Projects images to unnormalized 128-dimensional embeddings; pretrained is config-controlled. · `src/models/encoders.py`
- [Variant mapping](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/variants.py): method factory · Builds shrb or softmax behavior from the four-axis specification. · `src/variants.py`
- [NMFC head](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/heads_v3.py): classification head · Implements the configured affinity and logit head. · `src/models/heads_v3.py`

### Training core

- [Linear MFA warmup](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/train.py): training phase · Initial phase for forming scratch embedding geometry. · `src/train.py`
- [APT controller](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/apt.py): phase controller · Uses Fisher-ratio plateau and warmup bounds to transition training. · `src/nmfc/apt.py`
- [NMFC energy losses](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/losses.py): math layer · Computes affinity, local energies, softmin aggregation, geometric logits, and losses. · `src/nmfc/losses.py`
- [Energy functions](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/nmfc/energy.py): math layer · Provides reusable energy calculations for NMFC. · `src/nmfc/energy.py`

### Evaluation and outputs

- [Reference/core-set banks](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/models/banks.py): embedding bank · Fits post-training references used during inference and transfer. · `src/models/banks.py`
- [Closed and open evaluation](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/src/evaluate.py): evaluator · Reports accuracy, silhouette, and open-set/OOD AUC. · `src/evaluate.py`
- [Closed scratch metrics](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100s_shrb/metrics.json): result artifact · `산출물/c100s_shrb/metrics.json`
- [Open scratch metrics](https://github.com/junghwamin/nmfc-plan5-from-scratch-ablation/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/c100s_shrb_open/metrics.json): result artifact · `산출물/c100s_shrb_open/metrics.json`

## Connections

- Closed-set scratch config → Training CLI: configures
- Open-set scratch config → Training CLI: configures
- Open-set scratch config → 80-seen-class split: defines
- Training CLI → CIFAR-100 loader: loads images
- CIFAR-100 loader → PK batch sampler: feeds batches
- 80-seen-class split → PK batch sampler: filters training
- Training CLI → ResNet-50 encoder: sets pretrained flag
- Training CLI → Variant mapping: selects variant
- Variant mapping → NMFC head: constructs
- ResNet-50 encoder → Linear MFA warmup: produces embeddings
- PK batch sampler → Linear MFA warmup: supplies batches
- Linear MFA warmup → APT controller: monitors progress
- APT controller → NMFC energy losses: transitions to full loss
- NMFC head → NMFC energy losses: provides head behavior
- Energy functions → NMFC energy losses: supports
- NMFC energy losses → Reference/core-set banks: trained embeddings fit
- Reference/core-set banks → Closed and open evaluation: reference inference
- 80-seen-class split → Closed and open evaluation: seen/novel evaluation
- Closed and open evaluation → Closed scratch metrics: persists closed results
- Closed and open evaluation → Open scratch metrics: persists open results
