depth_fm¶
depth_fm ¶
Mars DepthFM — flow-matching DTM generation from monocular Mars imagery.
Public API:
from depth_fm import MarsDepthFM, build_model
from depth_fm import DepthFMLightningModule
Subpackages:
models/—MarsDepthFMwrapper + CompVis UNet backbone + experimental variants.training/— Lightning module andtrain_lightning.pyCLI entry point.data/— Map-style adapter, LitData streaming datamodule, normalization (scalers).objectives/— Loss functions and evaluation metrics.flow/— Flow-matching noise schedule.viz/— Publication figures (train_viz) and training-side diagnostics (debug_viz).
MarsDepthFM ¶
MarsDepthFM(backbone: Module, vae: AutoencoderKL, noising_step: int, empty_text_embed: Tensor, freeze_encoder: bool = False)
Bases: Module
Wrapper for DepthFM flow matching depth estimation.
Exposes a clean interface for the training loop
encode_to_latent — pixel → latent (frozen VAE, no grad) decode_from_latent — latent → pixel (frozen VAE, grad allowed for losses) predict_velocity — core UNet forward
Source code in src/depth_fm/models/mars_depthfm.py
encode_to_latent ¶
Encode pixel-space images (B, 3, H, W) ∈ [-1, 1] to latent space. Returns: (B, 4, h, w) scaled latents.
Source code in src/depth_fm/models/mars_depthfm.py
decode_from_latent ¶
Decode latent (B, 4, h, w) to pixel space (B, 3, H, W) ∈ [-1, 1].
Called WITHOUT torch.no_grad() when used for pixel-space training losses so gradients flow back through the frozen VAE decoder to v_pred.
Source code in src/depth_fm/models/mars_depthfm.py
forward ¶
Full inference forward — mirrors DepthFM.forward() line-for-line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ims
|
Tensor
|
(1, 3, H, W) in [-1, 1] |
required |
num_steps
|
int
|
Euler ODE steps |
2
|
ensemble_size
|
int
|
repeat image in batch; average depth at end |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
depth |
Tensor
|
(1, 1, H, W) in [0, 1] |
Source code in src/depth_fm/models/mars_depthfm.py
predict_depth ¶
Public inference API — mirrors DepthFM.predict_depth() exactly.
Returns:
| Name | Type | Description |
|---|---|---|
depth |
Tensor
|
(1, 1, H, W) in [0, 1] |
Source code in src/depth_fm/models/mars_depthfm.py
predict_velocity ¶
Predict velocity field v_θ(z_t, t; z_img).
The UNet concatenates z_img_cond onto z_t INTERNALLY before input_blocks, so we pass them as separate arguments (x and context).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z_t
|
Tensor
|
(B, 4, h, w) — noisy interpolant on the flow path |
required |
t
|
Tensor
|
(B,) in [0, 1] — continuous timestep |
required |
z_img_cond
|
Tensor
|
(B, 4, h, w) — clean image latent (conditioning) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
v_pred |
Tensor
|
(B, 4, h, w) — predicted velocity |
Source code in src/depth_fm/models/mars_depthfm.py
DepthFMLightningModule ¶
Bases: LightningModule
Lightning module for DepthFM flow matching training.
Handles: - Conditional flow matching velocity loss - Surface normals auxiliary loss with warmup - Logit-normal timestep sampling - Noise augmentation on source distribution - Multi-step Euler inference for validation - Full metric computation and logging
Source code in src/depth_fm/training/lightning_module.py
estimate_uncertainty ¶
estimate_uncertainty(z_img: Tensor, num_samples: int = 10, num_steps: int = 4) -> tuple[torch.Tensor, torch.Tensor]
Computes the epistemic uncertainty via stochastic ODE sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z_img
|
Tensor
|
(B, C, H, W) Encoded input orthoimage. |
required |
num_samples
|
int
|
Number of stochastic noise initializations (N). |
10
|
num_steps
|
int
|
Euler integration steps per sample. |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
mean_dtm |
Tensor
|
(B, 1, H, W) The expected topographic surface. |
var_dtm |
Tensor
|
(B, 1, H, W) The pixel-wise epistemic variance. |
Source code in src/depth_fm/training/lightning_module.py
on_train_end ¶
Final wandb upload and vis worker shutdown.
Source code in src/depth_fm/training/lightning_module.py
build_model ¶
Factory function: build the MarsDepthFM model from config.
Only supports backend="sd21" (the actual DepthFM architecture).
Source code in src/depth_fm/models/mars_depthfm.py
load_sd21_backend ¶
load_sd21_backend(depthfm_checkpoint: str, vae_id: str, device: str = 'cpu', use_checkpoint=True) -> Tuple[nn.Module, AutoencoderKL, int, torch.Tensor]
Load the actual DepthFM model from the official checkpoint.
Checkpoint format
noising_step — int, e.g. 200 ldm_hparams — dict of UNetModel constructor kwargs empty_text_embedding — (1, seq_len, context_dim) null text conditioning state_dict — UNetModel weights (keys match CompVis naming)
Returns:
| Name | Type | Description |
|---|---|---|
unet |
Module
|
trainable UNetModel |
vae |
AutoencoderKL
|
frozen SD 1.5 AutoencoderKL |
noising_step |
int
|
int |
empty_text_embed |
Tensor
|
(1, seq_len, context_dim) tensor |