Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import torchvision.transforms.v2 as transforms

from common_utils import (
assert_close,
assert_equal,
cache,
cpu_and_cuda,
Expand All @@ -41,7 +42,6 @@
)

from torch import nn
from torch.testing import assert_close
from torch.utils._pytree import tree_flatten, tree_map
from torch.utils.data import DataLoader, default_collate
from torchvision import tv_tensors
Expand Down Expand Up @@ -5449,7 +5449,18 @@ def test_kernel_image(self, dtype, device):
def test_kernel_video(self):
check_kernel(F.equalize_image, make_video())

@pytest.mark.parametrize("make_input", [make_image_tensor, make_image_pil, make_image, make_video])
@pytest.mark.parametrize(
"make_input",
[
make_image_tensor,
make_image_pil,
make_image,
make_video,
pytest.param(
make_image_cvcuda, marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available")
),
],
)
def test_functional(self, make_input):
check_functional(F.equalize, make_input())

Expand All @@ -5460,33 +5471,68 @@ def test_functional(self, make_input):
(F._color._equalize_image_pil, PIL.Image.Image),
(F.equalize_image, tv_tensors.Image),
(F.equalize_video, tv_tensors.Video),
pytest.param(
F._color._equalize_image_cvcuda,
None,
marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available"),
),
],
)
def test_functional_signature(self, kernel, input_type):
if kernel is F._color._equalize_image_cvcuda:
input_type = _import_cvcuda().Tensor
check_functional_kernel_signature_match(F.equalize, kernel=kernel, input_type=input_type)

@pytest.mark.parametrize(
"make_input",
[make_image_tensor, make_image_pil, make_image, make_video],
[
make_image_tensor,
make_image_pil,
make_image,
make_video,
pytest.param(
make_image_cvcuda, marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available")
),
],
)
def test_transform(self, make_input):
check_transform(transforms.RandomEqualize(p=1), make_input())

@pytest.mark.parametrize(("low", "high"), [(0, 64), (64, 192), (192, 256), (0, 1), (127, 128), (255, 256)])
@pytest.mark.parametrize(
"tensor_type",
[
torch.Tensor,
pytest.param(
"cvcuda.Tensor", marks=pytest.mark.skipif(not CVCUDA_AVAILABLE, reason="CVCUDA not available")
),
],
)
@pytest.mark.parametrize("fn", [F.equalize, transform_cls_to_functional(transforms.RandomEqualize, p=1)])
def test_image_correctness(self, low, high, fn):
def test_image_correctness(self, low, high, tensor_type, fn):
# We are not using the default `make_image` here since that uniformly samples the values over the whole value
# range. Since the whole point of F.equalize is to transform an arbitrary distribution of values into a uniform
# one over the full range, the information gain is low if we already provide something really close to the
# expected value.
image = tv_tensors.Image(
torch.testing.make_tensor((3, 117, 253), dtype=torch.uint8, device="cpu", low=low, high=high)
)
shape = (3, 117, 253)
if tensor_type == "cvcuda.Tensor":
shape = (1, *shape)
image = tv_tensors.Image(torch.testing.make_tensor(shape, dtype=torch.uint8, device="cpu", low=low, high=high))

if tensor_type == "cvcuda.Tensor":
image = F.to_cvcuda_tensor(image)

actual = fn(image)

if tensor_type == "cvcuda.Tensor":
image = F.cvcuda_to_tensor(image)[0].cpu()

expected = F.to_image(F.equalize(F.to_pil_image(image)))

assert_equal(actual, expected)
if tensor_type == "cvcuda.Tensor":
assert_close(actual, expected, rtol=1e-10, atol=1)
else:
assert_equal(actual, expected)


class TestUniformTemporalSubsample:
Expand Down
7 changes: 7 additions & 0 deletions torchvision/transforms/v2/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import torch
from torchvision import transforms as _transforms
from torchvision.transforms.v2 import functional as F, Transform
from torchvision.transforms.v2.functional._utils import _is_cvcuda_available, _is_cvcuda_tensor

from ._transform import _RandomApplyTransform
from ._utils import query_chw


CVCUDA_AVAILABLE = _is_cvcuda_available()


class Grayscale(Transform):
"""Convert images or videos to grayscale.

Expand Down Expand Up @@ -265,6 +269,9 @@ class RandomEqualize(_RandomApplyTransform):

_v1_transform_cls = _transforms.RandomEqualize

if CVCUDA_AVAILABLE:
_transformed_types = _RandomApplyTransform._transformed_types + (_is_cvcuda_tensor,)

def transform(self, inpt: Any, params: dict[str, Any]) -> Any:
return self._call_kernel(F.equalize, inpt)

Expand Down
5 changes: 3 additions & 2 deletions torchvision/transforms/v2/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from torchvision.transforms.transforms import _check_sequence_input, _setup_angle, _setup_size # noqa: F401
from torchvision.transforms.v2.functional import get_dimensions, get_size, is_pure_tensor
from torchvision.transforms.v2.functional._utils import _FillType, _FillTypeJIT
from torchvision.transforms.v2.functional._utils import _FillType, _FillTypeJIT, _is_cvcuda_tensor


def _setup_number_or_seq(arg: int | float | Sequence[int | float], name: str) -> Sequence[float]:
Expand Down Expand Up @@ -182,7 +182,7 @@ def query_chw(flat_inputs: list[Any]) -> tuple[int, int, int]:
chws = {
tuple(get_dimensions(inpt))
for inpt in flat_inputs
if check_type(inpt, (is_pure_tensor, tv_tensors.Image, PIL.Image.Image, tv_tensors.Video))
if check_type(inpt, (is_pure_tensor, tv_tensors.Image, PIL.Image.Image, tv_tensors.Video, _is_cvcuda_tensor))
}
if not chws:
raise TypeError("No image or video was found in the sample")
Expand All @@ -207,6 +207,7 @@ def query_size(flat_inputs: list[Any]) -> tuple[int, int]:
tv_tensors.Mask,
tv_tensors.BoundingBoxes,
tv_tensors.KeyPoints,
_is_cvcuda_tensor,
),
)
}
Expand Down
23 changes: 22 additions & 1 deletion torchvision/transforms/v2/functional/_color.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import TYPE_CHECKING

import PIL.Image
import torch
from torch.nn.functional import conv2d
Expand All @@ -9,7 +11,15 @@

from ._misc import _num_value_bits, to_dtype_image
from ._type_conversion import pil_to_tensor, to_pil_image
from ._utils import _get_kernel, _register_kernel_internal
from ._utils import _get_kernel, _import_cvcuda, _is_cvcuda_available, _register_kernel_internal


CVCUDA_AVAILABLE = _is_cvcuda_available()

if TYPE_CHECKING:
import cvcuda # type: ignore[import-not-found]
if CVCUDA_AVAILABLE:
cvcuda = _import_cvcuda() # noqa: F811


def rgb_to_grayscale(inpt: torch.Tensor, num_output_channels: int = 1) -> torch.Tensor:
Expand Down Expand Up @@ -649,6 +659,17 @@ def equalize_video(video: torch.Tensor) -> torch.Tensor:
return equalize_image(video)


def _equalize_image_cvcuda(
image: "cvcuda.Tensor",
) -> "cvcuda.Tensor":
cvcuda = _import_cvcuda()
return cvcuda.histogrameq(image, dtype=image.dtype)


if CVCUDA_AVAILABLE:
_register_kernel_internal(equalize, _import_cvcuda().Tensor)(_equalize_image_cvcuda)


def invert(inpt: torch.Tensor) -> torch.Tensor:
"""See :func:`~torchvision.transforms.v2.RandomInvert`."""
if torch.jit.is_scripting():
Expand Down
22 changes: 21 additions & 1 deletion torchvision/transforms/v2/functional/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def get_dimensions_video(video: torch.Tensor) -> list[int]:
return get_dimensions_image(video)


def get_dimensions_image_cvcuda(image: "cvcuda.Tensor") -> list[int]:
# CV-CUDA tensor is always in NHWC layout
# get_dimensions is CHW
return [image.shape[3], image.shape[1], image.shape[2]]


if CVCUDA_AVAILABLE:
_register_kernel_internal(get_dimensions, cvcuda.Tensor)(get_dimensions_image_cvcuda)


def get_num_channels(inpt: torch.Tensor) -> int:
if torch.jit.is_scripting():
return get_num_channels_image(inpt)
Expand Down Expand Up @@ -87,6 +97,16 @@ def get_num_channels_video(video: torch.Tensor) -> int:
get_image_num_channels = get_num_channels


def get_num_channels_image_cvcuda(image: "cvcuda.Tensor") -> int:
# CV-CUDA tensor is always in NHWC layout
# get_num_channels is C
return image.shape[3]


if CVCUDA_AVAILABLE:
_register_kernel_internal(get_num_channels, cvcuda.Tensor)(get_num_channels_image_cvcuda)


def get_size(inpt: torch.Tensor) -> list[int]:
if torch.jit.is_scripting():
return get_size_image(inpt)
Expand Down Expand Up @@ -125,7 +145,7 @@ def get_size_image_cvcuda(image: "cvcuda.Tensor") -> list[int]:


if CVCUDA_AVAILABLE:
_get_size_image_cvcuda = _register_kernel_internal(get_size, cvcuda.Tensor)(get_size_image_cvcuda)
_register_kernel_internal(get_size, _import_cvcuda().Tensor)(get_size_image_cvcuda)


@_register_kernel_internal(get_size, tv_tensors.Video, tv_tensor_wrapper=False)
Expand Down