-
Notifications
You must be signed in to change notification settings - Fork 7.2k
refactor Datapoint dispatch mechanism #7747
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d9e1379
[PoC] refactor Datapoint dispatch mechanism
pmeier 36b9d36
fix test
pmeier f36c64c
Merge branch 'main' into kernel-registration
pmeier bbaa35c
add dispatch to adjust_brightness
pmeier ca4ad32
enforce no register overwrite
pmeier d23a80e
[PoC] make wrapping interal kernel more convenient
pmeier bf47188
[PoC] enforce explicit no-ops
pmeier 74d5054
fix adjust_brightness tests and remove methods
pmeier e88be5e
Merge branch 'main' into kernel-registration
pmeier f178373
address minor comments
pmeier 65e80d0
make no-op registration a decorator
pmeier 9614477
Merge branch 'main'
pmeier 6ac08e4
explicit metadata
pmeier cac079b
implement dispatchers for erase five/ten_crop and temporal_subsample
pmeier c7256b4
make shape getters proper dispatchers
pmeier bf78cd6
fix
pmeier f86f89b
port normalize and to_dtype
pmeier d90daf6
address comments
pmeier 09eec9a
address comments and cleanup
pmeier 3730811
more cleanup
pmeier 7203453
Merge branch 'main' into kernel-registration
pmeier 31bee5f
port all remaining dispatchers to the new mechanism
pmeier a924013
put back legacy test_dispatch_datapoint
pmeier b3c2c88
minor test fixes
pmeier a1f5ea4
Update torchvision/transforms/v2/functional/_utils.py
pmeier d29d95b
reinstante antialias tests
pmeier 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
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
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 |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ | |
|
|
||
| from ._meta import clamp_bounding_box, convert_format_bounding_box, get_spatial_size_image_pil | ||
|
|
||
| from ._utils import is_simple_tensor | ||
| from ._utils import _get_kernel, is_simple_tensor, register_kernel | ||
|
|
||
|
|
||
| def _check_interpolation(interpolation: Union[InterpolationMode, int]) -> InterpolationMode: | ||
|
|
@@ -158,6 +158,32 @@ def _compute_resized_output_size( | |
| return __compute_resized_output_size(spatial_size, size=size, max_size=max_size) | ||
|
|
||
|
|
||
| def resize( | ||
|
Contributor
Author
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 need to move the definition of the dispatcher above the kernel definitions, since the dispatcher is used in the decorator. Other than that, only the datapoint branch was changed. |
||
| inpt: datapoints._InputTypeJIT, | ||
| size: List[int], | ||
| interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR, | ||
| max_size: Optional[int] = None, | ||
| antialias: Optional[Union[str, bool]] = "warn", | ||
| ) -> datapoints._InputTypeJIT: | ||
| if not torch.jit.is_scripting(): | ||
| _log_api_usage_once(resize) | ||
| if torch.jit.is_scripting() or is_simple_tensor(inpt): | ||
| return resize_image_tensor(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias) | ||
| elif isinstance(inpt, datapoints._datapoint.Datapoint): | ||
| kernel = _get_kernel(resize, type(inpt)) | ||
| return kernel(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias) | ||
| elif isinstance(inpt, PIL.Image.Image): | ||
| if antialias is False: | ||
| warnings.warn("Anti-alias option is always applied for PIL Image input. Argument antialias is ignored.") | ||
| return resize_image_pil(inpt, size, interpolation=interpolation, max_size=max_size) | ||
| else: | ||
| raise TypeError( | ||
| f"Input can either be a plain tensor, any TorchVision datapoint, or a PIL image, " | ||
| f"but got {type(inpt)} instead." | ||
| ) | ||
|
|
||
|
|
||
| @register_kernel(resize, datapoints.Image) | ||
pmeier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def resize_image_tensor( | ||
| image: torch.Tensor, | ||
| size: List[int], | ||
|
|
@@ -274,6 +300,11 @@ def resize_mask(mask: torch.Tensor, size: List[int], max_size: Optional[int] = N | |
| return output | ||
|
|
||
|
|
||
| @register_kernel(resize, datapoints.Mask) | ||
| def _resize_mask_dispatch(mask: torch.Tensor, size: List[int], max_size: Optional[int] = None, **kwargs): | ||
| return resize_mask(mask, size, max_size=max_size) | ||
pmeier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def resize_bounding_box( | ||
| bounding_box: torch.Tensor, spatial_size: Tuple[int, int], size: List[int], max_size: Optional[int] = None | ||
| ) -> Tuple[torch.Tensor, Tuple[int, int]]: | ||
|
|
@@ -292,6 +323,17 @@ def resize_bounding_box( | |
| ) | ||
|
|
||
|
|
||
| @register_kernel(resize, datapoints.BoundingBox, datapoint_wrapping=False) | ||
| def _resize_bounding_box_dispatch( | ||
| bounding_box: datapoints.BoundingBox, size: List[int], max_size: Optional[int] = None, **kwargs | ||
| ): | ||
pmeier marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| output, spatial_size = resize_bounding_box( | ||
| bounding_box.as_subclass(torch.Tensor), bounding_box.spatial_size, size, max_size=max_size | ||
| ) | ||
| return datapoints.BoundingBox.wrap_like(bounding_box, output, spatial_size=spatial_size) | ||
|
|
||
|
|
||
| @register_kernel(resize, datapoints.Video) | ||
| def resize_video( | ||
| video: torch.Tensor, | ||
| size: List[int], | ||
|
|
@@ -302,30 +344,6 @@ def resize_video( | |
| return resize_image_tensor(video, size=size, interpolation=interpolation, max_size=max_size, antialias=antialias) | ||
|
|
||
|
|
||
| def resize( | ||
| inpt: datapoints._InputTypeJIT, | ||
| size: List[int], | ||
| interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR, | ||
| max_size: Optional[int] = None, | ||
| antialias: Optional[Union[str, bool]] = "warn", | ||
| ) -> datapoints._InputTypeJIT: | ||
| if not torch.jit.is_scripting(): | ||
| _log_api_usage_once(resize) | ||
| if torch.jit.is_scripting() or is_simple_tensor(inpt): | ||
| return resize_image_tensor(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias) | ||
| elif isinstance(inpt, datapoints._datapoint.Datapoint): | ||
| return inpt.resize(size, interpolation=interpolation, max_size=max_size, antialias=antialias) | ||
| elif isinstance(inpt, PIL.Image.Image): | ||
| if antialias is False: | ||
| warnings.warn("Anti-alias option is always applied for PIL Image input. Argument antialias is ignored.") | ||
| return resize_image_pil(inpt, size, interpolation=interpolation, max_size=max_size) | ||
| else: | ||
| raise TypeError( | ||
| f"Input can either be a plain tensor, any TorchVision datapoint, or a PIL image, " | ||
| f"but got {type(inpt)} instead." | ||
| ) | ||
|
|
||
|
|
||
| def _affine_parse_args( | ||
| angle: Union[int, float], | ||
| translate: List[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
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.
Uh oh!
There was an error while loading. Please reload this page.