# czm369/mixpl architecture

> Architecture diagram of the GitHub repository czm369/mixpl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/czm369/mixpl
- Repository: https://github.com/czm369/mixpl

## Overview

##### Project Type and Purpose
- This is a research codebase built on top of the MMDetection (mmdet) library for semi-supervised object detection. Its goal is to implement the “Mixed Pseudo Labels (MixPL)” method—which augments pseudo-labeled data via MixUp and Mosaic and resamples labeled data to improve tail‐class performance—and integrate it seamlessly into the standard detection training and inference workflows.

##### Top‐Level Architectural Layers
- Configuration Layer (configs/, projects/MixPL/configs/)
- Core Library Layer (mmdet/)
- MixPL‐Specific Modules (projects/MixPL/mixpl.py and utilities under tools/)
- Data Layer (datasets, transforms, samplers under mmdet/datasets)
- Model Layer (backbone, neck, head, losses under mmdet/models)
- Engine Layer (training/inference runner under mmdet/engine)
- Utilities & Deployment (tools/, demo/, docs/, docker/)

##### Main Components
a. Config Manager
- Loads YAML/Python config files (configs/ and projects/MixPL/configs/)
- Knows hyperparameters for data splits, augmentations, model, optimizer, scheduler
b. Data Pipeline
- Labeled Dataset Loader (COCO, VOC, custom)
- Unlabeled Dataset Loader
- MixPL Augmentor (MixUp + Mosaic transform for unlabeled pseudo-labels)
- Resampler for tail‐class oversampling
- Batch Sampler / DataLoader
c. Pseudo-Label Generator
- Inference pass on current detector → produces pseudo labels for unlabeled images
d. Model
- Backbone (ResNet, Swin, etc.) → Neck (FPN, PAFPN) → Dense / Two‐stage Head
- Losses (classification, bbox, MixPL losses)
e. Training Engine
- Runner orchestrates epoch/iteration loops
- Scheduler, optimizer, hooks (logging, checkpoint, evaluation, mean-teacher hook)
- MixPL hook integrates pseudo-label refresh and mixed data injection
f. Evaluation & Inference
- Standard COCO/VOC metrics modules
- Inference APIs (mmdet/apis/) and demo scripts (demo/)
g. Deployment
- Docker images and entrypoints for serving trained models (docker/serve/)
h. Documentation & Examples
- Jupyter notebooks, docs/, tutorials in demo/, README figures

##### Component Interactions & Data Flow
- The Config Manager initializes Model, DataPipeline, Engine.
- For each training iteration:
1. Labeled images → standard transforms → model inputs
2. Unlabeled images → pseudo-label inference (via Pseudo-Label Generator) → MixPL Augmentor → mixed batch
3. DataLoader yields mixed batches
4. Model computes detections → Loss computed (MixPL losses incorporated)
5. Engine applies backprop, optimizer, scheduler
6. Periodic Hooks regenerate pseudo-labels, evaluate on validation set, checkpoint model
- For inference: Data → transforms → model → post-processing → results
- External tools (tools/, demo/) consume saved weights and configs for analysis, conversion, or GUI demos.

##### Architectural Patterns & Principles
- Plugin/Registry Pattern: model, dataset, hook, optimizer constructors are registered for easy extension
- Layered Modularization: clear separation of config, data, model, engine, tools, docs
- Hook System: training behavior (pseudo-label refresh, logging) injected without modifying core runner
- Config‐Driven: behavior is defined in Python/YAML config files, enabling low-code experimentation
- Reuse of MMDetection Core: MixPL overrides or extends only specific parts (augmentor, hook, config)

##### Technologies & Frameworks
- Python 3.x, PyTorch
- MMDetection core (mmdet)
- Jupyter Notebooks for demos
- Docker for deployment
- YAML/Python‐based configs
- COCO / VOC datasets
- Standard CV libraries (OpenCV, NumPy)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration & Utilities
    subgraph "Config & Utilities"
        direction TB
        ConfigManager["Config Manager"]:::gray
        MixPLConfigs["MixPL Config Overrides"]:::gray
        Tools["Tools / Scripts"]:::gray
        Docs["Documentation & Examples"]:::gray
        DockerDeploy["Docker Serve"]:::gray
    end

    %% Data Layer
    subgraph "Data Layer" 
        direction TB
        LabeledLoader["Labeled Dataset Loader"]:::blue
        UnlabeledLoader["Unlabeled Dataset Loader"]:::blue
        Transforms["MixPL Augmentor (MixUp/Mosaic)"]:::blue
        Resampler["Class-aware Resampler"]:::blue
        DataLoader["Batch Sampler / DataLoader"]:::blue
    end

    %% MixPL Modules
    subgraph "MixPL Modules"
        direction TB
        PseudoLabelGen["Pseudo-Label Generator"]:::yellow
        MixPLLoss["MixPL Losses"]:::yellow
        MixPLHook["MixPL Hook"]:::yellow
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        Backbone["Backbone"]:::green
        Neck["Neck"]:::green
        HeadDense["Dense Head"]:::green
        HeadROI["Two-stage Head"]:::green
        Losses["Loss Computation"]:::green
    end

    %% Engine & Hooks
    subgraph "Engine & Hooks"
        direction TB
        Runner["Runner & Loops"]:::orange
        Optimizer["Optimizer"]:::orange
        Scheduler["Scheduler"]:::orange
        Hooks["Core Hooks (logging, checkpoint, mean-teacher)"]:::orange
    end

    %% Inference & Deployment
    subgraph "Inference & Deployment"
        direction TB
        InferenceAPI["Inference APIs"]:::gray
        DetInferencer["Detection Inferencer"]:::gray
        DemoScripts["Demo Scripts"]:::gray
        TorchServeHandler["TorchServe Handler"]:::gray
    end

    %% Data Flow - Training
    ConfigManager -->|initializes| LabeledLoader
    ConfigManager -->|initializes| UnlabeledLoader
    ConfigManager -->|initializes| Runner

    LabeledLoader -->|standard transforms| DataLoader
    UnlabeledLoader --> PseudoLabelGen
    PseudoLabelGen --> Transforms
    Transforms --> Resampler
    Resampler --> DataLoader

    DataLoader --> Backbone
    Backbone --> Neck
    Neck --> HeadDense
    Neck --> HeadROI
    HeadDense --> Losses
    HeadROI --> Losses
    Losses -->|losses incl. MixPL| Runner

    Runner --> Optimizer
    Runner --> Scheduler
    Runner --> Hooks
    Runner -->|periodic refresh| PseudoLabelGen

    PseudoLabelGen -->|refresh pseudo-labels| Transforms

    %% Inference Flow
    LabeledLoader --> Backbone
    Backbone --> Neck
    Neck --> HeadDense
    HeadDense --> InferenceAPI
    InferenceAPI --> DetInferencer
    DetInferencer --> DemoScripts

    %% Deployment Flow
    Runner -->|outputs weights| DockerDeploy
    DockerDeploy --> TorchServeHandler

    %% Click Events
    click ConfigManager "https://github.com/czm369/mixpl/tree/main/configs/"
    click MixPLConfigs "https://github.com/czm369/mixpl/tree/main/projects/MixPL/configs/"
    click LabeledLoader "https://github.com/czm369/mixpl/tree/main/mmdet/datasets/"
    click UnlabeledLoader "https://github.com/czm369/mixpl/tree/main/mmdet/datasets/"
    click Transforms "https://github.com/czm369/mixpl/blob/main/projects/MixPL/mixpl/mixpl.py"
    click Resampler "https://github.com/czm369/mixpl/blob/main/mmdet/datasets/samplers/class_aware_sampler.py"
    click PseudoLabelGen "https://github.com/czm369/mixpl/blob/main/projects/MixPL/mixpl/mixpl.py"
    click Backbone "https://github.com/czm369/mixpl/tree/main/mmdet/models/backbones/"
    click Neck "https://github.com/czm369/mixpl/tree/main/mmdet/models/necks/"
    click HeadDense "https://github.com/czm369/mixpl/tree/main/mmdet/models/dense_heads/"
    click HeadROI "https://github.com/czm369/mixpl/tree/main/mmdet/models/roi_heads/"
    click MixPLLoss "https://github.com/czm369/mixpl/blob/main/projects/MixPL/mixpl/mixpl.py"
    click Runner "https://github.com/czm369/mixpl/blob/main/mmdet/engine/runner/loops.py"
    click Optimizer "https://github.com/czm369/mixpl/tree/main/mmdet/engine/optimizers/"
    click Scheduler "https://github.com/czm369/mixpl/tree/main/mmdet/engine/schedulers/"
    click Hooks "https://github.com/czm369/mixpl/blob/main/mmdet/engine/hooks/mean_teacher_hook.py"
    click MixPLHook "https://github.com/czm369/mixpl/blob/main/projects/MixPL/mixpl/mixpl.py"
    click InferenceAPI "https://github.com/czm369/mixpl/blob/main/mmdet/apis/inference.py"
    click DetInferencer "https://github.com/czm369/mixpl/blob/main/mmdet/apis/det_inferencer.py"
    click DemoScripts "https://github.com/czm369/mixpl/blob/main/demo/image_demo.py"
    click DockerDeploy "https://github.com/czm369/mixpl/tree/main/docker/serve/Dockerfile"
    click TorchServeHandler "https://github.com/czm369/mixpl/blob/main/tools/deployment/mmdet_handler.py"
    click Docs "https://github.com/czm369/mixpl/tree/main/docs/"
    click DemoScripts "https://github.com/czm369/mixpl/blob/main/demo/MMDet_Tutorial.ipynb"

    %% Styles
    classDef blue fill:#D0E8FF,stroke:#0366D6,color:#0366D6
    classDef green fill:#DFF8E2,stroke:#028A0F,color:#028A0F
    classDef yellow fill:#FFF4C1,stroke:#E09200,color:#E09200
    classDef orange fill:#FFE7D6,stroke:#D15B0B,color:#D15B0B
    classDef gray fill:#E8E8E8,stroke:#666666,color:#333333
```
