Spaces:
Running
on
Zero
Running
on
Zero
File size: 14,715 Bytes
a51c6d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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 |
from typing import *
from numbers import Number
import torch
import torch.nn.functional as F
import numpy as np
import utils3d
from ..utils.geometry_torch import (
weighted_mean,
mask_aware_nearest_resize,
intrinsics_to_fov
)
from ..utils.alignment import (
align_points_scale_z_shift,
align_points_scale_xyz_shift,
align_points_xyz_shift,
align_affine_lstsq,
align_depth_scale,
align_depth_affine,
align_points_scale,
)
from ..utils.tools import key_average, timeit
def rel_depth(pred: torch.Tensor, gt: torch.Tensor, eps: float = 1e-6):
rel = (torch.abs(pred - gt) / (gt + eps)).mean()
return rel.item()
def delta1_depth(pred: torch.Tensor, gt: torch.Tensor, eps: float = 1e-6):
delta1 = (torch.maximum(gt / pred, pred / gt) < 1.25).float().mean()
return delta1.item()
def rel_point(pred: torch.Tensor, gt: torch.Tensor, eps: float = 1e-6):
dist_gt = torch.norm(gt, dim=-1)
dist_err = torch.norm(pred - gt, dim=-1)
rel = (dist_err / (dist_gt + eps)).mean()
return rel.item()
def delta1_point(pred: torch.Tensor, gt: torch.Tensor, eps: float = 1e-6):
dist_pred = torch.norm(pred, dim=-1)
dist_gt = torch.norm(gt, dim=-1)
dist_err = torch.norm(pred - gt, dim=-1)
delta1 = (dist_err < 0.25 * torch.minimum(dist_gt, dist_pred)).float().mean()
return delta1.item()
def rel_point_local(pred: torch.Tensor, gt: torch.Tensor, diameter: torch.Tensor):
dist_err = torch.norm(pred - gt, dim=-1)
rel = (dist_err / diameter).mean()
return rel.item()
def delta1_point_local(pred: torch.Tensor, gt: torch.Tensor, diameter: torch.Tensor):
dist_err = torch.norm(pred - gt, dim=-1)
delta1 = (dist_err < 0.25 * diameter).float().mean()
return delta1.item()
def boundary_f1(pred: torch.Tensor, gt: torch.Tensor, mask: torch.Tensor, radius: int = 1):
neighbor_x, neight_y = torch.meshgrid(
torch.linspace(-radius, radius, 2 * radius + 1, device=pred.device),
torch.linspace(-radius, radius, 2 * radius + 1, device=pred.device),
indexing='xy'
)
neighbor_mask = (neighbor_x ** 2 + neight_y ** 2) <= radius ** 2 + 1e-5
pred_window = utils3d.torch.sliding_window_2d(pred, window_size=2 * radius + 1, stride=1, dim=(-2, -1)) # [H, W, 2*R+1, 2*R+1]
gt_window = utils3d.torch.sliding_window_2d(gt, window_size=2 * radius + 1, stride=1, dim=(-2, -1)) # [H, W, 2*R+1, 2*R+1]
mask_window = neighbor_mask & utils3d.torch.sliding_window_2d(mask, window_size=2 * radius + 1, stride=1, dim=(-2, -1)) # [H, W, 2*R+1, 2*R+1]
pred_rel = pred_window / pred[radius:-radius, radius:-radius, None, None]
gt_rel = gt_window / gt[radius:-radius, radius:-radius, None, None]
valid = mask[radius:-radius, radius:-radius, None, None] & mask_window
f1_list = []
w_list = t_list = torch.linspace(0.05, 0.25, 10).tolist()
for t in t_list:
pred_label = pred_rel > 1 + t
gt_label = gt_rel > 1 + t
TP = (pred_label & gt_label & valid).float().sum()
precision = TP / (gt_label & valid).float().sum().clamp_min(1e-12)
recall = TP / (pred_label & valid).float().sum().clamp_min(1e-12)
f1 = 2 * precision * recall / (precision + recall).clamp_min(1e-12)
f1_list.append(f1.item())
f1_avg = sum(w * f1 for w, f1 in zip(w_list, f1_list)) / sum(w_list)
return f1_avg
def compute_metrics(
pred: Dict[str, torch.Tensor],
gt: Dict[str, torch.Tensor],
vis: bool = False
) -> Tuple[Dict[str, Dict[str, Number]], Dict[str, torch.Tensor]]:
"""
A unified function to compute metrics for different types of predictions and ground truths.
#### Supported keys in pred:
- `disparity_affine_invariant`: disparity map predicted by a depth estimator with scale and shift invariant.
- `depth_scale_invariant`: depth map predicted by a depth estimator with scale invariant.
- `depth_affine_invariant`: depth map predicted by a depth estimator with scale and shift invariant.
- `depth_metric`: depth map predicted by a depth estimator with no scale or shift.
- `points_scale_invariant`: point map predicted by a point estimator with scale invariant.
- `points_affine_invariant`: point map predicted by a point estimator with scale and xyz shift invariant.
- `points_metric`: point map predicted by a point estimator with no scale or shift.
- `intrinsics`: normalized camera intrinsics matrix.
#### Required keys in gt:
- `depth`: depth map ground truth (in metric units if `depth_metric` is used)
- `points`: point map ground truth in camera coordinates.
- `mask`: mask indicating valid pixels in the ground truth.
- `intrinsics`: normalized ground-truth camera intrinsics matrix.
- `is_metric`: whether the depth is in metric units.
"""
metrics = {}
misc = {}
mask = gt['depth_mask']
gt_depth = gt['depth']
gt_points = gt['points']
height, width = mask.shape[-2:]
_, lr_mask, lr_index = mask_aware_nearest_resize(None, mask, (64, 64), return_index=True)
only_depth = not any('point' in k for k in pred)
pred_depth_aligned, pred_points_aligned = None, None
# Metric depth
if 'depth_metric' in pred and gt['is_metric']:
pred_depth, gt_depth = pred['depth_metric'], gt['depth']
metrics['depth_metric'] = {
'rel': rel_depth(pred_depth[mask], gt_depth[mask]),
'delta1': delta1_depth(pred_depth[mask], gt_depth[mask])
}
if pred_depth_aligned is None:
pred_depth_aligned = pred_depth
# Scale-invariant depth
if 'depth_scale_invariant' in pred:
pred_depth_scale_invariant = pred['depth_scale_invariant']
elif 'depth_metric' in pred:
pred_depth_scale_invariant = pred['depth_metric']
else:
pred_depth_scale_invariant = None
if pred_depth_scale_invariant is not None:
pred_depth = pred_depth_scale_invariant
pred_depth_lr_masked, gt_depth_lr_masked = pred_depth[lr_index][lr_mask], gt_depth[lr_index][lr_mask]
scale = align_depth_scale(pred_depth_lr_masked, gt_depth_lr_masked, 1 / gt_depth_lr_masked)
pred_depth = pred_depth * scale
metrics['depth_scale_invariant'] = {
'rel': rel_depth(pred_depth[mask], gt_depth[mask]),
'delta1': delta1_depth(pred_depth[mask], gt_depth[mask])
}
if pred_depth_aligned is None:
pred_depth_aligned = pred_depth
# Affine-invariant depth
if 'depth_affine_invariant' in pred:
pred_depth_affine_invariant = pred['depth_affine_invariant']
elif 'depth_scale_invariant' in pred:
pred_depth_affine_invariant = pred['depth_scale_invariant']
elif 'depth_metric' in pred:
pred_depth_affine_invariant = pred['depth_metric']
else:
pred_depth_affine_invariant = None
if pred_depth_affine_invariant is not None:
pred_depth = pred_depth_affine_invariant
pred_depth_lr_masked, gt_depth_lr_masked = pred_depth[lr_index][lr_mask], gt_depth[lr_index][lr_mask]
scale, shift = align_depth_affine(pred_depth_lr_masked, gt_depth_lr_masked, 1 / gt_depth_lr_masked)
pred_depth = pred_depth * scale + shift
metrics['depth_affine_invariant'] = {
'rel': rel_depth(pred_depth[mask], gt_depth[mask]),
'delta1': delta1_depth(pred_depth[mask], gt_depth[mask])
}
if pred_depth_aligned is None:
pred_depth_aligned = pred_depth
# Affine-invariant disparity
if 'disparity_affine_invariant' in pred:
pred_disparity_affine_invariant = pred['disparity_affine_invariant']
elif 'depth_scale_invariant' in pred:
pred_disparity_affine_invariant = 1 / pred['depth_scale_invariant']
elif 'depth_metric' in pred:
pred_disparity_affine_invariant = 1 / pred['depth_metric']
else:
pred_disparity_affine_invariant = None
if pred_disparity_affine_invariant is not None:
pred_disp = pred_disparity_affine_invariant
scale, shift = align_affine_lstsq(pred_disp[mask], 1 / gt_depth[mask])
pred_disp = pred_disp * scale + shift
# NOTE: The alignment is done on the disparity map could introduce extreme outliers at disparities close to 0.
# Therefore we clamp the disparities by minimum ground truth disparity.
pred_depth = 1 / pred_disp.clamp_min(1 / gt_depth[mask].max().item())
metrics['disparity_affine_invariant'] = {
'rel': rel_depth(pred_depth[mask], gt_depth[mask]),
'delta1': delta1_depth(pred_depth[mask], gt_depth[mask])
}
if pred_depth_aligned is None:
pred_depth_aligned = 1 / pred_disp.clamp_min(1e-6)
# Metric points
if 'points_metric' in pred and gt['is_metric']:
pred_points = pred['points_metric']
pred_points_lr_masked, gt_points_lr_masked = pred_points[lr_index][lr_mask], gt_points[lr_index][lr_mask]
shift = align_points_xyz_shift(pred_points_lr_masked, gt_points_lr_masked, 1 / gt_points_lr_masked.norm(dim=-1))
pred_points = pred_points + shift
metrics['points_metric'] = {
'rel': rel_point(pred_points[mask], gt_points[mask]),
'delta1': delta1_point(pred_points[mask], gt_points[mask])
}
if pred_points_aligned is None:
pred_points_aligned = pred['points_metric']
# Scale-invariant points (in camera space)
if 'points_scale_invariant' in pred:
pred_points_scale_invariant = pred['points_scale_invariant']
elif 'points_metric' in pred:
pred_points_scale_invariant = pred['points_metric']
else:
pred_points_scale_invariant = None
if pred_points_scale_invariant is not None:
pred_points = pred_points_scale_invariant
pred_points_lr_masked, gt_points_lr_masked = pred_points_scale_invariant[lr_index][lr_mask], gt_points[lr_index][lr_mask]
scale = align_points_scale(pred_points_lr_masked, gt_points_lr_masked, 1 / gt_points_lr_masked.norm(dim=-1))
pred_points = pred_points * scale
metrics['points_scale_invariant'] = {
'rel': rel_point(pred_points[mask], gt_points[mask]),
'delta1': delta1_point(pred_points[mask], gt_points[mask])
}
if vis and pred_points_aligned is None:
pred_points_aligned = pred['points_scale_invariant'] * scale
# Affine-invariant points
if 'points_affine_invariant' in pred:
pred_points_affine_invariant = pred['points_affine_invariant']
elif 'points_scale_invariant' in pred:
pred_points_affine_invariant = pred['points_scale_invariant']
elif 'points_metric' in pred:
pred_points_affine_invariant = pred['points_metric']
else:
pred_points_affine_invariant = None
if pred_points_affine_invariant is not None:
pred_points = pred_points_affine_invariant
pred_points_lr_masked, gt_points_lr_masked = pred_points[lr_index][lr_mask], gt_points[lr_index][lr_mask]
scale, shift = align_points_scale_xyz_shift(pred_points_lr_masked, gt_points_lr_masked, 1 / gt_points_lr_masked.norm(dim=-1))
pred_points = pred_points * scale + shift
metrics['points_affine_invariant'] = {
'rel': rel_point(pred_points[mask], gt_points[mask]),
'delta1': delta1_point(pred_points[mask], gt_points[mask])
}
if vis and pred_points_aligned is None:
pred_points_aligned = pred['points_affine_invariant'] * scale + shift
# Local points
if 'segmentation_mask' in gt and 'points' in gt and any('points' in k for k in pred.keys()):
pred_points = next(pred[k] for k in pred.keys() if 'points' in k)
gt_points = gt['points']
segmentation_mask = gt['segmentation_mask']
segmentation_labels = gt['segmentation_labels']
segmentation_mask_lr = segmentation_mask[lr_index]
local_points_metrics = []
for _, seg_id in segmentation_labels.items():
valid_mask = (segmentation_mask == seg_id) & mask
pred_points_masked = pred_points[valid_mask]
gt_points_masked = gt_points[valid_mask]
valid_mask_lr = (segmentation_mask_lr == seg_id) & lr_mask
if valid_mask_lr.sum().item() < 10:
continue
pred_points_masked_lr = pred_points[lr_index][valid_mask_lr]
gt_points_masked_lr = gt_points[lr_index][valid_mask_lr]
diameter = (gt_points_masked.max(dim=0).values - gt_points_masked.min(dim=0).values).max()
scale, shift = align_points_scale_xyz_shift(pred_points_masked_lr, gt_points_masked_lr, 1 / diameter.expand(gt_points_masked_lr.shape[0]))
pred_points_masked = pred_points_masked * scale + shift
local_points_metrics.append({
'rel': rel_point_local(pred_points_masked, gt_points_masked, diameter),
'delta1': delta1_point_local(pred_points_masked, gt_points_masked, diameter),
})
metrics['local_points'] = key_average(local_points_metrics)
# FOV. NOTE: If there is no random augmentation applied to the input images, all GT FOV are generallly the same.
# Fair evaluation of FOV requires random augmentation.
if 'intrinsics' in pred and 'intrinsics' in gt:
pred_intrinsics = pred['intrinsics']
gt_intrinsics = gt['intrinsics']
pred_fov_x, pred_fov_y = intrinsics_to_fov(pred_intrinsics)
gt_fov_x, gt_fov_y = intrinsics_to_fov(gt_intrinsics)
metrics['fov_x'] = {
'mae': torch.rad2deg(pred_fov_x - gt_fov_x).abs().mean().item(),
'deviation': torch.rad2deg(pred_fov_x - gt_fov_x).item(),
}
# Boundary F1
if pred_depth_aligned is not None and gt['has_sharp_boundary']:
metrics['boundary'] = {
'radius1_f1': boundary_f1(pred_depth_aligned, gt_depth, mask, radius=1),
'radius2_f1': boundary_f1(pred_depth_aligned, gt_depth, mask, radius=2),
'radius3_f1': boundary_f1(pred_depth_aligned, gt_depth, mask, radius=3),
}
if vis:
if pred_points_aligned is not None:
misc['pred_points'] = pred_points_aligned
if only_depth:
misc['pred_points'] = utils3d.torch.depth_to_points(pred_depth_aligned, intrinsics=gt['intrinsics'])
if pred_depth_aligned is not None:
misc['pred_depth'] = pred_depth_aligned
return metrics, misc |