-
Notifications
You must be signed in to change notification settings - Fork 665
[Torchvision API] Input metadata #6364
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
mdabek-nvidia
wants to merge
13
commits into
NVIDIA:main
Choose a base branch
from
mdabek-nvidia:torchvision_image_metadata
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.
+1,227
−0
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d3f473b
Torchvision API RandomApply implementation
mdabek-nvidia b0c754b
Greptile review fixes
mdabek-nvidia 57302e8
Adding 0 < p 1 tests
mdabek-nvidia a4d6209
Review fixes
mdabek-nvidia 693e9af
Torchvision API RandomCrop and crop operartors
mdabek-nvidia 518c4d1
Merge branch 'main' into torchvision_crop
mdabek-nvidia 2c7e9ef
Greptile review comments and "cpu"/"gpu" unit tests
mdabek-nvidia 08ebc42
Lint fixes
mdabek-nvidia 12dddd3
More tests
mdabek-nvidia 5d24a26
Review comments
mdabek-nvidia 645cbe6
Review fixes
mdabek-nvidia 5638ae7
Image information Torchvision's functional API
mdabek-nvidia b88990d
Review fixes
mdabek-nvidia 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
94 changes: 94 additions & 0 deletions
94
dali/python/nvidia/dali/experimental/torchvision/v2/functional/crop.py
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import operator | ||
|
|
||
| import nvidia.dali.experimental.dynamic as ndd | ||
| from nvidia.dali._typing import TensorLike | ||
| from nvidia.dali.experimental.dynamic._device import DeviceLike | ||
|
|
||
| from ..operator import adjust_input | ||
| from ..randomcrop import RandomCrop | ||
|
|
||
|
|
||
| def _validate_integer_param(value, name: str) -> int: | ||
| try: | ||
| return operator.index(value) | ||
| except TypeError as err: | ||
| raise TypeError(f"{name} must be an integer, got {type(value)}") from err | ||
|
|
||
|
|
||
| def _round_pil_box(top, left, height, width) -> tuple[int, int, int, int]: | ||
| try: | ||
| rounded_top = int(round(top)) | ||
| rounded_left = int(round(left)) | ||
| rounded_bottom = int(round(top + height)) | ||
| rounded_right = int(round(left + width)) | ||
| except TypeError as err: | ||
| raise TypeError("top, left, height, and width must be real numbers") from err | ||
|
|
||
| return ( | ||
| rounded_top, | ||
| rounded_left, | ||
| rounded_bottom - rounded_top, | ||
| rounded_right - rounded_left, | ||
| ) | ||
|
|
||
|
|
||
| def _is_pil_image_layout(inpt: TensorLike | ndd.Batch) -> bool: | ||
| return inpt.layout[-3:] == "HWC" | ||
|
|
||
|
|
||
| def _validate_crop_params(inpt, top, left, height, width) -> tuple[int, int, int, int]: | ||
| if _is_pil_image_layout(inpt): | ||
| return _round_pil_box(top, left, height, width) | ||
| return ( | ||
| _validate_integer_param(top, "top"), | ||
| _validate_integer_param(left, "left"), | ||
| _validate_integer_param(height, "height"), | ||
| _validate_integer_param(width, "width"), | ||
| ) | ||
|
|
||
|
|
||
| @adjust_input | ||
| def crop( | ||
| inpt: TensorLike | ndd.Batch, | ||
| top: int | float, | ||
| left: int | float, | ||
| height: int | float, | ||
| width: int | float, | ||
| device: DeviceLike = "cpu", | ||
| ) -> ndd.Tensor | ndd.Batch: | ||
| """ | ||
| Please refer to the ``RandomCrop`` operator for more details. | ||
| """ | ||
| top, left, height, width = _validate_crop_params(inpt, top, left, height, width) | ||
| RandomCrop.verify_args( | ||
| size=(height, width), | ||
| padding=None, | ||
| pad_if_needed=False, | ||
| padding_mode="constant", | ||
| fill=0, | ||
| ) | ||
|
|
||
| return ndd.slice( | ||
| inpt, | ||
| [float(left), float(top)], | ||
| [float(width), float(height)], | ||
| normalized_anchor=False, | ||
| normalized_shape=False, | ||
| out_of_bounds_policy="pad", | ||
| fill_values=0, | ||
| device=device, | ||
| ) |
82 changes: 82 additions & 0 deletions
82
dali/python/nvidia/dali/experimental/torchvision/v2/functional/image_metadata.py
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import List | ||
|
|
||
| from PIL import Image | ||
| import torch | ||
|
|
||
|
|
||
| def get_image_size(inpt: Image.Image | torch.Tensor) -> List[int]: | ||
| """ | ||
| Return the spatial size of an image as ``[width, height]``. | ||
|
|
||
| Mirrors ``torchvision.transforms.v2.functional.get_image_size``. | ||
|
|
||
| .. note:: | ||
| This function is provided for compatibility. The torchvision successor | ||
| ``get_size`` returns ``[height, width]`` instead. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| inpt : PIL Image or torch.Tensor | ||
| Input image. Tensors are expected in ``[…, H, W]`` layout (leading | ||
| channel / batch dimensions are ignored). | ||
|
|
||
| Returns | ||
| ------- | ||
| List[int] | ||
| ``[width, height]`` | ||
| """ | ||
| if isinstance(inpt, Image.Image): | ||
| return list(inpt.size) # PIL .size is (W, H) | ||
| elif isinstance(inpt, torch.Tensor): | ||
| if inpt.ndim < 2: | ||
| raise TypeError( | ||
| f"get_image_size requires a tensor with at least 2 dimensions, got {inpt.ndim}." | ||
| ) | ||
| return [inpt.shape[-1], inpt.shape[-2]] # [W, H] | ||
| raise TypeError(f"Unsupported input type: {type(inpt)}.") | ||
|
|
||
|
|
||
| def get_dimensions(inpt: Image.Image | torch.Tensor) -> List[int]: | ||
| """ | ||
| Return the number of channels, height, and width of an image as | ||
| ``[channels, height, width]``. | ||
|
|
||
| Mirrors ``torchvision.transforms.v2.functional.get_dimensions``. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| inpt : PIL Image or torch.Tensor | ||
| Input image. Tensors are expected in ``[H, W]`` or ``[…, C, H, W]`` layout | ||
| (leading batch dimensions are ignored). | ||
|
|
||
| Returns | ||
| ------- | ||
| List[int] | ||
| ``[channels, height, width]`` | ||
| """ | ||
| if isinstance(inpt, Image.Image): | ||
| w, h = inpt.size | ||
| return [len(inpt.getbands()), h, w] | ||
| elif isinstance(inpt, torch.Tensor): | ||
| if inpt.ndim < 2: | ||
| raise TypeError( | ||
| f"get_dimensions requires a tensor with at least 2 dimensions, got {inpt.ndim}." | ||
| ) | ||
| if inpt.ndim == 2: | ||
|
mdabek-nvidia marked this conversation as resolved.
|
||
| return [1, inpt.shape[-2], inpt.shape[-1]] | ||
| return [inpt.shape[-3], inpt.shape[-2], inpt.shape[-1]] # [C, H, W] | ||
| raise TypeError(f"Unsupported input type: {type(inpt)}.") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.