π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added kornia resize in object detection (#2526)
Jeffin Francis235b8eb4b7a0
diff --git a/kornia/contrib/object_detection.py b/kornia/contrib/object_detection.pyindex ac725ef53..c9a4ff279 100644--- a/kornia/contrib/object_detection.py+++ b/kornia/contrib/object_detection.py@@ -3,32 +3,32 @@ from __future__ import annotationsfrom typing import Anyimport torch-import torch.nn.functional as Ffrom kornia.core import Module, Tensor, concatenate+from kornia.geometry.transform import Resizeclass ResizePreProcessor(Module):"""This module resizes a list of image tensors to the given size, and also returns the original image sizes forfurther post-processing."""- def __init__(self, size: int | tuple[int, int], interpolation_mode: str = "bilinear") -> None:+ def __init__(self, size: tuple[int, int], interpolation_mode: str = "bilinear") -> None:"""Args:size: images will be resized to this value. If a 2-integer tuple is given, it is interpreted as- (height, width). If an integer is given, images will be resized to a square.+ (height, width).interpolation_mode: interpolation mode for image resizing. Supported values: ``nearest``, ``bilinear``,``bicubic``, ``area``, and ``nearest-exact``."""super().__init__()self.size = size- self.interpolation_mode = interpolation_mode+ self.resizer = Resize(self.size, interpolation=interpolation_mode)def forward(self, imgs: list[Tensor]) -> tuple[Tensor, dict[str, Any]]:# TODO: support other input formats e.g. file path, numpy# NOTE: antialias=False is used in F.interpolate()original_sizes = [(img.shape[1], img.shape[2]) for img in imgs]- resized_imgs = [F.interpolate(img.unsqueeze(0), self.size, mode=self.interpolation_mode) for img in imgs]+ resized_imgs = [self.resizer(img.unsqueeze(0)) for img in imgs]return concatenate(resized_imgs), {"original_size": original_sizes}diff --git a/test/contrib/test_contrib.py b/test/contrib/test_contrib.pyindex 467130e5c..018bc5cb4 100644--- a/test/contrib/test_contrib.py+++ b/test/contrib/test_contrib.py@@ -743,14 +743,17 @@ class TestObjectDetector:confidence = 0.3config = RTDETRConfig("resnet50d", 10, head_num_queries=10)model = RTDETR.from_config(config).to(device, dtype).eval()- pre_processor = kornia.contrib.object_detection.ResizePreProcessor(32)+ pre_processor = kornia.contrib.object_detection.ResizePreProcessor((32, 32))post_processor = DETRPostProcessor(confidence).to(device, dtype).eval()detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)sizes = torch.randint(5, 10, (batch_size, 2)) * 32imgs = [torch.randn(3, h, w, device=device, dtype=dtype) for h, w in sizes]+ pre_processor_out = pre_processor(imgs)detections = detector.predict(imgs)+ assert pre_processor_out[0].shape[-1] == 32+ assert pre_processor_out[0].shape[-2] == 32assert len(detections) == batch_sizefor dets in detections:assert dets.shape[1] == 6