@@ -668,130 +668,6 @@ def test_boundingbox_spatial_size(self, angle, expand):
668668 assert out_img .spatial_size == out_bbox .spatial_size
669669
670670
671- class TestRandomAffine :
672- def test_assertions (self ):
673- with pytest .raises (ValueError , match = "is a single number, it must be positive" ):
674- transforms .RandomAffine (- 0.7 )
675-
676- for d in [[- 0.7 ], [- 0.7 , 0 , 0.7 ]]:
677- with pytest .raises (ValueError , match = "degrees should be a sequence of length 2" ):
678- transforms .RandomAffine (d )
679-
680- with pytest .raises (TypeError , match = "Got inappropriate fill arg" ):
681- transforms .RandomAffine (12 , fill = "abc" )
682-
683- with pytest .raises (TypeError , match = "Got inappropriate fill arg" ):
684- transforms .RandomAffine (12 , fill = "abc" )
685-
686- for kwargs in [
687- {"center" : 12 },
688- {"translate" : 12 },
689- {"scale" : 12 },
690- ]:
691- with pytest .raises (TypeError , match = "should be a sequence of length" ):
692- transforms .RandomAffine (12 , ** kwargs )
693-
694- for kwargs in [{"center" : [1 , 2 , 3 ]}, {"translate" : [1 , 2 , 3 ]}, {"scale" : [1 , 2 , 3 ]}]:
695- with pytest .raises (ValueError , match = "should be a sequence of length" ):
696- transforms .RandomAffine (12 , ** kwargs )
697-
698- with pytest .raises (ValueError , match = "translation values should be between 0 and 1" ):
699- transforms .RandomAffine (12 , translate = [- 1.0 , 2.0 ])
700-
701- with pytest .raises (ValueError , match = "scale values should be positive" ):
702- transforms .RandomAffine (12 , scale = [- 1.0 , 2.0 ])
703-
704- with pytest .raises (ValueError , match = "is a single number, it must be positive" ):
705- transforms .RandomAffine (12 , shear = - 10 )
706-
707- for s in [[- 0.7 ], [- 0.7 , 0 , 0.7 ]]:
708- with pytest .raises (ValueError , match = "shear should be a sequence of length 2" ):
709- transforms .RandomAffine (12 , shear = s )
710-
711- @pytest .mark .parametrize ("degrees" , [23 , [0 , 45 ], (0 , 45 )])
712- @pytest .mark .parametrize ("translate" , [None , [0.1 , 0.2 ]])
713- @pytest .mark .parametrize ("scale" , [None , [0.7 , 1.2 ]])
714- @pytest .mark .parametrize ("shear" , [None , 2.0 , [5.0 , 15.0 ], [1.0 , 2.0 , 3.0 , 4.0 ]])
715- def test__get_params (self , degrees , translate , scale , shear , mocker ):
716- image = mocker .MagicMock (spec = datapoints .Image )
717- image .num_channels = 3
718- image .spatial_size = (24 , 32 )
719- h , w = image .spatial_size
720-
721- transform = transforms .RandomAffine (degrees , translate = translate , scale = scale , shear = shear )
722- params = transform ._get_params ([image ])
723-
724- if not isinstance (degrees , (list , tuple )):
725- assert - degrees <= params ["angle" ] <= degrees
726- else :
727- assert degrees [0 ] <= params ["angle" ] <= degrees [1 ]
728-
729- if translate is not None :
730- w_max = int (round (translate [0 ] * w ))
731- h_max = int (round (translate [1 ] * h ))
732- assert - w_max <= params ["translate" ][0 ] <= w_max
733- assert - h_max <= params ["translate" ][1 ] <= h_max
734- else :
735- assert params ["translate" ] == (0 , 0 )
736-
737- if scale is not None :
738- assert scale [0 ] <= params ["scale" ] <= scale [1 ]
739- else :
740- assert params ["scale" ] == 1.0
741-
742- if shear is not None :
743- if isinstance (shear , float ):
744- assert - shear <= params ["shear" ][0 ] <= shear
745- assert params ["shear" ][1 ] == 0.0
746- elif len (shear ) == 2 :
747- assert shear [0 ] <= params ["shear" ][0 ] <= shear [1 ]
748- assert params ["shear" ][1 ] == 0.0
749- else :
750- assert shear [0 ] <= params ["shear" ][0 ] <= shear [1 ]
751- assert shear [2 ] <= params ["shear" ][1 ] <= shear [3 ]
752- else :
753- assert params ["shear" ] == (0 , 0 )
754-
755- @pytest .mark .parametrize ("degrees" , [23 , [0 , 45 ], (0 , 45 )])
756- @pytest .mark .parametrize ("translate" , [None , [0.1 , 0.2 ]])
757- @pytest .mark .parametrize ("scale" , [None , [0.7 , 1.2 ]])
758- @pytest .mark .parametrize ("shear" , [None , 2.0 , [5.0 , 15.0 ], [1.0 , 2.0 , 3.0 , 4.0 ]])
759- @pytest .mark .parametrize ("fill" , [0 , [1 , 2 , 3 ], (2 , 3 , 4 )])
760- @pytest .mark .parametrize ("center" , [None , [2.0 , 3.0 ]])
761- def test__transform (self , degrees , translate , scale , shear , fill , center , mocker ):
762- interpolation = InterpolationMode .BILINEAR
763- transform = transforms .RandomAffine (
764- degrees ,
765- translate = translate ,
766- scale = scale ,
767- shear = shear ,
768- interpolation = interpolation ,
769- fill = fill ,
770- center = center ,
771- )
772-
773- if isinstance (degrees , (tuple , list )):
774- assert transform .degrees == [float (degrees [0 ]), float (degrees [1 ])]
775- else :
776- assert transform .degrees == [float (- degrees ), float (degrees )]
777-
778- fn = mocker .patch ("torchvision.transforms.v2.functional.affine" )
779- inpt = mocker .MagicMock (spec = datapoints .Image )
780- inpt .num_channels = 3
781- inpt .spatial_size = (24 , 32 )
782-
783- # vfdev-5, Feature Request: let's store params as Transform attribute
784- # This could be also helpful for users
785- # Otherwise, we can mock transform._get_params
786- torch .manual_seed (12 )
787- _ = transform (inpt )
788- torch .manual_seed (12 )
789- params = transform ._get_params ([inpt ])
790-
791- fill = transforms ._utils ._convert_fill_arg (fill )
792- fn .assert_called_once_with (inpt , ** params , interpolation = interpolation , fill = fill , center = center )
793-
794-
795671class TestRandomCrop :
796672 def test_assertions (self ):
797673 with pytest .raises (ValueError , match = "Please provide only two dimensions" ):
0 commit comments