π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(auto): symmetric magnitudes must negate, not zero (#4495)
Mohansree Vijayakumar1590df487f4d
diff --git a/changelog.d/4495.fixed.md b/changelog.d/4495.fixed.mdnew file mode 100644index 000000000..23d49ef82--- /dev/null+++ b/changelog.d/4495.fixed.md@@ -0,0 +1,8 @@+`Rotate`, `ShearX`, `ShearY`, `TranslateX` and `TranslateY` now draw negative magnitudes under+`symmetric_megnitude`, which is their default. The sampled magnitude was multiplied by a boolean+mask rather than a sign, so these ops never drew a negative magnitude: rows meant to get `-m` got+`0`, a silent no-op on roughly half of their draws. This reaches the five ops used directly and+`RandAugment`. `AutoAugment` builds them with `symmetric_megnitude=False` and is unaffected;+`TrivialAugment` samples without the sign and is also unaffected, tracked separately in #4441. For a+seeded pipeline the random stream, the ops picked and the rows that already drew `+m` are unchanged;+only the rows that drew `0` now draw `-m`. (#4440, #4494)diff --git a/kornia/augmentation/auto/operations/base.py b/kornia/augmentation/auto/operations/base.pyindex 82c75f63b..76a713ccb 100644--- a/kornia/augmentation/auto/operations/base.py+++ b/kornia/augmentation/auto/operations/base.py@@ -81,8 +81,10 @@ class OperationBase(nn.Module):def _random_flip(fn: Callable[[torch.Tensor], torch.Tensor]) -> Callable[[torch.Tensor], torch.Tensor]:def f(x: torch.Tensor) -> torch.Tensor:- flip = torch.rand((x.shape[0],), device=x.device) > 0.5- return fn(x) * flip+ # a sign, not a mask: multiplying by the bool would zero half the+ # magnitudes instead of negating them+ sign = torch.where(torch.rand((x.shape[0],), device=x.device) > 0.5, 1.0, -1.0)+ return fn(x) * sign.to(x.dtype)return fdiff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.pyindex 28d1e19dd..9e9e06f9d 100644--- a/tests/augmentation/test_auto_operation.py+++ b/tests/augmentation/test_auto_operation.py@@ -258,6 +258,30 @@ def test_operation_preserves_input_dtype(device, low_dtype):assert checked, f"no op could run in {low_dtype} on this build"+ ("op", "factor"),+ [+ (ops.Rotate, "degrees"),+ (ops.ShearX, "shear_x"),+ (ops.ShearY, "shear_y"),+ (ops.TranslateX, "translate_x"),+ (ops.TranslateY, "translate_y"),+ ],+)+def test_symmetric_magnitude_negates_rather_than_zeroing(op, factor):+ # ``symmetric_megnitude`` multiplied the magnitude by a bool mask, so half the draws+ # came out as 0 instead of -m: these five ops only ever ran in the positive direction+ # -- only counter-clockwise, only right, only down -- and were a silent no-op the rest+ # of the time. They are the only ops that set the flag, and ``RandAugment`` is the only+ # composer that samples through it. Sampling is device-independent, so this runs on CPU.+ operation = op(None, 1.0)+ torch.manual_seed(42)+ mags = torch.cat([operation.forward_parameters(torch.Size([64, 3, 8, 8]))[factor] for _ in range(8)])+ assert (mags < 0).any(), f"{factor}: no negative magnitude in {mags.numel()} draws"+ assert (mags > 0).any(), f"{factor}: no positive magnitude in {mags.numel()} draws"+ assert not (mags == 0).any(), f"{factor}: {(mags == 0).sum()} of {mags.numel()} draws were zeroed"++# AutoAugment sub-policies are ``(name, probability, magnitude_bin)``; the other two are# ``(name, min_magnitude, max_magnitude)``. RandAugment and TrivialAugment allow only one# operation per sub-policy, so a mixed draw has to come from two of them -- and cannot