π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
perf(augmentation): drop wasted ones_like fill in RandomGrayscale (#3822)
Edgar Ribae624b63ae5a0
diff --git a/kornia/augmentation/_2d/intensity/grayscale.py b/kornia/augmentation/_2d/intensity/grayscale.pyindex 8ce2b30c7..ac5cf9206 100644--- a/kornia/augmentation/_2d/intensity/grayscale.py+++ b/kornia/augmentation/_2d/intensity/grayscale.py@@ -17,7 +17,6 @@from typing import Any, Dict, Optional-import torchfrom torch import Tensorfrom kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D@@ -78,7 +77,8 @@ class RandomGrayscale(IntensityAugmentationBase2D):def apply_transform(self, input: Tensor, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None) -> Tensor:- # Make sure it returns (*, 3, H, W)- grayscale = torch.ones_like(input)- grayscale[:] = rgb_to_grayscale(input, rgb_weights=self.rgb_weights)- return grayscale+ # Return (*, 3, H, W) by repeating the single grayscale channel β a contiguous copy,+ # like the old code, but without its wasted `ones_like` allocation+fill (which was+ # immediately overwritten).+ gray = rgb_to_grayscale(input, rgb_weights=self.rgb_weights)+ return gray.repeat(*([1] * (input.ndim - 3)), 3, 1, 1)