π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): keep 4 to 8 bits in TrivialAugment's default posterize (#6247)
Saksham Sharma6966f9cfb108
diff --git a/changelog.d/6247.fixed.md b/changelog.d/6247.fixed.mdnew file mode 100644index 000000000..4f7a67d83--- /dev/null+++ b/changelog.d/6247.fixed.md@@ -0,0 +1 @@+The default `TrivialAugment` policy now posterizes to 4 to 8 bits, the same as `RandAugment` and `AutoAugment`. It used to draw from 0 to 4 bits, so one posterize draw in 8 returned an entirely black image, about 1% of all outputs. A policy passed explicitly is unchanged.diff --git a/kornia/augmentation/auto/trivial_augment/trivial_augment.py b/kornia/augmentation/auto/trivial_augment/trivial_augment.pyindex a674bee43..49bfcac1b 100644--- a/kornia/augmentation/auto/trivial_augment/trivial_augment.py+++ b/kornia/augmentation/auto/trivial_augment/trivial_augment.py@@ -31,7 +31,8 @@ default_policy: List[SUBPOLICY_CONFIG] = [[("auto_contrast", 0, 1)],[("equalize", 0, 1)],[("rotate", -30.0, 30.0)],- [("posterize", 0.0, 4)],+ # Bits kept, as in RandAugment and AutoAugment.+ [("posterize", 4.0, 8.0)],[("solarize", 0.0, 1.0)],# (Color, 0.1, 1.9),[("contrast", 0.1, 1.9)],@@ -58,6 +59,8 @@ class TrivialAugment(PolicyAugmentBase):- the operation wrapper's magnitude mapping applies, as under:class:`~kornia.augmentation.auto.RandAugment`. A ``("rotate", -30, 30)`` candidate draws angles in``[-30, 30]`` with a random sign, and ``("shear_x", -0.3, 0.3)`` shears by up to ``0.3 * 180`` degrees.+ - the default ``posterize`` candidate keeps ``4`` to ``8`` bits, the range of the paper's RA+ augmentation space (Table 8) and of :class:`~kornia.augmentation.auto.RandAugment`.Args:policy: candidate transformations. If None, a default candidate list will be used.diff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.pyindex cc69401e6..72ca5929d 100644--- a/tests/augmentation/test_auto_operation.py+++ b/tests/augmentation/test_auto_operation.py@@ -27,6 +27,7 @@ from kornia.augmentation.auto.operations import OperationBase, opsfrom kornia.augmentation.auto.rand_augment.rand_augment import RandAugmentfrom kornia.augmentation.auto.rand_augment.rand_augment import default_policy as randaug_configfrom kornia.augmentation.auto.trivial_augment import TrivialAugment+from kornia.augmentation.auto.trivial_augment.trivial_augment import default_policy as trivial_configfrom kornia.augmentation.container import AugmentationSequentialfrom kornia.geometry.bbox import bbox_to_mask@@ -264,6 +265,29 @@ class TestTrivialAugment(BaseTester):def test_sequential(augment_method, device, dtype):_test_sequential(TrivialAugment(), device=device, dtype=dtype)+ @pytest.mark.device_agnostic+ def test_default_posterize_keeps_four_to_eight_bits_6246(self):+ # #6246: the default candidate was (0, 4), so one posterize draw in 8 kept 0 bits, a black image, and one+ # in 4 kept 1 bit. It now keeps 4 to 8 bits, as RandAugment and AutoAugment do.+ candidate = next(policy for policy in trivial_config if policy[0][0] == "posterize")+ aug = TrivialAugment(policy=[candidate])+ with torch.random.fork_rng():+ torch.manual_seed(0)+ bits = torch.cat(+ [aug.forward_parameters(torch.Size([64, 3, 4, 4]))[0].data[0].data["bits_factor"] for _ in range(8)]+ )+ assert bits.min() == 4+ assert bits.max() == 8++ def test_default_posterize_never_blacks_out_an_image_6246(self, device, dtype):+ candidate = next(policy for policy in trivial_config if policy[0][0] == "posterize")+ aug = TrivialAugment(policy=[candidate])+ image = torch.linspace(0.05, 0.95, 64 * 3 * 4 * 4, device=device, dtype=dtype).view(64, 3, 4, 4)+ with torch.random.fork_rng(devices=[device] if device.type == "cuda" else []):+ torch.manual_seed(0)+ for _ in range(8):+ assert (aug(image).flatten(1).amax(1) > 0).all()+@pytest.mark.parametrize("low_dtype", [torch.float16, torch.bfloat16])def test_operation_preserves_input_dtype(device, low_dtype):