π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix/mixup cutmix label dtype (#4661)
NiteeshGowda721a16f0551d2f
diff --git a/changelog.d/4661.fixed.md b/changelog.d/4661.fixed.mdnew file mode 100644index 000000000..13a7517ac--- /dev/null+++ b/changelog.d/4661.fixed.md@@ -0,0 +1 @@+RandomMixUpV2 and RandomCutMixV2 preserve class-label precision when the input image uses float16 or bfloat16.diff --git a/kornia/augmentation/_2d/mix/cutmix.py b/kornia/augmentation/_2d/mix/cutmix.pyindex ec3078314..cdbe8c479 100644--- a/kornia/augmentation/_2d/mix/cutmix.py+++ b/kornia/augmentation/_2d/mix/cutmix.py@@ -121,7 +121,8 @@ class RandomCutMixV2(MixAugmentationBaseV2):height, width = params["image_shape"]out_labels = []- calc_dtype = input.dtype if input.dtype in (torch.float32, torch.float64) else torch.float32+ image_dtype = DType.to_torch(int(params["dtype"].item()))+ calc_dtype = image_dtype if image_dtype in (torch.float32, torch.float64) else torch.float32for pair, crop in zip(params["mix_pairs"], params["crop_src"]):labels_permute = input.index_select(dim=0, index=pair.to(input.device))w, h = infer_bbox_shape(crop)@@ -132,9 +133,9 @@ class RandomCutMixV2(MixAugmentationBaseV2):out_labels.append(torch.stack([- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- labels_permute.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- lam.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),+ input.to(device=input.device, dtype=calc_dtype),+ labels_permute.to(device=input.device, dtype=calc_dtype),+ lam.to(device=input.device, dtype=calc_dtype),],1,)@@ -146,13 +147,15 @@ class RandomCutMixV2(MixAugmentationBaseV2):self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Optional[Dict[str, Any]] = None) -> torch.Tensor:out_labels = []- lam = torch.zeros((len(input)), device=input.device, dtype=DType.to_torch(int(params["dtype"].item())))+ image_dtype = DType.to_torch(int(params["dtype"].item()))+ calc_dtype = image_dtype if image_dtype in (torch.float32, torch.float64) else torch.float32+ lam = torch.zeros((len(input)), device=input.device, dtype=calc_dtype)for _ in range(self._param_generator.num_mix):out_labels.append(torch.stack([- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),+ input.to(device=input.device, dtype=calc_dtype),+ input.to(device=input.device, dtype=calc_dtype),lam,],1,diff --git a/kornia/augmentation/_2d/mix/mixup.py b/kornia/augmentation/_2d/mix/mixup.pyindex d44be0a43..e4dc61d4c 100644--- a/kornia/augmentation/_2d/mix/mixup.py+++ b/kornia/augmentation/_2d/mix/mixup.py@@ -117,11 +117,13 @@ class RandomMixUpV2(MixAugmentationBaseV2):def apply_non_transform_class(self, input: torch.Tensor, params: Dict[str, torch.Tensor], maybe_flags: Optional[Dict[str, Any]] = None) -> torch.Tensor:+ image_dtype = DType.to_torch(int(params["dtype"].item()))+ calc_dtype = image_dtype if image_dtype in (torch.float32, torch.float64) else torch.float32out_labels = torch.stack([- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- torch.zeros((len(input),), device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),+ input.to(device=input.device, dtype=calc_dtype),+ input.to(device=input.device, dtype=calc_dtype),+ torch.zeros((len(input),), device=input.device, dtype=calc_dtype),],-1,)@@ -132,11 +134,13 @@ class RandomMixUpV2(MixAugmentationBaseV2):) -> torch.Tensor:labels_permute = input.index_select(dim=0, index=params["mixup_pairs"].to(input.device))+ image_dtype = DType.to_torch(int(params["dtype"].item()))+ calc_dtype = image_dtype if image_dtype in (torch.float32, torch.float64) else torch.float32out_labels = torch.stack([- input.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- labels_permute.to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),- params["mixup_lambdas"].to(device=input.device, dtype=DType.to_torch(int(params["dtype"].item()))),+ input.to(device=input.device, dtype=calc_dtype),+ labels_permute.to(device=input.device, dtype=calc_dtype),+ params["mixup_lambdas"].to(device=input.device, dtype=calc_dtype),],-1,)diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.pyindex 095d1e7fd..9c4467c16 100644--- a/tests/augmentation/test_augmentation_mix.py+++ b/tests/augmentation/test_augmentation_mix.py@@ -49,6 +49,8 @@ class TestRandomMixUpV2(BaseTester):)label = torch.tensor([1, 0], device=device, dtype=dtype)lam = torch.tensor([0.1320, 0.3074], device=device, dtype=dtype)+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ expected_lambda = torch.tensor([0.1320, 0.3074], device=device, dtype=label_dtype)expected = torch.stack([@@ -65,9 +67,17 @@ class TestRandomMixUpV2(BaseTester):rtol, atol = 1e-4, 1e-4self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[:, 0], label)- self.assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))- self.assert_close(out_label[:, 2], lam, rtol=rtol, atol=atol)+ self.assert_close(out_label[:, 0], label.to(label_dtype))+ self.assert_close(+ out_label[:, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )+ self.assert_close(+ out_label[:, 2],+ expected_lambda,+ rtol=rtol,+ atol=atol,+ )def test_random_mixup_p0(self, device, dtype):torch.manual_seed(0)@@ -84,7 +94,13 @@ class TestRandomMixUpV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(+ out_label[:, 2],+ lam.to(label_dtype),+ rtol=1e-4,+ atol=1e-4,+ )def test_random_mixup_lam0(self, device, dtype):torch.manual_seed(0)@@ -101,9 +117,18 @@ class TestRandomMixUpV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[:, 0], label)- self.assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))- self.assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[:, 0], label.to(label_dtype))+ self.assert_close(+ out_label[:, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )+ self.assert_close(+ out_label[:, 2],+ lam.to(label_dtype),+ rtol=1e-4,+ atol=1e-4,+ )def test_random_mixup_same_on_batch(self, device, dtype):torch.manual_seed(0)@@ -124,9 +149,31 @@ class TestRandomMixUpV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[:, 0], label)- self.assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))- self.assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[:, 0], label.to(label_dtype))+ self.assert_close(+ out_label[:, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )+ self.assert_close(+ out_label[:, 2],+ lam.to(label_dtype),+ rtol=1e-4,+ atol=1e-4,+ )++ @pytest.mark.parametrize("image_dtype", [torch.bfloat16, torch.float16])+ def test_class_labels_preserve_precision(self, image_dtype, device):+ image = torch.rand(4, 1, 8, 8, device=device, dtype=image_dtype)+ labels = torch.tensor([257, 999, 2049, 4097], device=device)++ aug = RandomMixUpV2(p=1.0, data_keys=["input", "class"])+ _, output_labels = aug(image, labels)++ self.assert_close(+ output_labels[:, 0],+ labels.to(torch.float32),+ )@pytest.mark.parametrize("p", [0.0, 0.5])def test_partial_batch_passthrough(self, p, device, dtype):@@ -171,9 +218,13 @@ class TestRandomCutMixV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[0, :, 0], label)- self.assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))- self.assert_close(out_label[0, :, 2], torch.tensor([0.5, 0.5], device=device, dtype=dtype))+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[0, :, 0], label.to(label_dtype))+ self.assert_close(+ out_label[0, :, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )+ self.assert_close(out_label[0, :, 2], torch.tensor([0.5, 0.5], device=device, dtype=label_dtype))def test_random_mixup_p0(self, device, dtype):torch.manual_seed(76)@@ -185,7 +236,8 @@ class TestRandomCutMixV2(BaseTester):label = torch.tensor([1, 0], device=device)expected = input.clone()- exp_label = torch.tensor([[[1, 1, 0], [0, 0, 0]]], device=device, dtype=dtype)+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ exp_label = torch.tensor([[[1, 1, 0], [0, 0, 0]]], device=device, dtype=label_dtype)out_image, out_label = f(input, label)@@ -215,12 +267,16 @@ class TestRandomCutMixV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[0, :, 0], label)- self.assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[0, :, 0], label.to(label_dtype))+ self.assert_close(+ out_label[0, :, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )# cut area = 4 / 12, but with use_correct_lambda=True the lambda calculation is differentself.assert_close(out_label[0, :, 2],- torch.tensor([0.66667, 0.66667], device=device, dtype=dtype),+ torch.tensor([0.66667, 0.66667], device=device, dtype=label_dtype),rtol=1e-4,atol=1e-4,)@@ -246,9 +302,15 @@ class TestRandomCutMixV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[:, :, 0], label.view(1, -1).expand(5, 2))+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[:, :, 0], label.view(1, -1).expand(5, 2).to(label_dtype))self.assert_close(- out_label[:, :, 1], torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]], device=device, dtype=dtype)+ out_label[:, :, 1],+ torch.tensor(+ [[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]],+ device=device,+ dtype=label_dtype,+ ),)# Updated expected values for use_correct_lambda=Trueself.assert_close(@@ -256,7 +318,7 @@ class TestRandomCutMixV2(BaseTester):torch.tensor([[0.9167, 0.6667], [1.0, 0.8333], [0.5, 0.9167], [0.9167, 1.0], [0.5, 0.6667]],device=device,- dtype=dtype,+ dtype=torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype,),rtol=1e-4,atol=1e-4,@@ -283,10 +345,27 @@ class TestRandomCutMixV2(BaseTester):out_image, out_label = f(input, label)self.assert_close(out_image, expected, rtol=1e-4, atol=1e-4)- self.assert_close(out_label[0, :, 0], label)- self.assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))+ label_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else dtype+ self.assert_close(out_label[0, :, 0], label.to(label_dtype))+ self.assert_close(+ out_label[0, :, 1],+ torch.tensor([0, 1], device=device, dtype=label_dtype),+ )+ self.assert_close(+ out_label[0, :, 2], torch.tensor([0.5000, 0.5000], device=device, dtype=label_dtype), rtol=1e-4, atol=1e-4+ )++ @pytest.mark.parametrize("image_dtype", [torch.bfloat16, torch.float16])+ def test_class_labels_preserve_precision(self, image_dtype, device):+ image = torch.rand(4, 1, 8, 8, device=device, dtype=image_dtype)+ labels = torch.tensor([257, 999, 2049, 4097], device=device)++ aug = RandomCutMixV2(p=1.0, data_keys=["input", "class"], use_correct_lambda=True)+ _, output_labels = aug(image, labels)+self.assert_close(- out_label[0, :, 2], torch.tensor([0.5000, 0.5000], device=device, dtype=dtype), rtol=1e-4, atol=1e-4+ output_labels[0, :, 0],+ labels.to(torch.float32),)@pytest.mark.parametrize("p", [0.0, 0.5])