@@ -163,7 +163,7 @@ def make_segmentation_masks(
163
163
yield make_segmentation_mask (size = size , dtype = dtype , extra_dims = extra_dims_ )
164
164
165
165
for dtype , extra_dims_ , num_objects_ in itertools .product (dtypes , extra_dims , num_objects ):
166
- yield make_segmentation_mask (num_objects = num_objects_ , dtype = dtype , extra_dims = extra_dims_ )
166
+ yield make_segmentation_mask (size = sizes [ 0 ], num_objects = num_objects_ , dtype = dtype , extra_dims = extra_dims_ )
167
167
168
168
169
169
class SampleInput :
@@ -904,21 +904,14 @@ def test_correctness_affine_bounding_box_on_fixed_input(device):
904
904
torch .testing .assert_close (output_boxes .tolist (), expected_bboxes )
905
905
906
906
907
- incorrect_expected_segmentation_mask_setup = pytest .mark .xfail (
908
- reason = "This test fails because the expected result computation is wrong. Fix ASAP." ,
909
- strict = False ,
910
- )
911
-
912
-
913
- @incorrect_expected_segmentation_mask_setup
914
907
@pytest .mark .parametrize ("angle" , [- 54 , 56 ])
915
908
@pytest .mark .parametrize ("translate" , [- 7 , 8 ])
916
909
@pytest .mark .parametrize ("scale" , [0.89 , 1.12 ])
917
910
@pytest .mark .parametrize ("shear" , [4 ])
918
911
@pytest .mark .parametrize ("center" , [None , (12 , 14 )])
919
912
def test_correctness_affine_segmentation_mask (angle , translate , scale , shear , center ):
920
913
def _compute_expected_mask (mask , angle_ , translate_ , scale_ , shear_ , center_ ):
921
- assert mask .ndim == 3 and mask . shape [ 0 ] == 1
914
+ assert mask .ndim == 3
922
915
affine_matrix = _compute_affine_matrix (angle_ , translate_ , scale_ , shear_ , center_ )
923
916
inv_affine_matrix = np .linalg .inv (affine_matrix )
924
917
inv_affine_matrix = inv_affine_matrix [:2 , :]
@@ -927,10 +920,11 @@ def _compute_expected_mask(mask, angle_, translate_, scale_, shear_, center_):
927
920
for out_y in range (expected_mask .shape [1 ]):
928
921
for out_x in range (expected_mask .shape [2 ]):
929
922
output_pt = np .array ([out_x + 0.5 , out_y + 0.5 , 1.0 ])
930
- input_pt = np .floor (np .dot (inv_affine_matrix , output_pt )).astype (np . int32 )
923
+ input_pt = np .floor (np .dot (inv_affine_matrix , output_pt )).astype ("int" )
931
924
in_x , in_y = input_pt [:2 ]
932
925
if 0 <= in_x < mask .shape [2 ] and 0 <= in_y < mask .shape [1 ]:
933
- expected_mask [0 , out_y , out_x ] = mask [0 , in_y , in_x ]
926
+ for i in range (expected_mask .shape [0 ]):
927
+ expected_mask [i , out_y , out_x ] = mask [i , in_y , in_x ]
934
928
return expected_mask .to (mask .device )
935
929
936
930
for mask in make_segmentation_masks (extra_dims = ((), (4 ,))):
@@ -1128,13 +1122,12 @@ def test_correctness_rotate_bounding_box_on_fixed_input(device, expand):
1128
1122
torch .testing .assert_close (output_boxes .tolist (), expected_bboxes )
1129
1123
1130
1124
1131
- @incorrect_expected_segmentation_mask_setup
1132
- @pytest .mark .parametrize ("angle" , range (- 90 , 90 , 37 ))
1125
+ @pytest .mark .parametrize ("angle" , range (- 89 , 90 , 37 ))
1133
1126
@pytest .mark .parametrize ("expand, center" , [(True , None ), (False , None ), (False , (12 , 14 ))])
1134
1127
def test_correctness_rotate_segmentation_mask (angle , expand , center ):
1135
1128
def _compute_expected_mask (mask , angle_ , expand_ , center_ ):
1136
- assert mask .ndim == 3 and mask . shape [ 0 ] == 1
1137
- image_size = mask .shape [ - 2 :]
1129
+ assert mask .ndim == 3
1130
+ c , * image_size = mask .shape
1138
1131
affine_matrix = _compute_affine_matrix (angle_ , [0.0 , 0.0 ], 1.0 , [0.0 , 0.0 ], center_ )
1139
1132
inv_affine_matrix = np .linalg .inv (affine_matrix )
1140
1133
@@ -1155,22 +1148,23 @@ def _compute_expected_mask(mask, angle_, expand_, center_):
1155
1148
max_vals = np .max (new_points , axis = 0 )[:2 ]
1156
1149
cmax = np .ceil (np .trunc (max_vals * 1e4 ) * 1e-4 )
1157
1150
cmin = np .floor (np .trunc ((min_vals + 1e-8 ) * 1e4 ) * 1e-4 )
1158
- new_width , new_height = (cmax - cmin ).astype ("int32 " ).tolist ()
1151
+ new_width , new_height = (cmax - cmin ).astype ("int " ).tolist ()
1159
1152
tr = np .array ([- (new_width - width ) / 2.0 , - (new_height - height ) / 2.0 , 1.0 ]) @ inv_affine_matrix .T
1160
1153
1161
1154
inv_affine_matrix [:2 , 2 ] = tr [:2 ]
1162
1155
image_size = [new_height , new_width ]
1163
1156
1164
1157
inv_affine_matrix = inv_affine_matrix [:2 , :]
1165
- expected_mask = torch .zeros (1 , * image_size , dtype = mask .dtype )
1158
+ expected_mask = torch .zeros (c , * image_size , dtype = mask .dtype )
1166
1159
1167
1160
for out_y in range (expected_mask .shape [1 ]):
1168
1161
for out_x in range (expected_mask .shape [2 ]):
1169
1162
output_pt = np .array ([out_x + 0.5 , out_y + 0.5 , 1.0 ])
1170
- input_pt = np .floor (np .dot (inv_affine_matrix , output_pt )).astype (np . int32 )
1163
+ input_pt = np .floor (np .dot (inv_affine_matrix , output_pt )).astype ("int" )
1171
1164
in_x , in_y = input_pt [:2 ]
1172
1165
if 0 <= in_x < mask .shape [2 ] and 0 <= in_y < mask .shape [1 ]:
1173
- expected_mask [0 , out_y , out_x ] = mask [0 , in_y , in_x ]
1166
+ for i in range (expected_mask .shape [0 ]):
1167
+ expected_mask [i , out_y , out_x ] = mask [i , in_y , in_x ]
1174
1168
return expected_mask .to (mask .device )
1175
1169
1176
1170
for mask in make_segmentation_masks (extra_dims = ((), (4 ,))):
@@ -1617,7 +1611,6 @@ def _compute_expected_bbox(bbox, pcoeffs_):
1617
1611
torch .testing .assert_close (output_bboxes , expected_bboxes , rtol = 1e-5 , atol = 1e-5 )
1618
1612
1619
1613
1620
- @incorrect_expected_segmentation_mask_setup
1621
1614
@pytest .mark .parametrize ("device" , cpu_and_gpu ())
1622
1615
@pytest .mark .parametrize (
1623
1616
"startpoints, endpoints" ,
@@ -1629,19 +1622,9 @@ def _compute_expected_bbox(bbox, pcoeffs_):
1629
1622
)
1630
1623
def test_correctness_perspective_segmentation_mask (device , startpoints , endpoints ):
1631
1624
def _compute_expected_mask (mask , pcoeffs_ ):
1632
- assert mask .ndim == 3 and mask .shape [0 ] == 1
1633
- m1 = np .array (
1634
- [
1635
- [pcoeffs_ [0 ], pcoeffs_ [1 ], pcoeffs_ [2 ]],
1636
- [pcoeffs_ [3 ], pcoeffs_ [4 ], pcoeffs_ [5 ]],
1637
- ]
1638
- )
1639
- m2 = np .array (
1640
- [
1641
- [pcoeffs_ [6 ], pcoeffs_ [7 ], 1.0 ],
1642
- [pcoeffs_ [6 ], pcoeffs_ [7 ], 1.0 ],
1643
- ]
1644
- )
1625
+ assert mask .ndim == 3
1626
+ m1 = np .array ([[pcoeffs_ [0 ], pcoeffs_ [1 ], pcoeffs_ [2 ]], [pcoeffs_ [3 ], pcoeffs_ [4 ], pcoeffs_ [5 ]]])
1627
+ m2 = np .array ([[pcoeffs_ [6 ], pcoeffs_ [7 ], 1.0 ], [pcoeffs_ [6 ], pcoeffs_ [7 ], 1.0 ]])
1645
1628
1646
1629
expected_mask = torch .zeros_like (mask .cpu ())
1647
1630
for out_y in range (expected_mask .shape [1 ]):
@@ -1654,7 +1637,8 @@ def _compute_expected_mask(mask, pcoeffs_):
1654
1637
1655
1638
in_x , in_y = input_pt [:2 ]
1656
1639
if 0 <= in_x < mask .shape [2 ] and 0 <= in_y < mask .shape [1 ]:
1657
- expected_mask [0 , out_y , out_x ] = mask [0 , in_y , in_x ]
1640
+ for i in range (expected_mask .shape [0 ]):
1641
+ expected_mask [i , out_y , out_x ] = mask [i , in_y , in_x ]
1658
1642
return expected_mask .to (mask .device )
1659
1643
1660
1644
pcoeffs = _get_perspective_coeffs (startpoints , endpoints )
@@ -1819,7 +1803,6 @@ def test_correctness_gaussian_blur_image_tensor(device, image_size, dt, ksize, s
1819
1803
torch .testing .assert_close (out , true_out , rtol = 0.0 , atol = 1.0 , msg = f"{ ksize } , { sigma } " )
1820
1804
1821
1805
1822
- @incorrect_expected_segmentation_mask_setup
1823
1806
@pytest .mark .parametrize ("device" , cpu_and_gpu ())
1824
1807
@pytest .mark .parametrize (
1825
1808
"fn, make_samples" , [(F .elastic_image_tensor , make_images ), (F .elastic_segmentation_mask , make_segmentation_masks )]
@@ -1829,10 +1812,11 @@ def test_correctness_elastic_image_or_mask_tensor(device, fn, make_samples):
1829
1812
for sample in make_samples (sizes = ((64 , 76 ),), extra_dims = ((), (4 ,))):
1830
1813
c , h , w = sample .shape [- 3 :]
1831
1814
# Setup a dummy image with 4 points
1832
- sample [..., in_box [1 ], in_box [0 ]] = torch .tensor ([12 , 34 , 96 , 112 ])[:c ]
1833
- sample [..., in_box [3 ] - 1 , in_box [0 ]] = torch .tensor ([12 , 34 , 96 , 112 ])[:c ]
1834
- sample [..., in_box [3 ] - 1 , in_box [2 ] - 1 ] = torch .tensor ([12 , 34 , 96 , 112 ])[:c ]
1835
- sample [..., in_box [1 ], in_box [2 ] - 1 ] = torch .tensor ([12 , 34 , 96 , 112 ])[:c ]
1815
+ print (sample .shape )
1816
+ sample [..., in_box [1 ], in_box [0 ]] = torch .arange (10 , 10 + c )
1817
+ sample [..., in_box [3 ] - 1 , in_box [0 ]] = torch .arange (20 , 20 + c )
1818
+ sample [..., in_box [3 ] - 1 , in_box [2 ] - 1 ] = torch .arange (30 , 30 + c )
1819
+ sample [..., in_box [1 ], in_box [2 ] - 1 ] = torch .arange (40 , 40 + c )
1836
1820
sample = sample .to (device )
1837
1821
1838
1822
if fn == F .elastic_image_tensor :
0 commit comments