Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Feat/codeflash-subpixel_step-compute (#3215)

Meggison Oritsemisan40d205f6bad1
diff --git a/kornia/filters/dissolving.py b/kornia/filters/dissolving.py
index 7666ed8e2..9984dc6dd 100644
--- a/kornia/filters/dissolving.py
+++ b/kornia/filters/dissolving.py
@@ -67,9 +67,15 @@ class _DissolvingWraper_HF:
@torch.no_grad()
def decode_tensor_to_latent(self, latents: Tensor) -> Tensor:
- latents = 1 / 0.18215 * latents.detach()
- image = self.model.vae.decode(latents)["sample"]
- image = (image / 2 + 0.5).clamp(0, 1)
+ # Perform in-place detach to reduce memory usage and copies
+ latents = latents.detach()
+ latents = latents * (1.0 / 0.18215) # Fused division as multiplication (faster)
+ # Reduce attribute lookups by localizing frequently used attributes
+ vae_decode = self.model.vae.decode
+ image = vae_decode(latents)["sample"]
+ # Use in-place arithmetic/clamp for throughput
+ image = image.div_(2).add_(0.5)
+ image.clamp_(0, 1)
return image
@torch.no_grad()
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index 3c218610e..ac8fc5c6c 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -33,6 +33,8 @@ from .camera import PinholeCamera, cam2pixel, pixel2cam, project_points, unproje
from .conversions import normalize_pixel_coordinates, normalize_points_with_intrinsics
from .linalg import compose_transformations, convert_points_to_homogeneous, inverse_transformation, transform_points
+"""Module containing operators to work on RGB-Depth images."""
+
__all__ = [
"DepthWarper",
"depth_from_disparity",
@@ -395,12 +397,35 @@ class DepthWarper(Module):
Szeliski, Richard, and Daniel Scharstein. "Symmetric sub-pixel stereo matching." European Conference on Computer
Vision. Springer Berlin Heidelberg, 2002.
"""
+ if self._dst_proj_src is None:
+ raise RuntimeError("Expected Tensor, but got None Type from the projection matrix")
+
delta_d = 0.01
- xy_m1 = self._compute_projection(self.width / 2, self.height / 2, 1.0 - delta_d)
- xy_p1 = self._compute_projection(self.width / 2, self.height / 2, 1.0 + delta_d)
- dx = torch.norm((xy_p1 - xy_m1), 2, dim=-1) / 2.0
- dxdd = dx / (delta_d) # pixel*(1/meter)
- # half pixel sampling, we're interested in the min for all cameras
+ center_x = self.width / 2
+ center_y = self.height / 2
+
+ # Batch both invds in one call (for potential fused kernels in future) for efficiency
+ invds = (1.0 - delta_d, 1.0 + delta_d)
+ # Instead of two calls, process both at once with minimal tensor construction
+ points = (
+ torch.tensor(
+ [[center_x, center_y, invds[0], 1.0], [center_x, center_y, invds[1], 1.0]],
+ dtype=self._dst_proj_src.dtype,
+ device=self._dst_proj_src.device,
+ )
+ .transpose(0, 1)
+ .unsqueeze(0)
+ ) # (1, 4, 2)
+ # Repeat projection matrix for batch
+ proj = self._dst_proj_src
+ flow = torch.matmul(proj, points) # (N, 3/4, 2)
+ zs = 1.0 / flow[:, 2] # (N, 2)
+ xs = flow[:, 0] * zs
+ ys = flow[:, 1] * zs
+ xys = torch.stack((xs, ys), dim=-1) # (N, 2, 2)
+ dxy = torch.norm(xys[:, 1] - xys[:, 0], p=2, dim=1) / 2.0
+ dxdd = dxy / delta_d
+ # half pixel sampling, min for all cameras
return torch.min(0.5 / dxdd)
def warp_grid(self, depth_src: Tensor) -> Tensor: