-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Implement Flip transforms with CVCUDA backend #9277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
98616f4
42fcc41
9423b4d
1e5e9ed
f8279c1
ab89bb5
39a1dba
a9554c7
3eb5cb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,12 @@ | |
| from torchvision.ops.boxes import box_iou | ||
| from torchvision.transforms.functional import _get_perspective_coeffs | ||
| from torchvision.transforms.v2 import functional as F, InterpolationMode, Transform | ||
| from torchvision.transforms.v2.functional._utils import _FillType | ||
| from torchvision.transforms.v2.functional._utils import ( | ||
| _FillType, | ||
| _import_cvcuda, | ||
| _is_cvcuda_available, | ||
| _is_cvcuda_tensor, | ||
| ) | ||
|
|
||
| from ._transform import _RandomApplyTransform | ||
| from ._utils import ( | ||
|
|
@@ -30,6 +35,10 @@ | |
| query_size, | ||
| ) | ||
|
|
||
| CVCUDA_AVAILABLE = _is_cvcuda_available() | ||
| if CVCUDA_AVAILABLE: | ||
| cvcuda = _import_cvcuda() | ||
|
||
|
|
||
|
|
||
| class RandomHorizontalFlip(_RandomApplyTransform): | ||
| """Horizontally flip the input with a given probability. | ||
|
|
@@ -45,6 +54,9 @@ class RandomHorizontalFlip(_RandomApplyTransform): | |
|
|
||
| _v1_transform_cls = _transforms.RandomHorizontalFlip | ||
|
|
||
| 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.horizontal_flip, inpt) | ||
|
|
||
|
|
@@ -63,6 +75,9 @@ class RandomVerticalFlip(_RandomApplyTransform): | |
|
|
||
| _v1_transform_cls = _transforms.RandomVerticalFlip | ||
|
|
||
| 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.vertical_flip, inpt) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import numbers | ||
| import warnings | ||
| from collections.abc import Sequence | ||
| from typing import Any, Optional, Union | ||
| from typing import Any, Optional, TYPE_CHECKING, Union | ||
|
|
||
| import PIL.Image | ||
| import torch | ||
|
|
@@ -26,7 +26,18 @@ | |
|
|
||
| from ._meta import _get_size_image_pil, clamp_bounding_boxes, convert_bounding_box_format | ||
|
|
||
| from ._utils import _FillTypeJIT, _get_kernel, _register_five_ten_crop_kernel_internal, _register_kernel_internal | ||
| from ._utils import ( | ||
| _FillTypeJIT, | ||
| _get_kernel, | ||
| _import_cvcuda, | ||
| _is_cvcuda_available, | ||
| _register_five_ten_crop_kernel_internal, | ||
| _register_kernel_internal, | ||
| ) | ||
|
|
||
| CVCUDA_AVAILABLE = _is_cvcuda_available() | ||
| if TYPE_CHECKING: | ||
| import cvcuda # type: ignore[import-not-found] | ||
|
|
||
|
|
||
| def _check_interpolation(interpolation: Union[InterpolationMode, int]) -> InterpolationMode: | ||
|
|
@@ -62,6 +73,14 @@ def _horizontal_flip_image_pil(image: PIL.Image.Image) -> PIL.Image.Image: | |
| return _FP.hflip(image) | ||
|
|
||
|
|
||
| def _horizontal_flip_image_cvcuda(image: "cvcuda.Tensor") -> "cvcuda.Tensor": | ||
AntoineSimoulin marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a bit of a nitpick, but could we rename the function to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the
The CVCUDA backend is basically of the same nature as the PIL backend. So It makes sense to keep it named
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NicolasHug just to be sure, you are saying it makes sense to keep it named
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks for catching! I'll edit above to avoid further confusion |
||
| return _import_cvcuda().flip(image, flipCode=1) | ||
|
|
||
|
|
||
| if CVCUDA_AVAILABLE: | ||
| _register_kernel_internal(horizontal_flip, _import_cvcuda().Tensor)(_horizontal_flip_image_cvcuda) | ||
|
|
||
|
|
||
| @_register_kernel_internal(horizontal_flip, tv_tensors.Mask) | ||
| def horizontal_flip_mask(mask: torch.Tensor) -> torch.Tensor: | ||
| return horizontal_flip_image(mask) | ||
|
|
@@ -150,6 +169,14 @@ def _vertical_flip_image_pil(image: PIL.Image.Image) -> PIL.Image.Image: | |
| return _FP.vflip(image) | ||
|
|
||
|
|
||
| def _vertical_flip_image_cvcuda(image: "cvcuda.Tensor") -> "cvcuda.Tensor": | ||
| return _import_cvcuda().flip(image, flipCode=0) | ||
|
|
||
|
|
||
| if CVCUDA_AVAILABLE: | ||
| _register_kernel_internal(vertical_flip, _import_cvcuda().Tensor)(_vertical_flip_image_cvcuda) | ||
|
|
||
|
|
||
| @_register_kernel_internal(vertical_flip, tv_tensors.Mask) | ||
| def vertical_flip_mask(mask: torch.Tensor) -> torch.Tensor: | ||
| return vertical_flip_image(mask) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.