π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
optimized denormalize (#3317)
Sidda2f06a977223
diff --git a/kornia/enhance/normalize.py b/kornia/enhance/normalize.pyindex 6ee6500df..0b7f49f16 100644--- a/kornia/enhance/normalize.py+++ b/kornia/enhance/normalize.py@@ -258,12 +258,20 @@ def denormalize(data: Tensor, mean: Union[Tensor, float], std: Union[Tensor, flomean = torch.as_tensor(mean, device=data.device, dtype=data.dtype)std = torch.as_tensor(std, device=data.device, dtype=data.dtype)- mean = mean[..., None]- std = std[..., None]+ if mean.dim() == 1:+ mean = mean.view(1, -1, *([1] * (data.dim() - 2)))+ # If the tensor is >1D (e.g., (B, C)), reshape to (B, C, 1, ...)+ else:+ while len(mean.shape) < data.dim():+ mean = mean.unsqueeze(-1)- out: Tensor = (data.view(shape[0], shape[1], -1) * std) + mean+ if std.dim() == 1:+ std = std.view(1, -1, *([1] * (data.dim() - 2)))+ else:+ while len(std.shape) < data.dim():+ std = std.unsqueeze(-1)- return out.view(shape)+ return torch.addcmul(mean, data, std)def normalize_min_max(x: Tensor, min_val: float = 0.0, max_val: float = 1.0, eps: float = 1e-6) -> Tensor: