# zju3dv/4k4d architecture

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

- Interactive diagram: https://gitdiagram.com/zju3dv/4k4d
- Repository: https://github.com/zju3dv/4k4d

## Overview

##### Project Type and Purpose
- This is a research‐oriented, full-stack Python/CUDA codebase for real-time neural volumetric video synthesis at 4K resolution (“4K4D”), built on top of the modular “EasyVolcap” framework.
- It provides:
- A core library (`easyvolcap/…`) for data loading, model definition, sampling, rendering, visualization, WebSocket viewer and training loops.
- A CLI frontend (`scripts/…` and entry points like `evc-gui`, `evc-test`, `evc-train`, `evc-ws`).
- A configuration system (`configs/…`) driving experiments, datasets, model specs, project presets, and run profiles.
- Documentation (`docs/…`), assets (`assets/…`), tests (`tests/…`) and glue files (`pyproject.toml`, `environment.yml`).

##### File-Structure Analysis
- Top-level directories:
- `easyvolcap/` – core Python package with submodules:
– `dataloaders` – dataset classes and samplers
– `engine` – config parsing, registry, file I/O, handlers (YAML, JSON, Pickle)
– `models` – camera models, network architectures (embedders, regressors, renderers, samplers)
– `runners` – training/evaluation loops, optimizers, recorders, WebSocket server
– `utils` – low-level utilities (CUDA, OpenGL, mesh, image, profiling, IO, shaders…)
– `scripts` – Python entry scripts (`main.py`, `client.py`, wrappers)
- `configs/` – hierarchical YAML files for:
– Base settings (`base.yaml`),
– Dataset definitions (`configs/datasets/...`),
– Experiment presets (`configs/exps/...`),
– Model architectures (`configs/models/...`),
– Project workflows (`configs/projects/...`),
– Spec overrides (`configs/specs/...`).
- `scripts/` – standalone preprocessing, dataset conversion, camera calibration, fusion, dataset download, and project-specific utilities.
- `docs/` – architectural design docs, usage guides, API references.
- `assets/` – fonts, images, 3D meshes (for demos).
- `tests/` – unit and integration tests (CUDA, rendering, memory).

##### Insights from README
- The README emphasizes:
- Modularity through YAML configs and registry-based instantiation
- Two main workflows: **Training** (`evc-train`) and **Rendering/Inference** (`evc-gui`, `evc-test`, `evc-ws`)
- Support for GPU backends: CUDA-tile rasterizer, PyTorch, PyTorch3D, tiny-cuda-nn, Open3D
- WebSocket server/client pairing for remote interactive viewing
- Preprocessing scripts for minimal datasets, visual hull extraction, optimized camera calibration

a. Main Components
1. CLI Frontend
– Commands: `evc-train`, `evc-test`, `evc-gui`, `evc-ws`
– Parses command-line args & points to one or more config YAMLs
2. Config Manager & Registry
– `easyvolcap.engine.parse` reads/merges YAMLs
– `easyvolcap.engine.registry` maps config keys to Python classes
3. File Client & I/O Handlers
– Abstracts local/remote files, supports JSON/YAML/Pickle
4. Data Pipeline
– `dataloaders` → dataset objects → batched samples (images, masks, cameras)
5. Model Pipeline
– Camera module → Embedder → Network → Sampler → Renderer
6. GPU Compute & Backends
– Tiny-cuda-nn networks, CUDA kernels (tile rasterizer), PyTorch ops, PyTorch3D
7. Runners & Optimizers
– Training/Evaluation loops manage forward/backward, optimizers, schedulers
8. Visualization & Viewer
– Native GUI rendering (`evc-gui`)
– WebSocket server (`easyvolcap.runners.websocket_server`)
– WebSocket client (`scripts/client.py` or `evc-ws`)
9. Storage
– Input data (datasets folder)
– Trained models (`data/trained_model`)
– Logs, tensorboard, checkpoints (`data/record`, `data/result`)

b. Relationships & Data Flow
- CLI → Config Manager → Registry → Instantiate Components
- File Client loads config & data → Dataloader → Model → Sampler → Renderer
- Renderer outputs frames → Visualizer (GUI or streamed via WebSocket)
- In training: Runner loops over data → Model forward/backward → Optimizer → Checkpoint
- In inference: Renderer forward only → Output images/video

c. Architectural Patterns & Design Principles
– Plugin/Registry pattern for easy extension of cameras, embedders, samplers, renderers
– Config-driven architecture: decoupling code from experiments
– Separation of concerns:
- I/O & Config (engine)
- Data (dataloaders)
- Compute (models, samplers, renderers)
- Control flow (runners)
- UX (GUI, WebSocket)
– GPU/CPU hybrid partitioning for high performance
– Modular CLI toolchain

d. Key Technologies & External Dependencies
– Python 3.8+ (PyTorch, PyTorch3D, tiny-cuda-nn, Open3D)
– CUDA (custom kernels, tile-based rasterizer)
– YAML/JSON config parsing
– WebSocket (remote viewing)
– Third-party scripts: Colmap, Blender, OpenGL, segmentation nets

##### Essential Elements to Capture
- Config file merging & registry instantiation
- Engine/FileClient abstraction
- Data pipeline (image/mask/camera sampling)
- Model/sampler/renderer separation
- Training loop vs inference flow
- Visualization: native GUI and WebSocket paths
- External and optional preprocessors (e.g., dataset conversion scripts)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Layers
    subgraph "CLI Frontend" 
        style CLI fill:#f9f,stroke:#333,stroke-width:1px
        CLI_Main["main.py"]:::cli
        CLI_Client["client.py"]:::cli
        CLI_Wrap["wrap.py"]:::cli
        CLI_EP["evc-commands (pyproject.toml)"]:::cli
    end

    subgraph "Config & Registry"
        style Config fill:#cfc,stroke:#333,stroke-width:1px
        Parse["parse.py"]:::core
        Config["config.py"]:::core
        Registry["registry.py"]:::core
    end

    subgraph "File I/O"
        style FileIO fill:#cfc,stroke:#333,stroke-width:1px
        FileClient["file_client.py"]:::core
        YAMLHandler["yaml_handler.py"]:::core
        JSONHandler["json_handler.py"]:::core
        PickleHandler["pickle_handler.py"]:::core
        IO["io.py"]:::core
    end

    subgraph "Data Pipeline"
        style Data fill:#ccc,stroke:#333,stroke-width:1px
        DataLoader["volumetric_video_dataloader.py"]:::core
        DataSamplers["datasamplers.py"]:::core
        DatasetTrain["volumetric_video_dataset.py"]:::core
        DatasetInfer["volumetric_video_inference_dataset.py"]:::core
    end

    subgraph "Model Pipeline"
        style Model fill:#cfc,stroke:#333,stroke-width:1px
        Camera["optimizable_camera.py"]:::core
        Embedder["embedders"]:::core
        Network["volumetric_video_network.py"]:::core
        MultiNet["multilevel_network.py"]:::core
        Sampler["r4dv_sampler.py"]:::core
        Renderer["volume_renderer.py"]:::core
    end

    subgraph "GPU Compute & Backends"
        style GPU fill:#ccf,stroke:#333,stroke-width:1px
        CUDAUtils["cuda_utils.py"]:::core
        RasterUtils["raster_utils.py"]:::core
        GLUtils["gl_utils.py"]:::core
        TCNNEmbedder["tcnn_hash_embedder.py"]:::core
    end

    subgraph "Runners & Optimizers"
        style Runners fill:#cfc,stroke:#333,stroke-width:1px
        Runner["volumetric_video_runner.py"]:::core
        Optimizers["optimizers.py"]:::core
        Schedulers["schedulers.py"]:::core
        Recorders["recorders.py"]:::core
    end

    subgraph "Visualization & Viewer"
        style Viz fill:#ffc,stroke:#333,stroke-width:1px
        Visualizer["volumetric_video_visualizer.py"]:::core
        WS_Server["websocket_server.py"]:::core
        Viewer["volumetric_video_viewer.py"]:::core
        WS_Client["scripts/client.py"]:::cli
    end

    subgraph "Documentation & Assets"
        style Docs fill:#eee,stroke:#333,stroke-width:1px
        Docs["docs/design/*.md"]:::external
        Fonts["assets/fonts/"]:::external
        Meshes["assets/meshes/bunny.ply"]:::external
    end

    %% Connections
    CLI_Main -->|args & config| Parse
    CLI_Client -->|start WS client| WS_Server
    CLI_Wrap -->|wrap entry| CLI_EP

    Parse -->|loads YAML| FileClient
    FileClient --> YAMLHandler
    FileClient --> JSONHandler
    FileClient --> PickleHandler
    FileClient --> IO

    Parse --> Config
    Parse --> Registry
    Registry -->|instantiate| DataLoader
    Registry -->|instantiate| Camera
    Registry -->|instantiate| Embedder
    Registry -->|instantiate| Network
    Registry -->|instantiate| Sampler
    Registry -->|instantiate| Renderer
    Registry -->|instantiate| Runner

    FileClient -->|load data| DatasetTrain
    FileClient -->|load data| DatasetInfer
    DataLoader -->|batches| DataSamplers
    DataSamplers -->|samples| DatasetTrain
    DataSamplers -->|samples| DatasetInfer

    DatasetTrain -->|images, masks, cams| Camera
    Camera --> Embedder
    Embedder --> Network
    MultiNet --> Network
    Network --> Sampler
    Sampler --> Renderer
    Renderer --> Viz
    Renderer --> WS_Server
    WS_Server --> WS_Client

    Runner -->|training loop| Network
    Runner --> Optimizers
    Runner --> Schedulers
    Runner --> Recorders
    Runner -->|checkpoints| FileClient

    CUDAUtils --> Network
    RasterUtils --> Renderer
    GLUtils --> Viz
    TCNNEmbedder --> Embedder

    %% Click Events
    click CLI_Main "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/scripts/main.py"
    click CLI_Client "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/scripts/client.py"
    click CLI_Wrap "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/scripts/wrap.py"
    click CLI_EP "https://github.com/zju3dv/4k4d/blob/main/pyproject.toml"

    click Parse "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/parse.py"
    click Config "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/config.py"
    click Registry "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/registry.py"

    click FileClient "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/file_client.py"
    click YAMLHandler "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/handlers/yaml_handler.py"
    click JSONHandler "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/handlers/json_handler.py"
    click PickleHandler "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/handlers/pickle_handler.py"
    click IO "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/engine/io.py"

    click DataLoader "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/dataloaders/volumetric_video_dataloader.py"
    click DataSamplers "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/dataloaders/datasamplers.py"
    click DatasetTrain "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/dataloaders/datasets/volumetric_video_dataset.py"
    click DatasetInfer "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/dataloaders/datasets/volumetric_video_inference_dataset.py"

    click Camera "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/cameras/optimizable_camera.py"
    click Embedder "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/networks/embedders/positional_encoding_embedder.py"
    click MultiNet "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/networks/multilevel_network.py"
    click Network "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/networks/volumetric_video_network.py"
    click Sampler "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/samplers/r4dv_sampler.py"
    click Renderer "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/renderers/volume_renderer.py"

    click CUDAUtils "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/utils/cuda_utils.py"
    click RasterUtils "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/utils/raster_utils.py"
    click GLUtils "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/utils/gl_utils.py"
    click TCNNEmbedder "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/models/networks/embedders/tcnn_hash_embedder.py"

    click Runner "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/volumetric_video_runner.py"
    click Optimizers "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/optimizers.py"
    click Schedulers "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/schedulers.py"
    click Recorders "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/recorders.py"

    click Visualizer "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/visualizers/volumetric_video_visualizer.py"
    click WS_Server "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/websocket_server.py"
    click Viewer "https://github.com/zju3dv/4k4d/blob/main/easyvolcap/runners/volumetric_video_viewer.py"
    click WS_Client "https://github.com/zju3dv/4k4d/blob/main/scripts/client.py"

    click Docs "https://github.com/zju3dv/4k4d/tree/main/docs/design/"
    click Fonts "https://github.com/zju3dv/4k4d/tree/main/assets/fonts/"
    click Meshes "https://github.com/zju3dv/4k4d/blob/main/assets/meshes/bunny.ply"

    %% Styles
    classDef cli fill:#ffeb99,stroke:#333,stroke-width:1px
    classDef core fill:#b3e6b3,stroke:#333,stroke-width:1px
    classDef external fill:#c2dfff,stroke:#333,stroke-width:1px
    classDef data fill:#dddddd,stroke:#333,stroke-width:1px
    classDef gpu fill:#ccccff,stroke:#333,stroke-width:1px
    class CLI_Main,CLI_Client,CLI_Wrap,CLI_EP cli
    class DataLoader,DataSamplers,DatasetTrain,DatasetInfer data
    class CUDAUtils,RasterUtils,GLUtils,TCNNEmbedder gpu
```
