Skip to content

Commit

Permalink
Fix GammaTransform int as tuple, division by zero (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipet authored and ternaus committed Sep 9, 2019
1 parent 01ef0aa commit 389d31a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions albumentations/augmentations/functional.py
Expand Up @@ -1077,9 +1077,9 @@ def channel_dropout(img, channels_to_drop, fill_value=0):


@preserve_shape
def gamma_transform(img, gamma):
def gamma_transform(img, gamma, eps=1e-7):
if img.dtype == np.uint8:
invGamma = 1.0 / gamma
invGamma = 1.0 / (gamma + eps)
table = (np.arange(0, 256.0 / 255, 1.0 / 255) ** invGamma) * 255
img = cv2.LUT(img, table.astype(np.uint8))
else:
Expand Down
9 changes: 5 additions & 4 deletions albumentations/augmentations/transforms.py
Expand Up @@ -2251,20 +2251,21 @@ class RandomGamma(ImageOnlyTransform):
uint8, float32
"""

def __init__(self, gamma_limit=(80, 120), always_apply=False, p=0.5):
def __init__(self, gamma_limit=(80, 120), eps=1e-7, always_apply=False, p=0.5):
super(RandomGamma, self).__init__(always_apply, p)
self.gamma_limit = gamma_limit
self.gamma_limit = to_tuple(gamma_limit)
self.eps = eps

def apply(self, img, gamma=1, **params):
return F.gamma_transform(img, gamma=gamma)
return F.gamma_transform(img, gamma=gamma, eps=self.eps)

def get_params(self):
return {
'gamma': random.randint(self.gamma_limit[0], self.gamma_limit[1]) / 100.0
}

def get_transform_init_args_names(self):
return ('gamma_limit',)
return ('gamma_limit', 'eps',)


class ToGray(ImageOnlyTransform):
Expand Down

0 comments on commit 389d31a

Please sign in to comment.