@@ -5047,3 +5047,82 @@ def test_transform_error_cuda(self):
5047
5047
ValueError , match = "Input tensor should be on the same device as transformation matrix and mean vector"
5048
5048
):
5049
5049
transform (input )
5050
+
5051
+
5052
+ def make_image_numpy (* args , ** kwargs ):
5053
+ image = make_image_tensor (* args , ** kwargs )
5054
+ return image .permute ((1 , 2 , 0 )).numpy ()
5055
+
5056
+
5057
+ class TestToImage :
5058
+ @pytest .mark .parametrize ("make_input" , [make_image_tensor , make_image_pil , make_image , make_image_numpy ])
5059
+ @pytest .mark .parametrize ("fn" , [F .to_image , transform_cls_to_functional (transforms .ToImage )])
5060
+ def test_functional_and_transform (self , make_input , fn ):
5061
+ input = make_input ()
5062
+ output = fn (input )
5063
+
5064
+ assert isinstance (output , tv_tensors .Image )
5065
+
5066
+ input_size = list (input .shape [:2 ]) if isinstance (input , np .ndarray ) else F .get_size (input )
5067
+ assert F .get_size (output ) == input_size
5068
+
5069
+ if isinstance (input , torch .Tensor ):
5070
+ assert output .data_ptr () == input .data_ptr ()
5071
+
5072
+ def test_functional_error (self ):
5073
+ with pytest .raises (TypeError , match = "Input can either be a pure Tensor, a numpy array, or a PIL image" ):
5074
+ F .to_image (object ())
5075
+
5076
+
5077
+ class TestToPILImage :
5078
+ @pytest .mark .parametrize ("make_input" , [make_image_tensor , make_image , make_image_numpy ])
5079
+ @pytest .mark .parametrize ("color_space" , ["RGB" , "GRAY" ])
5080
+ @pytest .mark .parametrize ("fn" , [F .to_pil_image , transform_cls_to_functional (transforms .ToPILImage )])
5081
+ def test_functional_and_transform (self , make_input , color_space , fn ):
5082
+ input = make_input (color_space = color_space )
5083
+ output = fn (input )
5084
+
5085
+ assert isinstance (output , PIL .Image .Image )
5086
+
5087
+ input_size = list (input .shape [:2 ]) if isinstance (input , np .ndarray ) else F .get_size (input )
5088
+ assert F .get_size (output ) == input_size
5089
+
5090
+ def test_functional_error (self ):
5091
+ with pytest .raises (TypeError , match = "pic should be Tensor or ndarray" ):
5092
+ F .to_pil_image (object ())
5093
+
5094
+ for ndim in [1 , 4 ]:
5095
+ with pytest .raises (ValueError , match = "pic should be 2/3 dimensional" ):
5096
+ F .to_pil_image (torch .empty (* [1 ] * ndim ))
5097
+
5098
+ with pytest .raises (ValueError , match = "pic should not have > 4 channels" ):
5099
+ num_channels = 5
5100
+ F .to_pil_image (torch .empty (num_channels , 1 , 1 ))
5101
+
5102
+
5103
+ class TestToTensor :
5104
+ @pytest .mark .parametrize ("make_input" , [make_image_tensor , make_image_pil , make_image , make_image_numpy ])
5105
+ def test_smoke (self , make_input ):
5106
+ with pytest .warns (UserWarning , match = "deprecated and will be removed" ):
5107
+ transform = transforms .ToTensor ()
5108
+
5109
+ input = make_input ()
5110
+ output = transform (input )
5111
+
5112
+ input_size = list (input .shape [:2 ]) if isinstance (input , np .ndarray ) else F .get_size (input )
5113
+ assert F .get_size (output ) == input_size
5114
+
5115
+
5116
+ class TestPILToTensor :
5117
+ @pytest .mark .parametrize ("color_space" , ["RGB" , "GRAY" ])
5118
+ @pytest .mark .parametrize ("fn" , [F .pil_to_tensor , transform_cls_to_functional (transforms .PILToTensor )])
5119
+ def test_functional_and_transform (self , color_space , fn ):
5120
+ input = make_image_pil (color_space = color_space )
5121
+ output = fn (input )
5122
+
5123
+ assert isinstance (output , torch .Tensor ) and not isinstance (output , tv_tensors .TVTensor )
5124
+ assert F .get_size (output ) == F .get_size (input )
5125
+
5126
+ def test_functional_error (self ):
5127
+ with pytest .raises (TypeError , match = "pic should be PIL Image" ):
5128
+ F .pil_to_tensor (object ())
0 commit comments