# captaineven/monodepthv2 architecture

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

- Interactive diagram: https://gitdiagram.com/captaineven/monodepthv2
- Repository: https://github.com/captaineven/monodepthv2

## Overview

#### Purpose and entry points
Monodepth2 is a PyTorch implementation of self-supervised monocular depth prediction, supporting monocular, stereo, and combined supervision on KITTI-style data. Primary executables are training (`train.py`), single-image inference (`test_simple.py`), depth evaluation (`evaluate_depth.py`), pose evaluation (`evaluate_pose.py`), and ground-truth-depth export (`export_gt_depth.py`).

#### Training orchestration
`train.py` parses experiment arguments and delegates the epoch/batch lifecycle, optimization, checkpointing, and TensorBoard logging to `trainer.py`. Shared CLI surface—including data root, split, frame IDs, stereo mode, resolution, and weight-loading options—is centralized in `options.py`. Training is designed for one CUDA GPU and writes model weights/events under the configured log directory.

#### Dataset and sample construction
The dataset boundary is the `MonoDataset` abstraction in `datasets/mono_dataset.py`; it constructs multi-scale image tensors for the target frame and requested temporal/stereo source frames. `datasets/kitti_dataset.py` supplies KITTI-specific path, calibration, and camera behavior. Split files select records independently of loader implementation, e.g. `splits/eigen_zhou/train_files.txt`, `splits/eigen_full/train_files.txt`, and `splits/odom/train_files.txt`.

#### Model pipeline
The depth network is an encoder-decoder: `networks/resnet_encoder.py` produces multi-scale visual features and `networks/depth_decoder.py` predicts disparity/depth at multiple scales. During monocular supervision, pose estimation uses either the ResNet-style decoder path (`networks/pose_decoder.py`) or convolutional pose model (`networks/pose_cnn.py`), selected by options. `networks/stereo_net.py` is a separate stereo-oriented network component. Networks are registered through `networks/__init__.py`.

#### Self-supervised geometry and losses
The trainer couples predicted disparity with source images and estimated poses to reconstruct the target view; stereo source frames provide a known-baseline alternative. Differentiable projection, backprojection, image warping, smoothness, and related tensor operations are centralized in `layers.py`. This is the core boundary between learned networks and camera geometry; KITTI calibration/data utilities reside in `kitti_utils.py` and `data_utils/kitti_util.py`.

#### Evaluation and outputs
`evaluate_depth.py` loads trained depth weights or external disparity arrays, applies split-specific depth ground truth and mono/stereo scaling rules, then reports KITTI metrics or emits benchmark PNGs. `export_gt_depth.py` creates evaluation depth arrays from KITTI raw data. `evaluate_pose.py` evaluates pose trajectories on odometry splits. Apollo stereo evaluation/finetuning is isolated under `evaluation/apollo_stereo/eval_stereo.py` and `evaluation/apollo_stereo/finetune.py`, with its own data/evaluation utilities.

#### Runtime and external dependencies
The material runtime is Python with PyTorch/Torchvision, CUDA-enabled single-GPU execution, OpenCV for evaluation, and TensorBoardX logging. External inputs/services are local KITTI raw/depth/odometry datasets, split manifests in `splits/`, ImageNet-pretrained encoder weights when enabled, and downloadable pretrained checkpoints hosted on Google Cloud Storage.

Main entry points: [`trainer.py`](https://github.com/captaineven/monodepthv2/blob/master/trainer.py) (Trainer), [`layers.py`](https://github.com/captaineven/monodepthv2/blob/master/layers.py) (Differentiable geometry), [`datasets/mono_dataset.py`](https://github.com/captaineven/monodepthv2/blob/master/datasets/mono_dataset.py) (MonoDataset), [`networks/resnet_encoder.py`](https://github.com/captaineven/monodepthv2/blob/master/networks/resnet_encoder.py) (ResNet encoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_train["Training"]
  node_train_entry["Training CLI<br/>Python entry point<br/>[train.py]"]
  node_options["Experiment options<br/>CLI configuration<br/>[options.py]"]
  node_trainer["Trainer<br/>training orchestration<br/>[trainer.py]"]
  node_geometry["Differentiable geometry<br/>projection and losses<br/>[layers.py]"]
  node_artifacts[("Weights and events<br/>training outputs")]
end

subgraph group_data["Data pipeline"]
  node_dataset["MonoDataset<br/>dataset abstraction<br/>[mono_dataset.py]"]
  node_kitti_dataset["KITTI dataset adapter<br/>dataset implementation<br/>[kitti_dataset.py]"]
  node_splits["Split manifests<br/>record manifests<br/>[train_files.txt]"]
  node_kitti_data[("KITTI data<br/>local external dataset")]
end

subgraph group_models["Networks"]
  node_encoder["ResNet encoder<br/>feature encoder<br/>[resnet_encoder.py]"]
  node_depth_decoder["Depth decoder<br/>depth head<br/>[depth_decoder.py]"]
  node_pose_models["Pose models<br/>pose estimation<br/>[pose_decoder.py]"]
  node_stereo_net["Stereo network<br/>stereo component<br/>[stereo_net.py]"]
end

subgraph group_eval["Evaluation"]
  node_depth_eval["Depth evaluation<br/>Python entry point<br/>[evaluate_depth.py]"]
  node_gt_export["Ground-truth export<br/>Python entry point<br/>[export_gt_depth.py]"]
  node_pose_eval["Pose evaluation<br/>Python entry point<br/>[evaluate_pose.py]"]
  node_apollo_eval["Apollo stereo tools<br/>separate evaluation pipeline<br/>[eval_stereo.py]"]
end

node_pretrained[("Pretrained weights<br/>external checkpoints")]

node_train_entry -->|"parses"| node_options
node_train_entry -->|"starts"| node_trainer
node_options -->|"configures"| node_trainer
node_trainer -->|"loads batches"| node_dataset
node_splits -->|"select records"| node_dataset
node_kitti_dataset -->|"implements"| node_dataset
node_kitti_data -->|"image and calibration data"| node_kitti_dataset
node_trainer -->|"trains"| node_encoder
node_encoder -->|"multi-scale features"| node_depth_decoder
node_trainer -->|"monocular pose"| node_pose_models
node_trainer -.->|"stereo mode"| node_stereo_net
node_depth_decoder -->|"predicted disparity"| node_geometry
node_pose_models -->|"estimated poses"| node_geometry
node_dataset -->|"target/source images"| node_geometry
node_geometry -->|"reconstruction losses"| node_trainer
node_trainer -->|"writes"| node_artifacts
node_pretrained -.->|"optional initialization"| node_encoder
node_artifacts -->|"trained weights"| node_depth_eval
node_kitti_data -->|"raw depth source"| node_gt_export
node_gt_export -->|"ground-truth arrays"| node_depth_eval
node_artifacts -->|"pose weights"| node_pose_eval
node_kitti_data -->|"odometry data"| node_pose_eval

click node_train_entry "https://github.com/captaineven/monodepthv2/blob/master/train.py"
click node_options "https://github.com/captaineven/monodepthv2/blob/master/options.py"
click node_trainer "https://github.com/captaineven/monodepthv2/blob/master/trainer.py"
click node_dataset "https://github.com/captaineven/monodepthv2/blob/master/datasets/mono_dataset.py"
click node_kitti_dataset "https://github.com/captaineven/monodepthv2/blob/master/datasets/kitti_dataset.py"
click node_splits "https://github.com/captaineven/monodepthv2/blob/master/splits/eigen_zhou/train_files.txt"
click node_encoder "https://github.com/captaineven/monodepthv2/blob/master/networks/resnet_encoder.py"
click node_depth_decoder "https://github.com/captaineven/monodepthv2/blob/master/networks/depth_decoder.py"
click node_pose_models "https://github.com/captaineven/monodepthv2/blob/master/networks/pose_decoder.py"
click node_stereo_net "https://github.com/captaineven/monodepthv2/blob/master/networks/stereo_net.py"
click node_geometry "https://github.com/captaineven/monodepthv2/blob/master/layers.py"
click node_depth_eval "https://github.com/captaineven/monodepthv2/blob/master/evaluate_depth.py"
click node_gt_export "https://github.com/captaineven/monodepthv2/blob/master/export_gt_depth.py"
click node_pose_eval "https://github.com/captaineven/monodepthv2/blob/master/evaluate_pose.py"
click node_apollo_eval "https://github.com/captaineven/monodepthv2/blob/master/evaluation/apollo_stereo/eval_stereo.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_train_entry,node_options,node_trainer,node_geometry,node_artifacts toneBlue
class node_dataset,node_kitti_dataset,node_splits,node_kitti_data toneAmber
class node_encoder,node_depth_decoder,node_pose_models,node_stereo_net toneMint
class node_depth_eval,node_gt_export,node_pose_eval,node_apollo_eval toneRose
class node_pretrained toneNeutral
```

## Components

### Training

- [Training CLI](https://github.com/captaineven/monodepthv2/blob/master/train.py): Python entry point · `train.py`
- [Experiment options](https://github.com/captaineven/monodepthv2/blob/master/options.py): CLI configuration · Shared training and evaluation arguments. · `options.py`
- [Trainer](https://github.com/captaineven/monodepthv2/blob/master/trainer.py): training orchestration · Runs epochs, optimization, checkpoints, and TensorBoard logging. · `trainer.py`
- [Differentiable geometry](https://github.com/captaineven/monodepthv2/blob/master/layers.py): projection and losses · Backprojects, warps, reconstructs, and regularizes views. · `layers.py`
- **Weights and events**: training outputs · Checkpoints and TensorBoard logs in the configured log directory.

### Data pipeline

- [MonoDataset](https://github.com/captaineven/monodepthv2/blob/master/datasets/mono_dataset.py): dataset abstraction · Builds multi-scale target and source-frame tensors. · `datasets/mono_dataset.py`
- [KITTI dataset adapter](https://github.com/captaineven/monodepthv2/blob/master/datasets/kitti_dataset.py): dataset implementation · Provides KITTI paths, calibration, and camera behavior. · `datasets/kitti_dataset.py`
- [Split manifests](https://github.com/captaineven/monodepthv2/blob/master/splits/eigen_zhou/train_files.txt): record manifests · Select training and evaluation records. · `splits/eigen_zhou/train_files.txt`
- **KITTI data**: local external dataset · Raw, depth, and odometry inputs.

### Networks

- [ResNet encoder](https://github.com/captaineven/monodepthv2/blob/master/networks/resnet_encoder.py): feature encoder · Produces multi-scale visual features. · `networks/resnet_encoder.py`
- [Depth decoder](https://github.com/captaineven/monodepthv2/blob/master/networks/depth_decoder.py): depth head · Predicts multi-scale disparity/depth. · `networks/depth_decoder.py`
- [Pose models](https://github.com/captaineven/monodepthv2/blob/master/networks/pose_decoder.py): pose estimation · Option-selected PoseDecoder or PoseCNN for temporal motion. · `networks/pose_decoder.py`
- [Stereo network](https://github.com/captaineven/monodepthv2/blob/master/networks/stereo_net.py): stereo component · `networks/stereo_net.py`

### Evaluation

- [Depth evaluation](https://github.com/captaineven/monodepthv2/blob/master/evaluate_depth.py): Python entry point · Scores depth predictions or writes benchmark PNGs. · `evaluate_depth.py`
- [Ground-truth export](https://github.com/captaineven/monodepthv2/blob/master/export_gt_depth.py): Python entry point · Creates KITTI evaluation depth arrays. · `export_gt_depth.py`
- [Pose evaluation](https://github.com/captaineven/monodepthv2/blob/master/evaluate_pose.py): Python entry point · Evaluates odometry pose trajectories. · `evaluate_pose.py`
- [Apollo stereo tools](https://github.com/captaineven/monodepthv2/blob/master/evaluation/apollo_stereo/eval_stereo.py): separate evaluation pipeline · Stereo evaluation and finetuning with dedicated utilities. · `evaluation/apollo_stereo/eval_stereo.py`

### Other components

- **Pretrained weights**: external checkpoints · ImageNet encoders and downloadable model checkpoints.

## Connections

- Training CLI → Experiment options: parses
- Training CLI → Trainer: starts
- Experiment options → Trainer: configures
- Trainer → MonoDataset: loads batches
- Split manifests → MonoDataset: select records
- KITTI dataset adapter → MonoDataset: implements
- KITTI data → KITTI dataset adapter: image and calibration data
- Trainer → ResNet encoder: trains
- ResNet encoder → Depth decoder: multi-scale features
- Trainer → Pose models: monocular pose
- Trainer → Stereo network: stereo mode
- Depth decoder → Differentiable geometry: predicted disparity
- Pose models → Differentiable geometry: estimated poses
- MonoDataset → Differentiable geometry: target/source images
- Differentiable geometry → Trainer: reconstruction losses
- Trainer → Weights and events: writes
- Pretrained weights → ResNet encoder: optional initialization
- Weights and events → Depth evaluation: trained weights
- KITTI data → Ground-truth export: raw depth source
- Ground-truth export → Depth evaluation: ground-truth arrays
- Weights and events → Pose evaluation: pose weights
- KITTI data → Pose evaluation: odometry data
