@@ -27,60 +27,77 @@ def test_resize():
2727
2828 assert torch .all (out == 1 )
2929 assert out .shape [- 2 :] == output_size
30- assert repr (transfo ) == f "Resize(output_size={ output_size } , interpolation='bilinear')"
30+ assert repr (transfo ) == "Resize(output_size=(32, 32) , interpolation='bilinear')"
3131
32- transfo = Resize (output_size , preserve_aspect_ratio = True )
32+ # Test with preserve_aspect_ratio
33+ output_size = (32 , 32 )
3334 input_t = torch .ones ((3 , 32 , 64 ), dtype = torch .float32 )
34- out = transfo (input_t )
3535
36+ # Asymmetric padding
37+ transfo = Resize (output_size , preserve_aspect_ratio = True )
38+ out = transfo (input_t )
3639 assert out .shape [- 2 :] == output_size
3740 assert not torch .all (out == 1 )
38- # Asymetric padding
3941 assert torch .all (out [:, - 1 ] == 0 ) and torch .all (out [:, 0 ] == 1 )
4042
41- # Symetric padding
42- transfo = Resize (output_size , preserve_aspect_ratio = True , symmetric_pad = True )
43- assert repr (transfo ) == (
44- f"Resize(output_size={ output_size } , interpolation='bilinear', preserve_aspect_ratio=True, symmetric_pad=True)"
45- )
43+ # Symmetric padding
44+ transfo = Resize (32 , preserve_aspect_ratio = True , symmetric_pad = True )
4645 out = transfo (input_t )
4746 assert out .shape [- 2 :] == output_size
48- # symetric padding
49- assert torch .all (out [:, - 1 ] == 0 ) and torch .all (out [:, 0 ] == 0 )
47+ assert torch .all (out [:, 0 ] == 0 ) and torch .all (out [:, - 1 ] == 0 )
5048
51- # Inverse aspect ratio
49+ expected = "Resize(output_size=(32, 32), interpolation='bilinear', preserve_aspect_ratio=True, symmetric_pad=True)"
50+ assert repr (transfo ) == expected
51+
52+ # Test with inverse resize
5253 input_t = torch .ones ((3 , 64 , 32 ), dtype = torch .float32 )
54+ transfo = Resize (32 , preserve_aspect_ratio = True , symmetric_pad = True )
5355 out = transfo (input_t )
56+ assert out .shape [- 2 :] == (32 , 32 )
5457
55- assert not torch .all (out == 1 )
56- assert out .shape [- 2 :] == output_size
57-
58- # Same aspect ratio
59- output_size = (32 , 128 )
60- transfo = Resize (output_size , preserve_aspect_ratio = True )
58+ # Test resize with same ratio
59+ transfo = Resize ((32 , 128 ), preserve_aspect_ratio = True )
6160 out = transfo (torch .ones ((3 , 16 , 64 ), dtype = torch .float32 ))
62- assert out .shape [- 2 :] == output_size
61+ assert out .shape [- 2 :] == ( 32 , 128 )
6362
64- # FP16
63+ # Test with fp16 input
64+ transfo = Resize ((32 , 128 ), preserve_aspect_ratio = True )
6565 input_t = torch .ones ((3 , 64 , 64 ), dtype = torch .float16 )
6666 out = transfo (input_t )
6767 assert out .dtype == torch .float16
6868
69- # --- Test with target (bounding boxes) ---
70-
71- target_boxes = np .array ([[0.1 , 0.1 , 0.9 , 0.9 ], [0.2 , 0.2 , 0.8 , 0.8 ]])
72- output_size = (64 , 64 )
73-
74- transfo = Resize (output_size , preserve_aspect_ratio = True )
69+ padding = [True , False ]
70+ for symmetric_pad in padding :
71+ # Test with target boxes
72+ target_boxes = np .array ([[0.1 , 0.1 , 0.3 , 0.4 ], [0.2 , 0.2 , 0.8 , 0.8 ]])
73+ transfo = Resize ((64 , 64 ), preserve_aspect_ratio = True , symmetric_pad = symmetric_pad )
74+ input_t = torch .ones ((3 , 32 , 64 ), dtype = torch .float32 )
75+ out , new_target = transfo (input_t , target_boxes )
76+
77+ assert out .shape [- 2 :] == (64 , 64 )
78+ assert new_target .shape == target_boxes .shape
79+ assert np .all ((0 <= new_target ) & (new_target <= 1 ))
80+
81+ # Test with target polygons
82+ target_boxes = np .array ([
83+ [[0.1 , 0.1 ], [0.9 , 0.1 ], [0.9 , 0.9 ], [0.1 , 0.9 ]],
84+ [[0.2 , 0.2 ], [0.8 , 0.2 ], [0.8 , 0.8 ], [0.2 , 0.8 ]],
85+ ])
86+ transfo = Resize ((64 , 64 ), preserve_aspect_ratio = True , symmetric_pad = symmetric_pad )
87+ input_t = torch .ones ((3 , 32 , 64 ), dtype = torch .float32 )
88+ out , new_target = transfo (input_t , target_boxes )
89+
90+ assert out .shape [- 2 :] == (64 , 64 )
91+ assert new_target .shape == target_boxes .shape
92+ assert np .all ((0 <= new_target ) & (new_target <= 1 ))
93+
94+ # Test with invalid target shape
7595 input_t = torch .ones ((3 , 32 , 64 ), dtype = torch .float32 )
76- out , new_target = transfo ( input_t , target_boxes )
96+ target = np . ones (( 2 , 5 )) # Invalid shape
7797
78- assert out .shape [- 2 :] == output_size
79- assert new_target .shape == target_boxes .shape
80- assert np .all (new_target >= 0 ) and np .all (new_target <= 1 )
81-
82- out = transfo (input_t )
83- assert out .shape [- 2 :] == output_size
98+ transfo = Resize ((64 , 64 ), preserve_aspect_ratio = True )
99+ with pytest .raises (AssertionError ):
100+ transfo (input_t , target )
84101
85102
86103@pytest .mark .parametrize (
0 commit comments