-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Affine CV-CUDA Backend #9294
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
Open
justincdavis
wants to merge
19
commits into
pytorch:main
Choose a base branch
from
justincdavis:feat/affine_cvcuda
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−10
Open
Affine CV-CUDA Backend #9294
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
44db71c
implement additional cvcuda infra for all branches to avoid duplicate…
justincdavis e3dd700
update make_image_cvcuda to have default batch dim
justincdavis c035df1
add stanardized setup to main for easier updating of PRs and branches
justincdavis 98d7dfb
update is_cvcuda_tensor
justincdavis ddc116d
add cvcuda to pil compatible to transforms by default
justincdavis e51dc7e
remove cvcuda from transform class
justincdavis e14e210
merge with main
justincdavis 4939355
resolve more formatting naming
justincdavis fbea584
update is cvcuda tensor impl
justincdavis 2ce9451
affine implemented and passing tests
justincdavis 23816f4
update transformed_types
justincdavis 982d21d
correct affine behavior, cvcuda center on top left
justincdavis 1860d73
update to main standards
justincdavis c0edb42
update interp table from resize
justincdavis d9d9fc8
refactor interp setup
justincdavis 58e2e0e
Merge remote-tracking branch 'upstream/main' into feat/affine_cvcuda
justincdavis 62d4591
merge with main
justincdavis f8d748c
drop unused meta funcs for cvcuda images
justincdavis 77e800b
correct RandomAffine transformed_Types def
justincdavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| from collections.abc import Sequence | ||
| from typing import Any, Optional, TYPE_CHECKING, Union | ||
|
|
||
| import numpy as np | ||
| import PIL.Image | ||
| import torch | ||
| from torch.nn.functional import grid_sample, interpolate, pad as torch_pad | ||
|
|
@@ -28,6 +29,7 @@ | |
|
|
||
| from ._utils import ( | ||
| _FillTypeJIT, | ||
| _get_cvcuda_interp, | ||
| _get_kernel, | ||
| _import_cvcuda, | ||
| _is_cvcuda_available, | ||
|
|
@@ -1331,6 +1333,59 @@ def affine_video( | |
| ) | ||
|
|
||
|
|
||
| def _affine_image_cvcuda( | ||
| image: "cvcuda.Tensor", | ||
| angle: Union[int, float], | ||
| translate: list[float], | ||
| scale: float, | ||
| shear: list[float], | ||
| interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST, | ||
| fill: _FillTypeJIT = None, | ||
| center: Optional[list[float]] = None, | ||
| ) -> "cvcuda.Tensor": | ||
| cvcuda = _import_cvcuda() | ||
|
|
||
| interpolation = _check_interpolation(interpolation) | ||
| angle, translate, shear, center = _affine_parse_args(angle, translate, scale, shear, interpolation, center) | ||
|
|
||
| height, width, num_channels = image.shape[1:] | ||
|
|
||
| # Determine the actual center point (cx, cy) | ||
| # torchvision uses image center by default, cvcuda transforms around upper-left (0,0) | ||
| # Unlike the tensor version which uses normalized coordinates centered at image center, | ||
| # CV-CUDA uses absolute pixel coordinates, so we pass actual center to _get_inverse_affine_matrix | ||
| if center is None: | ||
| cx, cy = width / 2.0, height / 2.0 | ||
| else: | ||
| cx, cy = float(center[0]), float(center[1]) | ||
|
|
||
| translate_f = [float(t) for t in translate] | ||
| matrix = _get_inverse_affine_matrix([cx, cy], angle, translate_f, scale, shear) | ||
|
|
||
| interp = _get_cvcuda_interp(interpolation) | ||
|
|
||
| xform = np.array([[matrix[0], matrix[1], matrix[2]], [matrix[3], matrix[4], matrix[5]]], dtype=np.float32) | ||
|
Contributor
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. We can rewrite this to |
||
|
|
||
| if fill is None: | ||
| border_value = np.zeros(num_channels, dtype=np.float32) | ||
| elif isinstance(fill, (int, float)): | ||
| border_value = np.full(num_channels, fill, dtype=np.float32) | ||
| else: | ||
| border_value = np.array(fill, dtype=np.float32)[:num_channels] | ||
|
|
||
| return cvcuda.warp_affine( | ||
| image, | ||
| xform, | ||
| flags=interp | cvcuda.Interp.WARP_INVERSE_MAP, | ||
| border_mode=cvcuda.Border.CONSTANT, | ||
| border_value=border_value, | ||
| ) | ||
|
|
||
|
|
||
| if CVCUDA_AVAILABLE: | ||
| _register_kernel_internal(affine, _import_cvcuda().Tensor)(_affine_image_cvcuda) | ||
|
|
||
|
|
||
| def rotate( | ||
| inpt: torch.Tensor, | ||
| angle: float, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum comparison is preferred to use "==".
Also see comment here
The threshold number might need more investigations. @NicolasHug