From 97937560c98a57c8e8b26225dcfb8c31523cddfa Mon Sep 17 00:00:00 2001 From: Maximilian Bachl Date: Sun, 23 Jul 2023 19:07:23 +0200 Subject: [PATCH 1/4] RandomGaussianBlur crashes when factor=0 ```>>> import keras_cv Using TensorFlow backend >>> blurrer = keras_cv.layers.RandomGaussianBlur(3, 0.) >>> blurrer.get_random_transformation() (, ) ``` --- keras_cv/layers/preprocessing/random_gaussian_blur.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/keras_cv/layers/preprocessing/random_gaussian_blur.py b/keras_cv/layers/preprocessing/random_gaussian_blur.py index 7b8d151302..5dd584210e 100644 --- a/keras_cv/layers/preprocessing/random_gaussian_blur.py +++ b/keras_cv/layers/preprocessing/random_gaussian_blur.py @@ -33,10 +33,12 @@ class RandomGaussianBlur(BaseImageAugmentationLayer): factor: A tuple of two floats, a single float or a `keras_cv.FactorSampler`. `factor` controls the extent to which the image is blurred. Mathematically, `factor` represents the `sigma` - value in a gaussian blur. `factor=0.0` makes this layer perform a - no-op operation, and high values make the blur stronger. In order to - ensure the value is always the same, please pass a tuple with two - identical floats: `(0.5, 0.5)`. + value in a gaussian blur. The closer `factor` is to 0, the more + this operation becomes a no-op operation, and high values make the + blur stronger. However, factor MUST NOT BECOME 0 as then numerical + instability occurs and `nan` results. In order to ensure the value + is always the same, please pass a tuple with two identical floats: + `(0.5, 0.5)`. """ def __init__(self, kernel_size, factor, **kwargs): From e7ae8131bf8eb1872c11b1b2b77dc37059d681f8 Mon Sep 17 00:00:00 2001 From: Maximilian Bachl Date: Fri, 28 Jul 2023 16:50:33 +0200 Subject: [PATCH 2/4] Make sure `factor` cannot become too small --- .../layers/preprocessing/random_gaussian_blur.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/keras_cv/layers/preprocessing/random_gaussian_blur.py b/keras_cv/layers/preprocessing/random_gaussian_blur.py index 5dd584210e..facbef1251 100644 --- a/keras_cv/layers/preprocessing/random_gaussian_blur.py +++ b/keras_cv/layers/preprocessing/random_gaussian_blur.py @@ -33,12 +33,10 @@ class RandomGaussianBlur(BaseImageAugmentationLayer): factor: A tuple of two floats, a single float or a `keras_cv.FactorSampler`. `factor` controls the extent to which the image is blurred. Mathematically, `factor` represents the `sigma` - value in a gaussian blur. The closer `factor` is to 0, the more - this operation becomes a no-op operation, and high values make the - blur stronger. However, factor MUST NOT BECOME 0 as then numerical - instability occurs and `nan` results. In order to ensure the value - is always the same, please pass a tuple with two identical floats: - `(0.5, 0.5)`. + value in a gaussian blur. `factor=0.0` makes this layer perform a + no-op operation, and high values make the blur stronger. In order to + ensure the value is always the same, please pass a tuple with two + identical floats: `(0.5, 0.5)`. """ def __init__(self, kernel_size, factor, **kwargs): @@ -63,7 +61,9 @@ def __init__(self, kernel_size, factor, **kwargs): ) def get_random_transformation(self, **kwargs): - factor = self.factor() + # `factor` must not become too small otherwise numerical issues occur. + # 0.01 behaves like 0 without causing `nan`s + factor = max(self.factor(), 0.01) blur_v = RandomGaussianBlur.get_kernel(factor, self.y) blur_h = RandomGaussianBlur.get_kernel(factor, self.x) blur_v = tf.reshape(blur_v, [self.y, 1, 1, 1]) From 877c635c534f6393c73fde9b5c6fff8617413f85 Mon Sep 17 00:00:00 2001 From: Maximilian Bachl Date: Mon, 31 Jul 2023 15:10:58 +0200 Subject: [PATCH 3/4] Use keras.backend.epsilon() instead of 0.01 --- keras_cv/layers/preprocessing/random_gaussian_blur.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/keras_cv/layers/preprocessing/random_gaussian_blur.py b/keras_cv/layers/preprocessing/random_gaussian_blur.py index facbef1251..cab1743008 100644 --- a/keras_cv/layers/preprocessing/random_gaussian_blur.py +++ b/keras_cv/layers/preprocessing/random_gaussian_blur.py @@ -62,8 +62,8 @@ def __init__(self, kernel_size, factor, **kwargs): def get_random_transformation(self, **kwargs): # `factor` must not become too small otherwise numerical issues occur. - # 0.01 behaves like 0 without causing `nan`s - factor = max(self.factor(), 0.01) + # keras.backend.epsilon() behaves like 0 without causing `nan`s + factor = max(self.factor(), keras.backend.epsilon()) blur_v = RandomGaussianBlur.get_kernel(factor, self.y) blur_h = RandomGaussianBlur.get_kernel(factor, self.x) blur_v = tf.reshape(blur_v, [self.y, 1, 1, 1]) From 39ad46d504c3167b1de01584a82e35f1b5c722a9 Mon Sep 17 00:00:00 2001 From: Maximilian Bachl Date: Mon, 31 Jul 2023 15:39:56 +0200 Subject: [PATCH 4/4] Update random_gaussian_blur.py --- keras_cv/layers/preprocessing/random_gaussian_blur.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_cv/layers/preprocessing/random_gaussian_blur.py b/keras_cv/layers/preprocessing/random_gaussian_blur.py index cab1743008..a2209f8bd5 100644 --- a/keras_cv/layers/preprocessing/random_gaussian_blur.py +++ b/keras_cv/layers/preprocessing/random_gaussian_blur.py @@ -63,7 +63,7 @@ def __init__(self, kernel_size, factor, **kwargs): def get_random_transformation(self, **kwargs): # `factor` must not become too small otherwise numerical issues occur. # keras.backend.epsilon() behaves like 0 without causing `nan`s - factor = max(self.factor(), keras.backend.epsilon()) + factor = tf.math.maximum(self.factor(), keras.backend.epsilon()) blur_v = RandomGaussianBlur.get_kernel(factor, self.y) blur_h = RandomGaussianBlur.get_kernel(factor, self.x) blur_v = tf.reshape(blur_v, [self.y, 1, 1, 1])