Skip to content

Commit

Permalink
RandomGaussianBlur crashes when factor=0 (keras-team#1968)
Browse files Browse the repository at this point in the history
* RandomGaussianBlur crashes when factor=0

```>>> import keras_cv
Using TensorFlow backend
>>> blurrer = keras_cv.layers.RandomGaussianBlur(3, 0.)
>>> blurrer.get_random_transformation()
(<tf.Tensor: shape=(3, 1, 1, 1), dtype=float32, numpy=
array([[[[nan]]],


       [[[nan]]],


       [[[nan]]]], dtype=float32)>, <tf.Tensor: shape=(1, 3, 1, 1), dtype=float32, numpy=
array([[[[nan]],

        [[nan]],

        [[nan]]]], dtype=float32)>)
```

* Make sure `factor` cannot become too small

* Use keras.backend.epsilon() instead of 0.01

* Update random_gaussian_blur.py
  • Loading branch information
muxamilian authored Jul 31, 2023
1 parent c8a80d4 commit d29146d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion keras_cv/layers/preprocessing/random_gaussian_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,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.
# keras.backend.epsilon() behaves like 0 without causing `nan`s
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])
Expand Down

0 comments on commit d29146d

Please sign in to comment.