# jinlinyi/sparseplanes architecture

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

- Interactive diagram: https://gitdiagram.com/jinlinyi/sparseplanes
- Repository: https://github.com/jinlinyi/sparseplanes

## Overview

1. This repository is a research codebase for reconstructing a coherent planar 3D scene from two RGB views. The top-level architecture is split into three phases: dataset preparation, model training/inference, and evaluation/visualization. The core implementation is a Detectron2-based PyTorch project inside `sparsePlane/`.

2. The model itself is organized around a multi-branch detection-and-optimization pipeline. A ResNet50-FPN backbone feeds a plane instance branch, an appearance/embedding branch, and a camera-prediction branch. The repository’s README describes a two-step inference process: first predict planes and relative camera hypotheses, then fuse those discrete predictions and refine them with continuous optimization.

3. The main modeling boundary is in `sparsePlane/sparseplane/modeling/`. `meta_arch/siamese_rcnn.py` is the central architecture entry point, with `camera_branch.py` handling the camera-pose side of the system. `roi_heads/` contains the plane-specific heads and losses, including `plane_head.py`, `embedding_head.py`, `plane_loss.py`, and `embedding_loss.py`, which together produce plane masks, plane parameters, and correspondence features.

4. Camera reasoning is treated as a separate subsystem from plane detection. `modeling/camera_net/camera_head.py` predicts relative camera transformation probabilities, while `utils/camera.py` and the precomputed `models/kmeans_rots_32.pkl` / `kmeans_trans_32.pkl` support a discrete pose-codebook style representation. This matches the repo’s optimization stage, where the system selects among candidate camera poses before refinement.

5. The data boundary centers on Matterport3D-style inputs. `sparseplane/data/datasets/mp3d.py` and `builtin.py` register datasets, while `augmentation.py` and `planercnn_transforms.py` adapt image/label transforms for plane detection training. The `data_preparation/` folder contains the offline conversion pipeline for MP3D meshes and planes, including scripts to convert `.ply`, fit planes, and render plane annotations.

6. `data_preparation/` is effectively an ETL pipeline for turning raw 3D scene assets into training-ready supervision. Its bash and Python scripts operate on mesh geometry, fit planar segments, and produce rendered outputs used by the learning pipeline. This is a major architectural boundary because the training data is not consumed directly from raw scans; it is preprocessed into plane-centric labels first.

7. Training and inference are exposed as command-line tools under `sparsePlane/tools/`. `train_net.py` is the training entry point, `inference_sparse_plane.py` runs reconstruction on image pairs, `eval.py` performs evaluation, and `visualize_gt.py` generates ground-truth visualizations. These scripts sit above the Detectron2-style config system and model registry, which is why `sparseplane/config/config.py` and the YAML configs are important orchestration points.

8. The configuration layer is explicit and split by stage. `sparsePlane/configs/step1-plane.yaml`, `step2-corr.yaml`, and `step3-camera.yaml` suggest a staged workflow: plane estimation, correspondence/association, then camera estimation. That staging is important architecturally because it mirrors the system’s decomposition into separate predictors and downstream fusion.

9. Evaluation is separated from training and uses dataset-specific metrics. `sparseplane/evaluation/mp3d_evaluation.py` and `detectron2coco.py` indicate COCO-style evaluation adapters plus Matterport3D-specific scoring. Supporting metrics and geometry utilities live under `sparseplane/utils/metrics.py`, `mesh_utils.py`, and `meshes.py`, showing that evaluation includes both segmentation-style and 3D reconstruction-style outputs.

10. Visualization and geometry utilities form the last major subsystem. `sparseplane/visualization/visualization.py`, `utils/vis.py`, `textures.py`, and mesh helpers generate rendered plane overlays and reconstructed scene views. These are not core learning components, but they are essential for interpreting the pipeline’s output: detected planes, correspondences, selected camera pose, and the final reconstructed planar surface.

11. The repo depends on Detectron2 conventions for model registration, data loaders, and training loops, with Python packaging configured under `sparsePlane/setup.py` and `setup.cfg`. The root `requirements.txt` plus the environment docs indicate a PyTorch-centric runtime, while bash wrappers in `data_preparation/` and `tools/` show that most workflows are executed as CLI jobs rather than through a long-running service.

12. In architecture-diagram terms, the cleanest flow is: raw scan/scene assets enter `data_preparation`, become plane-labeled datasets, flow into Detectron2-based training and inference in `sparseplane/modeling`, then pass through camera/plane fusion and refinement, and finally out through evaluation and visualization utilities. The key boundary to preserve is that plane detection, camera estimation, and geometric optimization are separate subsystems coordinated by the inference scripts rather than one monolithic network.

Main entry points: [`sparsePlane/sparseplane/modeling/meta_arch/siamese_rcnn.py`](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/meta_arch/siamese_rcnn.py) (Siamese RCNN), [`sparsePlane/tools/inference_sparse_plane.py`](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/inference_sparse_plane.py) (Infer), [`sparsePlane/sparseplane/modeling/roi_heads/roi_heads.py`](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/roi_heads.py) (ROI heads), [`data_preparation`](https://github.com/jinlinyi/sparseplanes/tree/main/data_preparation) (Data prep)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_data["Data Prep"]
  node_node_dataprep["Data prep<br/>ETL pipeline"]
  node_node_convert_ply["Convert PLY<br/>mesh conversion"]
  node_node_fit_planes["Fit planes<br/>plane fitting<br/>[fit_plane_ply.py]"]
  node_node_render_planes["Render planes<br/>annotation rendering<br/>[render_plane.py]"]
end

subgraph group_group_model["Core Model"]
  node_node_config["Config<br/>[config.py]"]
  node_node_siamese_rcnn["Siamese RCNN<br/>meta-arch<br/>[siamese_rcnn.py]"]
  node_node_camera_branch["Camera branch<br/>camera fusion<br/>[camera_branch.py]"]
  node_node_roi_heads["ROI heads<br/>plane heads<br/>[roi_heads.py]"]
  node_node_plane_head["Plane head<br/>mask/parameter head<br/>[plane_head.py]"]
  node_node_embedding_head["Embedding head<br/>correspondence head<br/>[embedding_head.py]"]
  node_node_camera_head["Camera head<br/>pose classifier<br/>[camera_head.py]"]
  node_node_depth_head["Depth head<br/>aux depth<br/>[depth_head.py]"]
  node_node_camera_utils["Camera utils<br/>pose codebook<br/>[camera.py]"]
end

subgraph group_group_runtime["Tools"]
  node_node_tools_train["Train<br/>cli entry<br/>[train_net.py]"]
  node_node_tools_infer["Infer<br/>cli entry"]
end

subgraph group_group_eval["Eval & Viz"]
  node_node_tools_eval["Eval tool<br/>cli entry<br/>[eval.py]"]
  node_node_mp3d_eval["MP3D eval<br/>metric adapter<br/>[mp3d_evaluation.py]"]
  node_node_viz["Visualization<br/>rendering<br/>[visualization.py]"]
  node_node_mesh_utils["Mesh utils<br/>geometry utils<br/>[mesh_utils.py]"]
end

node_node_dataprep -->|"preprocesses"| node_node_convert_ply
node_node_dataprep -->|"fits"| node_node_fit_planes
node_node_dataprep -->|"renders"| node_node_render_planes
node_node_fit_planes -->|"feeds"| node_node_render_planes
node_node_config -->|"configures"| node_node_siamese_rcnn
node_node_siamese_rcnn -->|"uses"| node_node_roi_heads
node_node_siamese_rcnn -->|"routes"| node_node_camera_branch
node_node_siamese_rcnn -->|"predicts"| node_node_camera_head
node_node_siamese_rcnn -.->|"auxiliary"| node_node_depth_head
node_node_roi_heads -->|"predicts"| node_node_plane_head
node_node_roi_heads -->|"associates"| node_node_embedding_head
node_node_roi_heads -->|"codes"| node_node_camera_utils
node_node_tools_train -->|"loads"| node_node_config
node_node_tools_train -->|"trains"| node_node_siamese_rcnn
node_node_tools_infer -->|"runs"| node_node_siamese_rcnn
node_node_tools_infer -->|"selects"| node_node_camera_utils
node_node_tools_infer -->|"refines"| node_node_mesh_utils
node_node_tools_eval -->|"delegates"| node_node_mp3d_eval
node_node_mp3d_eval -->|"measures"| node_node_mesh_utils
node_node_viz -->|"renders"| node_node_mesh_utils
node_node_tools_eval -->|"visualizes"| node_node_viz
node_node_tools_infer -->|"exports"| node_node_viz

click node_node_dataprep "https://github.com/jinlinyi/sparseplanes/tree/main/data_preparation"
click node_node_convert_ply "https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/convert_mp3d_ply.py"
click node_node_fit_planes "https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/fit_plane_ply.py"
click node_node_render_planes "https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/render_plane.py"
click node_node_config "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/config/config.py"
click node_node_siamese_rcnn "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/meta_arch/siamese_rcnn.py"
click node_node_camera_branch "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/meta_arch/camera_branch.py"
click node_node_roi_heads "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/roi_heads.py"
click node_node_plane_head "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/plane_head.py"
click node_node_embedding_head "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/embedding_head.py"
click node_node_camera_head "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/camera_net/camera_head.py"
click node_node_depth_head "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/depth_net/depth_head.py"
click node_node_camera_utils "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/utils/camera.py"
click node_node_tools_train "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/train_net.py"
click node_node_tools_infer "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/inference_sparse_plane.py"
click node_node_tools_eval "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/eval.py"
click node_node_mp3d_eval "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/evaluation/mp3d_evaluation.py"
click node_node_viz "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/visualization/visualization.py"
click node_node_mesh_utils "https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/utils/mesh_utils.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_dataprep,node_node_convert_ply,node_node_fit_planes,node_node_render_planes toneBlue
class node_node_config,node_node_siamese_rcnn,node_node_camera_branch,node_node_roi_heads,node_node_plane_head,node_node_embedding_head,node_node_camera_head,node_node_depth_head,node_node_camera_utils toneAmber
class node_node_tools_train,node_node_tools_infer toneMint
class node_node_tools_eval,node_node_mp3d_eval,node_node_viz,node_node_mesh_utils toneRose
```

## Components

### Data Prep

Offline ETL from raw Matterport3D assets to plane-labeled training data

- [Data prep](https://github.com/jinlinyi/sparseplanes/tree/main/data_preparation): ETL pipeline · Converts raw scene assets into plane-centric supervision · `data_preparation`
- [Convert PLY](https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/convert_mp3d_ply.py): mesh conversion · Turns MP3D meshes into intermediate training assets · `data_preparation/convert_mp3d_ply.py`
- [Fit planes](https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/fit_plane_ply.py): plane fitting · Derives planar segments and annotations from geometry · `data_preparation/fit_plane_ply.py`
- [Render planes](https://github.com/jinlinyi/sparseplanes/blob/main/data_preparation/render_plane.py): annotation rendering · Produces rendered plane supervision and previews · `data_preparation/render_plane.py`

### Core Model

Detectron2-based plane, correspondence, and camera prediction stack

- [Config](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/config/config.py): stage config · Detectron2-style config and staged YAML workflow · `sparsePlane/sparseplane/config/config.py`
- [Siamese RCNN](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/meta_arch/siamese_rcnn.py): meta-arch · Main two-view reconstruction architecture · `sparsePlane/sparseplane/modeling/meta_arch/siamese_rcnn.py`
- [Camera branch](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/meta_arch/camera_branch.py): camera fusion · Coordinates the camera-side path inside the meta-architecture · `sparsePlane/sparseplane/modeling/meta_arch/camera_branch.py`
- [ROI heads](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/roi_heads.py): plane heads · Plane and embedding heads plus associated losses · `sparsePlane/sparseplane/modeling/roi_heads/roi_heads.py`
- [Plane head](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/plane_head.py): mask/parameter head · Predicts plane masks and plane parameters · `sparsePlane/sparseplane/modeling/roi_heads/plane_head.py`
- [Embedding head](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/roi_heads/embedding_head.py): correspondence head · Builds correspondence features for plane association · `sparsePlane/sparseplane/modeling/roi_heads/embedding_head.py`
- [Camera head](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/camera_net/camera_head.py): pose classifier · Predicts discrete relative camera transformation probabilities · `sparsePlane/sparseplane/modeling/camera_net/camera_head.py`
- [Depth head](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/modeling/depth_net/depth_head.py): aux depth · Auxiliary depth prediction branch · `sparsePlane/sparseplane/modeling/depth_net/depth_head.py`
- [Camera utils](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/utils/camera.py): pose codebook · Discrete pose utilities backed by camera codebooks · `sparsePlane/sparseplane/utils/camera.py`

### Tools

Training, inference, and demo entry points

- [Train](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/train_net.py): cli entry · Training entry point for the Detectron2-style pipeline · `sparsePlane/tools/train_net.py`
- [Infer](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/inference_sparse_plane.py): cli entry · Runs two-view plane reconstruction and refinement · `sparsePlane/tools/inference_sparse_plane.py`

### Eval & Viz

Metrics, reconstruction utilities, and rendering/inspection

- [Eval tool](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/tools/eval.py): cli entry · Runs dataset-aware evaluation workflows · `sparsePlane/tools/eval.py`
- [MP3D eval](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/evaluation/mp3d_evaluation.py): metric adapter · Matterport3D-specific evaluation logic · `sparsePlane/sparseplane/evaluation/mp3d_evaluation.py`
- [Visualization](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/visualization/visualization.py): rendering · Renders plane overlays, reconstructions, and inspection outputs · `sparsePlane/sparseplane/visualization/visualization.py`
- [Mesh utils](https://github.com/jinlinyi/sparseplanes/blob/main/sparsePlane/sparseplane/utils/mesh_utils.py): geometry utils · Mesh and reconstruction helpers used by evaluation and visualization · `sparsePlane/sparseplane/utils/mesh_utils.py`

## Connections

- Data prep → Convert PLY: preprocesses · Raw scan assets are converted into usable mesh inputs
- Data prep → Fit planes: fits · Geometry is segmented into planar supervision
- Data prep → Render planes: renders · Plane annotations and previews are generated offline
- Fit planes → Render planes: feeds · Fitted planes are rendered into training artifacts
- Config → Siamese RCNN: configures · Stage configs and registry drive model assembly
- Siamese RCNN → ROI heads: uses · Central architecture delegates plane prediction to ROI heads
- Siamese RCNN → Camera branch: routes · Camera reasoning is handled as a separate branch
- Siamese RCNN → Camera head: predicts · Relative pose hypotheses are produced by the camera net
- Siamese RCNN → Depth head: auxiliary · Optional depth supervision supports reconstruction
- ROI heads → Plane head: predicts · Plane masks and plane parameters are produced here
- ROI heads → Embedding head: associates · Correspondence embeddings support plane matching
- ROI heads → Camera utils: codes · Discrete pose codebooks support camera selection
- Train → Config: loads · Training is driven by stage-aware config files
- Train → Siamese RCNN: trains · Training entry point executes the main model
- Infer → Siamese RCNN: runs · Inference performs two-view plane reconstruction
- Infer → Camera utils: selects · Candidate poses are drawn from discrete camera hypotheses
- Infer → Mesh utils: refines · Continuous geometric refinement uses mesh helpers
- Eval tool → MP3D eval: delegates · Evaluation tool dispatches to dataset-specific scoring
- MP3D eval → Mesh utils: measures · 3D geometry utilities support reconstruction metrics
- Visualization → Mesh utils: renders · Mesh helpers underpin scene reconstruction views
- Eval tool → Visualization: visualizes · Evaluation outputs can be inspected graphically
- Infer → Visualization: exports · Inference results are visualized as plane and camera outputs
