fix(augment): accept a scalar or a pair for Kornia range params - #1255
fix(augment): accept a scalar or a pair for Kornia range params#1255adhavan18 wants to merge 2 commits into
Conversation
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.
3ee2199 to
d6b10dc
Compare
Codecov Report❌ Patch coverage is ❌ 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:
|
There was a problem hiding this comment.
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_rangehelper to normalize scalar-or-pair inputs into a(min, max)tuple for Kornia builders. - Updated the Kornia
GaussianBlurbuilder to accept scalar-or-pairsigmaand to accept pairblur_limitby using the upper bound. - Added tests ensuring scalar and pair forms build successfully and that
blur_limitpair 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. |
| 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)) |
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 bareTypeErrorfrom inside the builder: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 sameaug_configtrains on one machine and crashes on another.Both forms are ordinary. Albumentations' own
GaussianBlurtakesblur_limitas a scalar by default andsigma_limitas a pair, andGaussNoise.std_rangeis a pair, so a user moving a config across, or writing one from the Albumentations docs, can land on either shape._make_rotatein the same module already accepts a scalar or a pair forlimit, so this brings the other builders in line rather than introducing a new convention:This adds a small
_as_rangehelper used bysigmaandstd_range.blur_limittakes the upper bound of a pair, since Kornia takes a single kernel size; that is the same direction theGaussNoisebuilder 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 tokernel_size == (7, 7)test_scalar_std_range_is_used_verbatim:0.05is used as-is rather than indexed intoAll five fail on
a700efcwith theTypeErrors above and pass here.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.