@@ -257,6 +257,51 @@ def test_crop_fails(self, error_message, params):
257257 transforms = [v2 .RandomCrop (** params )],
258258 )
259259
260+ @pytest .mark .parametrize ("seed" , [0 , 314 ])
261+ def test_random_crop_reusable_objects (self , seed ):
262+ torch .manual_seed (seed )
263+ random_crop = torchcodec .transforms .RandomCrop (size = (100 , 100 ))
264+
265+ # Create a spec which causes us to calculate the random crop location.
266+ _ = random_crop ._make_transform_spec ((1000 , 1000 ))
267+ first_top = random_crop ._top
268+ first_left = random_crop ._left
269+
270+ # Create a spec again, which should calculate a different random crop
271+ # location.
272+ _ = random_crop ._make_transform_spec ((1000 , 1000 ))
273+ assert first_top != random_crop ._top
274+ assert first_left != random_crop ._left
275+
276+ @pytest .mark .parametrize (
277+ "resize, random_crop" ,
278+ [
279+ (torchcodec .transforms .Resize , torchcodec .transforms .RandomCrop ),
280+ (v2 .Resize , v2 .RandomCrop ),
281+ ],
282+ )
283+ def test_transform_pipeline (self , resize , random_crop ):
284+ decoder = VideoDecoder (
285+ TEST_SRC_2_720P .path ,
286+ transforms = [
287+ # resized to bigger than original
288+ resize (size = (2160 , 3840 )),
289+ # crop to smaller than the resize, but still bigger than original
290+ random_crop (size = (1080 , 1920 )),
291+ ],
292+ )
293+
294+ num_frames = len (decoder )
295+ for frame_index in [
296+ 0 ,
297+ int (num_frames * 0.25 ),
298+ int (num_frames * 0.5 ),
299+ int (num_frames * 0.75 ),
300+ num_frames - 1 ,
301+ ]:
302+ frame = decoder [frame_index ]
303+ assert frame .shape == (TEST_SRC_2_720P .get_num_color_channels (), 1080 , 1920 )
304+
260305 def test_transform_fails (self ):
261306 with pytest .raises (
262307 ValueError ,
@@ -519,14 +564,14 @@ def test_crop_transform_fails(self):
519564
520565 with pytest .raises (
521566 RuntimeError ,
522- match = "x position out of bounds" ,
567+ match = "x start position, 9999, out of bounds" ,
523568 ):
524569 decoder = create_from_file (str (NASA_VIDEO .path ))
525570 add_video_stream (decoder , transform_specs = "crop, 100, 100, 9999, 100" )
526571
527572 with pytest .raises (
528573 RuntimeError ,
529- match = "y position out of bounds " ,
574+ match = r"Crop output height \(999\) is greater than input height \(270\) " ,
530575 ):
531576 decoder = create_from_file (str (NASA_VIDEO .path ))
532577 add_video_stream (decoder , transform_specs = "crop, 999, 100, 100, 100" )
0 commit comments