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

Gaussian blur #251

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions keras_cv/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from keras_cv.layers.preprocessing.channel_shuffle import ChannelShuffle
from keras_cv.layers.preprocessing.cut_mix import CutMix
from keras_cv.layers.preprocessing.equalization import Equalization
from keras_cv.layers.preprocessing.gaussian_blur import GaussianBlur
from keras_cv.layers.preprocessing.grayscale import Grayscale
from keras_cv.layers.preprocessing.grid_mask import GridMask
from keras_cv.layers.preprocessing.mix_up import MixUp
Expand Down
1 change: 1 addition & 0 deletions keras_cv/layers/preprocessing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from keras_cv.layers.preprocessing.channel_shuffle import ChannelShuffle
from keras_cv.layers.preprocessing.cut_mix import CutMix
from keras_cv.layers.preprocessing.equalization import Equalization
from keras_cv.layers.preprocessing.gaussian_blur import GaussianBlur
from keras_cv.layers.preprocessing.grayscale import Grayscale
from keras_cv.layers.preprocessing.grid_mask import GridMask
from keras_cv.layers.preprocessing.mix_up import MixUp
Expand Down
123 changes: 123 additions & 0 deletions keras_cv/layers/preprocessing/gaussian_blur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf


@tf.keras.utils.register_keras_serializable(package="keras_cv")
class GaussianBlur(tf.keras.__internal__.layers.BaseImageAugmentationLayer):
"""Applies a Gaussian Blur with random sigma to an image.

Args:
kernel_size: int, 2 element tuple or 2 element list. x and y dimensions for
the kernel used. If tuple or list, first element is used for the x dimension
and second element is used for y dimension. If int, kernel will be squared.
sigma: float, 2 element tuple or 2 element list. Interval in which sigma should
be sampled from. If float, interval is going to be [0, float), else the
first element represents the lower bound and the second element the upper
bound of the sampling interval.
"""

def __init__(self, kernel_size, sigma, **kwargs):
super().__init__(**kwargs)
self.kernel_size = kernel_size
self.sigma = sigma

if isinstance(kernel_size, (tuple, list)):
self.x = kernel_size[0]
self.y = kernel_size[1]
else:
if isinstance(kernel_size, int):
self.x = self.y = kernel_size
else:
raise ValueError(
"`kernel_size` must be list, tuple or integer "
", got {} ".format(type(self.kernel_size))
)

if isinstance(sigma, (tuple, list)):
self.sigma_min = sigma[0]
self.sigma_max = sigma[1]
else:
self.sigma_min = type(sigma)(0)
self.sigma_max = sigma

if not isinstance(self.sigma_min, type(self.sigma_max)):
raise ValueError(
"`sigma` must have lower bound and upper bound "
"with same type, got {} and {}".format(
type(self.width_lower), type(self.width_upper)
)
)

if self.sigma_max < self.sigma_min:
raise ValueError(
"`sigma` cannot have upper bound less than "
"lower bound, got {}".format(sigma)
)

self._sigma_is_float = isinstance(self.sigma, float)
if self._sigma_is_float:
if not self.sigma_min >= 0.0:
raise ValueError(
"`sigma` must be higher than 0"
"when is float, got {}".format(sigma)
)

def get_random_transformation(self, image=None, label=None, bounding_box=None):
sigma = self.get_sigma()
blur_v = GaussianBlur.get_kernel(sigma, self.y)
blur_h = GaussianBlur.get_kernel(sigma, self.x)
blur_v = tf.reshape(blur_v, [self.y, 1, 1, 1])
blur_h = tf.reshape(blur_h, [1, self.x, 1, 1])
return (blur_v, blur_h)

def get_sigma(self):
sigma = self._random_generator.random_uniform(
shape=(), minval=self.sigma_min, maxval=self.sigma_max
)
return sigma

def augment_image(self, image, transformation=None):

image = tf.expand_dims(image, axis=0)

num_channels = tf.shape(image)[-1]
blur_v, blur_h = transformation
blur_h = tf.tile(blur_h, [1, 1, num_channels, 1])
blur_v = tf.tile(blur_v, [1, 1, num_channels, 1])
blurred = tf.nn.depthwise_conv2d(
image, blur_h, strides=[1, 1, 1, 1], padding="SAME"
)
blurred = tf.nn.depthwise_conv2d(
blurred, blur_v, strides=[1, 1, 1, 1], padding="SAME"
)

return tf.squeeze(blurred, axis=0)

@staticmethod
def get_kernel(sigma, filter_size):
x = tf.cast(
tf.range(-filter_size // 2 + 1, filter_size // 2 + 1), dtype=tf.float32
)
blur_filter = tf.exp(
-tf.pow(x, 2.0) / (2.0 * tf.pow(tf.cast(sigma, dtype=tf.float32), 2.0))
)
blur_filter /= tf.reduce_sum(blur_filter)
return blur_filter

def get_config(self):
config = super().get_config()
config.update({"sigma": self.sigma, "kernel_size": self.kernel_size})
return config
79 changes: 79 additions & 0 deletions keras_cv/layers/preprocessing/gaussian_blur_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tensorflow as tf

from keras_cv.layers import preprocessing


class GaussianBlurTest(tf.test.TestCase):
def test_return_shapes(self):
layer = preprocessing.GaussianBlur(kernel_size=(3, 7), sigma=(0, 2))

# RGB
xs = tf.ones((2, 512, 512, 3))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 512, 512, 3])

# greyscale
xs = tf.ones((2, 512, 512, 1))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 512, 512, 1])

def test_in_single_image(self):
layer = preprocessing.GaussianBlur(kernel_size=(3, 7), sigma=(0, 2))

# RGB
xs = tf.cast(
tf.ones((512, 512, 3)),
dtype=tf.float32,
)

xs = layer(xs)
self.assertEqual(xs.shape, [512, 512, 3])

# greyscale
xs = tf.cast(
tf.ones((512, 512, 1)),
dtype=tf.float32,
)

xs = layer(xs)
self.assertEqual(xs.shape, [512, 512, 1])

def test_non_square_images(self):
layer = preprocessing.GaussianBlur(kernel_size=(3, 7), sigma=(0, 2))

# RGB
xs = tf.ones((2, 256, 512, 3))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 256, 512, 3])

# greyscale
xs = tf.ones((2, 256, 512, 1))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 256, 512, 1])

def test_single_input_args(self):
layer = preprocessing.GaussianBlur(kernel_size=7, sigma=2)

# RGB
xs = tf.ones((2, 512, 512, 3))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 512, 512, 3])

# greyscale
xs = tf.ones((2, 512, 512, 1))
xs = layer(xs)
self.assertEqual(xs.shape, [2, 512, 512, 1])