# hg-chung/dpir architecture

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

- Interactive diagram: https://gitdiagram.com/hg-chung/dpir
- Repository: https://github.com/hg-chung/dpir

## Overview

##### Project type and purpose
- This is a Python-based research codebase that implements “Differentiable Point-based Inverse Rendering” (DPIR).
- It provides two nearly identical pipelines:
a. **code_diligent** for multi-view multi-light (DiLiGenT-MV) data
b. **code_photometric** for synthetic photometric (Blender-rendered) data

##### File structure
- Top-level:
- README.md
- docs/ (contains figs)
- code_diligent/ and code_photometric/ each with:
– confs/ (configuration files per object)
– dataloader/ (dataset interfaces for train & eval)
– modules/ (model subcomponents: geometry_network, reflectance_network, specular_network, embedder, utils, losses, config, model_eval)
– main.py (train entrypoint)
– evaluation.py (eval entrypoint)
- Configuration files (HOCON) drive data paths, hyperparameters, model settings.
- External-build or deployment files are minimal (no Docker, but conda env instructions).

##### README for additional insights
- High-level architecture:
- Hybrid point-volumetric geometry + point-based splatting renderer
- Basis-BRDF reflectance model
- Efficient shadow detection via point-based shadow maps
- Dependencies: PyTorch, PyTorch3D, fvcore, iopath, numpy, matplotlib, open3d, etc.
- Two datasets: DiLiGenT-MV (5 real objects), Photometric (4 synthetic objects).

a. Main components to include:
1. Configuration Parser (reads confs/*.conf)
2. Data Ingestion
– Dataset Storage (file system or GDrive folders)
– Dataloader modules (dataset.py, dataset_eval.py, load_*.py)
3. Model Assembly
– Embedder
– Geometry Network (hybrid point-volumetric)
– Reflectance Network (basis-BRDF)
– Specular Network
4. Differentiable Renderer
– Point-based splatting engine (calls PyTorch3D primitives)
– Shadow map generator
5. Loss & Optimization
– Image losses (image_losses.py)
– Regularization terms
– Optimizer loop in main.py
6. Evaluation Pipeline
– model_eval.py
– evaluation.py
7. External Dependencies
– PyTorch & CUDA
– PyTorch3D
– Open3D, Kornia, LPIPS, etc.
b. Relationships & interactions:
- Data flows: Dataset → Dataloader → Model → Renderer → Loss → Optimizer → updated Model
- Evaluation: Dataset → Dataloader → Model (frozen) → Renderer → Metrics → Output files
- Config controls data paths & hyperparams, injected into Dataloader and Model modules
- Modules reference utils and config submodule for shared constants
- External libs feed into Data I/O, Model ops, and Rendering engine
c. Architectural patterns & design principles:
- Modular separation: dataloader vs. model vs. evaluation
- Config-driven pipeline allows swapping datasets or objects easily
- Single responsibility: each Python module has a focused task (e.g., embedder only handles positional encoding)
- Two symmetrical subprojects (diligent vs. photometric) indicate code reuse potential
d. Key technologies/frameworks:
- PyTorch for autograd
- PyTorch3D for point-based rendering primitives
- HOCON (pyhocon) for configuration
- Open3D for point‐cloud I/O
- Kornia, LPIPS for perceptual losses
- Blender (external) to generate photometric dataset

##### Final remarks
- Essential elements: configs, two pipelines, dataloaders, model internals, renderer, loss, optimizer, evaluation.
- The goal is to give any engineer a one-page view of how data and control flow through DPIR from raw images to final reconstructed geometry and reflectance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration
    ConfigParser["Configuration Parser"]:::config
    click ConfigParser "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/config.py"
    click ConfigParser "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/config.py"
    click ConfigParser "https://github.com/hg-chung/dpir/blob/main/code_diligent/confs/*.conf"
    click ConfigParser "https://github.com/hg-chung/dpir/blob/main/code_photometric/confs/*.conf"

    %% External Dependencies
    subgraph "External Dependencies"
        ExtLib["PyTorch, PyTorch3D, Open3D, Kornia, LPIPS, etc."]:::external
        README["README.md"]:::external
        docs["docs/"]:::external
    end
    click README "https://github.com/hg-chung/dpir/blob/main/README.md"
    click docs "https://github.com/hg-chung/dpir/tree/main/docs/"

    %% Training Pipeline
    subgraph "Training Pipeline"
        direction TB
        ConfigParser -->|loads| TrainStorage[(Dataset Storage)]:::data
        subgraph Data_Ingestion
            direction TB
            TrainStorage --> TrainDataLoader["DataLoader"]:::data
        end
        click TrainDataLoader "https://github.com/hg-chung/dpir/blob/main/code_diligent/dataloader/dataset.py"
        click TrainDataLoader "https://github.com/hg-chung/dpir/blob/main/code_diligent/dataloader/load_diligent.py"
        click TrainDataLoader "https://github.com/hg-chung/dpir/blob/main/code_photometric/dataloader/dataset.py"
        click TrainDataLoader "https://github.com/hg-chung/dpir/blob/main/code_photometric/dataloader/load_blender.py"

        TrainDataLoader --> Model["Model"]:::model

        subgraph "Model Internal"
            direction TB
            Embedder["Embedder"]:::model
            click Embedder "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/embedder.py"
            click Embedder "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/embedder.py"

            GeometryNet["Geometry Network"]:::model
            click GeometryNet "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/geometry_network.py"
            click GeometryNet "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/geometry_network.py"

            ReflectNet["Reflectance Network"]:::model
            click ReflectNet "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/reflectance_network.py"
            click ReflectNet "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/reflectance_network.py"

            SpecularNet["Specular Network"]:::model
            click SpecularNet "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/specular_network.py"
            click SpecularNet "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/specular_network.py"

            ModelOrch["Model Orchestration"]:::model
            click ModelOrch "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/model.py"
            click ModelOrch "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/model.py"

            Utils["Utilities & Shadow Maps"]:::model
            click Utils "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/utils.py"
            click Utils "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/DirectionsToRusink.py"
            click Utils "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/utils.py"
            click Utils "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/DirectionsToRusink.py"

            Embedder --> GeometryNet --> ReflectNet --> SpecularNet --> ModelOrch
        end

        Model --> Renderer["Point-based Renderer"]:::render
        click Renderer "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/model.py"
        click Renderer "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/model.py"

        subgraph "Loss & Optimization"
            direction TB
            Loss["Image & Regularization Losses"]:::render
            click Loss "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/image_losses.py"
            click Loss "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/image_losses.py"
            Optimizer["Optimizer Loop"]:::render
            click Optimizer "https://github.com/hg-chung/dpir/blob/main/code_diligent/main.py"
            click Optimizer "https://github.com/hg-chung/dpir/blob/main/code_photometric/main.py"
            Renderer --> Loss --> Optimizer --> Model
        end
    end

    %% Evaluation Pipeline
    subgraph "Evaluation Pipeline"
        direction TB
        ConfigParser -->|loads| EvalStorage[(Dataset Storage)]:::data
        EvalStorage --> EvalDataLoader["Eval DataLoader"]:::data
        click EvalDataLoader "https://github.com/hg-chung/dpir/blob/main/code_diligent/dataloader/dataset_eval.py"
        click EvalDataLoader "https://github.com/hg-chung/dpir/blob/main/code_diligent/dataloader/load_diligent_eval.py"
        click EvalDataLoader "https://github.com/hg-chung/dpir/blob/main/code_photometric/dataloader/dataset_eval.py"
        click EvalDataLoader "https://github.com/hg-chung/dpir/blob/main/code_photometric/dataloader/load_eval.py"

        EvalDataLoader --> ModelFrozen["Model (frozen)"]:::model
        ModelFrozen --> RendererEval["Point-based Renderer"]:::render
        click RendererEval "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/model.py"
        click RendererEval "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/model.py"

        subgraph "Evaluation"
            direction TB
            Metrics["Metrics & Output"]:::render
            click Metrics "https://github.com/hg-chung/dpir/blob/main/code_diligent/modules/model_eval.py"
            click Metrics "https://github.com/hg-chung/dpir/blob/main/code_photometric/modules/model_eval.py"
            click Metrics "https://github.com/hg-chung/dpir/blob/main/code_diligent/evaluation.py"
            click Metrics "https://github.com/hg-chung/dpir/blob/main/code_photometric/evaluation.py"
            RendererEval --> Metrics
        end
    end

    %% Configuration injection dashed arrows
    ConfigParser -.-> TrainDataLoader
    ConfigParser -.-> Model
    ConfigParser -.-> EvalDataLoader
    ConfigParser -.-> ModelFrozen

    %% External arrows
    ExtLib -.-> TrainDataLoader
    ExtLib -.-> Model
    ExtLib -.-> Renderer
    ExtLib -.-> Loss
    ExtLib -.-> EvalDataLoader
    ExtLib -.-> ModelFrozen
    ExtLib -.-> RendererEval

    classDef data fill:#BBDEFB,stroke:#333,stroke-width:1px
    classDef model fill:#C8E6C9,stroke:#333,stroke-width:1px
    classDef render fill:#FFE0B2,stroke:#333,stroke-width:1px
    classDef config fill:#D7CCC8,stroke:#333,stroke-width:1px
    classDef external fill:#EEEEEE,stroke:#333,stroke-width:1px
```
