Skip to content

Commit

Permalink
Implemented Emboss transform from img_aug
Browse files Browse the repository at this point in the history
  • Loading branch information
KiriLev committed Dec 17, 2020
1 parent c91b67c commit 4aa6860
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Pixel-level transforms will change just an input image and will leave any additi
- [ChannelShuffle](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.ChannelShuffle)
- [ColorJitter](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.ColorJitter)
- [Downscale](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.Downscale)
- [Emboss](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.Emboss)
- [Equalize](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.Equalize)
- [FDA](https://albumentations.ai/docs/api_reference/augmentations/domain_adaptation/#albumentations.augmentations.domain_adaptation.FDA)
- [FancyPCA](https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.FancyPCA)
Expand Down
52 changes: 52 additions & 0 deletions albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"GridDropout",
"ColorJitter",
"Sharpen",
"Emboss",
]


Expand Down Expand Up @@ -3153,3 +3154,54 @@ def apply(self, img, sharpening_matrix=None, **params):

def get_transform_init_args_names(self):
return ("alpha", "lightness")


class Emboss(ImageOnlyTransform):
"""Emboss the input image and overlays the result with the original image.
Args:
alpha ((float, float)): range to choose the visibility of the embossed image. At 0, only the original image is
visible,at 1.0 only its embossed version is visible. Default: (0.2, 0.5).
strength ((float, float)): strength range of the embossing. Default: (0.2, 0.7).
p (float): probability of applying the transform. Default: 0.5.
Targets:
image
"""

def __init__(self, alpha=(0.2, 0.5), strength=(0.2, 0.7), always_apply=False, p=0.5):
super(Emboss, self).__init__(always_apply, p)
self.alpha = self.__check_values(to_tuple(alpha, 0.0), name="alpha", bounds=(0.0, 1.0))
self.strength = self.__check_values(to_tuple(strength, 0.0))

@staticmethod
def __check_values(value, name, bounds=(0, float("inf"))):
if not bounds[0] <= value[0] <= value[1] <= bounds[1]:
raise ValueError("{} values should be between {}".format(name, bounds))
return value

@staticmethod
def __generate_emboss_matrix(alpha_sample, strength_sample):
matrix_nochange = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.float32)
matrix_effect = np.array(
[
[-1 - strength_sample, 0 - strength_sample, 0],
[0 - strength_sample, 1, 0 + strength_sample],
[0, 0 + strength_sample, 1 + strength_sample],
],
dtype=np.float32,
)
matrix = (1 - alpha_sample) * matrix_nochange + alpha_sample * matrix_effect
return matrix

def get_params(self):
alpha = random.uniform(*self.alpha)
strength = random.uniform(*self.strength)
emboss_matrix = self.__generate_emboss_matrix(alpha_sample=alpha, strength_sample=strength)
return {"emboss_matrix": emboss_matrix}

def apply(self, img, emboss_matrix=None, **params):
return F.convolve(img, emboss_matrix)

def get_transform_init_args_names(self):
return ("alpha", "strength")
17 changes: 4 additions & 13 deletions albumentations/imgaug/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
except ImportError:
import imgaug.imgaug.augmenters as iaa

from ..augmentations import Sharpen
from ..augmentations import Emboss, Perspective, Sharpen
from ..augmentations.bbox_utils import convert_bboxes_from_albumentations, convert_bboxes_to_albumentations
from ..augmentations.keypoints_utils import convert_keypoints_from_albumentations, convert_keypoints_to_albumentations
from ..core.transforms_interface import BasicTransform, DualTransform, ImageOnlyTransform, to_tuple
from ..augmentations import Perspective

import warnings

Expand Down Expand Up @@ -124,7 +123,7 @@ def get_transform_init_args_names(self):
return ()


class IAAEmboss(ImageOnlyIAATransform):
class IAAEmboss(Emboss):
"""Emboss the input image and overlays the result with the original image.
Args:
Expand All @@ -138,16 +137,8 @@ class IAAEmboss(ImageOnlyIAATransform):
"""

def __init__(self, alpha=(0.2, 0.5), strength=(0.2, 0.7), always_apply=False, p=0.5):
super(IAAEmboss, self).__init__(always_apply, p)
self.alpha = to_tuple(alpha, 0.0)
self.strength = to_tuple(strength, 0.0)

@property
def processor(self):
return iaa.Emboss(self.alpha, self.strength)

def get_transform_init_args_names(self):
return ("alpha", "strength")
warnings.warn("This augmentation is deprecated. Please use Emboss instead")
super().__init__(alpha=alpha, strength=strength, always_apply=always_apply, p=p)


class IAASuperpixels(ImageOnlyIAATransform):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
CLAHE,
ChannelShuffle,
InvertImg,
IAAEmboss,
IAASuperpixels,
IAASharpen,
IAAAdditiveGaussianNoise,
Expand Down Expand Up @@ -68,6 +67,7 @@
HistogramMatching,
Perspective,
Sharpen,
Emboss,
)


Expand Down Expand Up @@ -114,6 +114,7 @@
{"reference_images": [np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)], "read_fn": lambda x: x},
],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_image_only_augmentations(augmentation_cls, params, image, mask):
Expand Down Expand Up @@ -162,6 +163,7 @@ def test_image_only_augmentations(augmentation_cls, params, image, mask):
{"reference_images": [np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)], "read_fn": lambda x: x},
],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_image_only_augmentations_with_float_values(augmentation_cls, params, float_image, mask):
Expand Down Expand Up @@ -236,7 +238,7 @@ def test_dual_augmentations_with_float_values(augmentation_cls, params, float_im
assert data["mask"].dtype == np.uint8


@pytest.mark.parametrize("augmentation_cls", [IAAEmboss, IAASuperpixels, IAASharpen, IAAAdditiveGaussianNoise])
@pytest.mark.parametrize("augmentation_cls", [IAASuperpixels, IAASharpen, IAAAdditiveGaussianNoise])
def test_imgaug_image_only_augmentations(augmentation_cls, image, mask):
aug = augmentation_cls(p=1)
data = aug(image=image, mask=mask)
Expand Down Expand Up @@ -318,6 +320,7 @@ def test_imgaug_dual_augmentations(augmentation_cls, image, mask):
],
[Perspective, {}],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_augmentations_wont_change_input(augmentation_cls, params, image, mask):
Expand Down Expand Up @@ -387,6 +390,7 @@ def test_augmentations_wont_change_input(augmentation_cls, params, image, mask):
],
[Perspective, {}],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_augmentations_wont_change_float_input(augmentation_cls, params, float_image):
Expand Down Expand Up @@ -437,6 +441,7 @@ def test_augmentations_wont_change_float_input(augmentation_cls, params, float_i
[FDA, {"reference_images": [np.random.randint(0, 256, [100, 100], dtype=np.uint8)], "read_fn": lambda x: x}],
[Perspective, {}],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_augmentations_wont_change_shape_grayscale(augmentation_cls, params, image, mask):
Expand Down Expand Up @@ -514,6 +519,7 @@ def test_augmentations_wont_change_shape_grayscale(augmentation_cls, params, ima
],
[Perspective, {}],
[Sharpen, {}],
[Emboss, {}],
],
)
def test_augmentations_wont_change_shape_rgb(augmentation_cls, params, image, mask):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def set_seed(seed):
[A.ColorJitter, {}],
[A.Perspective, {}],
[A.Sharpen, {}],
[A.Emboss, {}],
],
)
@pytest.mark.parametrize("p", [0.5, 1])
Expand Down Expand Up @@ -248,6 +249,7 @@ def test_augmentations_serialization(augmentation_cls, params, p, seed, image, m
},
],
[A.Sharpen, {"alpha": [0.2, 0.5], "lightness": [0.5, 1.0]}],
[A.Emboss, {"alpha": [0.2, 0.5], "strength": [0.5, 1.0]}],
],
)

Expand Down Expand Up @@ -345,6 +347,7 @@ def test_augmentations_serialization_to_file_with_custom_parameters(
[A.ColorJitter, {}],
[A.Perspective, {}],
[A.Sharpen, {}],
[A.Emboss, {}],
],
)
@pytest.mark.parametrize("p", [0.5, 1])
Expand Down Expand Up @@ -412,6 +415,7 @@ def test_augmentations_for_bboxes_serialization(
[A.ColorJitter, {}],
[A.Perspective, {}],
[A.Sharpen, {}],
[A.Emboss, {}],
],
)
@pytest.mark.parametrize("p", [0.5, 1])
Expand All @@ -432,7 +436,6 @@ def test_augmentations_for_keypoints_serialization(augmentation_cls, params, p,
@pytest.mark.parametrize(
["augmentation_cls", "params"],
[
[A.IAAEmboss, {}],
[A.IAASuperpixels, {}],
[A.IAAAdditiveGaussianNoise, {}],
[A.IAACropAndPad, {}],
Expand Down Expand Up @@ -463,7 +466,6 @@ def test_imgaug_augmentations_serialization(augmentation_cls, params, p, seed, i
@pytest.mark.parametrize(
["augmentation_cls", "params"],
[
[A.IAAEmboss, {}],
[A.IAASuperpixels, {}],
[A.IAAAdditiveGaussianNoise, {}],
[A.IAACropAndPad, {}],
Expand Down Expand Up @@ -496,7 +498,6 @@ def test_imgaug_augmentations_for_bboxes_serialization(
@pytest.mark.parametrize(
["augmentation_cls", "params"],
[
[A.IAAEmboss, {}],
[A.IAASuperpixels, {}],
[A.IAAAdditiveGaussianNoise, {}],
[A.IAACropAndPad, {}],
Expand Down Expand Up @@ -699,6 +700,7 @@ def test_transform_pipeline_serialization_with_keypoints(seed, image, keypoints,
[A.ColorJitter, {}],
[A.Perspective, {}],
[A.Sharpen, {}],
[A.Emboss, {}],
],
)
@pytest.mark.parametrize("seed", TEST_SEEDS)
Expand Down

0 comments on commit 4aa6860

Please sign in to comment.