-
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
Draft
mdabek-nvidia
wants to merge
10
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.
Draft
Changes from all commits
Commits
Show all changes
10 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 15c1775
Image information Torchvision's functional API
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
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
| # 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 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 _get_crop_axes(inpt: TensorLike | ndd.Batch) -> list[int]: | ||
| layout = inpt.layout[-3:] | ||
| if layout == "HWC": | ||
| return [-3, -2] | ||
| if layout == "CHW": | ||
| return [-2, -1] | ||
| if inpt.layout[-2:] == "HW": | ||
| return [-2, -1] | ||
| raise ValueError(f"Unsupported layout: {inpt.layout!r}. Expected one of HWC, CHW, HW.") | ||
|
|
||
|
|
||
| def _verify_crop_coordinate(value, name: str) -> None: | ||
| if not isinstance(value, int): | ||
| raise TypeError(f"{name} must be int, got {type(value)}") | ||
|
|
||
|
|
||
| @adjust_input | ||
| def crop( | ||
| inpt: TensorLike | ndd.Batch, | ||
| top: int, | ||
| left: int, | ||
| height: int, | ||
| width: int, | ||
| device: DeviceLike = "cpu", | ||
| ) -> ndd.Tensor | ndd.Batch: | ||
| """ | ||
| Please refer to the ``RandomCrop`` operator for more details. | ||
| """ | ||
| _verify_crop_coordinate(top, "top") | ||
| _verify_crop_coordinate(left, "left") | ||
| RandomCrop.verify_args( | ||
| size=(height, width), | ||
| padding=None, | ||
| pad_if_needed=False, | ||
| padding_mode="constant", | ||
| fill=0, | ||
| ) | ||
|
|
||
| return ndd.slice( | ||
| inpt, | ||
| (top, left), | ||
| (height, width), | ||
| axes=_get_crop_axes(inpt), | ||
| 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: | ||||||||||||||||||||||
|
Comment on lines
+75
to
+79
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.
Same sentence-ending convention issue in
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||
| 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.
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.
Both error messages are missing a trailing period, violating the project convention that error messages must read as complete sentences.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!