# junghwamin/nmfc-plan7-retrieval-cluster architecture

> Architecture diagram of the GitHub repository junghwamin/nmfc-plan7-retrieval-cluster, 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-plan7-retrieval-cluster
- Repository: https://github.com/junghwamin/nmfc-plan7-retrieval-cluster

## Overview

#### Purpose and execution model
This repository is plan7 of an NMFC research series: a no-retraining analysis that compares shrb/NMFC and softmax using cached test embeddings only. Its practical question is whether embedding-geometry gains translate into retrieval, clustering, verification, and few-shot recognition. Primary runnable entry points are `scripts/eval_retrieval.py`, `scripts/eval_fewshot_verify.py`, and `scripts/build_report_figures.py`.

#### Input boundary: cached experiment embeddings
Analysis consumes external, gitignored `results/<experiment>/embeddings.npz` files containing `z_test` (expected 10,000 × 128) and `y_test`. The repository does not include those inputs; `산출물/_읽는입력_embeddings_안내.txt` documents this dependency. `src/utils/experiments.py` is the shared read-side boundary through `load_embeddings` and `collect_results`; analysis does not load checkpoints or invoke training.

#### Retrieval and clustering evaluation
`scripts/eval_retrieval.py` is the main geometry evaluator. It L2-normalizes embeddings, performs GPU-chunked cosine nearest-neighbor retrieval, excludes each query itself, and computes Recall@1, Recall@5, and mAP. It also clusters embeddings with KMeans for purity and NMI. Built-in assertions/self-tests protect AP, purity, class-count, and leave-one-out assumptions. Results are persisted in `산출물/retrieval_eval.json`.

#### Verification and few-shot evaluation
`scripts/eval_fewshot_verify.py` measures pairwise same/different-class separability via balanced sampled cosine-score AUC, then evaluates prototype nearest-class-mean recognition. Few-shot performance is swept across K-shot settings and summarized with a class-incremental gallery curve. Its self-test validates separable synthetic cases. Results are written to `산출물/fewshot_verify_eval.json`.

#### Reporting layer
`scripts/build_report_figures.py` reads both evaluation JSON outputs plus embeddings to create report-ready visualizations: t-SNE, retrieval comparison, few-shot curves, incremental curves, and reference-bank-related plots. It is a terminal consumer rather than part of metric computation, producing files under `산출물/figures`.

#### Inherited embedding-production pipeline
The `src/` tree is inherited and intentionally unmodified by plan7. In other plans, data flows from image datasets through an encoder and NMFC/softmax head to produce the cached embedding artifact:
- Data and sampling: `src/datasets/image.py`, `src/datasets/samplers.py`
- Encoder/head assembly: `src/models/encoders.py`, `src/models/heads.py`, `src/models/heads_v3.py`
- Reference-bank state: `src/models/banks.py`
- NMFC local-geometry computation: `src/nmfc/affinity.py`, `src/nmfc/energy.py`, `src/nmfc/losses.py`
- Training/evaluation orchestration: `src/train.py`, `src/evaluate.py`, `src/variants.py`

#### Runtime and external dependencies
The evaluation scripts require Python with NumPy/PyTorch for embedding and GPU-chunked similarity operations, scikit-learn for KMeans, NMI, ROC AUC, and likely t-SNE, plus matplotlib with a noninteractive Agg backend for reports. There is no service/API layer, configuration system, or training runtime required for plan7; the material infrastructure dependency is access to the external `results/` embedding cache.

Main entry points: [`scripts/build_report_figures.py`](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/build_report_figures.py) (Report figure builder), [`src/utils/experiments.py`](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/utils/experiments.py) (Experiment reader), [`scripts/eval_retrieval.py`](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_retrieval.py) (Retrieval evaluator), [`scripts/eval_fewshot_verify.py`](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_fewshot_verify.py) (Few-shot verifier)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_plan7["Plan7 analysis"]
  node_embedding_cache[("Cached test embeddings<br/>external artifact")]
  node_input_guide["Embedding input guide<br/>documentation"]
  node_experiment_reader["Experiment reader<br/>shared input boundary<br/>[experiments.py]"]
  node_retrieval_entry["Retrieval evaluator<br/>CLI script<br/>[eval_retrieval.py]"]
  node_retrieval_metrics["Retrieval metrics<br/>evaluation stage"]
  node_clustering_metrics["Clustering metrics<br/>evaluation stage"]
  node_retrieval_output["Retrieval results<br/>JSON artifact"]
  node_fewshot_entry["Few-shot verifier<br/>CLI script"]
  node_verification_metrics["Verification AUC<br/>evaluation stage"]
  node_prototype_metrics["Few-shot prototypes<br/>evaluation stage"]
  node_fewshot_output["Few-shot results<br/>JSON artifact"]
  node_report_builder["Report figure builder<br/>CLI script"]
  node_figures["Report figures<br/>visual artifacts"]
end

subgraph group_production["Inherited embedding production"]
  node_image_data["Image data and sampling<br/>data pipeline<br/>[image.py]"]
  node_samplers["Sampling<br/>data pipeline<br/>[samplers.py]"]
  node_model_assembly["Encoder and heads<br/>model assembly<br/>[encoders.py]"]
  node_reference_bank[("Reference-bank state<br/>model state<br/>[banks.py]")]
  node_nmfc_geometry["NMFC geometry<br/>loss computation<br/>[affinity.py]"]
  node_training_runtime["Training and evaluation<br/>orchestration<br/>[train.py]"]
end

node_input_guide -.->|"documents"| node_embedding_cache
node_embedding_cache -->|"loaded by"| node_experiment_reader
node_experiment_reader -->|"embeddings"| node_retrieval_entry
node_retrieval_entry -->|"cosine retrieval"| node_retrieval_metrics
node_retrieval_entry -->|"KMeans"| node_clustering_metrics
node_retrieval_metrics -->|"metrics"| node_retrieval_output
node_clustering_metrics -->|"metrics"| node_retrieval_output
node_experiment_reader -->|"embeddings"| node_fewshot_entry
node_fewshot_entry -->|"cosine pairs"| node_verification_metrics
node_fewshot_entry -->|"K-shot sweep"| node_prototype_metrics
node_verification_metrics -->|"AUC"| node_fewshot_output
node_prototype_metrics -->|"recognition curves"| node_fewshot_output
node_embedding_cache -->|"embeddings"| node_report_builder
node_retrieval_output -->|"retrieval data"| node_report_builder
node_fewshot_output -->|"few-shot data"| node_report_builder
node_report_builder -->|"renders"| node_figures
node_image_data -->|"images"| node_samplers
node_samplers -->|"batches"| node_model_assembly
node_model_assembly -->|"embeddings"| node_nmfc_geometry
node_reference_bank -->|"references"| node_nmfc_geometry
node_nmfc_geometry -->|"training loss"| node_training_runtime
node_model_assembly -->|"softmax or NMFC model"| node_training_runtime
node_training_runtime -.->|"exports in other plans"| node_embedding_cache

click node_input_guide "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/_%EC%9D%BD%EB%8A%94%EC%9E%85%EB%A0%A5_embeddings_%EC%95%88%EB%82%B4.txt"
click node_experiment_reader "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/utils/experiments.py"
click node_retrieval_entry "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_retrieval.py"
click node_retrieval_output "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/retrieval_eval.json"
click node_fewshot_entry "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_fewshot_verify.py"
click node_fewshot_output "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/fewshot_verify_eval.json"
click node_report_builder "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/build_report_figures.py"
click node_figures "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/tree/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/figures"
click node_image_data "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/datasets/image.py"
click node_samplers "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/datasets/samplers.py"
click node_model_assembly "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/models/encoders.py"
click node_reference_bank "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/models/banks.py"
click node_nmfc_geometry "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/nmfc/affinity.py"
click node_training_runtime "https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/train.py"

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_embedding_cache,node_input_guide,node_experiment_reader,node_retrieval_entry,node_retrieval_metrics,node_clustering_metrics,node_retrieval_output,node_fewshot_entry,node_verification_metrics,node_prototype_metrics,node_fewshot_output,node_report_builder,node_figures toneBlue
class node_image_data,node_samplers,node_model_assembly,node_reference_bank,node_nmfc_geometry,node_training_runtime toneAmber
```

## Components

### Plan7 analysis

No-retraining evaluation over cached test embeddings.

- **Cached test embeddings**: external artifact · Gitignored NPZ files containing z_test and y_test.
- [Embedding input guide](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/_%EC%9D%BD%EB%8A%94%EC%9E%85%EB%A0%A5_embeddings_%EC%95%88%EB%82%B4.txt): documentation · Documents the required external cache. · `산출물/_읽는입력_embeddings_안내.txt`
- [Experiment reader](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/utils/experiments.py): shared input boundary · Loads embeddings and collects experiment results. · `src/utils/experiments.py`
- [Retrieval evaluator](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_retrieval.py): CLI script · Main geometry evaluation entry point. · `scripts/eval_retrieval.py`
- **Retrieval metrics**: evaluation stage · Normalized chunked cosine search yields Recall@1/5 and mAP.
- **Clustering metrics**: evaluation stage · KMeans produces purity and NMI.
- [Retrieval results](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/retrieval_eval.json): JSON artifact · `산출물/retrieval_eval.json`
- [Few-shot verifier](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/eval_fewshot_verify.py): CLI script · Evaluates pairwise verification and prototype recognition. · `scripts/eval_fewshot_verify.py`
- **Verification AUC**: evaluation stage · Balanced same/different cosine-score sampling.
- **Few-shot prototypes**: evaluation stage · K-shot nearest-class-mean and incremental gallery curves.
- [Few-shot results](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/fewshot_verify_eval.json): JSON artifact · `산출물/fewshot_verify_eval.json`
- [Report figure builder](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/scripts/build_report_figures.py): CLI script · Terminal consumer for metrics and embeddings. · `scripts/build_report_figures.py`
- [Report figures](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/tree/main/%EC%82%B0%EC%B6%9C%EB%AC%BC/figures): visual artifacts · t-SNE, retrieval, few-shot, incremental, and bank-related plots. · `산출물/figures`

### Inherited embedding production

Unmodified pipeline that produces caches in other plans.

- [Image data and sampling](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/datasets/image.py): data pipeline · `src/datasets/image.py`
- [Sampling](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/datasets/samplers.py): data pipeline · `src/datasets/samplers.py`
- [Encoder and heads](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/models/encoders.py): model assembly · `src/models/encoders.py`
- [Reference-bank state](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/models/banks.py): model state · `src/models/banks.py`
- [NMFC geometry](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/nmfc/affinity.py): loss computation · Affinity, energy, and loss computation. · `src/nmfc/affinity.py`
- [Training and evaluation](https://github.com/junghwamin/nmfc-plan7-retrieval-cluster/blob/main/src/train.py): orchestration · Inherited runtime exports experiment embeddings. · `src/train.py`

## Connections

- Embedding input guide → Cached test embeddings: documents
- Cached test embeddings → Experiment reader: loaded by
- Experiment reader → Retrieval evaluator: embeddings
- Retrieval evaluator → Retrieval metrics: cosine retrieval
- Retrieval evaluator → Clustering metrics: KMeans
- Retrieval metrics → Retrieval results: metrics
- Clustering metrics → Retrieval results: metrics
- Experiment reader → Few-shot verifier: embeddings
- Few-shot verifier → Verification AUC: cosine pairs
- Few-shot verifier → Few-shot prototypes: K-shot sweep
- Verification AUC → Few-shot results: AUC
- Few-shot prototypes → Few-shot results: recognition curves
- Cached test embeddings → Report figure builder: embeddings
- Retrieval results → Report figure builder: retrieval data
- Few-shot results → Report figure builder: few-shot data
- Report figure builder → Report figures: renders
- Image data and sampling → Sampling: images
- Sampling → Encoder and heads: batches
- Encoder and heads → NMFC geometry: embeddings
- Reference-bank state → NMFC geometry: references
- NMFC geometry → Training and evaluation: training loss
- Encoder and heads → Training and evaluation: softmax or NMFC model
- Training and evaluation → Cached test embeddings: exports in other plans
