# megvii-research/caddm architecture

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

- Interactive diagram: https://gitdiagram.com/megvii-research/caddm
- Repository: https://github.com/megvii-research/caddm

## Overview

- Data Preparation & Ingestion:
- Data Directory: Contains raw videos and extracted images.
- lib/extract_frames_ldm_ff++.py: Component to extract frames from videos.
- Preprocessing: This lives in lib/data_preprocess with scripts for augmentations and specific cropping functions.
- Landmark Detector: Represented by the shape_predictor_81_face_landmarks.dat and its association via lib (for facial landmark extraction).

- Configuration and Control:
- configs Directory: Contains configuration files (e.g., caddm_train.cfg and caddm_test.cfg) that define parameters for training and evaluation.
- Command-line scripts (train.py and test.py): Serve as entrypoints for training and evaluation.

- Model and Network Backbones:
- backbones Directory: Contains different backbone implementations (such as ResNet and EfficientNet). Note the nested efficientnet_pytorch subdirectory.
- model.py: Likely the top-level model definition that assembles network components.

- Detection Layers and Loss Functions:

- Utility and Helper Functions:
- lib/util.py and other supporting utilities in the lib directory which handle things like image processing, configuration reading, and other helper processes.

##### Relationships and Data Flow
- That the Model is built on top of multiple backbone architectures, and detection layers are integrated to compute losses and manage outputs.
- The training and evaluation scripts (train.py and test.py) use the configuration files, thus forming the control layer over the entire process.

##### Architectural Patterns
- Modularity: The system is divided into modular components (data processing, backbone networks, detection layers) that can be independently maintained and updated.
- Extensibility: With clearly defined directories for different backbones and detection modules, adding new model architectures or detection functions is straightforward.
- Dependency Inversion: The configuration files and command-line interface separate the core model logic from runtime parameters.

- Within the model block, show sub-components for the chosen backbone (either ResNet-based or EfficientNet-based) and attach modules for detection layers and loss function computations.
- “Data Preparation & Extraction”
- “Data Preprocessing (Augmentation, Landmark Detection)”
- “Model (Backbone + Detection Layers)”
- “Training & Evaluation Control (Driven by Config Files)”

## Diagram (Mermaid)

```mermaid
graph TD
    %% Data Preparation & Ingestion
    subgraph "Data Preparation & Ingestion"
        DS["Data Directory"]:::datastore
        FE["Frame Extraction (lib/extract_frames_ldm_ff++.py)"]:::processing
        DP["Data Preprocessing (lib/data_preprocess)"]:::processing
        LD["Landmark Detector (shape_predictor_81_face_landmarks.dat)"]:::processing
        DS -->|"providesRawData"| FE
        FE -->|"extractsFrames"| DP
        DP -->|"integratesLandmarks"| LD
    end

    %% Model (Backbone + Detection Layers)
    subgraph "Model (Backbone + Detection Layers)"
        MD["Model Definition (model.py)"]:::model
        BB["Backbone Networks (ResNet/EfficientNet)"]:::model
        DL["Detection Layers & Loss Functions"]:::model
        MD --> BB
        BB --> DL
    end

    %% Configuration & Control
    subgraph "Configuration & Control"
        CT["Train Config (configs/caddm_train.cfg)"]:::control
        CET["Test Config (configs/caddm_test.cfg)"]:::control
        TS["Training Script (train.py)"]:::control
        TES["Testing Script (test.py)"]:::control
        CT --> TS
        CET --> TES
        TS -->|"triggersModel"| MD
        TES -->|"triggersModel"| MD
    end

    %% Utility & Helper Functions
    subgraph "Utility & Helpers"
        UT["Utility Functions (lib/util.py)"]:::utility
    end

    %% Data Flow Connections
    LD -->|"feedsProcessedData"| MD

    %% Styles
    classDef datastore fill:#f4e1d2,stroke:#8B4513,stroke-width:2px;
    classDef processing fill:#d1e8ff,stroke:#4682B4,stroke-width:2px;
    classDef model fill:#d1ffd1,stroke:#228B22,stroke-width:2px;
    classDef control fill:#ffd1dc,stroke:#FF1493,stroke-width:2px;
    classDef utility fill:#fffacd,stroke:#DAA520,stroke-width:2px;

    %% Click Events
    click DS "https://github.com/megvii-research/caddm/tree/master/data"
    click FE "https://github.com/megvii-research/caddm/blob/master/lib/extract_frames_ldm_ff++.py"
    click DP "https://github.com/megvii-research/caddm/tree/master/lib/data_preprocess"
    click LD "https://github.com/megvii-research/caddm/blob/master/lib/shape_predictor_81_face_landmarks.dat"
    click CT "https://github.com/megvii-research/caddm/blob/master/configs/caddm_train.cfg"
    click CET "https://github.com/megvii-research/caddm/blob/master/configs/caddm_test.cfg"
    click TS "https://github.com/megvii-research/caddm/blob/master/train.py"
    click TES "https://github.com/megvii-research/caddm/blob/master/test.py"
    click MD "https://github.com/megvii-research/caddm/blob/master/model.py"
    click BB "https://github.com/megvii-research/caddm/blob/master/backbones/resnet.py"
    click DL "https://github.com/megvii-research/caddm/blob/master/detection_layers/box_utils.py"
    click UT "https://github.com/megvii-research/caddm/blob/master/lib/util.py"
```
