π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Feat/random median blur (#2234)
Dasha1f0b4e17d527
diff --git a/docs/generate_example_images.py b/docs/generate_example_images.pyindex 604ca8f7d..0415ee925 100644--- a/docs/generate_example_images.py+++ b/docs/generate_example_images.py@@ -104,6 +104,7 @@ def main():"RandomHue": (((-0.5, 0.5),), 2, 2022),"RandomHorizontalFlip": ((), 1, 2020),"RandomInvert": ((), 1, 2020),+ "RandomMedianBlur": (((3, 3),), 1, 2023),"RandomMotionBlur": ((7, 35.0, 0.5), 2, 2020),"RandomPerspective": ((0.2,), 2, 2020),"RandomPlanckianJitter": ((), 2, 2022),diff --git a/kornia/augmentation/_2d/intensity/__init__.py b/kornia/augmentation/_2d/intensity/__init__.pyindex 56e35ed35..17705efc2 100644--- a/kornia/augmentation/_2d/intensity/__init__.py+++ b/kornia/augmentation/_2d/intensity/__init__.py@@ -14,6 +14,7 @@ from kornia.augmentation._2d.intensity.gaussian_noise import RandomGaussianNoisefrom kornia.augmentation._2d.intensity.grayscale import RandomGrayscalefrom kornia.augmentation._2d.intensity.hue import RandomHuefrom kornia.augmentation._2d.intensity.invert import RandomInvert+from kornia.augmentation._2d.intensity.median_blur import RandomMedianBlurfrom kornia.augmentation._2d.intensity.motion_blur import RandomMotionBlurfrom kornia.augmentation._2d.intensity.normalize import Normalizefrom kornia.augmentation._2d.intensity.planckian_jitter import RandomPlanckianJitterdiff --git a/kornia/augmentation/_2d/intensity/median_blur.py b/kornia/augmentation/_2d/intensity/median_blur.pynew file mode 100644index 000000000..044d80ab1--- /dev/null+++ b/kornia/augmentation/_2d/intensity/median_blur.py@@ -0,0 +1,51 @@+from typing import Any, Dict, Optional, Tuple++from torch import Tensor++from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D+from kornia.filters import median_blur+++class RandomMedianBlur(IntensityAugmentationBase2D):+ """Add random blur with a median filter to an image tensor.++ .. image:: _static/img/RandomMedianBlur.png++ Args:+ kernel_size: the blurring kernel size.+ same_on_batch: apply the same transformation across the batch.+ p: probability of applying the transformation.+ keepdim: whether to keep the output shape the same as input (True) or broadcast it+ to the batch form (False).+ .. note::+ This function internally uses :func:`kornia.filters.median_blur`.++ Examples:++ >>> img = torch.ones(1, 1, 4, 4)+ >>> out = RandomMedianBlur((3, 3), p = 1)(img)+ >>> out.shape+ torch.Size([1, 1, 4, 4])+ >>> out+ tensor([[[[0., 1., 1., 0.],+ [1., 1., 1., 1.],+ [1., 1., 1., 1.],+ [0., 1., 1., 0.]]]])++ To apply the exact augmenation again, you may take the advantage of the previous parameter state:+ >>> input = torch.randn(1, 3, 32, 32)+ >>> aug = RandomMedianBlur((7, 7), p=1.)+ >>> (aug(input) == aug(input, params=aug._params)).all()+ tensor(True)+ """++ def __init__(+ self, kernel_size: Tuple[int, int] = (3, 3), same_on_batch: bool = False, p: float = 0.5, keepdim: bool = False+ ) -> None:+ super().__init__(p=p, same_on_batch=same_on_batch, p_batch=1.0, keepdim=keepdim)+ self.flags = dict(kernel_size=kernel_size)++ def apply_transform(+ self, input: Tensor, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None+ ) -> Tensor:+ return median_blur(input, flags["kernel_size"])diff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.pyindex ec07235d9..edd0e3e2b 100644--- a/kornia/augmentation/__init__.py+++ b/kornia/augmentation/__init__.py@@ -29,6 +29,7 @@ from kornia.augmentation._2d import (RandomHue,RandomInvert,RandomJigsaw,+ RandomMedianBlur,RandomMixUpV2,RandomMosaic,RandomMotionBlur,@@ -98,6 +99,7 @@ __all__ = ["RandomShear","RandomTranslate","RandomBoxBlur",+ "RandomMedianBlur","RandomBrightness","RandomChannelShuffle","RandomContrast",diff --git a/temp.onnx b/temp.onnxnew file mode 100644index 000000000..18e4af5e5Binary files /dev/null and b/temp.onnx differdiff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex e37261bec..abcc3f9d4 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -31,6 +31,7 @@ from kornia.augmentation import (RandomHorizontalFlip,RandomHue,RandomInvert,+ RandomMedianBlur,RandomPlanckianJitter,RandomPlasmaBrightness,RandomPlasmaContrast,@@ -4112,3 +4113,24 @@ class TestRandomAutoContrast:input = utils.tensor_to_gradcheck_var(input) # to var# TODO: turned off with p=0assert gradcheck(kornia.augmentation.RandomAutoContrast(p=1.0), (input,), raise_exception=True, fast_mode=True)+++class TestRandomMedianBlur:+ def test_smoke(self, device, dtype):+ image = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)+ aug = RandomMedianBlur(p=0.8)+ assert image.shape == aug(image).shape++ def test_feature_median_blur(self, device, dtype):+ torch.manual_seed(0)++ img = torch.ones(1, 1, 4, 4, device=device, dtype=dtype)+ out = RandomMedianBlur((3, 3), p=0.5)(img)++ expected = torch.tensor(+ [[[[0.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 0.0]]]],+ device=device,+ dtype=dtype,+ )++ utils.assert_close(out, expected)