From 9ed24b495ea0752f5a0da79a14ea6777c5827c5d Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 2 May 2020 19:31:01 +0530 Subject: [PATCH] RGB to Grayscale --- .github/CODEOWNERS | 2 + tensorflow_addons/image/__init__.py | 1 + .../image/rgb_to_grayscale_op.py | 45 ++++++++++ .../image/tests/rgb_to_grayscale_op_test.py | 89 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100755 tensorflow_addons/image/rgb_to_grayscale_op.py create mode 100755 tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 3ce0a2aa6a..cd8e3c56ec 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -64,6 +64,8 @@ /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih /tensorflow_addons/image/tests/resampler_ops_test.py @autoih +/tensorflow_addons/image/rgb_to_grayscale_op.py @ghosalsattam +/tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py @ghosalsattam /tensorflow_addons/image/sparse_image_warp.py /tensorflow_addons/image/tests/sparse_image_warp_test.py /tensorflow_addons/image/transform_ops.py @mels630 diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index 15b92a5380..865468f149 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -29,6 +29,7 @@ from tensorflow_addons.image.cutout_ops import random_cutout from tensorflow_addons.image.distort_image_ops import random_hsv_in_yiq from tensorflow_addons.image.resampler_ops import resampler +from tensorflow_addons.image.rgb_to_grayscale_op import rgb_to_grayscale from tensorflow_addons.image.transform_ops import rotate from tensorflow_addons.image.transform_ops import shear_x from tensorflow_addons.image.transform_ops import shear_y diff --git a/tensorflow_addons/image/rgb_to_grayscale_op.py b/tensorflow_addons/image/rgb_to_grayscale_op.py new file mode 100755 index 0000000000..dcf93ba600 --- /dev/null +++ b/tensorflow_addons/image/rgb_to_grayscale_op.py @@ -0,0 +1,45 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# 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 +# +# http://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. +# ============================================================================== +"RGB to Grayscale op" + +from tensorflow_addons.utils.types import FloatTensorLike +from tensorflow_addons.image import utils as img_utils +import tensorflow as tf + +from typing import Optional + + +def rgb_to_grayscale( + image: FloatTensorLike, name: Optional[str] = None, +) -> FloatTensorLike: + """Perform RGB to Grayscale conversion of image(s). + + Args: + image: A 3-D `Tensor` of shape `[height, width, channels]`, + or a 4-D `Tensor` of shape `[batch_size, height, width, channels]`. + The number of channels must be equal to 3. + name: A name for this operation (optional). + """ + + with tf.name_scope(name or "rgb_to_grayscale"): + image = tf.cast(image, tf.float32) + original_ndims = img_utils.get_ndims(image) + image = img_utils.to_4D_image(image) + channels = tf.shape(image)[3] + if channels != 3: + raise ValueError("The image must be in RGB format") + grayscale_output = tf.image.rgb_to_grayscale(image) + grayscale_output = img_utils.from_4D_image(grayscale_output, original_ndims) + return grayscale_output diff --git a/tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py b/tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py new file mode 100755 index 0000000000..d493d48202 --- /dev/null +++ b/tensorflow_addons/image/tests/rgb_to_grayscale_op_test.py @@ -0,0 +1,89 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# 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 +# +# http://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. +# ============================================================================== +"rgb to grayscale op test" + + +import numpy as np +import tensorflow as tf +from tensorflow_addons.image.rgb_to_grayscale_op import rgb_to_grayscale +import pytest + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_rgb_to_grayscale_op(): + img = tf.constant( + [ + [ + [ + [0.0, 0.0, 0.0], + [2.0, 2.0, 2.0], + [4.0, 4.0, 4.0], + [6.0, 6.0, 6.0], + [8.0, 8.0, 8.0], + ], + [ + [10.0, 10.0, 10.0], + [12.0, 12.0, 12.0], + [14.0, 14.0, 14.0], + [16.0, 16.0, 16.0], + [18.0, 18.0, 18.0], + ], + [ + [20.0, 20.0, 20.0], + [22.0, 22.0, 22.0], + [24.0, 24.0, 24.0], + [26.0, 26.0, 26.0], + [28.0, 28.0, 28.0], + ], + [ + [30.0, 30.0, 30.0], + [32.0, 32.0, 32.0], + [34.0, 34.0, 34.0], + [36.0, 36.0, 36.0], + [38.0, 38.0, 38.0], + ], + [ + [40.0, 40.0, 40.0], + [42.0, 42.0, 42.0], + [44.0, 44.0, 44.0], + [46.0, 46.0, 46.0], + [48.0, 48.0, 48.0], + ], + ] + ], + dtype=tf.float32, + ) + r = np.arange(0, 50, 2) + count = 0 + exp_plane = np.ones([5, 5, 1]) + for i in range(5): + for j in range(5): + exp_plane[i][j][0] = ( + 0.2989 * r[count] + 0.5870 * r[count] + 0.1140 * r[count] + ) + count += 1 + grayscale_tfa = rgb_to_grayscale(img) + grayscale_tfa = grayscale_tfa.numpy() + grayscale_tfa = np.resize(grayscale_tfa, (5, 5)) + exp_plane = np.resize(exp_plane, (5, 5)) + np.testing.assert_allclose(grayscale_tfa, exp_plane, 0.06) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_rgb_to_grayscale_op_random_image(): + img = tf.random.uniform([2, 40, 40, 3], minval=0, maxval=255, dtype=tf.float32) + exp_plane = tf.image.rgb_to_grayscale(img) + grayscale_tfa = tf.image.rgb_to_grayscale(img) + np.testing.assert_allclose(grayscale_tfa, exp_plane, 0.06)