@@ -3945,3 +3945,122 @@ def test_transform_correctness(self, brightness, contrast, saturation, hue):
39453945
39463946 mae = (actual .float () - expected .float ()).abs ().mean ()
39473947 assert mae < 2
3948+
3949+
3950+ class TestRgbToGrayscale :
3951+ @pytest .mark .parametrize ("dtype" , [torch .uint8 , torch .float32 ])
3952+ @pytest .mark .parametrize ("device" , cpu_and_cuda ())
3953+ def test_kernel_image (self , dtype , device ):
3954+ check_kernel (F .rgb_to_grayscale_image , make_image (dtype = dtype , device = device ))
3955+
3956+ @pytest .mark .parametrize ("make_input" , [make_image_tensor , make_image_pil , make_image ])
3957+ def test_functional (self , make_input ):
3958+ check_functional (F .rgb_to_grayscale , make_input ())
3959+
3960+ @pytest .mark .parametrize (
3961+ ("kernel" , "input_type" ),
3962+ [
3963+ (F .rgb_to_grayscale_image , torch .Tensor ),
3964+ (F ._rgb_to_grayscale_image_pil , PIL .Image .Image ),
3965+ (F .rgb_to_grayscale_image , tv_tensors .Image ),
3966+ ],
3967+ )
3968+ def test_functional_signature (self , kernel , input_type ):
3969+ check_functional_kernel_signature_match (F .rgb_to_grayscale , kernel = kernel , input_type = input_type )
3970+
3971+ @pytest .mark .parametrize ("transform" , [transforms .Grayscale (), transforms .RandomGrayscale (p = 1 )])
3972+ @pytest .mark .parametrize ("make_input" , [make_image_tensor , make_image_pil , make_image ])
3973+ def test_transform (self , transform , make_input ):
3974+ check_transform (transform , make_input ())
3975+
3976+ @pytest .mark .parametrize ("num_output_channels" , [1 , 3 ])
3977+ @pytest .mark .parametrize ("fn" , [F .rgb_to_grayscale , transform_cls_to_functional (transforms .Grayscale )])
3978+ def test_image_correctness (self , num_output_channels , fn ):
3979+ image = make_image (dtype = torch .uint8 , device = "cpu" )
3980+
3981+ actual = fn (image , num_output_channels = num_output_channels )
3982+ expected = F .to_image (F .rgb_to_grayscale (F .to_pil_image (image ), num_output_channels = num_output_channels ))
3983+
3984+ assert_equal (actual , expected , rtol = 0 , atol = 1 )
3985+
3986+ @pytest .mark .parametrize ("num_input_channels" , [1 , 3 ])
3987+ def test_random_transform_correctness (self , num_input_channels ):
3988+ image = make_image (
3989+ color_space = {
3990+ 1 : "GRAY" ,
3991+ 3 : "RGB" ,
3992+ }[num_input_channels ],
3993+ dtype = torch .uint8 ,
3994+ device = "cpu" ,
3995+ )
3996+
3997+ transform = transforms .RandomGrayscale (p = 1 )
3998+
3999+ actual = transform (image )
4000+ expected = F .to_image (F .rgb_to_grayscale (F .to_pil_image (image ), num_output_channels = num_input_channels ))
4001+
4002+ assert_equal (actual , expected , rtol = 0 , atol = 1 )
4003+
4004+
4005+ class TestRandomZoomOut :
4006+ # Tests are light because this largely relies on the already tested `pad` kernels.
4007+
4008+ @pytest .mark .parametrize (
4009+ "make_input" ,
4010+ [
4011+ make_image_tensor ,
4012+ make_image_pil ,
4013+ make_image ,
4014+ make_bounding_boxes ,
4015+ make_segmentation_mask ,
4016+ make_detection_mask ,
4017+ make_video ,
4018+ ],
4019+ )
4020+ def test_transform (self , make_input ):
4021+ check_transform (transforms .RandomZoomOut (p = 1 ), make_input ())
4022+
4023+ def test_transform_error (self ):
4024+ for side_range in [None , 1 , [1 , 2 , 3 ]]:
4025+ with pytest .raises (
4026+ ValueError if isinstance (side_range , list ) else TypeError , match = "should be a sequence of length 2"
4027+ ):
4028+ transforms .RandomZoomOut (side_range = side_range )
4029+
4030+ for side_range in [[0.5 , 1.5 ], [2.0 , 1.0 ]]:
4031+ with pytest .raises (ValueError , match = "Invalid side range" ):
4032+ transforms .RandomZoomOut (side_range = side_range )
4033+
4034+ @pytest .mark .parametrize ("side_range" , [(1.0 , 4.0 ), [2.0 , 5.0 ]])
4035+ @pytest .mark .parametrize (
4036+ "make_input" ,
4037+ [
4038+ make_image_tensor ,
4039+ make_image_pil ,
4040+ make_image ,
4041+ make_bounding_boxes ,
4042+ make_segmentation_mask ,
4043+ make_detection_mask ,
4044+ make_video ,
4045+ ],
4046+ )
4047+ @pytest .mark .parametrize ("device" , cpu_and_cuda ())
4048+ def test_transform_params_correctness (self , side_range , make_input , device ):
4049+ if make_input is make_image_pil and device != "cpu" :
4050+ pytest .skip ("PIL image tests with parametrization device!='cpu' will degenerate to that anyway." )
4051+
4052+ transform = transforms .RandomZoomOut (side_range = side_range )
4053+
4054+ input = make_input ()
4055+ height , width = F .get_size (input )
4056+
4057+ params = transform ._get_params ([input ])
4058+ assert "padding" in params
4059+
4060+ padding = params ["padding" ]
4061+ assert len (padding ) == 4
4062+
4063+ assert 0 <= padding [0 ] <= (side_range [1 ] - 1 ) * width
4064+ assert 0 <= padding [1 ] <= (side_range [1 ] - 1 ) * height
4065+ assert 0 <= padding [2 ] <= (side_range [1 ] - 1 ) * width
4066+ assert 0 <= padding [3 ] <= (side_range [1 ] - 1 ) * height
0 commit comments