Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix/randaugment 4655 (#4677)

NiteeshGowda729d45bd145338
diff --git a/changelog.d/4677.fixed.md b/changelog.d/4677.fixed.md
new file mode 100644
index 000000000..a77f6a9df
--- /dev/null
+++ b/changelog.d/4677.fixed.md
@@ -0,0 +1 @@
+Fix RandAugment posterize and translation magnitudes. Posterize now uses the 4–8 bit range, preventing low magnitudes from producing black images, and translation magnitudes are converted from image-relative fractions to pixel values before being applied.
diff --git a/kornia/augmentation/auto/rand_augment/rand_augment.py b/kornia/augmentation/auto/rand_augment/rand_augment.py
index 14642c27f..6766b6325 100644
--- a/kornia/augmentation/auto/rand_augment/rand_augment.py
+++ b/kornia/augmentation/auto/rand_augment/rand_augment.py
@@ -32,7 +32,7 @@ default_policy: List[SUBPOLICY_CONFIG] = [
[("equalize", 0, 1)],
[("invert", 0, 1)],
[("rotate", -30.0, 30.0)],
- [("posterize", 0.0, 4)],
+ [("posterize", 4.0, 8.0)],
[("solarize", 0.0, 1.0)],
[("solarize_add", 0.0, 0.43)],
[("color", 0.1, 1.9)],
@@ -181,7 +181,19 @@ class RandAugment(PolicyAugmentBase):
mag = None
if op.magnitude_range is not None:
minval, maxval = op.magnitude_range
- mag = m * float(maxval - minval) + minval
+ if op._factor_name == "bits_factor":
+ mag = (1 - m) * float(maxval - minval) + minval
+ else:
+ mag = m * float(maxval - minval) + minval
+
+ # RandAugment magnitudes for translation are specified as fractions
+ # of the image dimensions, while RandomTranslate stores pixel
+ # translations in its sampled parameters.
+ if op._factor_name == "translate_x":
+ mag = mag * batch_shape[-1]
+ elif op._factor_name == "translate_y":
+ mag = mag * batch_shape[-2]
+
mod_param = op.forward_parameters(batch_shape, mag=mag)
# Compose it
param = ParamItem(name, [ParamItem(next(iter(module.named_children()))[0], mod_param)])
diff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.py
index 9e9e06f9d..2c8b83ea5 100644
--- a/tests/augmentation/test_auto_operation.py
+++ b/tests/augmentation/test_auto_operation.py
@@ -111,6 +111,44 @@ class TestAutoAugment(BaseTester):
class TestRandAugment(BaseTester):
+ @pytest.mark.parametrize(
+ ("factor", "policy"),
+ [
+ ("translate_x", [[("translate_x", -0.5, 0.5)]]),
+ ("translate_y", [[("translate_y", -0.5, 0.5)]]),
+ ],
+ )
+ def test_translation_magnitude_is_scaled_to_pixels(self, factor, policy):
+ aug = RandAugment(n=1, m=29, policy=policy)
+ batch_shape = torch.Size([4, 3, 24, 40])
+ params = aug.forward_parameters(batch_shape)
+
+ magnitude = params[0].data[0].data[factor]
+ dimension = 40 if factor == "translate_x" else 24
+ expected = torch.full_like(magnitude, (29 / 30) * 0.5 * dimension)
+
+ assert torch.allclose(magnitude.abs(), expected)
+
+ @pytest.mark.parametrize(
+ ("m", "expected_bits"),
+ [
+ (3, 7),
+ (7, 7),
+ (8, 6),
+ (29, 4),
+ ],
+ )
+ def test_posterize_default_range(self, m, expected_bits):
+ posterize_policy = next(policy for policy in randaug_config if policy[0][0] == "posterize")
+ aug = RandAugment(n=1, m=m, policy=[posterize_policy])
+ batch_shape = torch.Size([4, 3, 32, 32])
+ params = aug.forward_parameters(batch_shape)
+
+ bits = params[0].data[0].data["bits_factor"]
+ expected = torch.full_like(bits, expected_bits)
+
+ assert torch.equal(bits, expected)
+
@pytest.mark.parametrize("policy", [None, [[("translate_y", -0.5, 0.5)]]])
def test_smoke(self, policy):
if policy is None: