Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): reject duplicate ColorJitter order indices (#4760)

NiteeshGowda7242296680518c
diff --git a/changelog.d/4760.breaking.md b/changelog.d/4760.breaking.md
new file mode 100644
index 000000000..e6f493960
--- /dev/null
+++ b/changelog.d/4760.breaking.md
@@ -0,0 +1,3 @@
+`ColorJitter(order=...)` and `ColorJiggle(order=...)` now raise `ValueError` for a repeated index. They used to
+accept it and apply that adjustment once per occurrence with the same drawn factor, so `order=(0, 0, 1)` applied
+brightness twice. (#4719)
diff --git a/kornia/augmentation/_2d/intensity/color_jiggle.py b/kornia/augmentation/_2d/intensity/color_jiggle.py
index 3e82fcd29..f03319a46 100644
--- a/kornia/augmentation/_2d/intensity/color_jiggle.py
+++ b/kornia/augmentation/_2d/intensity/color_jiggle.py
@@ -88,9 +88,10 @@ class ColorJiggle(IntensityAugmentationBase2D):
keepdim: whether to keep the output shape the same as input (True) or broadcast it
to the batch form (False).
order: a fixed application order, as indices into (brightness, contrast, saturation, hue); a subset
- applies only those. ``None`` (the default) draws a random order on every call. A fixed order makes
- the transform ``torch.compile`` fullgraph-safe for RGB inputs. The parameter generator still draws
- an ``order`` entry into ``_params``, and with a fixed order that entry is ignored, including on replay.
+ applies only those, and a repeated index raises ``ValueError``. ``None`` (the default) draws a random
+ order on every call. A fixed order makes the transform ``torch.compile`` fullgraph-safe for RGB inputs.
+ The parameter generator still draws an ``order`` entry into ``_params``, and with a fixed order that
+ entry is ignored, including on replay.
Shape:
- Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`, Optional: :math:`(B, 3, 3)`
- Output: :math:`(B, C, H, W)`
@@ -195,6 +196,8 @@ class ColorJiggle(IntensityAugmentationBase2D):
raise ValueError(
f"`order` entries must be in 0..3 (brightness, contrast, saturation, hue). Got {order}"
)
+ if len(order) != len(set(order)):
+ raise ValueError(f"`order` must not repeat an index; each adjustment applies at most once. Got {order}")
self._fixed_order: Optional[Tuple[int, ...]] = order
# torch.cond raises where Dynamo is unavailable (torch 2.5.1 on Python 3.13), so a fixed order keeps
# the Python dispatch there. Checked here because Dynamo cannot trace the check inside forward.
diff --git a/kornia/augmentation/_2d/intensity/color_jitter.py b/kornia/augmentation/_2d/intensity/color_jitter.py
index 67a505234..51394598f 100644
--- a/kornia/augmentation/_2d/intensity/color_jitter.py
+++ b/kornia/augmentation/_2d/intensity/color_jitter.py
@@ -52,9 +52,10 @@ class ColorJitter(IntensityAugmentationBase2D):
keepdim: whether to keep the output shape the same as input (True) or broadcast it
to the batch form (False).
order: a fixed application order, as indices into (brightness, contrast, saturation, hue); a subset
- applies only those. ``None`` (the default) draws a random order on every call. A fixed order makes
- the transform ``torch.compile`` fullgraph-safe. The parameter generator still draws an ``order``
- entry into ``_params``, and with a fixed order that entry is ignored, including on replay.
+ applies only those, and a repeated index raises ``ValueError``. ``None`` (the default) draws a random
+ order on every call. A fixed order makes the transform ``torch.compile`` fullgraph-safe. The parameter
+ generator still draws an ``order`` entry into ``_params``, and with a fixed order that entry is ignored,
+ including on replay.
Shape:
- Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`, Optional: :math:`(B, 3, 3)`
- Output: :math:`(B, C, H, W)`
@@ -156,6 +157,8 @@ class ColorJitter(IntensityAugmentationBase2D):
raise ValueError(
f"`order` entries must be in 0..3 (brightness, contrast, saturation, hue). Got {order}"
)
+ if len(order) != len(set(order)):
+ raise ValueError(f"`order` must not repeat an index; each adjustment applies at most once. Got {order}")
self._fixed_order: Optional[Tuple[int, ...]] = order
# native functions
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 35da0506f..9f418a498 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -1347,6 +1347,8 @@ class TestColorJiggle(BaseTester):
assert torch.equal(sampled(image, params=params), expected)
with pytest.raises(ValueError, match=r"entries must be in 0\.\.3"):
ColorJiggle(order=(0, 1, 9))
+ with pytest.raises(ValueError, match="must not repeat an index"):
+ ColorJiggle(order=(0, 0, 1))
@pytest.mark.device_agnostic
def test_fixed_order_keeps_distribution_validation(self):
@@ -1803,8 +1805,10 @@ class TestColorJitter(BaseTester):
img = torch.rand(2, 3, 8, 8, device=device, dtype=dtype)
out = ColorJitter(0.2, 0.2, 0.2, 0.1, p=1.0, order=(0, 1, 2, 3))(img)
assert out.shape == img.shape
- with pytest.raises(ValueError):
+ with pytest.raises(ValueError, match=r"entries must be in 0\.\.3"):
ColorJitter(0.2, 0.2, 0.2, 0.1, order=(0, 1, 9))
+ with pytest.raises(ValueError, match="must not repeat an index"):
+ ColorJitter(0.2, 0.2, 0.2, 0.1, order=(0, 0, 1))
def test_dynamo_fixed_order(self, device, dtype, torch_optimizer):
# A fixed `order` avoids iterating the random order tensor, so it is fullgraph-safe.