Skip to content

[Torchvision API] resized_crop and RandomResizedCrop#6369

Open
mdabek-nvidia wants to merge 11 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_resizedcrop
Open

[Torchvision API] resized_crop and RandomResizedCrop#6369
mdabek-nvidia wants to merge 11 commits into
NVIDIA:mainfrom
mdabek-nvidia:torchvision_resizedcrop

Conversation

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator

Category:

New feature

Description:

Torchvision API operators:

  • RandomResizedCrop
  • Functional resized_crop functional
  • Unification of InterpolationMode validation

Additional information:

Affected modules and functionalities:

Key points relevant for the review:

Tests:

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Checklist

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: N/A

@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

@greptileai review please

Comment thread dali/test/python/torchvision/test_tv_randomresizedcrop.py Dismissed
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 25, 2026

Greptile Summary

This PR adds two new torchvision-API-compatible operators to the DALI experimental torchvision layer: the resized_crop functional (crop + resize in one call) and the RandomResizedCrop transform class. It also unifies PIL integer interpolation-code handling via a shared Resize.normalize_interpolation classmethod, and tightens size validation in _ValidateSize.

  • resized_crop (functional): crops via the refactored _crop helper, validates dimensions through RandomCrop.verify_args and Resize.verify_args, then calls ndd.resize with aspect-ratio-aware target sizing via calculate_target_size_dynamic_mode.
  • RandomResizedCrop (class): delegates directly to fn.random_resized_crop with new validation classes for scale/ratio ranges and interpolation, keeping the implementation thin.
  • Both new operators and the updated test_tv_crop.py now use assert_raises(glob=...) patterns throughout.

Confidence Score: 5/5

Safe to merge; the new operators correctly implement the torchvision contract and delegate to well-tested DALI primitives.

The core crop-then-resize pipeline in resized_crop is straightforward and tested pixel-level against torchvision reference output. RandomResizedCrop is a thin wrapper over fn.random_resized_crop with clean validation. The only finding is a dead helper function that was defined but never wired in, which has no runtime impact.

crop.py — contains the unreachable _verify_crop_coordinate helper that should be removed.

Important Files Changed

Filename Overview
dali/python/nvidia/dali/experimental/torchvision/v2/functional/crop.py Adds resized_crop functional: extracts _crop helper, validates via RandomCrop.verify_args + Resize.verify_args, then calls ndd.resize on the cropped result. One unused helper function _verify_crop_coordinate was added but is never called.
dali/python/nvidia/dali/experimental/torchvision/v2/randomcrop.py Adds RandomResizedCrop operator delegating to fn.random_resized_crop. Validation classes for scale/ratio and interpolation are well-structured and consistent with existing patterns.
dali/python/nvidia/dali/experimental/torchvision/v2/resize.py Adds normalize_interpolation classmethod for PIL int-code to InterpolationMode conversion, int_to_interpolation_mode map, and additional size positivity/length checks in _ValidateSize. Clean changes used by both new operators.
dali/test/python/torchvision/test_tv_randomresizedcrop.py New test file covering shape, correctness vs torchvision (identity crop), interpolation modes, antialias, and all validation error paths with assert_raises(glob=...) patterns.
dali/test/python/torchvision/test_tv_resized_crop.py New test file with comprehensive pixel-level comparison against torchvision for tensor inputs and shape-level comparison for PIL inputs; covers crop positions, padding, interpolation modes, antialias, and all validation paths.
dali/test/python/torchvision/test_tv_crop.py Updated existing crop tests to use assert_raises(glob=...) patterns for all error-path assertions, satisfying the test-edge-cases rule.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["resized_crop(inpt, top, left, height, width, size, ...)"] --> B["@adjust_input\n(PIL ↔ DALI tensor)"]
    B --> C["_validate_crop_params\n(tensor: _validate_integer_param\nPIL: _round_pil_box)"]
    C --> D["RandomCrop.verify_args\n(size=(height,width))"]
    D --> E["Resize.normalize_interpolation\n(int → InterpolationMode)"]
    E --> F["Resize.verify_args\n(size, interpolation)"]
    F --> G["_crop(inpt, top, left, h, w)\nndd.slice with out_of_bounds pad"]
    G --> H["Resize.calculate_target_size_dynamic_mode\n(height,width) + size_normalized"]
    H --> I["ndd.resize(cropped, size=(target_h,target_w))"]

    J["RandomResizedCrop.__init__(size, scale, ratio, ...)"] --> K["Resize.normalize_interpolation"]
    K --> L["Operator.__init__ → arg_rules:\n_ValidateSizeDescriptor\n_ValidateCropSize\n_ValidateRandomResizedCropScaleRatio\n_ValidateRandomResizedCropInterpolation"]
    L --> M["_kernel → fn.random_resized_crop\n(size, random_area=scale,\nrandom_aspect_ratio=ratio)"]
Loading

Reviews (4): Last reviewed commit: "Flake8 fixes - rebase artifacts" | Re-trigger Greptile

Comment thread dali/python/nvidia/dali/experimental/torchvision/v2/randomcrop.py
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_resizedcrop branch 3 times, most recently from 7075ce9 to dc208ff Compare May 27, 2026 15:02
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_resizedcrop branch from a7434df to 0d9eef9 Compare May 29, 2026 09:14
@mdabek-nvidia mdabek-nvidia marked this pull request as ready for review May 29, 2026 09:15
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
* Different index support for tensor and PILImages

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
* RandomResizedCrop
* resized_crop functional API
* InterpoltionMode validation and unification

Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia mdabek-nvidia force-pushed the torchvision_resizedcrop branch from 0d9eef9 to a1722b9 Compare May 29, 2026 09:55
Signed-off-by: Marek Dabek <mdabek@nvidia.com>
@mdabek-nvidia
Copy link
Copy Markdown
Collaborator Author

@greptileai re-review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants