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

Fix glass_blur for float dtype #826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,10 +1512,8 @@ def fancy_pca(img, alpha=0.1):
return orig_img


@clipped
def glass_blur(img, sigma, max_delta, iterations, dxy, mode):
coef = MAX_VALUES_BY_DTYPE[img.dtype]
x = np.uint8(cv2.GaussianBlur(np.array(img) / coef, sigmaX=sigma, ksize=(0, 0)) * coef)
x = cv2.GaussianBlur(np.array(img), sigmaX=sigma, ksize=(0, 0))

if mode == "fast":

Expand All @@ -1542,7 +1540,7 @@ def glass_blur(img, sigma, max_delta, iterations, dxy, mode):
dx = dxy[ind, i, 1]
x[h, w], x[h + dy, w + dx] = x[h + dy, w + dx], x[h, w]

return np.clip(cv2.GaussianBlur(x / coef, sigmaX=sigma, ksize=(0, 0)), 0, 1) * coef
return cv2.GaussianBlur(x, sigmaX=sigma, ksize=(0, 0))


def _adjust_brightness_torchvision_uint8(img, factor):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,27 @@ def test_shift_scale_separate_shift_x_shift_y(image, mask):
)
assert np.array_equal(data["image"], expected_image)
assert np.array_equal(data["mask"], expected_mask)


@pytest.mark.parametrize(["val_uint8"], [[0], [1], [128], [255]])
def test_glass_blur_float_uint8_diff_less_than_two(val_uint8):

x_uint8 = np.zeros((5, 5)).astype(np.uint8)
x_uint8[2, 2] = val_uint8

x_float32 = np.zeros((5, 5)).astype(np.float32)
x_float32[2, 2] = val_uint8 / 255.0

glassblur = A.GlassBlur(always_apply=True, max_delta=1)

np.random.seed(0)
blur_uint8 = glassblur(image=x_uint8)["image"]

np.random.seed(0)
blur_float32 = glassblur(image=x_float32)["image"]

# Before comparison, rescale the blur_float32 to [0, 255]
diff = np.abs(blur_uint8 - blur_float32 * 255)

# The difference between the results of float32 and uint8 will be at most 2.
assert np.all(diff <= 2.0)