|
37 | 37 |
|
38 | 38 |
|
39 | 39 | class TestPublicVideoDecoderTransformOps: |
| 40 | + @pytest.mark.parametrize( |
| 41 | + "height_scaling_factor, width_scaling_factor", |
| 42 | + ((1.5, 1.31), (0.5, 0.71), (0.7, 1.31), (1.5, 0.71), (1.0, 1.0), (2.0, 2.0)), |
| 43 | + ) |
| 44 | + @pytest.mark.parametrize("video", [NASA_VIDEO, TEST_SRC_2_720P]) |
| 45 | + def test_resize_torchvision( |
| 46 | + self, video, height_scaling_factor, width_scaling_factor |
| 47 | + ): |
| 48 | + height = int(video.get_height() * height_scaling_factor) |
| 49 | + width = int(video.get_width() * width_scaling_factor) |
| 50 | + |
| 51 | + decoder_resize = VideoDecoder( |
| 52 | + video.path, transforms=[v2.Resize(size=(height, width))] |
| 53 | + ) |
| 54 | + |
| 55 | + decoder_full = VideoDecoder(video.path) |
| 56 | + |
| 57 | + num_frames = len(decoder_resize) |
| 58 | + assert num_frames == len(decoder_full) |
| 59 | + |
| 60 | + for frame_index in [ |
| 61 | + 0, |
| 62 | + int(num_frames * 0.1), |
| 63 | + int(num_frames * 0.2), |
| 64 | + int(num_frames * 0.3), |
| 65 | + int(num_frames * 0.4), |
| 66 | + int(num_frames * 0.5), |
| 67 | + int(num_frames * 0.75), |
| 68 | + int(num_frames * 0.90), |
| 69 | + num_frames - 1, |
| 70 | + ]: |
| 71 | + expected_shape = (video.get_num_color_channels(), height, width) |
| 72 | + frame_resize = decoder_resize[frame_index] |
| 73 | + frame_full = decoder_full[frame_index] |
| 74 | + |
| 75 | + frame_tv = v2.functional.resize(frame_full, size=(height, width)) |
| 76 | + frame_tv_no_antialias = v2.functional.resize( |
| 77 | + frame_full, size=(height, width), antialias=False |
| 78 | + ) |
| 79 | + |
| 80 | + assert frame_resize.shape == expected_shape |
| 81 | + assert frame_tv.shape == expected_shape |
| 82 | + assert frame_tv_no_antialias.shape == expected_shape |
| 83 | + |
| 84 | + assert_tensor_close_on_at_least( |
| 85 | + frame_resize, frame_tv, percentage=99.8, atol=1 |
| 86 | + ) |
| 87 | + torch.testing.assert_close(frame_resize, frame_tv, rtol=0, atol=6) |
| 88 | + |
| 89 | + if height_scaling_factor < 1 or width_scaling_factor < 1: |
| 90 | + # Antialias only relevant when down-scaling! |
| 91 | + with pytest.raises(AssertionError, match="Expected at least"): |
| 92 | + assert_tensor_close_on_at_least( |
| 93 | + frame_resize, frame_tv_no_antialias, percentage=99, atol=1 |
| 94 | + ) |
| 95 | + with pytest.raises(AssertionError, match="Tensor-likes are not close"): |
| 96 | + torch.testing.assert_close( |
| 97 | + frame_resize, frame_tv_no_antialias, rtol=0, atol=6 |
| 98 | + ) |
| 99 | + |
40 | 100 | def test_resize_fails(self): |
41 | 101 | with pytest.raises( |
42 | 102 | ValueError, |
@@ -187,68 +247,6 @@ def test_transform_fails(self): |
187 | 247 | ): |
188 | 248 | add_video_stream(decoder, transform_specs="invalid, 1, 2") |
189 | 249 |
|
190 | | - @pytest.mark.parametrize( |
191 | | - "height_scaling_factor, width_scaling_factor", |
192 | | - ((1.5, 1.31), (0.5, 0.71), (0.7, 1.31), (1.5, 0.71), (1.0, 1.0), (2.0, 2.0)), |
193 | | - ) |
194 | | - @pytest.mark.parametrize("video", [NASA_VIDEO, TEST_SRC_2_720P]) |
195 | | - def test_resize_torchvision( |
196 | | - self, video, height_scaling_factor, width_scaling_factor |
197 | | - ): |
198 | | - num_frames = self.get_num_frames_core_ops(video) |
199 | | - |
200 | | - height = int(video.get_height() * height_scaling_factor) |
201 | | - width = int(video.get_width() * width_scaling_factor) |
202 | | - resize_spec = f"resize, {height}, {width}" |
203 | | - |
204 | | - decoder_resize = create_from_file(str(video.path)) |
205 | | - add_video_stream(decoder_resize, transform_specs=resize_spec) |
206 | | - |
207 | | - decoder_full = create_from_file(str(video.path)) |
208 | | - add_video_stream(decoder_full) |
209 | | - |
210 | | - for frame_index in [ |
211 | | - 0, |
212 | | - int(num_frames * 0.1), |
213 | | - int(num_frames * 0.2), |
214 | | - int(num_frames * 0.3), |
215 | | - int(num_frames * 0.4), |
216 | | - int(num_frames * 0.5), |
217 | | - int(num_frames * 0.75), |
218 | | - int(num_frames * 0.90), |
219 | | - num_frames - 1, |
220 | | - ]: |
221 | | - expected_shape = (video.get_num_color_channels(), height, width) |
222 | | - frame_resize, *_ = get_frame_at_index( |
223 | | - decoder_resize, frame_index=frame_index |
224 | | - ) |
225 | | - |
226 | | - frame_full, *_ = get_frame_at_index(decoder_full, frame_index=frame_index) |
227 | | - frame_tv = v2.functional.resize(frame_full, size=(height, width)) |
228 | | - frame_tv_no_antialias = v2.functional.resize( |
229 | | - frame_full, size=(height, width), antialias=False |
230 | | - ) |
231 | | - |
232 | | - assert frame_resize.shape == expected_shape |
233 | | - assert frame_tv.shape == expected_shape |
234 | | - assert frame_tv_no_antialias.shape == expected_shape |
235 | | - |
236 | | - assert_tensor_close_on_at_least( |
237 | | - frame_resize, frame_tv, percentage=99.8, atol=1 |
238 | | - ) |
239 | | - torch.testing.assert_close(frame_resize, frame_tv, rtol=0, atol=6) |
240 | | - |
241 | | - if height_scaling_factor < 1 or width_scaling_factor < 1: |
242 | | - # Antialias only relevant when down-scaling! |
243 | | - with pytest.raises(AssertionError, match="Expected at least"): |
244 | | - assert_tensor_close_on_at_least( |
245 | | - frame_resize, frame_tv_no_antialias, percentage=99, atol=1 |
246 | | - ) |
247 | | - with pytest.raises(AssertionError, match="Tensor-likes are not close"): |
248 | | - torch.testing.assert_close( |
249 | | - frame_resize, frame_tv_no_antialias, rtol=0, atol=6 |
250 | | - ) |
251 | | - |
252 | 250 | def test_resize_ffmpeg(self): |
253 | 251 | height = 135 |
254 | 252 | width = 240 |
|
0 commit comments