Repository: pytorch/vision
Pull request: proper comparison of input and outpus size before resizing
fix https://github.com/pytorch/vision/issues/7518

cc @vfdev-5

Problem statement:
Resize transform applied even if input size equal to desired output size
### 🐛 Describe the bug

The functional transform resize, will be applied even if the input image is at the same size like the desired output.


```python
import torch
from torchvision.transforms import functional as F

x = torch.Tensor(1,100,200)
x_resized = F.resize(x, (100,200))
print(id(x), id(x_resized))
```

Yields a different id for the output image

### Versions

PyTorch version: 2.0.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.3.1 (arm64)
GCC version: Could not collect
Clang version: 14.0.3 (clang-1403.0.22.14.1)
CMake version: version 3.24.2
Libc version: N/A

Python version: 3.9.12 (main, Apr  5 2022, 01:52:34)  [Clang 12.0.0 ] (64-bit runtime)
Python platform: macOS-13.3.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M1 Max

Versions of relevant libraries:
[pip3] numpy==1.24.2
[pip3] torch==2.0.0
[pip3] torchvision==0.16.0a0+b78d98b
[conda] No relevant packages


cc @vfdev-5

Hints from issue discussion:

Commit message: proper comparison of input and outpus size before resizing

Patch to review:
diff --git a/test/test_transforms.py b/test/test_transforms.py
index c96fbb28496..269b6089694 100644
--- a/test/test_transforms.py
+++ b/test/test_transforms.py
@@ -462,6 +462,16 @@ def test_resize_size_equals_small_edge_size(height, width):
     result = t(img)
     assert max(result.size) == max_size
 
+@pytest.mark.parametrize("height, width", [(28, 28)])
+def test_resize_equal_input_output_sizes(height, width):
+    # Regression test for https://github.com/pytorch/vision/issues/7518
+    img = Image.new("RGB", size=(width, height))
+
+    t = transforms.Resize(((height, width)), antialias=True)
+    result = t(img)
+    print(id(result),id(img))
+    assert id(result) == id(img)
+
 
 class TestPad:
     @pytest.mark.parametrize("fill", [85, 85.0])
diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py
index 09c8bdbcfeb..e4e9c9733f3 100644
--- a/torchvision/transforms/functional.py
+++ b/torchvision/transforms/functional.py
@@ -478,7 +478,7 @@ def resize(
         size = [size]
     output_size = _compute_resized_output_size((image_height, image_width), size, max_size)
 
-    if (image_height, image_width) == output_size:
+    if [image_height, image_width] == output_size:
         return img
 
     antialias = _check_antialias(img, antialias, interpolation)


Code review comments:
diff_hunk: @@ -462,6 +462,16 @@ def test_resize_size_equals_small_edge_size(height, width):
     result = t(img)
     assert max(result.size) == max_size
 
+@pytest.mark.parametrize("height, width", [(28, 28)])
+def test_resize_equal_input_output_sizes(height, width):
+    # Regression test for https://github.com/pytorch/vision/issues/7518
+    img = Image.new("RGB", size=(width, height))
+
+    t = transforms.Resize(((height, width)), antialias=True)
+    result = t(img)
+    print(id(result),id(img))
+    assert id(result) == id(img)
path: test/test_transforms.py
text: @user1:
`id(obj1) == id(obj2)` is the same as `obj1 is obj2`:

```suggestion
    assert result is img
```

diff_hunk: @@ -462,6 +462,16 @@ def test_resize_size_equals_small_edge_size(height, width):
     result = t(img)
     assert max(result.size) == max_size
 
+@pytest.mark.parametrize("height, width", [(28, 28)])
+def test_resize_equal_input_output_sizes(height, width):
+    # Regression test for https://github.com/pytorch/vision/issues/7518
+    img = Image.new("RGB", size=(width, height))
+
+    t = transforms.Resize(((height, width)), antialias=True)
+    result = t(img)
+    print(id(result),id(img))
path: test/test_transforms.py
text: @user1:
```suggestion
```