Skip to content

Commit 87e07ca

Browse files
committed
update to main standards
1 parent fe4824b commit 87e07ca

File tree

5 files changed

+22
-32
lines changed

5 files changed

+22
-32
lines changed

test/test_transforms_v2.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
assert_equal,
2626
cache,
2727
cpu_and_cuda,
28-
cvcuda_to_pil_compatible_tensor,
2928
freeze_rng_state,
3029
ignore_jit_no_profile_information_warning,
3130
make_bounding_boxes,
@@ -6431,7 +6430,7 @@ def test_functional(self, make_input):
64316430
(F._color._rgb_to_grayscale_image_pil, PIL.Image.Image),
64326431
(F.rgb_to_grayscale_image, tv_tensors.Image),
64336432
pytest.param(
6434-
F._color._rgb_to_grayscale_cvcuda,
6433+
F._color._rgb_to_grayscale_image_cvcuda,
64356434
"cvcuda.Tensor",
64366435
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available"),
64376436
),
@@ -6480,7 +6479,7 @@ def test_image_correctness(self, num_output_channels, color_space, make_input, f
64806479
actual = fn(image, num_output_channels=num_output_channels)
64816480

64826481
if make_input is make_image_cvcuda:
6483-
image = cvcuda_to_pil_compatible_tensor(image)
6482+
image = F.cvcuda_to_tensor(image)[0].cpu()
64846483

64856484
expected = F.to_image(F.rgb_to_grayscale(F.to_pil_image(image), num_output_channels=num_output_channels))
64866485

@@ -6540,7 +6539,7 @@ def test_functional(self, make_input):
65406539
(F._color._rgb_to_grayscale_image_pil, PIL.Image.Image),
65416540
(F.rgb_to_grayscale_image, tv_tensors.Image),
65426541
pytest.param(
6543-
F._color._rgb_to_grayscale_cvcuda,
6542+
F._color._rgb_to_grayscale_image_cvcuda,
65446543
"cvcuda.Tensor",
65456544
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available"),
65466545
),
@@ -6581,7 +6580,7 @@ def test_image_correctness(self, make_input, fn):
65816580
actual = fn(image)
65826581

65836582
if make_input is make_image_cvcuda:
6584-
image = cvcuda_to_pil_compatible_tensor(image)
6583+
image = F.cvcuda_to_tensor(image)[0].cpu()
65856584

65866585
expected = F.to_image(F.grayscale_to_rgb(F.to_pil_image(image)))
65876586

torchvision/transforms/v2/_color.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import torch
66
from torchvision import transforms as _transforms
77
from torchvision.transforms.v2 import functional as F, Transform
8+
from torchvision.transforms.v2.functional._utils import _is_cvcuda_available, _is_cvcuda_tensor
89

910
from ._transform import _RandomApplyTransform
10-
from ._utils import is_cvcuda_tensor, query_chw
11+
from ._utils import query_chw
12+
13+
14+
CVCUDA_AVAILABLE = _is_cvcuda_available()
1115

1216

1317
class Grayscale(Transform):
@@ -22,7 +26,8 @@ class Grayscale(Transform):
2226

2327
_v1_transform_cls = _transforms.Grayscale
2428

25-
_transformed_types = Transform._transformed_types + (is_cvcuda_tensor,)
29+
if CVCUDA_AVAILABLE:
30+
_transformed_types = Transform._transformed_types + (_is_cvcuda_tensor,)
2631

2732
def __init__(self, num_output_channels: int = 1):
2833
super().__init__()
@@ -46,7 +51,8 @@ class RandomGrayscale(_RandomApplyTransform):
4651

4752
_v1_transform_cls = _transforms.RandomGrayscale
4853

49-
_transformed_types = _RandomApplyTransform._transformed_types + (is_cvcuda_tensor,)
54+
if CVCUDA_AVAILABLE:
55+
_transformed_types = _RandomApplyTransform._transformed_types + (_is_cvcuda_tensor,)
5056

5157
def __init__(self, p: float = 0.1) -> None:
5258
super().__init__(p=p)
@@ -66,7 +72,8 @@ class RGB(Transform):
6672
to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions
6773
"""
6874

69-
_transformed_types = Transform._transformed_types + (is_cvcuda_tensor,)
75+
if CVCUDA_AVAILABLE:
76+
_transformed_types = Transform._transformed_types + (_is_cvcuda_tensor,)
7077

7178
def __init__(self):
7279
super().__init__()

torchvision/transforms/v2/functional/_augment.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import io
2-
from typing import TYPE_CHECKING
32

43
import PIL.Image
54

@@ -9,15 +8,7 @@
98
from torchvision.transforms.functional import pil_to_tensor, to_pil_image
109
from torchvision.utils import _log_api_usage_once
1110

12-
from ._utils import _get_kernel, _import_cvcuda, _is_cvcuda_available, _register_kernel_internal
13-
14-
15-
CVCUDA_AVAILABLE = _is_cvcuda_available()
16-
17-
if TYPE_CHECKING:
18-
import cvcuda # type: ignore[import-not-found]
19-
if CVCUDA_AVAILABLE:
20-
cvcuda = _import_cvcuda() # noqa: F811
11+
from ._utils import _get_kernel, _register_kernel_internal
2112

2213

2314
def erase(

torchvision/transforms/v2/functional/_color.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _rgb_to_grayscale_image_pil(image: PIL.Image.Image, num_output_channels: int
7373
return _FP.to_grayscale(image, num_output_channels=num_output_channels)
7474

7575

76-
def _rgb_to_grayscale_cvcuda(
76+
def _rgb_to_grayscale_image_cvcuda(
7777
image: "cvcuda.Tensor",
7878
num_output_channels: int = 1,
7979
) -> "cvcuda.Tensor":
@@ -102,7 +102,7 @@ def _rgb_to_grayscale_cvcuda(
102102

103103

104104
if CVCUDA_AVAILABLE:
105-
_register_kernel_internal(rgb_to_grayscale, _import_cvcuda().Tensor)(_rgb_to_grayscale_cvcuda)
105+
_register_kernel_internal(rgb_to_grayscale, _import_cvcuda().Tensor)(_rgb_to_grayscale_image_cvcuda)
106106

107107

108108
def grayscale_to_rgb(inpt: torch.Tensor) -> torch.Tensor:
@@ -131,7 +131,7 @@ def grayscale_to_rgb_image_pil(image: PIL.Image.Image) -> PIL.Image.Image:
131131
return image.convert(mode="RGB")
132132

133133

134-
def _grayscale_to_rgb_cvcuda(
134+
def _grayscale_to_rgb_image_cvcuda(
135135
image: "cvcuda.Tensor",
136136
) -> "cvcuda.Tensor":
137137
cvcuda = _import_cvcuda()
@@ -153,7 +153,7 @@ def _grayscale_to_rgb_cvcuda(
153153

154154

155155
if CVCUDA_AVAILABLE:
156-
_register_kernel_internal(grayscale_to_rgb, _import_cvcuda().Tensor)(_grayscale_to_rgb_cvcuda)
156+
_register_kernel_internal(grayscale_to_rgb, _import_cvcuda().Tensor)(_grayscale_to_rgb_image_cvcuda)
157157

158158

159159
def _blend(image1: torch.Tensor, image2: torch.Tensor, ratio: float) -> torch.Tensor:

torchvision/transforms/v2/functional/_misc.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import math
2-
from typing import Optional, TYPE_CHECKING
2+
from typing import Optional
33

44
import PIL.Image
55
import torch
@@ -13,14 +13,7 @@
1313

1414
from ._meta import _convert_bounding_box_format
1515

16-
from ._utils import _get_kernel, _import_cvcuda, _is_cvcuda_available, _register_kernel_internal, is_pure_tensor
17-
18-
CVCUDA_AVAILABLE = _is_cvcuda_available()
19-
20-
if TYPE_CHECKING:
21-
import cvcuda # type: ignore[import-not-found]
22-
if CVCUDA_AVAILABLE:
23-
cvcuda = _import_cvcuda() # noqa: F811
16+
from ._utils import _get_kernel, _register_kernel_internal, is_pure_tensor
2417

2518

2619
def normalize(

0 commit comments

Comments
 (0)