Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gamma invert transform #1309

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,17 @@ def gamma_transform(img, gamma):
return img


@preserve_shape
def gamma_invert_transform(img, gamma):
if img.dtype == np.uint8:
table = (1 - ((1 - np.arange(0, 256.0 / 255, 1.0 / 255)) ** gamma)) * 255
img = cv2.LUT(img, np.round(table).astype(np.uint8))
else:
img = 1 - np.power(1 - img, gamma)
Comment on lines +794 to +795
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to take max value from MAX_VALUES_BY_DTYPE because img might have int32 dtype


return img


@clipped
def gauss_noise(image, gauss):
image = image.astype("float32")
Expand Down
17 changes: 13 additions & 4 deletions albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,8 @@ class RandomGamma(ImageOnlyTransform):
Args:
gamma_limit (float or (float, float)): If gamma_limit is a single float value,
the range will be (-gamma_limit, gamma_limit). Default: (80, 120).
p_invert (float): Probability of applying transform symmetrical to gamma transform with respect to the y=x.
Identical to sequentially applied InvertImg, RandomGamma and InvertImg. Default: 0.0.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think would be great add there link to the issue or to the article directly

eps: Deprecated.

Targets:
Expand All @@ -1380,16 +1382,23 @@ class RandomGamma(ImageOnlyTransform):
uint8, float32
"""

def __init__(self, gamma_limit=(80, 120), eps=None, always_apply=False, p=0.5):
def __init__(self, gamma_limit=(80, 120), p_invert=0.0, eps=None, always_apply=False, p=0.5):
super(RandomGamma, self).__init__(always_apply, p)
self.gamma_limit = to_tuple(gamma_limit)
self.p_invert = p_invert
self.eps = eps

def apply(self, img, gamma=1, **params):
return F.gamma_transform(img, gamma=gamma)
def apply(self, img, gamma=1, invert=False, **params):
if invert:
return F.gamma_invert_transform(img, gamma=gamma)
else:
return F.gamma_transform(img, gamma=gamma)

def get_params(self):
return {"gamma": random.uniform(self.gamma_limit[0], self.gamma_limit[1]) / 100.0}
return {
"gamma": random.uniform(self.gamma_limit[0], self.gamma_limit[1]) / 100.0,
"invert": random.random() > self.p_invert,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect condition. Must be random.random() < self.p_invert

}

def get_transform_init_args_names(self):
return ("gamma_limit", "eps")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add p_invert to get_transform_init_args_names

Expand Down
31 changes: 31 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,37 @@ def test_gamma_float_equal_uint8():
assert (np.abs(img - img_f) <= 1).all()


@pytest.mark.parametrize(["gamma", "expected"], [(1, 1), (0.8, 3)])
def test_gamma_invert_transform(gamma, expected):
img = np.ones((100, 100, 3), dtype=np.uint8)
img = F.gamma_invert_transform(img, gamma=gamma)
assert img.dtype == np.dtype("uint8")
assert (img == expected).all()


@pytest.mark.parametrize(["gamma", "expected"], [(1, 0.4), (10, 0.9939534)])
def test_gamma_invert_transform_float(gamma, expected):
img = np.ones((100, 100, 3), dtype=np.float32) * 0.4
expected = np.ones((100, 100, 3), dtype=np.float32) * expected
img = F.gamma_invert_transform(img, gamma=gamma)
assert img.dtype == np.dtype("float32")
assert np.allclose(img, expected)


def test_gamma_invert_float_equal_uint8():
img = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
img_f = img.astype(np.float32) / 255.0
gamma = 0.5

img = F.gamma_invert_transform(img, gamma)
img_f = F.gamma_invert_transform(img_f, gamma)

img = img.astype(np.float32)
img_f *= 255.0

assert (np.abs(img - img_f) <= 1).all()


@pytest.mark.parametrize(["dtype", "divider"], [(np.uint8, 255), (np.uint16, 65535), (np.uint32, 4294967295)])
def test_to_float_without_max_value_specified(dtype, divider):
img = np.ones((100, 100, 3), dtype=dtype)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_augmentations_serialization(augmentation_cls, params, p, seed, image, m
[A.GaussianBlur, {"blur_limit": 3}],
[A.GaussNoise, {"var_limit": (20, 90), "mean": 10, "per_channel": False}],
[A.CLAHE, {"clip_limit": 2, "tile_grid_size": (12, 12)}],
[A.RandomGamma, {"gamma_limit": (10, 90)}],
[A.RandomGamma, {"gamma_limit": (10, 90), "p_invert": 0.5}],
[A.Cutout, {"num_holes": 4, "max_h_size": 4, "max_w_size": 4}],
[A.CoarseDropout, {"max_holes": 4, "max_height": 4, "max_width": 4}],
[A.RandomSnow, {"snow_point_lower": 0.2, "snow_point_upper": 0.4, "brightness_coeff": 4}],
Expand Down