@@ -163,11 +163,15 @@ def test_random_crop_torchvision(
163163
164164 # We want both kinds of RandomCrop objects to get arrive at the same
165165 # locations to crop, so we need to make sure they get the same random
166- # seed.
166+ # seed. It's used in RandomCrop's _make_transform_spec() method, called
167+ # by the VideoDecoder.
167168 torch .manual_seed (seed )
168169 tc_random_crop = torchcodec .transforms .RandomCrop (size = (height , width ))
169170 decoder_random_crop = VideoDecoder (video .path , transforms = [tc_random_crop ])
170171
172+ # Resetting manual seed for when TorchCodec's RandomCrop, created from
173+ # the TorchVision RandomCrop, is used inside of the VideoDecoder. It
174+ # needs to match the call above.
171175 torch .manual_seed (seed )
172176 decoder_random_crop_tv = VideoDecoder (
173177 video .path ,
@@ -193,14 +197,11 @@ def test_random_crop_torchvision(
193197 expected_shape = (video .get_num_color_channels (), height , width )
194198 assert frame_random_crop_tv .shape == expected_shape
195199
200+ # Resetting manual seed to make sure the invocation of the
201+ # TorchVision RandomCrop matches the two calls above.
202+ torch .manual_seed (seed )
196203 frame_full = decoder_full [frame_index ]
197- frame_tv = v2 .functional .crop (
198- frame_full ,
199- top = tc_random_crop ._top ,
200- left = tc_random_crop ._left ,
201- height = tc_random_crop .size [0 ],
202- width = tc_random_crop .size [1 ],
203- )
204+ frame_tv = v2 .RandomCrop (size = (height , width ))(frame_full )
204205 assert_frames_equal (frame_random_crop , frame_tv )
205206
206207 @pytest .mark .parametrize (
@@ -260,18 +261,23 @@ def test_crop_fails(self, error_message, params):
260261 @pytest .mark .parametrize ("seed" , [0 , 314 ])
261262 def test_random_crop_reusable_objects (self , seed ):
262263 torch .manual_seed (seed )
263- random_crop = torchcodec .transforms .RandomCrop (size = (100 , 100 ))
264+ random_crop = torchcodec .transforms .RandomCrop (size = (99 , 99 ))
264265
265266 # 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
267+ first_spec = random_crop ._make_transform_spec ((888 , 888 ))
269268
270269 # 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
270+ # location. Despite having the same image size, the specs should be
271+ # different because the crop should be at a different location
272+ second_spec = random_crop ._make_transform_spec ((888 , 888 ))
273+ assert first_spec != second_spec
274+
275+ # Create a spec again, but with a different image size. The specs should
276+ # obviously be different, but the original image size should not be in
277+ # the spec at all.
278+ third_spec = random_crop ._make_transform_spec ((777 , 777 ))
279+ assert third_spec != first_spec
280+ assert "888" not in third_spec
275281
276282 @pytest .mark .parametrize (
277283 "resize, random_crop" ,
0 commit comments