Repository: kornia/kornia

Issue:
Use Kornia resize in ResizePreprocessor in ObjectDetector
              Use Kornia resize in ResizePreprocessor

_Originally posted by @edgarriba in https://github.com/kornia/kornia/pull/2363#discussion_r1257304346_
            


Discussion:
@edgarriba Did you mean to replace `[F.interpolate(img.unsqueeze(0), self.size, mode=self.interpolation_mode) for img in imgs]` with `kornia.augmentation.Resize` ? Can you please guide me on this issue i would like to work on this if this is still open 
@jeffin07 yes, that's the idea. For contributing please check https://github.com/kornia/kornia/blob/master/CONTRIBUTING.md

/cc @gau-nernst 

Resolving patch:
diff --git a/kornia/contrib/object_detection.py b/kornia/contrib/object_detection.py
--- a/kornia/contrib/object_detection.py
+++ b/kornia/contrib/object_detection.py
@@ -3,32 +3,32 @@
 from typing import Any
 
 import torch
-import torch.nn.functional as F
 
 from kornia.core import Module, Tensor, concatenate
+from kornia.geometry.transform import Resize
 
 
 class ResizePreProcessor(Module):
     """This module resizes a list of image tensors to the given size, and also returns the original image sizes for
     further 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}
 
 


Test patch:
diff --git a/test/contrib/test_contrib.py b/test/contrib/test_contrib.py
--- a/test/contrib/test_contrib.py
+++ b/test/contrib/test_contrib.py
@@ -743,14 +743,17 @@ def test_smoke(self, device, dtype):
         confidence = 0.3
         config = 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)) * 32
         imgs = [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] == 32
         assert len(detections) == batch_size
         for dets in detections:
             assert dets.shape[1] == 6