# geopavlakos/hamer architecture

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

- Interactive diagram: https://gitdiagram.com/geopavlakos/hamer
- Repository: https://github.com/geopavlakos/hamer

## Overview

##### Project Type and Purpose
- HaMeR is a research code repository for hand mesh recovery using transformers. It’s a deep learning project aimed at reconstructing 3D hand poses and meshes.
- The repository contains training, evaluation, and demo scripts. Its purpose is to provide a pipeline for hand mesh estimation that is flexible (using Hydra for configuration) and modular.

##### File Structure
- Top-Level Files & Directories:
- Scripts like demo.py, train.py, and eval.py drive different modes (demo, training, evaluation).
- The “hamer” package contains subdirectories for configurations (both plain and Hydra‐based), datasets, models, and utilities.
- “hamer/models” includes backbones, transformer components, and a specific “mano_wrapper.py” module that handles MANO model integration.
- “hamer/datasets” holds dataset classes for different types of data (e.g., image_dataset, json_dataset, mocap_dataset, etc.).
- “hamer/utils” covers a range of functions, including those responsible for rendering (mesh_renderer, renderer, skeleton_renderer, and render_openpose), which are central to output processing.
- Docker-related files under the docker/ directory show environment/deployment support.

##### README for Additional Insights
- The README confirms the project’s focus by emphasizing its role as the implementation for “Reconstructing Hands in 3D with Transformers.”
- It outlines different usage scenarios (demo, training, evaluation) and provides commands for both direct execution and Docker deployment.
- Specific instructions in the README indicate that external dependencies (like the MANO model) must be integrated into the pipeline.

a. Main Components:
- Configuration Manager:
- Uses Hydra-based configuration files (hamer/configs_hydra) and standard config files (hamer/configs) to define experiments, datasets, and training parameters.
- Data Ingestion & Preprocessing:
- Dataset modules under hamer/datasets load and preprocess image data, JSON annotations, and possibly motion capture data.
- Scripts like fetch_demo_data.sh and fetch_training_data.sh are responsible for obtaining the necessary input data.
- Model Architecture:
- Core model components include backbones (e.g., ViT in hamer/models/backbones/vit.py) and transformers (e.g., pose_transformer in hamer/models/components/pose_transformer.py).
- The “mano_wrapper.py” module integrates the MANO hand model, handling specific function-level logic to convert model outputs into 3D hand mesh representations.
- Inference and Processing Pipeline:
- The demo (demo.py) and training (train.py) scripts invoke the model.
- Within inference, camera input processing logic (likely encapsulated within utility functions using, for example, image_dataset processing and any provided camera-related input methods, even if externally sourced) should be visualized.
- Output Processing:
- Utility modules for rendering and visualization (mesh_renderer.py, renderer.py, render_openpose.py, skeleton_renderer.py) handle the final formatting and visualization of the reconstructed 3D hand meshes.
- Explicit functions here process the raw output (e.g., from mano_wrapper and pose utilities) and prepare meshes or annotated images for display or saving.

b. Relationships and Interactions:
- Data flows from raw inputs (image files or live camera streams) into Dataset loaders.
- Preprocessed data passes into the Model Architecture where, for instance, the hand pose transformer and convolutional backbones generate predictions.
- The MANO logic (function level detail) is invoked within the model pipeline to refine the hand mesh components.
- The outputs, including hand meshes and 3D pose estimations, then go into Output Processing modules where rendering functions transform them into visual outputs.
- Additional modules such as logging utilities (pylogger.py) and experiment control (Hydra launcher scripts) interconnect these components.

c. Architectural Patterns and Design Principles:
- Modularity: Clear separation between configuration, data loading, model computation, and utilities.
- Configurability: Extensive use of Hydra (see hamer/configs_hydra) to allow users to swap settings for data, training, and launching modes (local or SLURM).
- Reusability: Functions in the “utils” directory are designed to be reused across different modules (rendering, geometry processing).
- Function-Level Specifics:
- MANO Integration: In modules like “hamer/models/mano_wrapper.py”, include functions that load and process the MANO_RIGHT.pkl file. This is key to converting network outputs into realistic hand mesh parameters.
- Output Processing: Include function-level modules within “hamer/utils” (such as mesh_renderer.py and renderer.py) to denote the logic that reconstructs the mesh from predicted parameters and generates visual outputs.

d. Relevant Technologies and Frameworks:
- Frameworks: PyTorch (for deep learning), Hydra (for configuration management), and Docker (for environment deployment).
- Associated Libraries: Utilities might include rendering libraries (OpenGL or similar in the mesh_renderer module) and integration of external projects like ViTPose.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Configuration & Control Layer
    subgraph "Configuration & Control Layer"
        ConfigManager["Configuration Manager"]
        Hydra["Hydra Config Files"]
        DemoScript["Demo Script (demo.py)"]
        TrainScript["Train Script (train.py)"]
        EvalScript["Eval Script (eval.py)"]
    end

    %% Data Input Layer
    subgraph "Data Input Layer"
        DataLoader["Data Ingestion & Preprocessing"]
        Camera["Camera Input Preprocessing\n(process_side_view_input(),adjust_input_frame())"]
    end

    %% Model Processing Layer
    subgraph "Model Processing Layer"
        Backbone["Backbone Network (ViT)"]
        PoseTransformer["Pose Transformer"]
        MANO["MANO Integration Module\n(load_mano_model(),process_hand_params(),integrate_with_network_output())"]
        MANO_Head["MANO Head"]
    end

    %% Output Processing Layer
    subgraph "Output Processing Layer"
        OutputProcessing["Output Processing & Rendering\n(render_mesh(),postprocess_output(),output_to_file())"]
        MeshRenderer["Mesh Renderer"]
        Renderer["Renderer"]
        RenderOpenPose["Render OpenPose"]
        SkeletonRenderer["Skeleton Renderer"]
    end

    %% Connections between layers
    ConfigManager --> DataLoader
    DemoScript --> DataLoader
    TrainScript --> DataLoader
    EvalScript --> DataLoader
    DataLoader --> Camera
    Camera --> Backbone
    Backbone --> PoseTransformer
    PoseTransformer --> MANO
    MANO --> MANO_Head
    MANO --> OutputProcessing
    OutputProcessing --> MeshRenderer
    MeshRenderer --> Renderer
    Renderer --> RenderOpenPose
    RenderOpenPose --> SkeletonRenderer

    %% Click Events
    click ConfigManager "https://github.com/geopavlakos/hamer/tree/main/hamer/configs"
    click Hydra "https://github.com/geopavlakos/hamer/tree/main/hamer/configs_hydra/"
    click DataLoader "https://github.com/geopavlakos/hamer/tree/main/hamer/datasets/"
    click Backbone "https://github.com/geopavlakos/hamer/blob/main/hamer/models/backbones/vit.py"
    click PoseTransformer "https://github.com/geopavlakos/hamer/blob/main/hamer/models/components/pose_transformer.py"
    click MANO "https://github.com/geopavlakos/hamer/blob/main/hamer/models/mano_wrapper.py"
    click MANO_Head "https://github.com/geopavlakos/hamer/blob/main/hamer/models/heads/mano_head.py"
    click DemoScript "https://github.com/geopavlakos/hamer/blob/main/demo.py"
    click TrainScript "https://github.com/geopavlakos/hamer/blob/main/train.py"
    click EvalScript "https://github.com/geopavlakos/hamer/blob/main/eval.py"
    click MeshRenderer "https://github.com/geopavlakos/hamer/blob/main/hamer/utils/mesh_renderer.py"
    click Renderer "https://github.com/geopavlakos/hamer/blob/main/hamer/utils/renderer.py"
    click RenderOpenPose "https://github.com/geopavlakos/hamer/blob/main/hamer/utils/render_openpose.py"
    click SkeletonRenderer "https://github.com/geopavlakos/hamer/blob/main/hamer/utils/skeleton_renderer.py"

    %% Styling for color coding
    classDef config fill:#e3f2fd,stroke:#1e88e5,stroke-width:2px;
    classDef data fill:#fce4ec,stroke:#d81b60,stroke-width:2px;
    classDef model fill:#e8f5e9,stroke:#43a047,stroke-width:2px;
    classDef output fill:#fff3e0,stroke:#fb8c00,stroke-width:2px;

    class ConfigManager,Hydra,DemoScript,TrainScript,EvalScript config;
    class DataLoader,Camera data;
    class Backbone,PoseTransformer,MANO,MANO_Head model;
    class OutputProcessing,MeshRenderer,Renderer,RenderOpenPose,SkeletonRenderer output;
```
