@@ -350,7 +350,7 @@ def test__transform(self, padding, fill, padding_mode, mocker):
350
350
transform = transforms .Pad (padding , fill = fill , padding_mode = padding_mode )
351
351
352
352
fn = mocker .patch ("torchvision.prototype.transforms.functional.pad" )
353
- inpt = mocker .MagicMock (spec = torch . Tensor )
353
+ inpt = mocker .MagicMock (spec = features . Image )
354
354
_ = transform (inpt )
355
355
356
356
fn .assert_called_once_with (inpt , padding = padding , fill = fill , padding_mode = padding_mode )
@@ -369,11 +369,12 @@ def test_assertions(self):
369
369
370
370
@pytest .mark .parametrize ("fill" , [0 , [1 , 2 , 3 ], (2 , 3 , 4 )])
371
371
@pytest .mark .parametrize ("side_range" , [(1.0 , 4.0 ), [2.0 , 5.0 ]])
372
- def test__get_params (self , fill , side_range ):
372
+ def test__get_params (self , fill , side_range , mocker ):
373
373
transform = transforms .RandomZoomOut (fill = fill , side_range = side_range )
374
374
375
- image = features .Image (torch .rand (1 , 3 , 32 , 32 ))
376
- c , h , w = image .shape [- 3 :]
375
+ image = mocker .MagicMock (spec = features .Image )
376
+ c = image .num_channels = 3
377
+ h , w = image .image_size = (24 , 32 )
377
378
378
379
params = transform ._get_params (image )
379
380
@@ -387,19 +388,22 @@ def test__get_params(self, fill, side_range):
387
388
@pytest .mark .parametrize ("fill" , [0 , [1 , 2 , 3 ], (2 , 3 , 4 )])
388
389
@pytest .mark .parametrize ("side_range" , [(1.0 , 4.0 ), [2.0 , 5.0 ]])
389
390
def test__transform (self , fill , side_range , mocker ):
390
- image = features .Image (torch .rand (1 , 3 , 32 , 32 ))
391
+ inpt = mocker .MagicMock (spec = features .Image )
392
+ inpt .num_channels = 3
393
+ inpt .image_size = (24 , 32 )
394
+
391
395
transform = transforms .RandomZoomOut (fill = fill , side_range = side_range , p = 1 )
392
396
393
397
fn = mocker .patch ("torchvision.prototype.transforms.functional.pad" )
394
398
# vfdev-5, Feature Request: let's store params as Transform attribute
395
399
# This could be also helpful for users
396
400
torch .manual_seed (12 )
397
- _ = transform (image )
401
+ _ = transform (inpt )
398
402
torch .manual_seed (12 )
399
403
torch .rand (1 ) # random apply changes random state
400
- params = transform ._get_params (image )
404
+ params = transform ._get_params (inpt )
401
405
402
- fn .assert_called_once_with (image , ** params )
406
+ fn .assert_called_once_with (inpt , ** params )
403
407
404
408
405
409
class TestRandomRotation :
@@ -449,7 +453,7 @@ def test__transform(self, degrees, expand, fill, center, mocker):
449
453
assert transform .degrees == [float (- degrees ), float (degrees )]
450
454
451
455
fn = mocker .patch ("torchvision.prototype.transforms.functional.rotate" )
452
- inpt = mocker .MagicMock (spec = torch . Tensor )
456
+ inpt = mocker .MagicMock (spec = features . Image )
453
457
# vfdev-5, Feature Request: let's store params as Transform attribute
454
458
# This could be also helpful for users
455
459
torch .manual_seed (12 )
@@ -504,9 +508,11 @@ def test_assertions(self):
504
508
@pytest .mark .parametrize ("translate" , [None , [0.1 , 0.2 ]])
505
509
@pytest .mark .parametrize ("scale" , [None , [0.7 , 1.2 ]])
506
510
@pytest .mark .parametrize ("shear" , [None , 2.0 , [5.0 , 15.0 ], [1.0 , 2.0 , 3.0 , 4.0 ]])
507
- def test__get_params (self , degrees , translate , scale , shear ):
508
- image = features .Image (torch .rand (1 , 3 , 32 , 32 ))
509
- h , w = image .shape [- 2 :]
511
+ def test__get_params (self , degrees , translate , scale , shear , mocker ):
512
+ image = mocker .MagicMock (spec = features .Image )
513
+ image .num_channels = 3
514
+ image .image_size = (24 , 32 )
515
+ h , w = image .image_size
510
516
511
517
transform = transforms .RandomAffine (degrees , translate = translate , scale = scale , shear = shear )
512
518
params = transform ._get_params (image )
@@ -564,7 +570,10 @@ def test__transform(self, degrees, translate, scale, shear, fill, center, mocker
564
570
assert transform .degrees == [float (- degrees ), float (degrees )]
565
571
566
572
fn = mocker .patch ("torchvision.prototype.transforms.functional.affine" )
567
- inpt = features .Image (torch .rand (1 , 3 , 32 , 32 ))
573
+ inpt = mocker .MagicMock (spec = features .Image )
574
+ inpt .num_channels = 3
575
+ inpt .image_size = (24 , 32 )
576
+
568
577
# vfdev-5, Feature Request: let's store params as Transform attribute
569
578
# This could be also helpful for users
570
579
torch .manual_seed (12 )
@@ -592,9 +601,11 @@ def test_assertions(self):
592
601
with pytest .raises (ValueError , match = "Padding mode should be either" ):
593
602
transforms .RandomCrop ([10 , 12 ], padding = 1 , padding_mode = "abc" )
594
603
595
- def test__get_params (self ):
596
- image = features .Image (torch .rand (1 , 3 , 32 , 32 ))
597
- h , w = image .shape [- 2 :]
604
+ def test__get_params (self , mocker ):
605
+ image = mocker .MagicMock (spec = features .Image )
606
+ image .num_channels = 3
607
+ image .image_size = (24 , 32 )
608
+ h , w = image .image_size
598
609
599
610
transform = transforms .RandomCrop ([10 , 10 ])
600
611
params = transform ._get_params (image )
@@ -614,7 +625,10 @@ def test_forward(self, padding, pad_if_needed, fill, padding_mode, mocker):
614
625
output_size , padding = padding , pad_if_needed = pad_if_needed , fill = fill , padding_mode = padding_mode
615
626
)
616
627
617
- inpt = features .Image (torch .rand (1 , 3 , 32 , 32 ))
628
+ inpt = mocker .MagicMock (spec = features .Image )
629
+ inpt .num_channels = 3
630
+ inpt .image_size = (32 , 32 )
631
+
618
632
expected = mocker .MagicMock (spec = features .Image )
619
633
expected .num_channels = 3
620
634
if isinstance (padding , int ):
@@ -696,7 +710,10 @@ def test__transform(self, kernel_size, sigma, mocker):
696
710
assert transform .sigma == (sigma , sigma )
697
711
698
712
fn = mocker .patch ("torchvision.prototype.transforms.functional.gaussian_blur" )
699
- inpt = features .Image (torch .rand (1 , 3 , 32 , 32 ))
713
+ inpt = mocker .MagicMock (spec = features .Image )
714
+ inpt .num_channels = 3
715
+ inpt .image_size = (24 , 32 )
716
+
700
717
# vfdev-5, Feature Request: let's store params as Transform attribute
701
718
# This could be also helpful for users
702
719
torch .manual_seed (12 )
@@ -730,3 +747,58 @@ def test__transform(self, p, transform_cls, func_op_name, kwargs, mocker):
730
747
fn .assert_called_once_with (inpt , ** kwargs )
731
748
else :
732
749
fn .call_count == 0
750
+
751
+
752
+ class TestRandomPerspective :
753
+ def test_assertions (self ):
754
+ with pytest .raises (ValueError , match = "Argument distortion_scale value should be between 0 and 1" ):
755
+ transforms .RandomPerspective (distortion_scale = - 1.0 )
756
+
757
+ with pytest .raises (TypeError , match = "Got inappropriate fill arg" ):
758
+ transforms .RandomPerspective (0.5 , fill = "abc" )
759
+
760
+ def test__get_params (self , mocker ):
761
+ dscale = 0.5
762
+ transform = transforms .RandomPerspective (dscale )
763
+ image = mocker .MagicMock (spec = features .Image )
764
+ image .num_channels = 3
765
+ image .image_size = (24 , 32 )
766
+
767
+ params = transform ._get_params (image )
768
+
769
+ h , w = image .image_size
770
+ assert len (params ["startpoints" ]) == 4
771
+ for x , y in params ["startpoints" ]:
772
+ assert x in (0 , w - 1 )
773
+ assert y in (0 , h - 1 )
774
+
775
+ assert len (params ["endpoints" ]) == 4
776
+ for (x , y ), name in zip (params ["endpoints" ], ["tl" , "tr" , "br" , "bl" ]):
777
+ if "t" in name :
778
+ assert 0 <= y <= int (dscale * h // 2 ), (x , y , name )
779
+ if "b" in name :
780
+ assert h - int (dscale * h // 2 ) - 1 <= y <= h , (x , y , name )
781
+ if "l" in name :
782
+ assert 0 <= x <= int (dscale * w // 2 ), (x , y , name )
783
+ if "r" in name :
784
+ assert w - int (dscale * w // 2 ) - 1 <= x <= w , (x , y , name )
785
+
786
+ @pytest .mark .parametrize ("distortion_scale" , [0.1 , 0.7 ])
787
+ def test__transform (self , distortion_scale , mocker ):
788
+ interpolation = InterpolationMode .BILINEAR
789
+ fill = 12
790
+ transform = transforms .RandomPerspective (distortion_scale , fill = fill , interpolation = interpolation )
791
+
792
+ fn = mocker .patch ("torchvision.prototype.transforms.functional.perspective" )
793
+ inpt = mocker .MagicMock (spec = features .Image )
794
+ inpt .num_channels = 3
795
+ inpt .image_size = (24 , 32 )
796
+ # vfdev-5, Feature Request: let's store params as Transform attribute
797
+ # This could be also helpful for users
798
+ torch .manual_seed (12 )
799
+ _ = transform (inpt )
800
+ torch .manual_seed (12 )
801
+ torch .rand (1 ) # random apply changes random state
802
+ params = transform ._get_params (inpt )
803
+
804
+ fn .assert_called_once_with (inpt , ** params , fill = fill , interpolation = interpolation )
0 commit comments