Skip to content

Commit 95dcf5d

Browse files
committed
update pad to main standards
1 parent e2b0b3d commit 95dcf5d

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
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,
@@ -4750,14 +4749,14 @@ def test_functional(self, make_input):
47504749
(F.pad_mask, tv_tensors.Mask),
47514750
(F.pad_video, tv_tensors.Video),
47524751
pytest.param(
4753-
F._geometry._pad_cvcuda,
4754-
"cvcuda.Tensor",
4752+
F._geometry._pad_image_cvcuda,
4753+
None,
47554754
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="test requires CVCUDA"),
47564755
),
47574756
],
47584757
)
47594758
def test_functional_signature(self, kernel, input_type):
4760-
if input_type == "cvcuda.Tensor":
4759+
if kernel is F._geometry._pad_image_cvcuda:
47614760
input_type = _import_cvcuda().Tensor
47624761
check_functional_kernel_signature_match(F.pad, kernel=kernel, input_type=input_type)
47634762

@@ -4824,7 +4823,7 @@ def test_image_correctness(self, make_input, padding, padding_mode, fill, fn):
48244823
actual = fn(image, padding=padding, padding_mode=padding_mode, fill=fill)
48254824

48264825
if make_input is make_image_cvcuda:
4827-
image = cvcuda_to_pil_compatible_tensor(image)
4826+
image = F.cvcuda_to_tensor(image)[0].cpu()
48284827

48294828
expected = F.to_image(F.pad(F.to_pil_image(image), padding=padding, padding_mode=padding_mode, fill=fill))
48304829

torchvision/transforms/v2/_geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
get_bounding_boxes,
2727
has_all,
2828
has_any,
29-
is_cvcuda_tensor,
3029
is_pure_tensor,
3130
query_size,
3231
)
@@ -466,7 +465,8 @@ class Pad(Transform):
466465

467466
_v1_transform_cls = _transforms.Pad
468467

469-
_transformed_types = Transform._transformed_types + (is_cvcuda_tensor,)
468+
if CVCUDA_AVAILABLE:
469+
_transformed_types = Transform._transformed_types + (_is_cvcuda_tensor,)
470470

471471
def _extract_params_for_v1_transform(self) -> dict[str, Any]:
472472
params = super()._extract_params_for_v1_transform()

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from ._utils import (
3030
_FillTypeJIT,
31+
_get_cvcuda_border_from_pad_mode,
3132
_get_kernel,
3233
_import_cvcuda,
3334
_is_cvcuda_available,
@@ -1682,26 +1683,15 @@ def _pad_with_vector_fill(
16821683
_pad_image_pil = _register_kernel_internal(pad, PIL.Image.Image)(_FP.pad)
16831684

16841685

1685-
if CVCUDA_AVAILABLE:
1686-
_pad_mode_to_cvcuda = {
1687-
"constant": cvcuda.Border.CONSTANT,
1688-
"reflect": cvcuda.Border.REFLECT101,
1689-
"replicate": cvcuda.Border.REPLICATE,
1690-
"edge": cvcuda.Border.REPLICATE,
1691-
"symmetric": cvcuda.Border.REFLECT,
1692-
}
1693-
1694-
1695-
def _pad_cvcuda(
1686+
def _pad_image_cvcuda(
16961687
image: "cvcuda.Tensor",
16971688
padding: list[int],
16981689
fill: Optional[Union[int, float, list[float]]] = None,
16991690
padding_mode: str = "constant",
17001691
) -> "cvcuda.Tensor":
17011692
cvcuda = _import_cvcuda()
17021693

1703-
if _pad_mode_to_cvcuda.get(padding_mode) is None:
1704-
raise ValueError(f"Padding mode '{padding_mode}' is not supported with CVCUDA")
1694+
border_mode = _get_cvcuda_border_from_pad_mode(padding_mode)
17051695

17061696
if fill is None:
17071697
fill = 0
@@ -1712,7 +1702,7 @@ def _pad_cvcuda(
17121702

17131703
return cvcuda.copymakeborder(
17141704
image,
1715-
border_mode=_pad_mode_to_cvcuda[padding_mode],
1705+
border_mode=border_mode,
17161706
border_value=fill,
17171707
top=top,
17181708
left=left,
@@ -1722,7 +1712,7 @@ def _pad_cvcuda(
17221712

17231713

17241714
if CVCUDA_AVAILABLE:
1725-
_register_kernel_internal(pad, _import_cvcuda().Tensor)(_pad_cvcuda)
1715+
_register_kernel_internal(pad, _import_cvcuda().Tensor)(_pad_image_cvcuda)
17261716

17271717

17281718
@_register_kernel_internal(pad, tv_tensors.Mask)

torchvision/transforms/v2/functional/_utils.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import functools
22
from collections.abc import Sequence
3-
from typing import Any, Callable, Optional, Union
3+
from typing import Any, Callable, Optional, TYPE_CHECKING, Union
44

55
import torch
66
from torchvision import tv_tensors
77

8+
if TYPE_CHECKING:
9+
import cvcuda # type: ignore[import-not-found]
10+
811
_FillType = Union[int, float, Sequence[int], Sequence[float], None]
912
_FillTypeJIT = Optional[list[float]]
1013

@@ -177,3 +180,26 @@ def _is_cvcuda_tensor(inpt: Any) -> bool:
177180
return isinstance(inpt, cvcuda.Tensor)
178181
except ImportError:
179182
return False
183+
184+
185+
_pad_mode_to_cvcuda_border: dict[str, "cvcuda.Border"] = {}
186+
187+
188+
def _populate_cvcuda_pad_to_border_tables():
189+
cvcuda = _import_cvcuda()
190+
191+
global _pad_mode_to_cvcuda_border
192+
193+
_pad_mode_to_cvcuda_border = {
194+
"constant": cvcuda.Border.CONSTANT,
195+
"reflect": cvcuda.Border.REFLECT101,
196+
"replicate": cvcuda.Border.REPLICATE,
197+
"edge": cvcuda.Border.REPLICATE,
198+
"symmetric": cvcuda.Border.REFLECT,
199+
}
200+
201+
202+
def _get_cvcuda_border_from_pad_mode(pad_mode: str) -> "cvcuda.Border":
203+
if len(_pad_mode_to_cvcuda_border) == 0:
204+
_populate_cvcuda_pad_to_border_tables()
205+
return _pad_mode_to_cvcuda_border[pad_mode]

0 commit comments

Comments
 (0)