Skip to content

Commit f59e9e9

Browse files
committed
begin work on pad
1 parent fbea584 commit f59e9e9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/test_transforms_v2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,6 +4728,7 @@ def test_kernel_video(self):
47284728
make_segmentation_mask,
47294729
make_video,
47304730
make_keypoints,
4731+
make_image_cvcuda,
47314732
],
47324733
)
47334734
def test_functional(self, make_input):
@@ -4746,9 +4747,16 @@ def test_functional(self, make_input):
47464747
(F.pad_bounding_boxes, tv_tensors.BoundingBoxes),
47474748
(F.pad_mask, tv_tensors.Mask),
47484749
(F.pad_video, tv_tensors.Video),
4750+
pytest.param(
4751+
F.pad_image_cvcuda,
4752+
"cvcuda.Tensor",
4753+
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="test requires CVCUDA"),
4754+
),
47494755
],
47504756
)
47514757
def test_functional_signature(self, kernel, input_type):
4758+
if input_type == "cvcuda.Tensor":
4759+
input_type = _import_cvcuda().Tensor
47524760
check_functional_kernel_signature_match(F.pad, kernel=kernel, input_type=input_type)
47534761

47544762
@pytest.mark.parametrize(
@@ -4761,6 +4769,7 @@ def test_functional_signature(self, kernel, input_type):
47614769
make_segmentation_mask,
47624770
make_video,
47634771
make_keypoints,
4772+
make_image_cvcuda,
47644773
],
47654774
)
47664775
def test_transform(self, make_input):

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,49 @@ def _pad_with_vector_fill(
16821682
_pad_image_pil = _register_kernel_internal(pad, PIL.Image.Image)(_FP.pad)
16831683

16841684

1685+
if _CVCUDA_AVAILABLE:
1686+
cvcuda = _import_cvcuda()
1687+
_pad_mode_to_cvcuda = {
1688+
"constant": cvcuda.BorderType.CONSTANT,
1689+
"reflect": cvcuda.BorderType.REFLECT,
1690+
"replicate": cvcuda.BorderType.REPLICATE,
1691+
"symmetric": cvcuda.BorderType.WRAP,
1692+
}
1693+
1694+
1695+
def _pad_cvcuda(
1696+
image: "cvcuda.Tensor",
1697+
padding: list[int],
1698+
fill: Optional[Union[int, float, list[float]]] = None,
1699+
padding_mode: str = "constant",
1700+
) -> "cvcuda.Tensor":
1701+
cvcuda = _import_cvcuda()
1702+
1703+
if padding_mode not in _pad_mode_to_cvcuda:
1704+
raise ValueError(f"Padding mode '{padding_mode}' is not supported with CVCUDA")
1705+
1706+
if fill is None:
1707+
fill = 0
1708+
if isinstance(fill, (int, float)):
1709+
fill = [fill] * image.shape[3]
1710+
1711+
left, right, top, bottom = _parse_pad_padding(padding)
1712+
1713+
return cvcuda.copymakeborder(
1714+
image,
1715+
border_mode=_pad_mode_to_cvcuda[padding_mode],
1716+
border_value=fill,
1717+
top=top,
1718+
left=left,
1719+
bottom=bottom,
1720+
right=right,
1721+
)
1722+
1723+
1724+
if _CVCUDA_AVAILABLE:
1725+
_register_kernel_internal(pad, _import_cvcuda().Tensor)(_pad_cvcuda)
1726+
1727+
16851728
@_register_kernel_internal(pad, tv_tensors.Mask)
16861729
def pad_mask(
16871730
mask: torch.Tensor,

0 commit comments

Comments
 (0)