Add src/depth_refiner.py
Browse files- src/depth_refiner.py +59 -0
src/depth_refiner.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Depth refinement module for S23DR 2026.
|
| 2 |
+
|
| 3 |
+
Learn a per-scene scale + shift correction for MoGe v2 depth maps
|
| 4 |
+
using COLMAP sparse depth as supervision.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
from typing import Tuple, Optional
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def ransac_scale_fit(dense_depth, sparse_depth, house_mask=None, n_iterations=100, inlier_threshold=0.5, min_samples=3):
|
| 14 |
+
mask = (sparse_depth > 0) & (dense_depth > 0) & (sparse_depth < 50) & (dense_depth < 50)
|
| 15 |
+
if house_mask is not None: mask = mask & house_mask
|
| 16 |
+
X, Y = dense_depth[mask], sparse_depth[mask]
|
| 17 |
+
if len(X) < min_samples:
|
| 18 |
+
return (np.median(Y / X), 0.0) if len(X) > 0 else (1.0, 0.0)
|
| 19 |
+
best_scale, best_shift, best_inliers = 1.0, 0.0, 0
|
| 20 |
+
rng = np.random.RandomState(42)
|
| 21 |
+
for _ in range(n_iterations):
|
| 22 |
+
idx = rng.choice(len(X), min(min_samples, len(X)), replace=False)
|
| 23 |
+
A = np.column_stack([X[idx], np.ones(len(idx))])
|
| 24 |
+
try: params, _, _, _ = np.linalg.lstsq(A, Y[idx], rcond=None); scale, shift = params[0], params[1]
|
| 25 |
+
except: continue
|
| 26 |
+
if scale <= 0: continue
|
| 27 |
+
inliers = (np.abs(scale * X + shift - Y) < inlier_threshold).sum()
|
| 28 |
+
if inliers > best_inliers: best_inliers = inliers; best_scale = scale; best_shift = shift
|
| 29 |
+
inlier_mask = np.abs(best_scale * X + best_shift - Y) < inlier_threshold
|
| 30 |
+
if inlier_mask.sum() >= min_samples:
|
| 31 |
+
A = np.column_stack([X[inlier_mask], np.ones(inlier_mask.sum())])
|
| 32 |
+
try: params, _, _, _ = np.linalg.lstsq(A, Y[inlier_mask], rcond=None); best_scale, best_shift = params[0], params[1]
|
| 33 |
+
except: pass
|
| 34 |
+
return best_scale, best_shift
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def fit_depth_to_colmap(dense_depth, colmap_rec, img_id, ade_seg=None, method='ransac'):
|
| 38 |
+
from hoho2025.example_solutions import get_sparse_depth, get_house_mask
|
| 39 |
+
sparse_depth, found, col_img, proj_pts = get_sparse_depth(colmap_rec, img_id, dense_depth)
|
| 40 |
+
if not found: return dense_depth, False
|
| 41 |
+
house_mask = get_house_mask(ade_seg) if ade_seg is not None else None
|
| 42 |
+
if method == 'ransac':
|
| 43 |
+
scale, shift = ransac_scale_fit(dense_depth, sparse_depth, house_mask)
|
| 44 |
+
fitted = scale * dense_depth + shift
|
| 45 |
+
else:
|
| 46 |
+
from hoho2025.example_solutions import fit_scale_robust_median
|
| 47 |
+
_, fitted = fit_scale_robust_median(dense_depth, sparse_depth, house_mask)
|
| 48 |
+
return np.clip(fitted, 0, None), True
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class DepthRefinerNet(nn.Module):
|
| 52 |
+
def __init__(self, in_channels=5):
|
| 53 |
+
super().__init__()
|
| 54 |
+
self.net = nn.Sequential(nn.Conv2d(in_channels, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 16, 3, padding=1), nn.ReLU(), nn.Conv2d(16, 2, 1))
|
| 55 |
+
def forward(self, x, dense_depth):
|
| 56 |
+
correction = self.net(x)
|
| 57 |
+
scale = torch.sigmoid(correction[:, 0:1]) * 4.0
|
| 58 |
+
shift = correction[:, 1:2] * 2.0
|
| 59 |
+
return torch.clamp(scale * dense_depth + shift, min=0)
|