π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
optimized image_list_to_tensor by 2.92x (292% speedup) (#3245)
Sidd21faa14b5cf2
diff --git a/kornia/utils/image.py b/kornia/utils/image.pyindex c0f4318e6..4b5f054f7 100644--- a/kornia/utils/image.py+++ b/kornia/utils/image.py@@ -90,15 +90,22 @@ def image_list_to_tensor(images: List[Any]) -> Tensor:"""if not images:- raise ValueError("Input list of numpy images is empty")- if len(images[0].shape) != 3:- raise ValueError("Input images must be three dimensional arrays")-- list_of_tensors: List[Tensor] = []- for image in images:- list_of_tensors.append(image_to_tensor(image))- tensor: Tensor = torch.stack(list_of_tensors)- return tensor+ raise ValueError("Input list of images is empty")++ images_t = []+ for img in images:+ if not torch.is_tensor(img):+ img = torch.as_tensor(img)+ images_t.append(img)++ shape = images_t[0].shape+ if len(shape) != 3:+ raise ValueError("Each image must have shape (H, W, C)")+ if any(img.shape != shape for img in images_t):+ raise ValueError("All images must have the same shape")++ # Stack into (N, H, W, C) then permute to (N, C, H, W)+ return torch.stack(images_t, dim=0).permute(0, 3, 1, 2)def _to_bchw(tensor: Tensor) -> Tensor: