Enterprise-ready security: ISO 27001 + SOC 2 Type I compliant.

Link to this sectionReference for ultralytics/models/yolo/depth/val.py#

Improvements

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! 🙏


Summary

Link to this sectionClass ultralytics.models.yolo.depth.val.DepthValidator#

def __init__(
    self,
    dataloader=None,
    save_dir: str | Path | None = None,
    args=None,
    _callbacks: dict | None = None,
) -> None

Bases: 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

NameTypeDescriptionDefault
dataloaderNone
save_dir`strPathNone`
argsNone
_callbacks`dictNone`

Methods

NameDescription
finalize_metricsSet final values for metrics speed.
gather_statsSum depth metric accumulators across DDP ranks onto rank 0.
get_descReturn description for progress bar.
get_statsFinalize and return the metrics dict.
init_metricsInitialize the DepthMetrics accumulator with the dataset's depth range.
plot_predictionsSave predicted depth overlays to val_batch{ni}_pred.jpg.
postprocessNo NMS needed for depth — return predictions as-is.
preprocessPreprocess batch — move to device, normalize images, and keep depth as float32.
print_resultsLog the headline depth metrics in the detection-style aligned table format.
update_metricsAccumulate depth metrics for a batch.
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.finalize_metrics#

def finalize_metrics(self) -> None

Set final values for metrics speed.

Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

def finalize_metrics(self) -> None:
    """Set final values for metrics speed."""
    self.metrics.speed = self.speed
    self.metrics.save_dir = self.save_dir

Link to this sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.gather_stats#

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.

Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.get_desc#

def get_desc(self) -> str

Return description for progress bar.

Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

def get_desc(self) -> str:
    """Return description for progress bar."""
    return ("%22s" + "%11s" * 5) % ("Class", "Images", "delta1", "abs_rel", "rmse", "silog")

Link to this sectionMethod 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

View on GitHub

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_dict

Link to this sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.init_metrics#

def init_metrics(self, model: torch.nn.Module) -> None

Initialize the DepthMetrics accumulator with the dataset's depth range.

Args

NameTypeDescriptionDefault
modeltorch.nn.Modulerequired
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.plot_predictions#

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.

Args

NameTypeDescriptionDefault
batchdict[str, Any]required
predstorch.Tensorrequired
niintrequired
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.postprocess#

def postprocess(self, preds: torch.Tensor) -> torch.Tensor

No NMS needed for depth — return predictions as-is.

Args

NameTypeDescriptionDefault
predstorch.Tensorrequired
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

def postprocess(self, preds: torch.Tensor) -> torch.Tensor:
    """No NMS needed for depth — return predictions as-is."""
    return preds

Link to this sectionMethod 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

NameTypeDescriptionDefault
batchdict[str, Any]required
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 batch

Link to this sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.print_results#

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").

Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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 sectionMethod ultralytics.models.yolo.depth.val.DepthValidator.update_metrics#

def update_metrics(self, preds: torch.Tensor, batch: dict[str, Any]) -> None

Accumulate depth metrics for a batch.

Args

NameTypeDescriptionDefault
predstorch.Tensorrequired
batchdict[str, Any]required
Source code in ultralytics/models/yolo/depth/val.py

View on GitHub

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)



Contributors