Skip to content

Commit ad27932

Browse files
committed
Revert "Implement Flip transforms with CVCUDA backend (pytorch#9277)"
This reverts commit 6b56de1.
1 parent 5e40fe8 commit ad27932

File tree

5 files changed

+27
-143
lines changed

5 files changed

+27
-143
lines changed

test/common_utils.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
from torch.testing._comparison import BooleanPair, NonePair, not_close_error_metas, NumberPair, TensorLikePair
2121
from torchvision import io, tv_tensors
2222
from torchvision.transforms._functional_tensor import _max_value as get_max_value
23-
from torchvision.transforms.v2.functional import cvcuda_to_tensor, to_cvcuda_tensor, to_image, to_pil_image
24-
from torchvision.transforms.v2.functional._utils import _is_cvcuda_available, _is_cvcuda_tensor
23+
from torchvision.transforms.v2.functional import to_cvcuda_tensor, to_image, to_pil_image
2524
from torchvision.utils import _Image_fromarray
2625

2726

@@ -285,24 +284,8 @@ def __init__(
285284
mae=False,
286285
**other_parameters,
287286
):
288-
# Convert PIL images to tv_tensors.Image (regardless of what the other is)
289-
if isinstance(actual, PIL.Image.Image):
290-
actual = to_image(actual)
291-
if isinstance(expected, PIL.Image.Image):
292-
expected = to_image(expected)
293-
294-
if _is_cvcuda_available():
295-
if _is_cvcuda_tensor(actual):
296-
actual = cvcuda_to_tensor(actual)
297-
# Remove batch dimension if it's 1 for easier comparison against 3D PIL images
298-
if actual.shape[0] == 1:
299-
actual = actual[0]
300-
actual = actual.cpu()
301-
if _is_cvcuda_tensor(expected):
302-
expected = cvcuda_to_tensor(expected)
303-
if expected.shape[0] == 1:
304-
expected = expected[0]
305-
expected = expected.cpu()
287+
if all(isinstance(input, PIL.Image.Image) for input in [actual, expected]):
288+
actual, expected = (to_image(input) for input in [actual, expected])
306289

307290
super().__init__(actual, expected, **other_parameters)
308291
self.mae = mae
@@ -417,8 +400,8 @@ def make_image_pil(*args, **kwargs):
417400
return to_pil_image(make_image(*args, **kwargs))
418401

419402

420-
def make_image_cvcuda(*args, batch_dims=(1,), **kwargs):
421-
return to_cvcuda_tensor(make_image(*args, batch_dims=batch_dims, **kwargs))
403+
def make_image_cvcuda(*args, **kwargs):
404+
return to_cvcuda_tensor(make_image(*args, **kwargs))
422405

423406

424407
def make_keypoints(canvas_size=DEFAULT_SIZE, *, num_points=4, dtype=None, device="cpu"):
@@ -558,9 +541,5 @@ def ignore_jit_no_profile_information_warning():
558541
# with varying `INT1` and `INT2`. Since these are uninteresting for us and only clutter the test summary, we ignore
559542
# them.
560543
with warnings.catch_warnings():
561-
warnings.filterwarnings(
562-
"ignore",
563-
message=re.escape("operator() profile_node %"),
564-
category=UserWarning,
565-
)
544+
warnings.filterwarnings("ignore", message=re.escape("operator() profile_node %"), category=UserWarning)
566545
yield

test/test_transforms_v2.py

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,10 +1240,6 @@ def test_kernel_video(self):
12401240
make_image_tensor,
12411241
make_image_pil,
12421242
make_image,
1243-
pytest.param(
1244-
make_image_cvcuda,
1245-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1246-
),
12471243
make_bounding_boxes,
12481244
make_segmentation_mask,
12491245
make_video,
@@ -1259,20 +1255,13 @@ def test_functional(self, make_input):
12591255
(F.horizontal_flip_image, torch.Tensor),
12601256
(F._geometry._horizontal_flip_image_pil, PIL.Image.Image),
12611257
(F.horizontal_flip_image, tv_tensors.Image),
1262-
pytest.param(
1263-
F._geometry._horizontal_flip_image_cvcuda,
1264-
None,
1265-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1266-
),
12671258
(F.horizontal_flip_bounding_boxes, tv_tensors.BoundingBoxes),
12681259
(F.horizontal_flip_mask, tv_tensors.Mask),
12691260
(F.horizontal_flip_video, tv_tensors.Video),
12701261
(F.horizontal_flip_keypoints, tv_tensors.KeyPoints),
12711262
],
12721263
)
12731264
def test_functional_signature(self, kernel, input_type):
1274-
if kernel is F._geometry._horizontal_flip_image_cvcuda:
1275-
input_type = _import_cvcuda().Tensor
12761265
check_functional_kernel_signature_match(F.horizontal_flip, kernel=kernel, input_type=input_type)
12771266

12781267
@pytest.mark.parametrize(
@@ -1281,10 +1270,6 @@ def test_functional_signature(self, kernel, input_type):
12811270
make_image_tensor,
12821271
make_image_pil,
12831272
make_image,
1284-
pytest.param(
1285-
make_image_cvcuda,
1286-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1287-
),
12881273
make_bounding_boxes,
12891274
make_segmentation_mask,
12901275
make_video,
@@ -1298,23 +1283,13 @@ def test_transform(self, make_input, device):
12981283
@pytest.mark.parametrize(
12991284
"fn", [F.horizontal_flip, transform_cls_to_functional(transforms.RandomHorizontalFlip, p=1)]
13001285
)
1301-
@pytest.mark.parametrize(
1302-
"make_input",
1303-
[
1304-
make_image,
1305-
pytest.param(
1306-
make_image_cvcuda,
1307-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1308-
),
1309-
],
1310-
)
1311-
def test_image_correctness(self, fn, make_input):
1312-
image = make_input()
1286+
def test_image_correctness(self, fn):
1287+
image = make_image(dtype=torch.uint8, device="cpu")
1288+
13131289
actual = fn(image)
1314-
if make_input is make_image_cvcuda:
1315-
image = F.cvcuda_to_tensor(image)[0].cpu()
1316-
expected = F.horizontal_flip(F.to_pil_image(image))
1317-
assert_equal(actual, expected)
1290+
expected = F.to_image(F.horizontal_flip(F.to_pil_image(image)))
1291+
1292+
torch.testing.assert_close(actual, expected)
13181293

13191294
def _reference_horizontal_flip_bounding_boxes(self, bounding_boxes: tv_tensors.BoundingBoxes):
13201295
affine_matrix = np.array(
@@ -1370,10 +1345,6 @@ def test_keypoints_correctness(self, fn):
13701345
make_image_tensor,
13711346
make_image_pil,
13721347
make_image,
1373-
pytest.param(
1374-
make_image_cvcuda,
1375-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1376-
),
13771348
make_bounding_boxes,
13781349
make_segmentation_mask,
13791350
make_video,
@@ -1383,8 +1354,11 @@ def test_keypoints_correctness(self, fn):
13831354
@pytest.mark.parametrize("device", cpu_and_cuda())
13841355
def test_transform_noop(self, make_input, device):
13851356
input = make_input(device=device)
1357+
13861358
transform = transforms.RandomHorizontalFlip(p=0)
1359+
13871360
output = transform(input)
1361+
13881362
assert_equal(output, input)
13891363

13901364

@@ -1882,10 +1856,6 @@ def test_kernel_video(self):
18821856
make_image_tensor,
18831857
make_image_pil,
18841858
make_image,
1885-
pytest.param(
1886-
make_image_cvcuda,
1887-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1888-
),
18891859
make_bounding_boxes,
18901860
make_segmentation_mask,
18911861
make_video,
@@ -1901,20 +1871,13 @@ def test_functional(self, make_input):
19011871
(F.vertical_flip_image, torch.Tensor),
19021872
(F._geometry._vertical_flip_image_pil, PIL.Image.Image),
19031873
(F.vertical_flip_image, tv_tensors.Image),
1904-
pytest.param(
1905-
F._geometry._vertical_flip_image_cvcuda,
1906-
None,
1907-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1908-
),
19091874
(F.vertical_flip_bounding_boxes, tv_tensors.BoundingBoxes),
19101875
(F.vertical_flip_mask, tv_tensors.Mask),
19111876
(F.vertical_flip_video, tv_tensors.Video),
19121877
(F.vertical_flip_keypoints, tv_tensors.KeyPoints),
19131878
],
19141879
)
19151880
def test_functional_signature(self, kernel, input_type):
1916-
if kernel is F._geometry._vertical_flip_image_cvcuda:
1917-
input_type = _import_cvcuda().Tensor
19181881
check_functional_kernel_signature_match(F.vertical_flip, kernel=kernel, input_type=input_type)
19191882

19201883
@pytest.mark.parametrize(
@@ -1923,10 +1886,6 @@ def test_functional_signature(self, kernel, input_type):
19231886
make_image_tensor,
19241887
make_image_pil,
19251888
make_image,
1926-
pytest.param(
1927-
make_image_cvcuda,
1928-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1929-
),
19301889
make_bounding_boxes,
19311890
make_segmentation_mask,
19321891
make_video,
@@ -1938,23 +1897,13 @@ def test_transform(self, make_input, device):
19381897
check_transform(transforms.RandomVerticalFlip(p=1), make_input(device=device))
19391898

19401899
@pytest.mark.parametrize("fn", [F.vertical_flip, transform_cls_to_functional(transforms.RandomVerticalFlip, p=1)])
1941-
@pytest.mark.parametrize(
1942-
"make_input",
1943-
[
1944-
make_image,
1945-
pytest.param(
1946-
make_image_cvcuda,
1947-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
1948-
),
1949-
],
1950-
)
1951-
def test_image_correctness(self, fn, make_input):
1952-
image = make_input()
1900+
def test_image_correctness(self, fn):
1901+
image = make_image(dtype=torch.uint8, device="cpu")
1902+
19531903
actual = fn(image)
1954-
if make_input is make_image_cvcuda:
1955-
image = F.cvcuda_to_tensor(image)[0].cpu()
1956-
expected = F.vertical_flip(F.to_pil_image(image))
1957-
assert_equal(actual, expected)
1904+
expected = F.to_image(F.vertical_flip(F.to_pil_image(image)))
1905+
1906+
torch.testing.assert_close(actual, expected)
19581907

19591908
def _reference_vertical_flip_bounding_boxes(self, bounding_boxes: tv_tensors.BoundingBoxes):
19601909
affine_matrix = np.array(
@@ -2006,10 +1955,6 @@ def test_keypoints_correctness(self, fn):
20061955
make_image_tensor,
20071956
make_image_pil,
20081957
make_image,
2009-
pytest.param(
2010-
make_image_cvcuda,
2011-
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA is not available"),
2012-
),
20131958
make_bounding_boxes,
20141959
make_segmentation_mask,
20151960
make_video,
@@ -2019,8 +1964,11 @@ def test_keypoints_correctness(self, fn):
20191964
@pytest.mark.parametrize("device", cpu_and_cuda())
20201965
def test_transform_noop(self, make_input, device):
20211966
input = make_input(device=device)
1967+
20221968
transform = transforms.RandomVerticalFlip(p=0)
1969+
20231970
output = transform(input)
1971+
20241972
assert_equal(output, input)
20251973

20261974

torchvision/transforms/v2/_geometry.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from torchvision.ops.boxes import box_iou
1212
from torchvision.transforms.functional import _get_perspective_coeffs
1313
from torchvision.transforms.v2 import functional as F, InterpolationMode, Transform
14-
from torchvision.transforms.v2.functional._utils import _FillType, _is_cvcuda_available, _is_cvcuda_tensor
14+
from torchvision.transforms.v2.functional._utils import _FillType
1515

1616
from ._transform import _RandomApplyTransform
1717
from ._utils import (
@@ -30,8 +30,6 @@
3030
query_size,
3131
)
3232

33-
CVCUDA_AVAILABLE = _is_cvcuda_available()
34-
3533

3634
class RandomHorizontalFlip(_RandomApplyTransform):
3735
"""Horizontally flip the input with a given probability.
@@ -47,9 +45,6 @@ class RandomHorizontalFlip(_RandomApplyTransform):
4745

4846
_v1_transform_cls = _transforms.RandomHorizontalFlip
4947

50-
if CVCUDA_AVAILABLE:
51-
_transformed_types = _RandomApplyTransform._transformed_types + (_is_cvcuda_tensor,)
52-
5348
def transform(self, inpt: Any, params: dict[str, Any]) -> Any:
5449
return self._call_kernel(F.horizontal_flip, inpt)
5550

@@ -68,9 +63,6 @@ class RandomVerticalFlip(_RandomApplyTransform):
6863

6964
_v1_transform_cls = _transforms.RandomVerticalFlip
7065

71-
if CVCUDA_AVAILABLE:
72-
_transformed_types = _RandomApplyTransform._transformed_types + (_is_cvcuda_tensor,)
73-
7466
def transform(self, inpt: Any, params: dict[str, Any]) -> Any:
7567
return self._call_kernel(F.vertical_flip, inpt)
7668

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numbers
33
import warnings
44
from collections.abc import Sequence
5-
from typing import Any, Optional, TYPE_CHECKING, Union
5+
from typing import Any, Optional, Union
66

77
import PIL.Image
88
import torch
@@ -26,18 +26,7 @@
2626

2727
from ._meta import _get_size_image_pil, clamp_bounding_boxes, convert_bounding_box_format
2828

29-
from ._utils import (
30-
_FillTypeJIT,
31-
_get_kernel,
32-
_import_cvcuda,
33-
_is_cvcuda_available,
34-
_register_five_ten_crop_kernel_internal,
35-
_register_kernel_internal,
36-
)
37-
38-
CVCUDA_AVAILABLE = _is_cvcuda_available()
39-
if TYPE_CHECKING:
40-
import cvcuda # type: ignore[import-not-found]
29+
from ._utils import _FillTypeJIT, _get_kernel, _register_five_ten_crop_kernel_internal, _register_kernel_internal
4130

4231

4332
def _check_interpolation(interpolation: Union[InterpolationMode, int]) -> InterpolationMode:
@@ -73,14 +62,6 @@ def _horizontal_flip_image_pil(image: PIL.Image.Image) -> PIL.Image.Image:
7362
return _FP.hflip(image)
7463

7564

76-
def _horizontal_flip_image_cvcuda(image: "cvcuda.Tensor") -> "cvcuda.Tensor":
77-
return _import_cvcuda().flip(image, flipCode=1)
78-
79-
80-
if CVCUDA_AVAILABLE:
81-
_register_kernel_internal(horizontal_flip, _import_cvcuda().Tensor)(_horizontal_flip_image_cvcuda)
82-
83-
8465
@_register_kernel_internal(horizontal_flip, tv_tensors.Mask)
8566
def horizontal_flip_mask(mask: torch.Tensor) -> torch.Tensor:
8667
return horizontal_flip_image(mask)
@@ -169,14 +150,6 @@ def _vertical_flip_image_pil(image: PIL.Image.Image) -> PIL.Image.Image:
169150
return _FP.vflip(image)
170151

171152

172-
def _vertical_flip_image_cvcuda(image: "cvcuda.Tensor") -> "cvcuda.Tensor":
173-
return _import_cvcuda().flip(image, flipCode=0)
174-
175-
176-
if CVCUDA_AVAILABLE:
177-
_register_kernel_internal(vertical_flip, _import_cvcuda().Tensor)(_vertical_flip_image_cvcuda)
178-
179-
180153
@_register_kernel_internal(vertical_flip, tv_tensors.Mask)
181154
def vertical_flip_mask(mask: torch.Tensor) -> torch.Tensor:
182155
return vertical_flip_image(mask)

torchvision/transforms/v2/functional/_utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,3 @@ def _is_cvcuda_available():
169169
return True
170170
except ImportError:
171171
return False
172-
173-
174-
def _is_cvcuda_tensor(inpt: Any) -> bool:
175-
try:
176-
cvcuda = _import_cvcuda()
177-
return isinstance(inpt, cvcuda.Tensor)
178-
except ImportError:
179-
return False

0 commit comments

Comments
 (0)