How yzd-v/cls_kd works
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: classifiers Classifiers · datasets Datasets · structures Structures · registry.py Registry
Components 22
Configs
Layered experiment definitions and reusable fragments
- Base Configs · Shared dataset, model, schedule, and runtime pieces
configs/_base_ - Model Recipes · Canonical model families and pretrained recipes
configs - Distill Configs · Teacher-student experiment wiring
configs/distillers
Core Library
Main package boundary for models, data, runtime, and evaluation
- Registry · Factory layer that binds configs to implementations
mmcls/registry.py - Classifiers · Base, image, timm, Hugging Face, and distiller classifiers
mmcls/models/classifiers - Backbones · CNN, transformer, and hybrid backbone families
mmcls/models/backbones - Heads · Classification, multi-label, multi-task, and special-purpose heads
mmcls/models/heads - Necks · Pooling and feature fusion adapters between backbone and head
mmcls/models/necks - Losses · Core classification losses and variants
mmcls/models/losses - Distill Losses · KD, DKD, WSLD, MGD, SRRL, NKD, USKD, and ViTKD methods
mmcls/models/dis_losses - Datasets · Dataset definitions, wrappers, samplers, and dataset utils
mmcls/datasets - Transforms · Preprocessing, augmentation, and formatting transforms
mmcls/datasets/transforms - Batch Augment · Mixup, cutmix, resizemix, and wrapper utilities
mmcls/models/utils/batch_augments - Engine · Hooks and optimizer plugins for training behavior
mmcls/engine - Metrics · Single-label, multi-label, retrieval, VOC, multi-task, and corruption metrics
mmcls/evaluation/metrics - APIs · Model loading and inference entry points
mmcls/apis - Structures · Typed samples passed through model and evaluation pipelines
mmcls/structures
Ops
Training entry points, conversion, serving, and examples
- Train/Test · Distributed training and testing scripts
tools/train.py - Analysis Tools · Logging, FLOPs, config printing, dataset verification, and visualization helpers
tools/analysis_tools - Model Convert · Checkpoint conversion, reparameterization, and publishing utilities
tools/model_converters - Projects · Example and research extension areas built on the core library
projects - Deploy · TorchServe and deploy-specific configs for export-ready models
tools/torchserve
Connections 22
- 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