# bigrayss/motion2vec architecture

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

- Interactive diagram: https://gitdiagram.com/bigrayss/motion2vec
- Repository: https://github.com/bigrayss/motion2vec

## Overview

##### Project type and purpose
- This is a scientific/ML project for learning vector representations of 3D human motion (“motion2vec”) and fine-tuning on downstream tasks: 3D pose estimation (Human3.6M, MPI-INF-3DHP) and action recognition.
- It provides data preprocessing, model definitions (Motion2Vec, MotionAGFormer), training scripts, evaluation, and visualization tools.

##### File structure
Top-level directories and their roles:
- action/ : downstream action-classification pipeline (data loader, ActionAGFormer, loss, training).
- model/ : core model definitions for MotionAGFormer attention/graph modules.
- motion2vec/ : pretraining utilities, EMA module, model configuration.
- reader/ : dataset readers for H3.6M, MPI-INF-3DHP, generic motion datasets.
- configs/ : YAML config files for pretraining and each downstream dataset.
- utils/ & action/utils/ : shared helper functions (data loading, learning rate schedulers, metrics, visualization).
- loss/ : pose3d loss functions.
- train*.py & pre_train.py : entry-point scripts for pretraining and various downstream training/eval modes.
- new_P2V.py : possibly a conversion/preprocessing entry point.
- requirements.txt : environment.

##### Architecture from the README
- Framework: Python 3.8, PyTorch 2.0, CUDA 12.2.
- Data flow: raw detections → data/preprocess → serialized .npz → reader → model → training/eval → GIF visualization.
- External dependencies: MotionBERT, P-STMO, MHFormer, MotionAGFormer.

a. Components
1. Data Storage
- Raw detection files (Stacked Hourglass outputs, .npz files) under data/motion3d.
- Preprocessed dataset directory.
2. Data Preprocessing
- Scripts in data/preprocess (outside this repo, implied).
- reader/ module: `h36m.py`, `generator_3dhp.py`, `motion_dataset.py`.
3. Pretraining Pipeline
- pre_train.py with configs/pretrain/motion2vec*.yaml.
- motion2vec/ (EMA, modelcfg).
4. Core Model Components
- model/MotionAGFormer.py & model/modules/…
- action/model/MotionAGFormer_action.py.
- model_loss: loss/pose3d.py & action/model/loss.py.
5. Downstream Training & Evaluation
- train.py (3D pose estimation), train_3dhp.py, train_action.py.
- Evaluation mode: `--eval-only`.
6. Utilities
- utils/ & action/utils/: data transforms, schedulers, logging, visualization.
7. External Services
- GPU/ CUDA runtime.
- External checkpoint & dataset downloads.

b. Relationships & interactions
- Data Storage → Data Preprocessing → Preprocessed .npz files.
- Preprocessed files → reader/ → DataLoader objects.
- DataLoader → Model (Motion2Vec for pretrain; MotionAGFormer for downstream).
- Model outputs → Loss Functions (pose3d or action) → Backprop via PyTorch.
- Training Scripts orchestrate DataLoader, Model, Optimizer, Scheduler, EMA.
- On evaluation or visualization, outputs → utils/tools & visualization scripts → metrics/GIF.

c. Architectural patterns & design principles
- Modular layered architecture: clear separation of data, model, training, and utility layers.
- Configuration-driven: YAML files isolate hyperparameters from code.
- Extensibility: abstract `reader` and `model/modules` allow plugging new datasets and layers.
- Reuse via inheritance: action branch extends core MotionAGFormer for classification.
- EMA (exponential moving average) wrapper around model weights for stability.

d. Technologies & frameworks
- Python 3.8, PyTorch 2.0, CUDA 12.2.
- YAML for configs (Hydra-like pattern).
- numpy for data, tqdm/logging for monitors.
- Optional dependencies: imageio or Matplotlib for GIF creation.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Systems
    GPU["GPU/ CUDA Runtime"]:::external
    Storage["Cloud Storage & Checkpoints"]:::external

    %% Preprocessing Lane
    subgraph "Preprocessing Lane"
        RawData["Raw Data Files"]:::external
        Preprocess["Data Preprocessor Scripts"]:::preproc
        NPZ["Preprocessed .npz Files"]:::preproc
        RawData -->|"raw detections"| Preprocess
        Preprocess -->|"serialized .npz"| NPZ
        click Preprocess "https://github.com/bigrayss/motion2vec/blob/master/new_P2V.py"
    end

    %% Pretraining Lane
    subgraph "Pretraining Lane"
        PT_Configs["Pretraining Configs"]:::pretrain
        PT_Script["pre_train.py"]:::pretrain
        M2VModule["Motion2Vec Module"]:::pretrain
        EMA["EMA Module"]:::pretrain
        ModelCFG["Motion2Vec Config"]:::pretrain
        Checkpoints["Checkpoint Outputs"]:::external

        PT_Configs -.->|"configs"| PT_Script
        PT_Script -->|"batch of frames"| M2VModule
        M2VModule -->|"model weights"| EMA
        PT_Script -->|"logs & ckpt"| Checkpoints
        PT_Script -.->|"uses"| ModelCFG
        GPU --> PT_Script
        Storage --> RawData
        Storage --> Checkpoints

        click PT_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/pretrain/motion2vec.yaml"
        click PT_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/pretrain/motion2vec_2.yaml"
        click PT_Script "https://github.com/bigrayss/motion2vec/blob/master/pre_train.py"
        click EMA "https://github.com/bigrayss/motion2vec/blob/master/motion2vec/ema_module.py"
        click ModelCFG "https://github.com/bigrayss/motion2vec/blob/master/motion2vec/modelcfg.py"
    end

    %% Downstream Lane
    subgraph "Downstream Lane"
        DS_Configs["Downstream Configs"]:::downstream
        TrainRun["Training Scripts"]:::downstream
        Reader["Reader Modules"]:::downstream
        AGForm["MotionAGFormer Model"]:::downstream
        LossFn["Loss Functions"]:::downstream
        Results["Results & Visualization"]:::downstream

        DS_Configs -.->|"configs"| TrainRun
        TrainRun -->|"loads .npz"| Reader
        TrainRun -->|"initial weights"| AGForm
        AGForm -->|"predictions"| LossFn
        LossFn -->|"loss gradient"| TrainRun
        TrainRun -->|"metrics/GIF"| Results
        GPU --> TrainRun

        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/h36m/motion2vec-base.yaml"
        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/h36m/motion2vec-large.yaml"
        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/mpi/motion2vec-base.yaml"
        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/mpi/motion2vec-large.yaml"
        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/action/MB_train_NTU60_xsub.yaml"
        click DS_Configs "https://github.com/bigrayss/motion2vec/blob/master/configs/action/MB_train_NTU60_xview.yaml"
        click TrainRun "https://github.com/bigrayss/motion2vec/blob/master/train.py"
        click TrainRun "https://github.com/bigrayss/motion2vec/blob/master/train_3dhp.py"
        click TrainRun "https://github.com/bigrayss/motion2vec/blob/master/train_action.py"
        click Reader "https://github.com/bigrayss/motion2vec/blob/master/reader/generator_3dhp.py"
        click Reader "https://github.com/bigrayss/motion2vec/blob/master/reader/h36m.py"
        click Reader "https://github.com/bigrayss/motion2vec/blob/master/reader/motion_dataset.py"
        click AGForm "https://github.com/bigrayss/motion2vec/blob/master/model/MotionAGFormer.py"
        click AGForm "https://github.com/bigrayss/motion2vec/blob/master/action/model/MotionAGFormer_action.py"
        click AGForm "https://github.com/bigrayss/motion2vec/blob/master/action/model/model_action.py"
        click LossFn "https://github.com/bigrayss/motion2vec/blob/master/loss/pose3d.py"
        click LossFn "https://github.com/bigrayss/motion2vec/blob/master/action/model/loss.py"
    end

    %% Styles
    classDef preproc fill:#FDEBD0,stroke:#D35400,color:#000;
    classDef pretrain fill:#D6EAF8,stroke:#21618C,color:#000;
    classDef downstream fill:#D5F5E3,stroke:#196F3D,color:#000;
    classDef external fill:#D5D8DC,stroke:#7B7D7D,color:#000;
```
