Skip to content

fix(augment): accept a scalar or a pair for Kornia range params - #1255

Open
adhavan18 wants to merge 2 commits into
roboflow:developfrom
adhavan18:fix/kornia-range-params-scalar-or-pair
Open

fix(augment): accept a scalar or a pair for Kornia range params#1255
adhavan18 wants to merge 2 commits into
roboflow:developfrom
adhavan18:fix/kornia-range-params-scalar-or-pair

Conversation

@adhavan18

Copy link
Copy Markdown
Contributor

Description

Three Kornia builders assume a range parameter is always a (min, max) pair, so a config that is valid on the Albumentations path raises a bare TypeError from inside the builder:

GaussianBlur sigma=1.5        -> TypeError: object of type 'float' has no len()
GaussianBlur blur_limit=(3,7) -> TypeError: unsupported operand type(s) for %: 'tuple' and 'int'
GaussNoise   std_range=0.05   -> TypeError: 'float' object is not subscriptable

All three build fine through AlbumentationsWrapper, which is where the asymmetry bites: "cpu"/"auto" resolves to Kornia when it is installed and CUDA is available, so the same aug_config trains on one machine and crashes on another.

Both forms are ordinary. Albumentations' own GaussianBlur takes blur_limit as a scalar by default and sigma_limit as a pair, and GaussNoise.std_range is a pair, so a user moving a config across, or writing one from the Albumentations docs, can land on either shape.

_make_rotate in the same module already accepts a scalar or a pair for limit, so this brings the other builders in line rather than introducing a new convention:

limit = params.get("limit", 15)
degrees = tuple(limit) if isinstance(limit, (list, tuple)) else (-limit, limit)

This adds a small _as_range helper used by sigma and std_range. blur_limit takes the upper bound of a pair, since Kornia takes a single kernel size; that is the same direction the GaussNoise builder already resolves its range, and it keeps the existing odd-number rounding.

No behaviour change for any config that works today, including all four shipped presets.

Testing

Five tests in tests/datasets/test_kornia_transforms.py, CPU-only like the rest of that module:

  • test_scalar_or_pair_range_params_build (parametrised over the three shapes above)
  • test_blur_limit_pair_uses_upper_bound: (3, 7) resolves to kernel_size == (7, 7)
  • test_scalar_std_range_is_used_verbatim: 0.05 is used as-is rather than indexed into

All five fail on a700efc with the TypeErrors above and pass here.

$ pytest tests/datasets/test_kornia_transforms.py tests/datasets/test_augmentations.py
217 passed

$ ruff check src/rfdetr/datasets/kornia_transforms.py tests/datasets/test_kornia_transforms.py
All checks passed!

$ ruff format --check ...
2 files already formatted

Ran on Windows, CPU only, kornia 0.8.3 / albumentations 2.0.8 / torch 2.13.0+cpu.

Related: #1252 tracks the wider gap between the documented transform list and what the Kornia registry accepts. This PR is about parameter shapes on the eight that are already supported, so it stands on its own.

Albumentations accepts either form for these, and _make_rotate already
treats 'limit' the same way, but three builders indexed or took len() of
the value directly. A config that is valid on the CPU path raised a bare
TypeError from inside the Kornia builder:

  GaussianBlur sigma=1.5        -> TypeError: object of type 'float' has no len()
  GaussianBlur blur_limit=(3,7) -> TypeError: unsupported operand type(s) for %
  GaussNoise   std_range=0.05   -> TypeError: 'float' object is not subscriptable

Add a shared _as_range helper and use it for 'sigma' and 'std_range'.
'blur_limit' takes the upper bound of a pair, since Kornia takes a single
kernel size, matching how the GaussNoise builder already resolves its
range.
@adhavan18
adhavan18 force-pushed the fix/kornia-range-params-scalar-or-pair branch from 3ee2199 to d6b10dc Compare July 30, 2026 15:14
@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84%. Comparing base (c3294a5) to head (3a23870).

❌ Your project check has failed because the head coverage (84%) is below the target coverage (95%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #1255   +/-   ##
=======================================
- Coverage       84%     84%   -0%     
=======================================
  Files          108     108           
  Lines        13453   13461    +8     
=======================================
+ Hits         11274   11280    +6     
- Misses        2179    2181    +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a review from Copilot July 30, 2026 17:47
@Borda Borda added the bug Something isn't working label Jul 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a backend parity issue in RF-DETR’s GPU (Kornia) augmentation builders where certain “range” parameters (sigma, std_range, blur_limit) previously assumed a (min, max) pair and could crash when given a scalar value that is valid on the Albumentations (CPU) path. The change improves config portability across environments where "auto" may resolve to different backends.

Changes:

  • Added an internal _as_range helper to normalize scalar-or-pair inputs into a (min, max) tuple for Kornia builders.
  • Updated the Kornia GaussianBlur builder to accept scalar-or-pair sigma and to accept pair blur_limit by using the upper bound.
  • Added tests ensuring scalar and pair forms build successfully and that blur_limit pair behavior is deterministic.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/rfdetr/datasets/kornia_transforms.py Adds _as_range and updates Kornia builders to accept scalar-or-pair range params (improves CPU/GPU backend config symmetry).
tests/datasets/test_kornia_transforms.py Adds regression tests covering scalar/pair range parameters and blur_limit pair semantics.

Comment on lines +276 to +280
if isinstance(value, (list, tuple)):
if len(value) == 1:
return (float(value[0]), float(value[0]))
return (float(value[0]), float(value[1]))
return (float(value), float(value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants