depth_fm.data.adapter¶
adapter ¶
DepthFM dataset adapter for MarsHiRISEDTM.
Map-style wrapper around MarsHiRISEDTM + HiRISEGeoSampler that produces
DepthFM-ready training pairs::
image: (3, H, W) float32 in [-1, 1] — orthoimage (1ch → 3ch replicated)
dtm: (3, H, W) float32 in [-1, 1] — normalised elevation (1ch → 3ch)
Stereo augmentation: each __getitem__ randomly selects the left or right
orthoimage as the conditioning input. Both pair with the same DTM, doubling
the effective training data.
The heavy image-processing primitives (void filling, seam detection, sun
vector estimation, TIN-artifact detection, topographic residual) used to live
inline in this file. They are now in
depth_fm.data.image_processing.* and re-exported below for the existing
callers (build_litdata.py, train_lightning.py, debug_viz.py,
depthfm_pipeline_diagram.py).
Usage::
from dataset.core.dtm import MarsHiRISEDTM
from dataset.sampling.sampler import HiRISEGeoSampler
from depth_fm.data.adapter import DepthFMHiRISEAdapterCached
base = MarsHiRISEDTM(root="/scratch/mars_hirise_dtm", include_ortho=True,
ortho_type="RED", download=True)
sampler = HiRISEGeoSampler(base, size=0.009, length=10000)
adapter = DepthFMHiRISEAdapterCached(
base_dataset=base, sampler=sampler, resolution=512,
dtm_normalization="relative",
stats_path="dataset_stats/dtm/dataset_stats.json",
)
SeamResult
dataclass
¶
SeamResult(seam_score: float, ortho_score: float, dtm_score: float, cohens_d: float, best_angle_rad: float, best_y: int, best_x: int, line_length: int, num_angles: int, span: float, sparsity: float, composite_score: float, is_seam: bool, seam_heatmap: Optional[ndarray] = None, cohens_d_heatmap: Optional[ndarray] = None, per_angle_max: Optional[ndarray] = None, diag_hot_mask: Optional[ndarray] = None, diag_closed_components: Optional[ndarray] = None, diag_hough_lines: Optional[ndarray] = None, diag_isolation_profile: Optional[tuple] = None)
line_endpoints ¶
Return (y1, x1, y2, x2) pixel endpoints of the best-scoring line.
Source code in src/depth_fm/data/image_processing/seam_detection.py
DepthFMHiRISEAdapterCached ¶
DepthFMHiRISEAdapterCached(base_dataset: MarsHiRISEDTM, sampler: HiRISEGeoSampler, resolution: int = 512, dtm_normalization: Literal['relative', 'log', 'linear'] = 'relative', random_flip: bool = True, random_jitter: bool = False, brightness_jitter: float = 0.1, stats_path: str | None = None, clip: bool = False, use_manifest: bool = True, manifest_workers: int = 16, manifest_dir: str | None = None, erode_radius: int = 2, multiprocessing_context='fork')
Bases: Dataset
Adapter that converts MarsHiRISEDTM samples into DepthFM training pairs.
Each __getitem__ draws a geo-slice from the sampler, loads elevation +
orthoimage(s) via the base dataset, runs the manifest-cached preprocessing
(sun vector, valid mask, void fill), and returns a normalised dict ready
for the DepthFM training loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dataset
|
MarsHiRISEDTM
|
An initialised |
required |
sampler
|
HiRISEGeoSampler
|
A TorchGeo geo-sampler that yields GeoSlice indices. |
required |
resolution
|
int
|
Output spatial resolution in pixels (square crop). |
512
|
dtm_normalization
|
Literal['relative', 'log', 'linear']
|
|
'relative'
|
random_flip
|
bool
|
Apply random horizontal/vertical flips + 90° rotations. |
True
|
brightness_jitter
|
float
|
Max relative brightness perturbation on image only. |
0.1
|
stats_path
|
str | None
|
Path to |
None
|
use_manifest
|
bool
|
If True, build / load a parquet manifest of clean patches
so |
True
|
Source code in src/depth_fm/data/adapter.py
erode_valid_mask ¶
Erode a binary mask to trim noisy boundary pixels.
Source code in src/depth_fm/data/image_processing/mask_ops.py
fill_dtm_smart_diffusion ¶
fill_dtm_smart_diffusion(tensor: Tensor, valid_mask: Tensor, iterations: int = 64, erode_radius: int = 2) -> torch.Tensor
Fill invalid regions using Laplacian diffusion (heat equation).
Source code in src/depth_fm/data/image_processing/void_filling.py
fill_invalid_nearest_neighbor ¶
Fill invalid regions using nearest-neighbor propagation via EDT.
Supports (H, W), (C, H, W), and (B, C, H, W) tensors.
Source code in src/depth_fm/data/image_processing/void_filling.py
fill_voids_gmrf ¶
fill_voids_gmrf(image: Tensor, dtm: Tensor, valid_mask: Tensor, *, erode_radius: int = 2, connectivity: int = 4, tau: float = 1.0, nugget: float = 1e-06) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Fill voids in an orthoimage and DTM using GMRF conditional distribution.
One sparse linear solve per channel. No iteration. O(n^{3/2}) on 2D grids.
Returns (filled_image, filled_dtm, eroded_mask). The eroded mask records which pixels were considered trustworthy (1) vs infilled (0) — all pixels are valid in the filled outputs.
Source code in src/depth_fm/data/image_processing/void_filling.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
fill_voids_kriging ¶
fill_voids_kriging(image: Tensor, dtm: Tensor, valid_mask: Tensor, *, erode_radius: int = 2, max_training_points: int = 2500, variogram_model: str = 'linear', seed: int = 42) -> tuple[torch.Tensor, torch.Tensor]
Fill voids in an orthoimage and DTM with a single kriging pass per channel.
Optimised for 512x512 tiles: subsamples valid pixels for training, then predicts every void pixel at once. No iteration, no per-void labelling, no diffusion loop.
Source code in src/depth_fm/data/image_processing/void_filling.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
compute_artifact_multipliers ¶
compute_artifact_multipliers(seam_heatmap: ndarray, valid_mask: ndarray, threshold: float = 0.2) -> tuple[float, float, np.ndarray, np.ndarray]
Compute structural multipliers that distinguish seams from natural features.
Returns (span_ratio, sparsity, hot_mask, labels): * span_ratio — defeats short craters; true seams cross the whole tile. * sparsity — defeats dense dunes; true seams are a singular line.
Source code in src/depth_fm/data/image_processing/seam_detection.py
compute_piecewise_linearity ¶
compute_piecewise_linearity(seam_heatmap: ndarray, threshold_ratio: float = 0.3) -> tuple[float, np.ndarray]
Score how piecewise-linear the high-scoring pixels are (handles corners).
Returns (linearity_score, line_mask) where linearity is in [0.0, 1.0]: 1.0 = highly structured/linear, 0.0 = curved/messy.
Source code in src/depth_fm/data/image_processing/seam_detection.py
compute_spatial_isolation ¶
compute_spatial_isolation(score_map: Tensor, valid_mask: Tensor, best_x: int, best_y: int, angle_rad: float, profile_length: int = 50, exclusion_zone: int = 12) -> tuple[float, Optional[tuple]]
Sample a perpendicular slice across the seam.
Returns the ratio of the central peak to the surrounding parallel background, plus the raw profile data arrays for visualization.
Source code in src/depth_fm/data/image_processing/seam_detection.py
detect_seam_artifact ¶
detect_seam_artifact(ortho: Tensor, elevation: Tensor, valid_mask: Tensor, line_length: int = 41, num_angles: int = 12, side_offset: int = 2, min_valid_ratio: float = 0.6, ortho_weight: float = 1.0, dtm_weight: float = 0.3, erosion_kernel: int = 9, seam_threshold: float = 2.4, return_diagnostics: bool = False) -> SeamResult
Detect seam artifacts (mosaicking discontinuities) in a HiRISE patch.
Returns a SeamResult. With return_diagnostics=True, the result also
carries seam_heatmap, cohens_d_heatmap, and per_angle_max, used by
the visualization and refinement UI to show where and along which
angle the detector fired.
Source code in src/depth_fm/data/image_processing/seam_detection.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
is_tin_artifact ¶
Scale-invariant TIN-artifact detection using localized maximum density.
Returns the maximum local fraction of zero-curvature pixels within any
kernel_size × kernel_size window. Patches with stretched-triangle TIN
artifacts produce values near 1.0; natural terrain produces values <0.5.
Source code in src/depth_fm/data/image_processing/seam_detection.py
estimate_sun_vector_irls ¶
estimate_sun_vector_irls(dtm: Tensor, ortho: Tensor, valid_mask: Tensor, max_iter: int = 15, tol: float = 0.0001) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Decoupled IRLS sun-vector estimator.
Separates ambient light estimation from the linear system to prevent the Nz / bias collinearity trap from inverting the sun vector. Returns (sun_vec, intensity, ambient).
Source code in src/depth_fm/data/image_processing/sun_vector.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
estimate_sun_vector_ols ¶
estimate_sun_vector_ols(dtm: Tensor, ortho: Tensor, valid_mask: Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Estimates the sun vector [sx, sy, sz] using Ordinary Least Squares.
Handles shapes (C, H, W) or (1, C, H, W). Batch size must be 1.
Source code in src/depth_fm/data/image_processing/sun_vector.py
compute_topographic_residual ¶
Fit a 2D plane to elevation and return the RMS residual.
Removes macroscopic slopes so the returned scalar isolates true topographic roughness (used as a manifest filter to reject overly-flat patches).