How captaineven/monodepthv2 works
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 Trainer · layers.py Differentiable geometry · mono_dataset.py MonoDataset · resnet_encoder.py ResNet encoder
Components 18
Training
- Training CLI
train.py - Experiment options · Shared training and evaluation arguments.
options.py - Trainer · Runs epochs, optimization, checkpoints, and TensorBoard logging.
trainer.py - Differentiable geometry · Backprojects, warps, reconstructs, and regularizes views.
layers.py - Weights and events · Checkpoints and TensorBoard logs in the configured log directory.
Data pipeline
- MonoDataset · Builds multi-scale target and source-frame tensors.
datasets/mono_dataset.py - KITTI dataset adapter · Provides KITTI paths, calibration, and camera behavior.
datasets/kitti_dataset.py - Split manifests · Select training and evaluation records.
splits/eigen_zhou/train_files.txt - KITTI data · Raw, depth, and odometry inputs.
Networks
- ResNet encoder · Produces multi-scale visual features.
networks/resnet_encoder.py - Depth decoder · Predicts multi-scale disparity/depth.
networks/depth_decoder.py - Pose models · Option-selected PoseDecoder or PoseCNN for temporal motion.
networks/pose_decoder.py - Stereo network
networks/stereo_net.py
Evaluation
- Depth evaluation · Scores depth predictions or writes benchmark PNGs.
evaluate_depth.py - Ground-truth export · Creates KITTI evaluation depth arrays.
export_gt_depth.py - Pose evaluation · Evaluates odometry pose trajectories.
evaluate_pose.py - Apollo stereo tools · Stereo evaluation and finetuning with dedicated utilities.
evaluation/apollo_stereo/eval_stereo.py
Other components
- Pretrained weights · ImageNet encoders and downloadable model checkpoints.
Connections 22
- 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