# yzd-v/cls_kd architecture

> Architecture diagram of the GitHub repository yzd-v/cls_kd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yzd-v/cls_kd
- Repository: https://github.com/yzd-v/cls_kd

## Overview

1. This repository is a PyTorch-based image classification and knowledge-distillation codebase built on the OpenMMLab/MMCls configuration-and-registry style. The core architectural idea is to compose experiments from reusable config fragments rather than hard-coded training scripts.

2. The main application logic lives under `mmcls/`, which is the library boundary. It contains:
   - `models/` for classifiers, backbones, heads, necks, losses, and distillation losses
   - `datasets/` for dataset definitions, wrappers, samplers, and transforms
   - `evaluation/` for task-specific metrics
   - `engine/` for runtime hooks and optimizer extensions
   - `apis/` for inference/model loading
   - `structures/` for typed data containers passed through the pipeline

3. The distillation subsystem is a first-class architectural slice, not an add-on. `mmcls/models/classifiers/distiller.py` coordinates teacher/student training, while `mmcls/models/dis_losses/` contains method-specific losses such as KD, DKD, WSLD, MGD, SRRL, NKD, USKD, and ViTKD. The distillation configs under `configs/distillers/` wire these pieces together for specific teacher-student pairs and datasets.

4. Model assembly is modular. A classifier typically combines a backbone with an optional neck and a head, while some families have specialized heads or necks. The repo includes many backbone families, from classic CNNs like ResNet, VGG, DenseNet, and MobileNet to transformer-style models like ViT, DeiT, Swin, Conformer, XCiT, and EVA. This means the diagram should show a shared classifier interface feeding multiple model families.

5. Data flows start in dataset modules and transforms. The repo supports multiple classification and retrieval datasets such as ImageNet, CIFAR, CUB, VOC, MNIST, InShop, and custom multi-label/multi-task formats. Preprocessing is handled by pipeline transforms, batch augmentations like mixup/cutmix/resizemix, and model-side data preprocessors, so the input path spans both dataset code and model utilities.

6. Configuration is layered. `configs/_base_/` provides shared dataset, schedule, runtime, and model fragments; top-level model folders define canonical recipes; and `configs/distillers/` defines experiment-specific distillation setups. This structure is central to the architecture because it determines how experiments are built and how model/dataset/schedule choices are combined.

7. Training/runtime concerns are separated into `mmcls/engine/`. Hooks handle things like EMA, precise batch norm, class-count checks, recipe switching, retriever behavior, and visualization. Optimizer variants such as Adan-T and LAMB are also registered here, which indicates the runtime is designed for plug-in training behavior rather than a fixed loop.

8. Evaluation is explicit and pluggable. `mmcls/evaluation/metrics/` contains single-label, multi-label, retrieval, VOC multi-label, multi-task, and corruption-error metrics. This means the repo supports several task boundaries beyond standard single-label classification, and the evaluation path is chosen by config.

9. The repository includes operational entry points under `tools/` for distributed training, testing, log analysis, FLOPs/complexity analysis, dataset verification, and config printing. There are also model-conversion and reparameterization utilities, which serve as bridges between training-time models and deployable or external checkpoints.

10. Deployment is represented as a separate boundary. `tools/torchserve/`, `docker/serve/`, and several `deploy` config variants for models such as MobileOne, LeViT, ReplKNet, and RepMLP show that some architectures have an inference-ready/export path distinct from training.

11. The repo also carries example and research-extension areas. `projects/example_project/` demonstrates how to add a custom model and config, while `projects/ood_eval/` shows an out-of-distribution evaluation workflow. These project folders are useful for understanding how the core library is extended without modifying the main package.

12. Tests and docs mirror the architecture. The test suite exercises datasets, transforms, hooks, metrics, models, utilities, and visualization components, which confirms the major subsystem boundaries. The docs explain configuration, pipelines, runtime, schedules, and data flow, reinforcing that the repo is organized around composable training/evaluation pipelines for distillation and classification.

Main entry points: [`mmcls/models/classifiers`](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/classifiers) (Classifiers), [`mmcls/datasets`](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/datasets) (Datasets), [`mmcls/structures`](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/structures) (Structures), [`mmcls/registry.py`](https://github.com/yzd-v/cls_kd/blob/1.0/mmcls/registry.py) (Registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_configs["Configs"]
  node_configs_base["Base Configs<br/>config fragments"]
  node_configs_models["Model Recipes<br/>model configs"]
  node_configs_distillers["Distill Configs<br/>distillation configs"]
end

subgraph group_group_mmcls["Core Library"]
  node_mmcls_registry{{"Registry<br/>[registry.py]"}}
  node_mmcls_classifiers["Classifiers<br/>model interfaces"]
  node_mmcls_backbones["Backbones<br/>feature extractors"]
  node_mmcls_heads["Heads"]
  node_mmcls_necks["Necks<br/>feature adapters"]
  node_mmcls_losses["Losses"]
  node_mmcls_distill_losses["Distill Losses<br/>KD losses"]
  node_mmcls_datasets["Datasets<br/>data loaders"]
  node_mmcls_transforms["Transforms<br/>data pipeline"]
  node_mmcls_batch_aug["Batch Augment<br/>mixup family"]
  node_mmcls_engine["Engine<br/>runtime extensions"]
  node_mmcls_metrics["Metrics"]
  node_mmcls_apis["APIs"]
  node_mmcls_structures["Structures<br/>data containers"]
end

subgraph group_group_ops["Ops"]
  node_tools_train["Train/Test<br/>entry points<br/>[train.py]"]
  node_tools_ops["Analysis Tools<br/>utility scripts"]
  node_tools_convert["Model Convert<br/>conversion tools"]
  node_projects_ext["Projects<br/>extensions"]
  node_deploy_boundary["Deploy<br/>serving path"]
end

node_configs_base -->|"compose"| node_mmcls_registry
node_configs_models -->|"instantiate"| node_mmcls_registry
node_configs_distillers -->|"wire KD"| node_mmcls_distill_losses
node_configs_distillers -->|"train pairs"| node_mmcls_classifiers
node_mmcls_classifiers -->|"uses"| node_mmcls_backbones
node_mmcls_classifiers -->|"optional"| node_mmcls_necks
node_mmcls_classifiers -->|"predicts with"| node_mmcls_heads
node_mmcls_classifiers -->|"optimizes"| node_mmcls_losses
node_mmcls_classifiers -->|"distills with"| node_mmcls_distill_losses
node_mmcls_datasets -->|"pipes through"| node_mmcls_transforms
node_mmcls_transforms -->|"augments"| node_mmcls_batch_aug
node_mmcls_datasets -->|"produces"| node_mmcls_structures
node_mmcls_structures -->|"consumed by"| node_mmcls_classifiers
node_mmcls_engine -->|"hooks"| node_mmcls_classifiers
node_mmcls_engine -->|"checks/controls"| node_mmcls_datasets
node_mmcls_metrics -->|"evaluates"| node_mmcls_structures
node_mmcls_apis -->|"loads"| node_mmcls_classifiers
node_tools_train -->|"runs"| node_configs_models
node_tools_ops -->|"verifies"| node_mmcls_datasets
node_tools_convert -->|"bridges"| node_deploy_boundary
node_projects_ext -->|"extends"| node_mmcls_registry
node_deploy_boundary -->|"serves"| node_mmcls_apis

click node_configs_base "https://github.com/yzd-v/cls_kd/tree/1.0/configs/_base_"
click node_configs_models "https://github.com/yzd-v/cls_kd/tree/1.0/configs"
click node_configs_distillers "https://github.com/yzd-v/cls_kd/tree/1.0/configs/distillers"
click node_mmcls_registry "https://github.com/yzd-v/cls_kd/blob/1.0/mmcls/registry.py"
click node_mmcls_classifiers "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/classifiers"
click node_mmcls_backbones "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/backbones"
click node_mmcls_heads "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/heads"
click node_mmcls_necks "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/necks"
click node_mmcls_losses "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/losses"
click node_mmcls_distill_losses "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/dis_losses"
click node_mmcls_datasets "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/datasets"
click node_mmcls_transforms "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/datasets/transforms"
click node_mmcls_batch_aug "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/utils/batch_augments"
click node_mmcls_engine "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/engine"
click node_mmcls_metrics "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/evaluation/metrics"
click node_mmcls_apis "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/apis"
click node_mmcls_structures "https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/structures"
click node_tools_train "https://github.com/yzd-v/cls_kd/blob/1.0/tools/train.py"
click node_tools_ops "https://github.com/yzd-v/cls_kd/tree/1.0/tools/analysis_tools"
click node_tools_convert "https://github.com/yzd-v/cls_kd/tree/1.0/tools/model_converters"
click node_projects_ext "https://github.com/yzd-v/cls_kd/tree/1.0/projects"
click node_deploy_boundary "https://github.com/yzd-v/cls_kd/tree/1.0/tools/torchserve"

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_base,node_configs_models,node_configs_distillers toneBlue
class node_mmcls_registry,node_mmcls_classifiers,node_mmcls_backbones,node_mmcls_heads,node_mmcls_necks,node_mmcls_losses,node_mmcls_distill_losses,node_mmcls_datasets,node_mmcls_transforms,node_mmcls_batch_aug,node_mmcls_engine,node_mmcls_metrics,node_mmcls_apis,node_mmcls_structures toneAmber
class node_tools_train,node_tools_ops,node_tools_convert,node_projects_ext,node_deploy_boundary toneMint
```

## Components

### Configs

Layered experiment definitions and reusable fragments

- [Base Configs](https://github.com/yzd-v/cls_kd/tree/1.0/configs/_base_): config fragments · Shared dataset, model, schedule, and runtime pieces · `configs/_base_`
- [Model Recipes](https://github.com/yzd-v/cls_kd/tree/1.0/configs): model configs · Canonical model families and pretrained recipes · `configs`
- [Distill Configs](https://github.com/yzd-v/cls_kd/tree/1.0/configs/distillers): distillation configs · Teacher-student experiment wiring · `configs/distillers`

### Core Library

Main package boundary for models, data, runtime, and evaluation

- [Registry](https://github.com/yzd-v/cls_kd/blob/1.0/mmcls/registry.py): component registry · Factory layer that binds configs to implementations · `mmcls/registry.py`
- [Classifiers](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/classifiers): model interfaces · Base, image, timm, Hugging Face, and distiller classifiers · `mmcls/models/classifiers`
- [Backbones](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/backbones): feature extractors · CNN, transformer, and hybrid backbone families · `mmcls/models/backbones`
- [Heads](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/heads): prediction heads · Classification, multi-label, multi-task, and special-purpose heads · `mmcls/models/heads`
- [Necks](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/necks): feature adapters · Pooling and feature fusion adapters between backbone and head · `mmcls/models/necks`
- [Losses](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/losses): training losses · Core classification losses and variants · `mmcls/models/losses`
- [Distill Losses](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/dis_losses): KD losses · KD, DKD, WSLD, MGD, SRRL, NKD, USKD, and ViTKD methods · `mmcls/models/dis_losses`
- [Datasets](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/datasets): data loaders · Dataset definitions, wrappers, samplers, and dataset utils · `mmcls/datasets`
- [Transforms](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/datasets/transforms): data pipeline · Preprocessing, augmentation, and formatting transforms · `mmcls/datasets/transforms`
- [Batch Augment](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/models/utils/batch_augments): mixup family · Mixup, cutmix, resizemix, and wrapper utilities · `mmcls/models/utils/batch_augments`
- [Engine](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/engine): runtime extensions · Hooks and optimizer plugins for training behavior · `mmcls/engine`
- [Metrics](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/evaluation/metrics): evaluation metrics · Single-label, multi-label, retrieval, VOC, multi-task, and corruption metrics · `mmcls/evaluation/metrics`
- [APIs](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/apis): inference APIs · Model loading and inference entry points · `mmcls/apis`
- [Structures](https://github.com/yzd-v/cls_kd/tree/1.0/mmcls/structures): data containers · Typed samples passed through model and evaluation pipelines · `mmcls/structures`

### Ops

Training entry points, conversion, serving, and examples

- [Train/Test](https://github.com/yzd-v/cls_kd/blob/1.0/tools/train.py): entry points · Distributed training and testing scripts · `tools/train.py`
- [Analysis Tools](https://github.com/yzd-v/cls_kd/tree/1.0/tools/analysis_tools): utility scripts · Logging, FLOPs, config printing, dataset verification, and visualization helpers · `tools/analysis_tools`
- [Model Convert](https://github.com/yzd-v/cls_kd/tree/1.0/tools/model_converters): conversion tools · Checkpoint conversion, reparameterization, and publishing utilities · `tools/model_converters`
- [Projects](https://github.com/yzd-v/cls_kd/tree/1.0/projects): extensions · Example and research extension areas built on the core library · `projects`
- [Deploy](https://github.com/yzd-v/cls_kd/tree/1.0/tools/torchserve): serving path · TorchServe and deploy-specific configs for export-ready models · `tools/torchserve`

## Connections

- Base Configs → Registry: compose · Base fragments resolve through the registry into concrete components
- Model Recipes → Registry: instantiate · Model recipes select registered backbones, heads, necks, and losses
- Distill Configs → Distill Losses: wire KD · Distillation recipes bind teacher-student methods to distillation losses
- Distill Configs → Classifiers: train pairs · Teacher and student classifiers are composed from the same model boundary
- Classifiers → Backbones: uses · Classifiers embed backbone feature extractors
- Classifiers → Necks: optional · Necks adapt features before prediction
- Classifiers → Heads: predicts with · Heads turn features into task outputs
- Classifiers → Losses: optimizes · Standard classifiers attach task losses
- Classifiers → Distill Losses: distills with · Distiller classifier coordinates teacher-student supervision
- Datasets → Transforms: pipes through · Datasets hand samples to preprocessing and augmentation
- Transforms → Batch Augment: augments · Pipeline transforms can delegate to batch-level augmentation
- Datasets → Structures: produces · Dataset code materializes typed data samples
- Structures → Classifiers: consumed by · Models consume structured samples during training and inference
- Engine → Classifiers: hooks · Runtime hooks modify classifier training behavior
- Engine → Datasets: checks/controls · Engine hooks also coordinate dataset-specific runtime behavior
- Metrics → Structures: evaluates · Metrics read prediction and label structures
- APIs → Classifiers: loads · Inference APIs construct and execute model graphs
- Train/Test → Model Recipes: runs · Training entry points execute configured experiments
- Analysis Tools → Datasets: verifies · Utility scripts inspect datasets and pipelines
- Model Convert → Deploy: bridges · Conversion and reparameterization prepare deployable artifacts
- Projects → Registry: extends · Projects register custom models and workflows without changing core code
- Deploy → APIs: serves · Deployment paths reuse inference-facing APIs
