Repository navigation
[Torchvision API] Support for volumetric data - #6544
mdabek-nvidia wants to merge 14 commits into
Conversation
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
!build |
[Medium impact] The latest changes appear safe to merge, with a non-blocking mismatch between the documented and actual unsupported-layout exception. Findings
|
|
CI MESSAGE: [75843191]: BUILD STARTED |
TorchVision samples a single parameter set per call and applies it across all leading dimensions (..., C, H, W). Compose drew random values per sample instead, so images in a batch and frames of a video were transformed differently. PipelineCHW now flattens the leading dimensions into frames of FCHW sequences: - GPU: the whole call is processed as a single sample, which is also faster than per-sample processing (1.2-1.9x in RRC+flip+normalize). - CPU: frames are split into up to min(batch_size, num_threads) sequences to keep thread parallelism. Random values drawn for the first sequence are broadcast with fn.permute_batch through the new share_random_value() helper. RandomResizedCrop on CPU generates the crop window once with fn.random_crop_generator and resizes with an ROI. Normalize uses a 4D mean/std for FCHW input. batch_size no longer limits the leading dimensions of the input. Signed-off-by: Marek Dabek <mdabek@nvidia.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
CI MESSAGE: [75843191]: BUILD FAILED |
|
CI MESSAGE: [75843191]: BUILD PASSED |
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
|
!build |
|
CI MESSAGE: [76146454]: BUILD STARTED |
|
!build |
|
CI MESSAGE: [76151558]: BUILD STARTED |
|
CI MESSAGE: [76151558]: BUILD PASSED |
|
CI MESSAGE: [76146454]: BUILD PASSED |
|
@greptile-apps please review |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
|
@greptile-apps please re-review |
|
!build |
|
CI MESSAGE: [77011372]: BUILD STARTED |
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
|
!build |
|
CI MESSAGE: [77264341]: BUILD STARTED |
| elif self.sample_layout == "FCHW": | ||
| # Any leading dimensions are flattened into the frames dimension, as in torchvision | ||
| tail = 3 | ||
| supported = data_input.ndim > tail | ||
| expected = "at least 4 dimensions" | ||
| else: | ||
| raise ValueError("CHW layout is currently supported for torch.Tensor only.\ | ||
| Please check if samples have the same format.") | ||
| tail = 3 | ||
| supported = data_input.ndim == tail | ||
| expected = "exactly 3 dimensions" | ||
| if not supported: |
There was a problem hiding this comment.
| elif self.sample_layout == "FCHW": | |
| # Any leading dimensions are flattened into the frames dimension, as in torchvision | |
| tail = 3 | |
| supported = data_input.ndim > tail | |
| expected = "at least 4 dimensions" | |
| else: | |
| raise ValueError("CHW layout is currently supported for torch.Tensor only.\ | |
| Please check if samples have the same format.") | |
| tail = 3 | |
| supported = data_input.ndim == tail | |
| expected = "exactly 3 dimensions" | |
| if not supported: | |
| else: | |
| tail = 3 # we always need at least CHW | |
| # Any leading dimensions are flattened into the frames dimension, as in torchvision | |
| expected_dims = 4 if sample_layout == "FCHW" else 3 | |
| supported = data_input.ndim >= expected_dims | |
| expected = f"at least {expected_dims} dimensions" | |
| if not supported: |
There was a problem hiding this comment.
That's actually invalid fix - the supported will pass layout "CHW" and input tensor (4, 3, 8, 8,) leading to DALI throwing exception about the invalid batch size.
I've managed to simplify the code anyway.
| "NFCHW": (PipelineFCHW, True), | ||
| } | ||
| _UNSUPPORTED_LAYOUTS = ("NHWC", "FHWC", "NFHWC") | ||
| _ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NHWC", "NCHW", "FCHW", "FHWC", "NFCHW", "NFHWC") |
There was a problem hiding this comment.
As a user I wouldn't be happy if the program first showed me an "allowed" layout only to find out it's not supported.
There was a problem hiding this comment.
That's correct - I removed unsupported layouts from allowed layouts, but I am leaving the _UNSUPPORTED_LAYOUTS, for the future support.
| **dali_pipeline_kwargs, | ||
| ): | ||
| if layout not in _ALLOWED_LAYOUTS: | ||
| raise ValueError(f"Unknown layout {layout!r}. Allowed values: {_ALLOWED_LAYOUTS}.") |
There was a problem hiding this comment.
First we say a layout is allowed, but it may yet be unsupported. This is user-hostile.
There was a problem hiding this comment.
Changed the _ALLOWED_LAYOUTS to reflect what is supported.
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
| "NFCHW": (PipelineFCHW, True), | ||
| } | ||
| _UNSUPPORTED_LAYOUTS = ("NHWC", "FHWC", "NFHWC") | ||
| _ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NCHW", "FCHW", "NFCHW") |
There was a problem hiding this comment.
Unsupported layouts raise another error
[Bug] Removing NHWC, FHWC, and NFHWC from _ALLOWED_LAYOUTS makes them raise ValueError before reaching the NotImplementedError check. The input-layout guide still promises NotImplementedError, so users following that guide would catch the wrong exception. Keep the documented exception or update the guide and remove the unreachable check.
| _ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NCHW", "FCHW", "NFCHW") | |
| _ALLOWED_LAYOUTS = (None, "HWC", "CHW", "NHWC", "NCHW", "FCHW", "FHWC", "NFCHW", "NFHWC") |
|
CI MESSAGE: [77264341]: BUILD FAILED |
|
CI MESSAGE: [77264341]: BUILD PASSED |
Category:
Bug fix
Description:
Torchvision.v2 accepts arbitrary leading dimensions: (..., C,H,W) the current implementation of Torchvision API uses only CHW or NCHW. That does not allow to process other data layouts like NFCHW (e.g. video frames)
The default behavior, to match the original Torchvision is to set CHW or FCHW layouts for tensors, depending on the first input data (changing layout mid-pipeline run would cause huge performance penalty, so we are not doing that). Next, the API is extended (by explicit layout and batch size arguments) to allow passing NCHW and NFCHW.
Currently, we do infer HWC only from PIL images and they do not support batching, so we do not allow: FHWC, NHWC or NFHWC, since it is unlikely to find them in the existing SW, that currently uses Torchvision. However, it is possible that these layouts will be implemented in the future as the enhancement to the existing Torchvison capabilities.
Additional information:
Affected modules and functionalities:
Reshaping the input and recovering the input shape were added to the data layout transformation.
Key points relevant for the review:
Tests:
Checklist
Documentation
DALI team only
Requirements
REQ IDs: N/A
JIRA TASK: DALI-4891