Link to this sectionReference for ultralytics/models/yolo/depth/val.py#
This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/depth/val.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏
Link to this section ultralytics.models.yolo.depth.val.DepthValidator#
def __init__(
self,
dataloader=None,
save_dir: str | Path | None = None,
args=None,
_callbacks: dict | None = None,
) -> NoneBases: DetectionValidator
Validator for YOLO depth estimation models.
Computes standard depth metrics: delta1, abs_rel, rmse, silog. Uses validation loss as the primary training signal.
Args
| Name | Type | Description | Default |
|---|---|---|---|
dataloader | None | ||
save_dir | `str | Path | None` |
args | None | ||
_callbacks | `dict | None` |
Methods
| Name | Description |
|---|---|
finalize_metrics | Set final values for metrics speed. |
gather_stats | Sum depth metric accumulators across DDP ranks onto rank 0. |
get_desc | Return description for progress bar. |
get_stats | Finalize and return the metrics dict. |
init_metrics | Initialize the DepthMetrics accumulator with the dataset's depth range. |
plot_predictions | Save predicted depth overlays to val_batch{ni}_pred.jpg. |
postprocess | No NMS needed for depth — return predictions as-is. |
preprocess | Preprocess batch — move to device, normalize images, and keep depth as float32. |
print_results | Log the headline depth metrics in the detection-style aligned table format. |
update_metrics | Accumulate depth metrics for a batch. |
Source code in ultralytics/models/yolo/depth/val.py
class DepthValidator(DetectionValidator):
"""Validator for YOLO depth estimation models.
Computes standard depth metrics: delta1, abs_rel, rmse, silog. Uses validation loss as the primary training signal.
"""
def __init__(
self,
dataloader=None,
save_dir: str | Path | None = None,
args=None,
_callbacks: dict | None = None,
) -> None:
"""Initialize DepthValidator."""
super().__init__(dataloader, save_dir, args, _callbacks)
self.args.task = "depth"Link to this section ultralytics.models.yolo.depth.val.DepthValidator.finalize_metrics#
def finalize_metrics(self) -> NoneSet final values for metrics speed.
Source code in ultralytics/models/yolo/depth/val.py
def finalize_metrics(self) -> None:
"""Set final values for metrics speed."""
self.metrics.speed = self.speed
self.metrics.save_dir = self.save_dirLink to this section ultralytics.models.yolo.depth.val.DepthValidator.gather_stats#
def gather_stats(self) -> NoneSum depth metric accumulators across DDP ranks onto rank 0.
Validation is sharded (ContiguousDistributedSampler gives each rank a distinct chunk of the val set), so each rank holds only its shard's summed statistics. All-reduce the sums so rank 0's get_stats() computes metrics over the full val set instead of a single shard. Overrides DetectionValidator.gather_stats(), which reduces detection-specific stats/box attributes that DepthMetrics does not have.
Source code in ultralytics/models/yolo/depth/val.py
def gather_stats(self) -> None:
"""Sum depth metric accumulators across DDP ranks onto rank 0.
Validation is sharded (ContiguousDistributedSampler gives each rank a distinct chunk of the
val set), so each rank holds only its shard's summed statistics. All-reduce the sums so
rank 0's get_stats() computes metrics over the full val set instead of a single shard.
Overrides DetectionValidator.gather_stats(), which reduces detection-specific stats/box
attributes that DepthMetrics does not have.
"""
if RANK == -1 or not dist.is_initialized():
return
totals = self.metrics._totals
totals = (
totals.to(self.device) if totals is not None else torch.zeros(7, dtype=torch.float64, device=self.device)
)
count = torch.tensor([self.metrics._count], dtype=torch.float64, device=self.device)
dist.all_reduce(totals, op=dist.ReduceOp.SUM)
dist.all_reduce(count, op=dist.ReduceOp.SUM)
self.metrics._totals = totals
self.metrics._count = float(count.item())Link to this section ultralytics.models.yolo.depth.val.DepthValidator.get_desc#
def get_desc(self) -> strReturn description for progress bar.
Source code in ultralytics/models/yolo/depth/val.py
def get_desc(self) -> str:
"""Return description for progress bar."""
return ("%22s" + "%11s" * 5) % ("Class", "Images", "delta1", "abs_rel", "rmse", "silog")Link to this section ultralytics.models.yolo.depth.val.DepthValidator.get_stats#
def get_stats(self) -> dict[str, float]Finalize and return the metrics dict.
Cross-rank metric reduction is handled by gather_stats() (called before this on all ranks); this runs on rank 0 with the already-summed accumulators.
Source code in ultralytics/models/yolo/depth/val.py
def get_stats(self) -> dict[str, float]:
"""Finalize and return the metrics dict.
Cross-rank metric reduction is handled by gather_stats() (called before this on all ranks);
this runs on rank 0 with the already-summed accumulators.
"""
self.metrics.process()
return self.metrics.results_dictLink to this section ultralytics.models.yolo.depth.val.DepthValidator.init_metrics#
def init_metrics(self, model: torch.nn.Module) -> NoneInitialize the DepthMetrics accumulator with the dataset's depth range.
Args
| Name | Type | Description | Default |
|---|---|---|---|
model | torch.nn.Module | required |
Source code in ultralytics/models/yolo/depth/val.py
def init_metrics(self, model: torch.nn.Module) -> None:
"""Initialize the DepthMetrics accumulator with the dataset's depth range."""
self.metrics = DepthMetrics(max_depth=self.data.get("max_depth") or 100.0)
self.metrics.clear_stats()Link to this section ultralytics.models.yolo.depth.val.DepthValidator.plot_predictions#
def plot_predictions(self, batch: dict[str, Any], preds: torch.Tensor, ni: int) -> NoneSave predicted depth overlays to val_batch{ni}_pred.jpg.
Depth has no boxes/classes, so the detection-style plotter is replaced with a depth heatmap overlay through the shared plot_images path, matching the semantic-segmentation visualization style.
Args
| Name | Type | Description | Default |
|---|---|---|---|
batch | dict[str, Any] | required | |
preds | torch.Tensor | required | |
ni | int | required |
Source code in ultralytics/models/yolo/depth/val.py
def plot_predictions(self, batch: dict[str, Any], preds: torch.Tensor, ni: int) -> None:
"""Save predicted depth overlays to val_batch{ni}_pred.jpg.
Depth has no boxes/classes, so the detection-style plotter is replaced with a depth heatmap overlay
through the shared ``plot_images`` path, matching the semantic-segmentation visualization style.
"""
plot_images(
labels={"depth": preds},
images=batch["img"],
paths=batch["im_file"],
fname=self.save_dir / f"val_batch{ni}_pred.jpg",
names=self.names,
on_plot=self.on_plot,
)Link to this section ultralytics.models.yolo.depth.val.DepthValidator.postprocess#
def postprocess(self, preds: torch.Tensor) -> torch.TensorNo NMS needed for depth — return predictions as-is.
Args
| Name | Type | Description | Default |
|---|---|---|---|
preds | torch.Tensor | required |
Source code in ultralytics/models/yolo/depth/val.py
def postprocess(self, preds: torch.Tensor) -> torch.Tensor:
"""No NMS needed for depth — return predictions as-is."""
return predsLink to this section ultralytics.models.yolo.depth.val.DepthValidator.preprocess#
def preprocess(self, batch: dict[str, Any]) -> dict[str, Any]Preprocess batch — move to device, normalize images, and keep depth as float32.
Args
| Name | Type | Description | Default |
|---|---|---|---|
batch | dict[str, Any] | required |
Source code in ultralytics/models/yolo/depth/val.py
def preprocess(self, batch: dict[str, Any]) -> dict[str, Any]:
"""Preprocess batch — move to device, normalize images, and keep depth as float32."""
batch = super().preprocess(batch)
batch["depth"] = batch["depth"].float()
return batchLink to this section ultralytics.models.yolo.depth.val.DepthValidator.print_results#
def print_results(self) -> NoneLog the headline depth metrics in the detection-style aligned table format.
Columns line up with get_desc(): Class, Images, delta1, abs_rel, rmse, silog. Uses "depth_val" as the row label (depth has no classes, where detection prints "all").
Source code in ultralytics/models/yolo/depth/val.py
def print_results(self) -> None:
"""Log the headline depth metrics in the detection-style aligned table format.
Columns line up with get_desc(): Class, Images, delta1, abs_rel, rmse, silog.
Uses "depth_val" as the row label (depth has no classes, where detection prints "all").
"""
r = self.metrics.results_dict
n_images = len(self.dataloader.dataset) if self.dataloader is not None else (self.seen or 0)
pf = "%22s" + "%11i" + "%11.4g" * 4 # label, Images, delta1, abs_rel, rmse, silog
LOGGER.info(
pf
% (
"depth_val",
n_images,
r.get("metrics/delta1", 0.0),
r.get("metrics/abs_rel", 0.0),
r.get("metrics/rmse", 0.0),
r.get("metrics/silog", 0.0),
)
)Link to this section ultralytics.models.yolo.depth.val.DepthValidator.update_metrics#
def update_metrics(self, preds: torch.Tensor, batch: dict[str, Any]) -> NoneAccumulate depth metrics for a batch.
Args
| Name | Type | Description | Default |
|---|---|---|---|
preds | torch.Tensor | required | |
batch | dict[str, Any] | required |
Source code in ultralytics/models/yolo/depth/val.py
def update_metrics(self, preds: torch.Tensor, batch: dict[str, Any]) -> None:
"""Accumulate depth metrics for a batch."""
gt_depth = batch["depth"]
if gt_depth.ndim == 3:
gt_depth = gt_depth.unsqueeze(1)
if preds.ndim == 3:
preds = preds.unsqueeze(1)
if preds.shape[-2:] != gt_depth.shape[-2:]:
preds = F.interpolate(preds.float(), size=gt_depth.shape[-2:], mode="bilinear", align_corners=True)
self.metrics.update_stats(preds, gt_depth)