From e27bd7fc69dced120c919adde8c1031e428487ec Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Fri, 27 Mar 2020 09:47:33 +0530 Subject: [PATCH 01/71] Gaussian Blur op --- gaussianBlur_ops.py | 86 ++++++++++++++++++++++++++++++++++++++++ gaussianBlur_ops_test.py | 13 ++++++ 2 files changed, 99 insertions(+) create mode 100644 gaussianBlur_ops.py create mode 100644 gaussianBlur_ops_test.py diff --git a/gaussianBlur_ops.py b/gaussianBlur_ops.py new file mode 100644 index 0000000000..ea1969778b --- /dev/null +++ b/gaussianBlur_ops.py @@ -0,0 +1,86 @@ +# 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. +# ============================================================================= +"""GaussuanBlur Op""" + + +import numpy as np +import matplotlib.pyplot as plt +import cv2 +from tensorflow.python.ops import nn_ops +from tensorflow.python.framework import tensor_shape +import tensorflow as tf + + +def getImageHeightWidth(images,dataFormat): + if(dataFormat=='channels_last'): + h,w=tf.shape(images)[1],tf.shape(images)[0] + else: + h,w=tf.shape(images)[2], tf.shape(images)[3] + return h,w + +class GaussianBlur(): + """ + This class is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel + which follows normal distribution then convolves the image with the kernel. + Args: + img: A tensor of shape + (batch_size, height, width, channels) + (NHWC), (batch_size, channels, height, width)(NCHW). + sigma:A constant of type float64. It is the standard deviation of the normal distribution. + The more the sigma, the more the blurring effect. + G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) + kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. + A kernel of size [kSize*kSize] is generated. + """ + def __init__(self,img,sigma,kSize): + if(sigma==0): + raise ValueError("Sigma should not be zero") + self.sigma=sigma + if(kSize%2==0): + raise ValueError("kSize should be odd") + + self.kSize=kSize + self.gaussianKernelNumpy=(np.zeros([kSize,kSize])) + self.findKernel() + self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) + self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) + self.img=tf.convert_to_tensor(img) + self.gaussianKernelTensor=tf.convert_to_tensor(self.gaussianKernelNumpy) + gaussian_filter_shape=self.gaussianKernelTensor.get_shape() + self.conv_ops=nn_ops.Convolution(input_shape=img.get_shape(), + filter_shape=gaussian_filter_shape, + padding='SAME') + + + def findKernel(self): + "This function creates a kernel of size [kSize*kSize]" + for i in range(-self.kSize//2,self.kSize//2+1): + for j in range(-self.kSize//2,self.kSize//2+1): + self.gaussianKernelNumpy[i+self.kSize//2][j+self.kSize//2]=1/(2*np.pi*(self.sigma)**2)*np.exp(-(i**2+j**2)/(2*self.sigma**2)) + return + + + + def convolve(self): + "This function is responsible for convolving the given image with the Gaussian Kernel" + out=self.conv_ops(self.img,self.gaussianKernelTensor) + return out + + +#with tf.session() as sess: + + + + diff --git a/gaussianBlur_ops_test.py b/gaussianBlur_ops_test.py new file mode 100644 index 0000000000..a299169dc7 --- /dev/null +++ b/gaussianBlur_ops_test.py @@ -0,0 +1,13 @@ +from gaussianBlur_ops import GaussianBlur +import tensorflow as tf + + + +def test(dtype,batch,hight,width): + test_image=tf.ones([batch,width,hight,1],dtype=dtype) + gb=GaussianBlur(test_image,1,7) + print(gb.convolve()) + +test(tf.float64,1,40,40) + + From e1c1562972d8517e7e3e05f3183eb1d726dd8023 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Fri, 27 Mar 2020 11:08:11 +0530 Subject: [PATCH 02/71] Adds Average smoothing op --- averageSmoothing.py | 82 ++++++++++++++++++++++++++++++++++++++++ averageSmoothing_test.py | 13 +++++++ 2 files changed, 95 insertions(+) create mode 100644 averageSmoothing.py create mode 100644 averageSmoothing_test.py diff --git a/averageSmoothing.py b/averageSmoothing.py new file mode 100644 index 0000000000..c3f6cab1ae --- /dev/null +++ b/averageSmoothing.py @@ -0,0 +1,82 @@ +# 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. +# ============================================================================= +"""AverageSmoothing Op""" + + +import numpy as np +import matplotlib.pyplot as plt +import cv2 +from tensorflow.python.ops import nn_ops +from tensorflow.python.framework import tensor_shape +import tensorflow as tf + + +def getImageHeightWidth(images,dataFormat): + if(dataFormat=='channels_last'): + h,w=tf.shape(images)[1],tf.shape(images)[0] + else: + h,w=tf.shape(images)[2], tf.shape(images)[3] + return h,w + +class AverageSmooth(): + """ + This class is responsible for having Average Smoothing. It takes the image as input, computes an average-smoothing- + kernel then convolves the image with the kernel. + Args: + img: A tensor of shape + (batch_size, height, width, channels) + (NHWC), (batch_size, channels, height, width)(NCHW). + + kSize:It is the tuple of shape(height,width). + A kernel of size [height*width] is generated. + """ + def __init__(self,img,kSize): + + + + self.kHeight=kSize[0] + self.kWidth=kSize[1] + self.gaussianKernelNumpy=(np.ones([self.kHeight,self.kWidth])) + self.findKernel() + self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) + self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) + self.img=tf.convert_to_tensor(img) + self.gaussianKernelTensor=tf.convert_to_tensor(self.gaussianKernelNumpy) + gaussian_filter_shape=self.gaussianKernelTensor.get_shape() + self.conv_ops=nn_ops.Convolution(input_shape=img.get_shape(), + filter_shape=gaussian_filter_shape, + padding='SAME') + + + def findKernel(self): + "This function creates a kernel of size [height*width]" + for i in range (self.kHeight): + for j in range(self.kWidth): + self.gaussianKernelNumpy[i][j]=1/(self.kHeight*self.kWidth) + return + + + + def convolve(self): + "This function is responsible for convolving the given image with the Gaussian Kernel" + out=self.conv_ops(self.img,self.gaussianKernelTensor) + return out + + +#with tf.session() as sess: + + + + diff --git a/averageSmoothing_test.py b/averageSmoothing_test.py new file mode 100644 index 0000000000..936c8c4477 --- /dev/null +++ b/averageSmoothing_test.py @@ -0,0 +1,13 @@ +from averageSmoothing import AverageSmooth +import tensorflow as tf + + + +def test(dtype,batch,hight,width): + test_image=tf.ones([batch,width,hight,1],dtype=dtype) + gb=AverageSmooth(test_image,(3,4)) + print(gb.convolve()) + +test(tf.float64,1,40,40) + + From 0eba6f2be4ee11a30761150c6ae68c615b35d4c5 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 28 Mar 2020 02:37:39 +0530 Subject: [PATCH 03/71] Gaussian Filter --- tensorflow_addons/image/BUILD | 12 +++ .../image/gaussian_filter_ops.py | 75 +++++++++++++++++++ .../image/gaussian_filter_ops_test.py | 48 ++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tensorflow_addons/image/gaussian_filter_ops.py create mode 100644 tensorflow_addons/image/gaussian_filter_ops_test.py diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index c8358b372a..717e913da5 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -19,6 +19,7 @@ py_library( "resampler_ops.py", "compose_ops.py", "cutout_ops.py", + "gaussian_filter_ops.py", ]), data = [ ":sparse_image_warp_test_data", @@ -190,3 +191,14 @@ py_test( ":image", ], ) +py_test( + name = "gaussian_filter_ops_test", + size = "medium", + srcs = [ + "gaussian_filter_ops.py", + ], + main = "gaussian_filter_ops_test.py", + deps = [ + ":image", + ], +) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py new file mode 100644 index 0000000000..619c24c631 --- /dev/null +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -0,0 +1,75 @@ +# 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. +# ============================================================================= +"""GaussuanBlur Op""" + + +import numpy as np +import matplotlib.pyplot as plt +import cv2 + +import tensorflow as tf + + +def gaussian_blur(img,sigma,kSize): + """ + This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel + which follows normal distribution then convolves the image with the kernel.Presently it works only on + grayscale images. + Args: + img: A tensor of shape + (batch_size, height, width, channels) + (NHWC), (batch_size, channels, height, width)(NCHW). + sigma:A constant of type float64. It is the standard deviation of the normal distribution. + The more the sigma, the more the blurring effect. + G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) + kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. + A kernel of size [kSize*kSize] is generated. + """ + + if(sigma==0): + raise ValueError("Sigma should not be zero") + + if(kSize%2==0): + raise ValueError("kSize should be odd") + + + gaussianFilter=tf.Variable(tf.zeros(shape=(kSize,kSize),dtype=tf.float64)) + gaussianFilter=findKernel(sigma,kSize,gaussianFilter) + + gaussianKernel=(tf.expand_dims(gaussianFilter,axis=2)) + gaussianKernel=(tf.expand_dims(gaussianKernel,axis=2)) + + gaussian_filter_shape=gaussianKernel.get_shape() + conv_ops=tf.nn.convolution(input=img, + filters=gaussianKernel, + padding='SAME') + return conv_ops + + +def findKernel(sigma,kSize,gaussianFilter): + "This function creates a kernel of size [kSize*kSize]" + rowFilter=tf.Variable(tf.zeros(kSize,dtype=tf.float64),dtype=tf.float64) + for i in range(-kSize//2,kSize//2+1): + for j in range(-kSize//2,kSize//2+1): + rowFilter[j+kSize//2].assign(1/(2*np.pi*(sigma)**2)*tf.exp(tf.cast(-(i**2+j**2)/(2*sigma**2.00),tf.float64))) + + gaussianFilter[i+kSize//2].assign(rowFilter) + #print(gaussianFilter) + return gaussianFilter + + + + + diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py new file mode 100644 index 0000000000..af01d9c284 --- /dev/null +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -0,0 +1,48 @@ +from gaussian_filter_ops import gaussian_blur +import cv2 +import numpy as np +import tensorflow as tf +import matplotlib.pyplot as plt + +import pytest + +def test(dtype,batch,hight,width): + test_image_tf=tf.zeros([batch,width,hight,1],dtype=dtype) + gb=gaussian_blur(test_image_tf,1,7) + gb=gb.numpy() + gb1=np.resize(gb,(width,hight)) + + plt.imshow(gb1,'gray') + #plt.show() + #print(gb) + test_image_cv=np.zeros([40,40]) + gb2=cv2.GaussianBlur(test_image_cv,(7,7),1) + plt.imshow(gb2,'gray') + #plt.show() + accuracy=0 + #print(gb1) + #print(gb2) + for i in range(len(gb1)): + for j in range(len(gb1[0])): + if(gb1[i][j]==gb2[i][j]): + accuracy+=1 + print("Accuracy w.r.t opencv=",accuracy/(len(gb1)*len(gb1[0]))) + + +def testWithImage(path): + img=cv2.imread(path,cv2.IMREAD_GRAYSCALE ) + + img=np.expand_dims(img,axis=2) + img=np.expand_dims(img,axis=0) + img_test_tensor=tf.convert_to_tensor(img,dtype=tf.float64) + + gb=gaussian_blur(img_test_tensor,1,7) + print(img_test_tensor) + gb=gb.numpy() + gb1=np.resize(gb,(img.size()[1],img.size()[0])) + plt.imshow(gb1,'gray') + plt.show() +test(tf.float64,1,40,30) +#testWithImage("sample_grayscale.jpg") + + From ca68de659e8ef5ca323681db4d38de029277d415 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:10:57 +0530 Subject: [PATCH 04/71] Delete averageSmoothing.py remove averageSmoothing.py --- averageSmoothing.py | 82 --------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 averageSmoothing.py diff --git a/averageSmoothing.py b/averageSmoothing.py deleted file mode 100644 index c3f6cab1ae..0000000000 --- a/averageSmoothing.py +++ /dev/null @@ -1,82 +0,0 @@ -# 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. -# ============================================================================= -"""AverageSmoothing Op""" - - -import numpy as np -import matplotlib.pyplot as plt -import cv2 -from tensorflow.python.ops import nn_ops -from tensorflow.python.framework import tensor_shape -import tensorflow as tf - - -def getImageHeightWidth(images,dataFormat): - if(dataFormat=='channels_last'): - h,w=tf.shape(images)[1],tf.shape(images)[0] - else: - h,w=tf.shape(images)[2], tf.shape(images)[3] - return h,w - -class AverageSmooth(): - """ - This class is responsible for having Average Smoothing. It takes the image as input, computes an average-smoothing- - kernel then convolves the image with the kernel. - Args: - img: A tensor of shape - (batch_size, height, width, channels) - (NHWC), (batch_size, channels, height, width)(NCHW). - - kSize:It is the tuple of shape(height,width). - A kernel of size [height*width] is generated. - """ - def __init__(self,img,kSize): - - - - self.kHeight=kSize[0] - self.kWidth=kSize[1] - self.gaussianKernelNumpy=(np.ones([self.kHeight,self.kWidth])) - self.findKernel() - self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) - self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) - self.img=tf.convert_to_tensor(img) - self.gaussianKernelTensor=tf.convert_to_tensor(self.gaussianKernelNumpy) - gaussian_filter_shape=self.gaussianKernelTensor.get_shape() - self.conv_ops=nn_ops.Convolution(input_shape=img.get_shape(), - filter_shape=gaussian_filter_shape, - padding='SAME') - - - def findKernel(self): - "This function creates a kernel of size [height*width]" - for i in range (self.kHeight): - for j in range(self.kWidth): - self.gaussianKernelNumpy[i][j]=1/(self.kHeight*self.kWidth) - return - - - - def convolve(self): - "This function is responsible for convolving the given image with the Gaussian Kernel" - out=self.conv_ops(self.img,self.gaussianKernelTensor) - return out - - -#with tf.session() as sess: - - - - From 60be4463131a5a8f933da58e62dfa73299162efe Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:11:27 +0530 Subject: [PATCH 05/71] Delete averageSmoothing_test.py --- averageSmoothing_test.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 averageSmoothing_test.py diff --git a/averageSmoothing_test.py b/averageSmoothing_test.py deleted file mode 100644 index 936c8c4477..0000000000 --- a/averageSmoothing_test.py +++ /dev/null @@ -1,13 +0,0 @@ -from averageSmoothing import AverageSmooth -import tensorflow as tf - - - -def test(dtype,batch,hight,width): - test_image=tf.ones([batch,width,hight,1],dtype=dtype) - gb=AverageSmooth(test_image,(3,4)) - print(gb.convolve()) - -test(tf.float64,1,40,40) - - From 1c89c6b47f197c51d8e702abfa598ce950abc8a8 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:11:47 +0530 Subject: [PATCH 06/71] Delete gaussianBlur_ops.py --- gaussianBlur_ops.py | 86 --------------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 gaussianBlur_ops.py diff --git a/gaussianBlur_ops.py b/gaussianBlur_ops.py deleted file mode 100644 index ea1969778b..0000000000 --- a/gaussianBlur_ops.py +++ /dev/null @@ -1,86 +0,0 @@ -# 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. -# ============================================================================= -"""GaussuanBlur Op""" - - -import numpy as np -import matplotlib.pyplot as plt -import cv2 -from tensorflow.python.ops import nn_ops -from tensorflow.python.framework import tensor_shape -import tensorflow as tf - - -def getImageHeightWidth(images,dataFormat): - if(dataFormat=='channels_last'): - h,w=tf.shape(images)[1],tf.shape(images)[0] - else: - h,w=tf.shape(images)[2], tf.shape(images)[3] - return h,w - -class GaussianBlur(): - """ - This class is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel - which follows normal distribution then convolves the image with the kernel. - Args: - img: A tensor of shape - (batch_size, height, width, channels) - (NHWC), (batch_size, channels, height, width)(NCHW). - sigma:A constant of type float64. It is the standard deviation of the normal distribution. - The more the sigma, the more the blurring effect. - G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) - kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. - A kernel of size [kSize*kSize] is generated. - """ - def __init__(self,img,sigma,kSize): - if(sigma==0): - raise ValueError("Sigma should not be zero") - self.sigma=sigma - if(kSize%2==0): - raise ValueError("kSize should be odd") - - self.kSize=kSize - self.gaussianKernelNumpy=(np.zeros([kSize,kSize])) - self.findKernel() - self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) - self.gaussianKernelNumpy=np.expand_dims(self.gaussianKernelNumpy,axis=2) - self.img=tf.convert_to_tensor(img) - self.gaussianKernelTensor=tf.convert_to_tensor(self.gaussianKernelNumpy) - gaussian_filter_shape=self.gaussianKernelTensor.get_shape() - self.conv_ops=nn_ops.Convolution(input_shape=img.get_shape(), - filter_shape=gaussian_filter_shape, - padding='SAME') - - - def findKernel(self): - "This function creates a kernel of size [kSize*kSize]" - for i in range(-self.kSize//2,self.kSize//2+1): - for j in range(-self.kSize//2,self.kSize//2+1): - self.gaussianKernelNumpy[i+self.kSize//2][j+self.kSize//2]=1/(2*np.pi*(self.sigma)**2)*np.exp(-(i**2+j**2)/(2*self.sigma**2)) - return - - - - def convolve(self): - "This function is responsible for convolving the given image with the Gaussian Kernel" - out=self.conv_ops(self.img,self.gaussianKernelTensor) - return out - - -#with tf.session() as sess: - - - - From 725c3c2bca3dceaf8d549938071938164edb890a Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:12:03 +0530 Subject: [PATCH 07/71] Delete gaussianBlur_ops_test.py --- gaussianBlur_ops_test.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 gaussianBlur_ops_test.py diff --git a/gaussianBlur_ops_test.py b/gaussianBlur_ops_test.py deleted file mode 100644 index a299169dc7..0000000000 --- a/gaussianBlur_ops_test.py +++ /dev/null @@ -1,13 +0,0 @@ -from gaussianBlur_ops import GaussianBlur -import tensorflow as tf - - - -def test(dtype,batch,hight,width): - test_image=tf.ones([batch,width,hight,1],dtype=dtype) - gb=GaussianBlur(test_image,1,7) - print(gb.convolve()) - -test(tf.float64,1,40,40) - - From f6a952fe5d698047a92e15c6f7a96fb13257a4d2 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:12:03 +0530 Subject: [PATCH 08/71] Delete gaussianBlur_ops_test.py --- gaussianBlur_ops_test.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 gaussianBlur_ops_test.py diff --git a/gaussianBlur_ops_test.py b/gaussianBlur_ops_test.py deleted file mode 100644 index a299169dc7..0000000000 --- a/gaussianBlur_ops_test.py +++ /dev/null @@ -1,13 +0,0 @@ -from gaussianBlur_ops import GaussianBlur -import tensorflow as tf - - - -def test(dtype,batch,hight,width): - test_image=tf.ones([batch,width,hight,1],dtype=dtype) - gb=GaussianBlur(test_image,1,7) - print(gb.convolve()) - -test(tf.float64,1,40,40) - - From 462f5db34878034e4ccc3cf1d8757d0ae1f7a0a0 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 28 Mar 2020 22:50:36 +0530 Subject: [PATCH 09/71] improved test --- .../image/gaussian_filter_ops.py | 2 + .../image/gaussian_filter_ops_test.py | 50 ++++++++----------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 619c24c631..4611ef61c0 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -67,6 +67,8 @@ def findKernel(sigma,kSize,gaussianFilter): gaussianFilter[i+kSize//2].assign(rowFilter) #print(gaussianFilter) + s=tf.math.reduce_sum(gaussianFilter) + gaussianFilter=tf.math.divide(gaussianFilter,s) return gaussianFilter diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index af01d9c284..a2646460e7 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -1,48 +1,38 @@ -from gaussian_filter_ops import gaussian_blur +from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur +#from gaussian_filter_ops import gaussian_blur import cv2 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import pytest +fp1=open("file1.txt","w") +fp2=open("file2.txt","w") -def test(dtype,batch,hight,width): - test_image_tf=tf.zeros([batch,width,hight,1],dtype=dtype) + +def test(): + test_image_tf=tf.random.uniform([1,40,40,1],minval=0,maxval=255,dtype=tf.float64) + gb=gaussian_blur(test_image_tf,1,7) gb=gb.numpy() - gb1=np.resize(gb,(width,hight)) + gb1=np.resize(gb,(40,40)) + + + test_image_cv=test_image_tf.numpy() + test_image_cv=np.resize(test_image_cv,[40,40]) - plt.imshow(gb1,'gray') - #plt.show() - #print(gb) - test_image_cv=np.zeros([40,40]) gb2=cv2.GaussianBlur(test_image_cv,(7,7),1) - plt.imshow(gb2,'gray') - #plt.show() + accuracy=0 - #print(gb1) - #print(gb2) + for i in range(len(gb1)): for j in range(len(gb1[0])): - if(gb1[i][j]==gb2[i][j]): + if(abs(gb1[i][j]-gb2[i][j])<10): accuracy+=1 - print("Accuracy w.r.t opencv=",accuracy/(len(gb1)*len(gb1[0]))) - -def testWithImage(path): - img=cv2.imread(path,cv2.IMREAD_GRAYSCALE ) - - img=np.expand_dims(img,axis=2) - img=np.expand_dims(img,axis=0) - img_test_tensor=tf.convert_to_tensor(img,dtype=tf.float64) - - gb=gaussian_blur(img_test_tensor,1,7) - print(img_test_tensor) - gb=gb.numpy() - gb1=np.resize(gb,(img.size()[1],img.size()[0])) - plt.imshow(gb1,'gray') - plt.show() -test(tf.float64,1,40,30) -#testWithImage("sample_grayscale.jpg") + print(accuracy/1600) + assert accuracy>=.80 + +test() From 2b133f5da6f9dc288e2635cc8d6f46fd2a563723 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 28 Mar 2020 23:04:38 +0530 Subject: [PATCH 10/71] improved test --- tensorflow_addons/image/gaussian_filter_ops_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index a2646460e7..68e91d2d18 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -6,8 +6,7 @@ import matplotlib.pyplot as plt import pytest -fp1=open("file1.txt","w") -fp2=open("file2.txt","w") + def test(): From 494d4f83fe3692cb34927107b8b6ffebea8c241c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 28 Mar 2020 23:39:31 +0530 Subject: [PATCH 11/71] added main --- tensorflow_addons/image/gaussian_filter_ops_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 68e91d2d18..cacf240845 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -33,5 +33,5 @@ def test(): print(accuracy/1600) assert accuracy>=.80 -test() - +if __name__ == "__main__": + sys.exit(pytest.main([__file__])) From f82e21ea1527f25615ad533e8ff05074d9f0be58 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sat, 28 Mar 2020 23:47:56 +0530 Subject: [PATCH 12/71] gaussian --- .../image/gaussian_filter_ops_test.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index cacf240845..def6a3919e 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -1,10 +1,29 @@ +# 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. +# ============================================================================== +"""Tests for gaussian blur.""" + + + + from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur #from gaussian_filter_ops import gaussian_blur import cv2 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt - +import sys import pytest From a0eba389752a4257a73be4cf85b52385732b6f89 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 00:10:46 +0530 Subject: [PATCH 13/71] test for gaussian Blur --- .../image/gaussian_filter_ops_test.py | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index def6a3919e..50b4a90d4e 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -15,10 +15,9 @@ """Tests for gaussian blur.""" - - from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur -#from gaussian_filter_ops import gaussian_blur + +# from gaussian_filter_ops import gaussian_blur import cv2 import numpy as np import tensorflow as tf @@ -27,30 +26,30 @@ import pytest - def test(): - test_image_tf=tf.random.uniform([1,40,40,1],minval=0,maxval=255,dtype=tf.float64) - - gb=gaussian_blur(test_image_tf,1,7) - gb=gb.numpy() - gb1=np.resize(gb,(40,40)) - - - test_image_cv=test_image_tf.numpy() - test_image_cv=np.resize(test_image_cv,[40,40]) - - gb2=cv2.GaussianBlur(test_image_cv,(7,7),1) - - accuracy=0 - - for i in range(len(gb1)): - for j in range(len(gb1[0])): - if(abs(gb1[i][j]-gb2[i][j])<10): - accuracy+=1 - - - print(accuracy/1600) - assert accuracy>=.80 - + test_image_tf = tf.random.uniform( + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + ) + + gb = gaussian_blur(test_image_tf, 1, 7) + gb = gb.numpy() + gb1 = np.resize(gb, (40, 40)) + + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [40, 40]) + + gb2 = cv2.GaussianBlur(test_image_cv, (7, 7), 1) + + accuracy = 0 + + for i in range(len(gb1)): + for j in range(len(gb1[0])): + if abs(gb1[i][j] - gb2[i][j]) < 10: + accuracy += 1 + + print(accuracy / 1600) + assert accuracy >= 0.80 + + if __name__ == "__main__": sys.exit(pytest.main([__file__])) From 4d2f5ee58700b12f4774de5803a3dcdd70e0f2af Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 00:51:58 +0530 Subject: [PATCH 14/71] after flake test --- .../image/gaussian_filter_ops.py | 82 ++++++++----------- .../image/gaussian_filter_ops_test.py | 3 - 2 files changed, 35 insertions(+), 50 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 4611ef61c0..d600caf394 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -16,62 +16,50 @@ import numpy as np -import matplotlib.pyplot as plt -import cv2 - import tensorflow as tf def gaussian_blur(img,sigma,kSize): - """ - This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel - which follows normal distribution then convolves the image with the kernel.Presently it works only on - grayscale images. - Args: - img: A tensor of shape + """ + This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel + which follows normal distribution then convolves the image with the kernel.Presently it works only on + grayscale images. + Args: + img: A tensor of shape (batch_size, height, width, channels) (NHWC), (batch_size, channels, height, width)(NCHW). sigma:A constant of type float64. It is the standard deviation of the normal distribution. - The more the sigma, the more the blurring effect. - G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) + The more the sigma, the more the blurring effect. + G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. - A kernel of size [kSize*kSize] is generated. - """ - - if(sigma==0): - raise ValueError("Sigma should not be zero") - - if(kSize%2==0): - raise ValueError("kSize should be odd") - - - gaussianFilter=tf.Variable(tf.zeros(shape=(kSize,kSize),dtype=tf.float64)) - gaussianFilter=findKernel(sigma,kSize,gaussianFilter) + A kernel of size [kSize*kSize] is generated. + """ - gaussianKernel=(tf.expand_dims(gaussianFilter,axis=2)) - gaussianKernel=(tf.expand_dims(gaussianKernel,axis=2)) - - gaussian_filter_shape=gaussianKernel.get_shape() - conv_ops=tf.nn.convolution(input=img, - filters=gaussianKernel, - padding='SAME') - return conv_ops - - -def findKernel(sigma,kSize,gaussianFilter): - "This function creates a kernel of size [kSize*kSize]" - rowFilter=tf.Variable(tf.zeros(kSize,dtype=tf.float64),dtype=tf.float64) - for i in range(-kSize//2,kSize//2+1): - for j in range(-kSize//2,kSize//2+1): - rowFilter[j+kSize//2].assign(1/(2*np.pi*(sigma)**2)*tf.exp(tf.cast(-(i**2+j**2)/(2*sigma**2.00),tf.float64))) - - gaussianFilter[i+kSize//2].assign(rowFilter) - #print(gaussianFilter) - s=tf.math.reduce_sum(gaussianFilter) - gaussianFilter=tf.math.divide(gaussianFilter,s) - return gaussianFilter + if(sigma == 0): + raise ValueError("Sigma should not be zero") + + if(kSize % 2 == 0): + raise ValueError("kSize should be odd") - + gaussianFilter = tf.Variable(tf.zeros(shape=(kSize,kSize),dtype=tf.float64)) + gaussianFilter = findKernel(sigma,kSize,gaussianFilter) - + gaussianKernel = (tf.expand_dims(gaussianFilter,axis=2)) + gaussianKernel = (tf.expand_dims(gaussianKernel,axis=2)) + conv_ops = tf.nn.convolution(input=img, + filters=gaussianKernel, + padding='SAME') + return conv_ops + + +def findKernel(sigma,kSize,gaussianFilter): + "This function creates a kernel of size [kSize*kSize]" + rowFilter = tf.Variable(tf.zeros(kSize,dtype=tf.float64),dtype=tf.float64) + for i in range(-kSize//2,kSize//2+1): + for j in range(-kSize//2,kSize//2+1): + rowFilter[j+kSize//2].assign(1/(2*np.pi*(sigma)**2)*tf.exp(tf.cast(-(i**2+j**2)/(2*sigma**2.00),tf.float64))) + gaussianFilter[i+kSize//2].assign(rowFilter) + s = tf.math.reduce_sum(gaussianFilter) + gaussianFilter = tf.math.divide(gaussianFilter,s) + return gaussianFilter diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 50b4a90d4e..7ec1213802 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -16,12 +16,9 @@ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur - -# from gaussian_filter_ops import gaussian_blur import cv2 import numpy as np import tensorflow as tf -import matplotlib.pyplot as plt import sys import pytest From d56257889207aae64591e5c1cec95eb0d357b17c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 01:52:06 +0530 Subject: [PATCH 15/71] python black --- .../image/gaussian_filter_ops.py | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index d600caf394..73de7ef9a4 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -19,7 +19,7 @@ import tensorflow as tf -def gaussian_blur(img,sigma,kSize): +def gaussian_blur(img, sigma, kSize): """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel which follows normal distribution then convolves the image with the kernel.Presently it works only on @@ -35,31 +35,33 @@ def gaussian_blur(img,sigma,kSize): A kernel of size [kSize*kSize] is generated. """ - if(sigma == 0): + if sigma == 0: raise ValueError("Sigma should not be zero") - if(kSize % 2 == 0): + if kSize % 2 == 0: raise ValueError("kSize should be odd") - gaussianFilter = tf.Variable(tf.zeros(shape=(kSize,kSize),dtype=tf.float64)) - gaussianFilter = findKernel(sigma,kSize,gaussianFilter) + gaussianFilter = tf.Variable(tf.zeros(shape=(kSize, kSize), dtype=tf.float64)) + gaussianFilter = findKernel(sigma, kSize, gaussianFilter) - gaussianKernel = (tf.expand_dims(gaussianFilter,axis=2)) - gaussianKernel = (tf.expand_dims(gaussianKernel,axis=2)) + gaussianKernel = tf.expand_dims(gaussianFilter, axis=2) + gaussianKernel = tf.expand_dims(gaussianKernel, axis=2) - conv_ops = tf.nn.convolution(input=img, - filters=gaussianKernel, - padding='SAME') + conv_ops = tf.nn.convolution(input=img, filters=gaussianKernel, padding="SAME") return conv_ops -def findKernel(sigma,kSize,gaussianFilter): +def findKernel(sigma, kSize, gaussianFilter): "This function creates a kernel of size [kSize*kSize]" - rowFilter = tf.Variable(tf.zeros(kSize,dtype=tf.float64),dtype=tf.float64) - for i in range(-kSize//2,kSize//2+1): - for j in range(-kSize//2,kSize//2+1): - rowFilter[j+kSize//2].assign(1/(2*np.pi*(sigma)**2)*tf.exp(tf.cast(-(i**2+j**2)/(2*sigma**2.00),tf.float64))) - gaussianFilter[i+kSize//2].assign(rowFilter) + rowFilter = tf.Variable(tf.zeros(kSize, dtype=tf.float64), dtype=tf.float64) + for i in range(-kSize // 2, kSize // 2 + 1): + for j in range(-kSize // 2, kSize // 2 + 1): + rowFilter[j + kSize // 2].assign( + 1 + / (2 * np.pi * (sigma) ** 2) + * tf.exp(tf.cast(-(i ** 2 + j ** 2) / (2 * sigma ** 2.00), tf.float64)) + ) + gaussianFilter[i + kSize // 2].assign(rowFilter) s = tf.math.reduce_sum(gaussianFilter) - gaussianFilter = tf.math.divide(gaussianFilter,s) + gaussianFilter = tf.math.divide(gaussianFilter, s) return gaussianFilter From 8c79b0732c9e19478d2f128255dc7d87554bb8bb Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 02:31:01 +0530 Subject: [PATCH 16/71] Codeowners --- .github/CODEOWNERS | 1 + tensorflow_addons/image/gaussian_filter_ops_test.py | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 1382800cd4..d9aae48567 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -39,6 +39,7 @@ /tensorflow_addons/image/connected_components*.py @sayoojbk /tensorflow_addons/image/cutout_ops*.py @fsx950223 /tensorflow_addons/image/dense_image_warp*.py @windQAQ +/tensorflow_addons/image/gaussian_filter_ops*.py @ghosalsattam /tensorflow_addons/image/distance_transform*.py @mels630 /tensorflow_addons/image/distort_image_ops*.py @windqaq /tensorflow_addons/image/filters*.py @mainak431 diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 7ec1213802..137aa25002 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -23,6 +23,7 @@ import pytest +@pytest.mark.usefixtures("maybe_run_functions_eagerly") def test(): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 From a2e3950e02f9918cef9ea19032dda32563c953ee Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 02:56:11 +0530 Subject: [PATCH 17/71] BUILD --- tensorflow_addons/image/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 717e913da5..c168c91f45 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -193,7 +193,7 @@ py_test( ) py_test( name = "gaussian_filter_ops_test", - size = "medium", + size = "small", srcs = [ "gaussian_filter_ops.py", ], From f8f5ca2f4578d2768c23b2d5bdc0871e117f30b3 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 03:06:31 +0530 Subject: [PATCH 18/71] __init__ --- tensorflow_addons/image/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index 61cefcb29c..1045703509 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -33,3 +33,4 @@ from tensorflow_addons.image.translate_ops import translate_xy from tensorflow_addons.image.compose_ops import blend from tensorflow_addons.image.cutout_ops import random_cutout, cutout +from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur From 83128e4c1d741c8d8f2423d745d00ac81441de79 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 10:12:47 +0530 Subject: [PATCH 19/71] gaussian_blur --- tensorflow_addons/image/gaussian_filter_ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 73de7ef9a4..562cd57fd0 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -17,9 +17,10 @@ import numpy as np import tensorflow as tf +from tensorflow_addons.utils.types import FloatTensorLike, Number -def gaussian_blur(img, sigma, kSize): +def gaussian_blur(img: FloatTensorLike, sigma: tf.float64, kSize: tf.float64): """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel which follows normal distribution then convolves the image with the kernel.Presently it works only on From 7b0186519f2588fca75a64026d4b945ef59326a6 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 10:26:31 +0530 Subject: [PATCH 20/71] ci test --- tensorflow_addons/image/gaussian_filter_ops.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 562cd57fd0..aab547a52a 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -17,10 +17,12 @@ import numpy as np import tensorflow as tf -from tensorflow_addons.utils.types import FloatTensorLike, Number +from tensorflow_addons.utils.types import FloatTensorLike -def gaussian_blur(img: FloatTensorLike, sigma: tf.float64, kSize: tf.float64): +def gaussian_blur(img: FloatTensorLike, + sigma: tf.float64, + kSize: tf.float64)->FloatTensorLike: """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel which follows normal distribution then convolves the image with the kernel.Presently it works only on From f27eb5d6060524bc791fda3ced2003be29fe2c7f Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 11:06:20 +0530 Subject: [PATCH 21/71] github check --- tensorflow_addons/image/gaussian_filter_ops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index aab547a52a..9b47e8f460 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -20,9 +20,9 @@ from tensorflow_addons.utils.types import FloatTensorLike -def gaussian_blur(img: FloatTensorLike, - sigma: tf.float64, - kSize: tf.float64)->FloatTensorLike: +def gaussian_blur( + img: FloatTensorLike, sigma: tf.float64, kSize: tf.float64 +) -> FloatTensorLike: """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel which follows normal distribution then convolves the image with the kernel.Presently it works only on From b189c349d6cc443bd6e48f8d1144491e9e69f4e9 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 13:54:51 +0530 Subject: [PATCH 22/71] github check --- tensorflow_addons/image/BUILD | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index c168c91f45..3560949c59 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -191,11 +191,12 @@ py_test( ":image", ], ) + py_test( name = "gaussian_filter_ops_test", size = "small", srcs = [ - "gaussian_filter_ops.py", + "gaussian_filter_ops_test.py", ], main = "gaussian_filter_ops_test.py", deps = [ From 169a33fdba3d3113d1b651d501bc147de5afeaa3 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 15:08:49 +0530 Subject: [PATCH 23/71] github check --- tensorflow_addons/utils/ensure_tf_install.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/utils/ensure_tf_install.py b/tensorflow_addons/utils/ensure_tf_install.py index e5dcae52fe..faa6621097 100644 --- a/tensorflow_addons/utils/ensure_tf_install.py +++ b/tensorflow_addons/utils/ensure_tf_install.py @@ -47,7 +47,7 @@ def _ensure_tf_install(): """ # Update this whenever we need to depend on a newer TensorFlow release. - required_tf_version = "2.1.0" + required_tf_version = "2.2.0" if LooseVersion(tf.__version__) != LooseVersion(required_tf_version): message = warning_template.format( From 32b2ae95a1cf39622ba5a20482fd22159cf73b1c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 16:02:06 +0530 Subject: [PATCH 24/71] github test --- tensorflow_addons/utils/ensure_tf_install.py | 2 +- tools/install_deps/pytest.txt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/utils/ensure_tf_install.py b/tensorflow_addons/utils/ensure_tf_install.py index faa6621097..e5dcae52fe 100644 --- a/tensorflow_addons/utils/ensure_tf_install.py +++ b/tensorflow_addons/utils/ensure_tf_install.py @@ -47,7 +47,7 @@ def _ensure_tf_install(): """ # Update this whenever we need to depend on a newer TensorFlow release. - required_tf_version = "2.2.0" + required_tf_version = "2.1.0" if LooseVersion(tf.__version__) != LooseVersion(required_tf_version): message = warning_template.format( diff --git a/tools/install_deps/pytest.txt b/tools/install_deps/pytest.txt index 784f443281..f716f90e63 100644 --- a/tools/install_deps/pytest.txt +++ b/tools/install_deps/pytest.txt @@ -2,4 +2,5 @@ pytest~=5.3 pytest-xdist~=1.31 scikit-learn~=0.22 scikit-image~=0.15.0 -Pillow~=7.0.0 \ No newline at end of file +Pillow~=7.0.0 +opencv~=4.1.1 From 3eeca8aadf4077d0d8ad4c82b4cedd45e0068e46 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 16:10:28 +0530 Subject: [PATCH 25/71] github check --- tools/install_deps/pytest.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/install_deps/pytest.txt b/tools/install_deps/pytest.txt index f716f90e63..b3fd6b16ba 100644 --- a/tools/install_deps/pytest.txt +++ b/tools/install_deps/pytest.txt @@ -3,4 +3,4 @@ pytest-xdist~=1.31 scikit-learn~=0.22 scikit-image~=0.15.0 Pillow~=7.0.0 -opencv~=4.1.1 +opencv-python~=4.1.1 From a4d32aa47cc3570c7546ea8ff8b61c3da0292525 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 17:49:56 +0530 Subject: [PATCH 26/71] github check --- tensorflow_addons/image/gaussian_filter_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 137aa25002..327bfd2408 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -16,8 +16,8 @@ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur -import cv2 import numpy as np +import cv2 import tensorflow as tf import sys import pytest From 4b1fa53ece90218270c7c1a32594b51da3fad6dd Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Sun, 29 Mar 2020 18:22:15 +0530 Subject: [PATCH 27/71] github check --- tensorflow_addons/image/gaussian_filter_ops_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 327bfd2408..d36a90efe7 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -17,6 +17,7 @@ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur import numpy as np +import numpy.core.multiarray import cv2 import tensorflow as tf import sys From 7bd60d50754b12cf53434607c71c01c2262b5ce2 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 01:46:31 +0530 Subject: [PATCH 28/71] git check --- tensorflow_addons/image/gaussian_filter_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index d36a90efe7..b0381ae093 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -"""Tests for gaussian blur.""" +""" Tests for gaussian blur. """ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur From f796c9b66ab5177d6eb5d000f59299c04b4a98ee Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 02:12:38 +0530 Subject: [PATCH 29/71] github check --- tensorflow_addons/image/gaussian_filter_ops_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index b0381ae093..5bf2be97c1 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -18,7 +18,7 @@ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur import numpy as np import numpy.core.multiarray -import cv2 +from scipy.ndimage import gaussian_filter import tensorflow as tf import sys import pytest @@ -37,7 +37,7 @@ def test(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = cv2.GaussianBlur(test_image_cv, (7, 7), 1) + gb2 = gaussian_filter(test_image_cv, 1) accuracy = 0 From 8b8286bb7113583e078b665dd94d7fb8e2320b17 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 02:17:14 +0530 Subject: [PATCH 30/71] github check --- tensorflow_addons/image/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index 54c092ba12..29d47e0cc1 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -39,4 +39,3 @@ from tensorflow_addons.image.compose_ops import blend from tensorflow_addons.image.cutout_ops import random_cutout, cutout from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur - From bf13d8504b769488fcb5ff42b0f025c73504645c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 02:31:59 +0530 Subject: [PATCH 31/71] check --- tensorflow_addons/image/BUILD | 3 +-- tensorflow_addons/image/__init__.py | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 7341094967..bb7a770a7d 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -28,8 +28,7 @@ py_library( "compose_ops.py", "cutout_ops.py", "gaussian_filter_ops.py", -r - ]), +]), data = [ ":sparse_image_warp_test_data", "//tensorflow_addons/custom_ops/image:_distort_image_ops.so", diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index 29d47e0cc1..c0fc87e016 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -35,7 +35,4 @@ from tensorflow_addons.image.transform_ops import transform from tensorflow_addons.image.translate_ops import translate from tensorflow_addons.image.translate_ops import translate_xy - -from tensorflow_addons.image.compose_ops import blend -from tensorflow_addons.image.cutout_ops import random_cutout, cutout from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur From 9f1d492878800eb24546419241668f7678494587 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 02:40:48 +0530 Subject: [PATCH 32/71] check --- tensorflow_addons/image/BUILD | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index bb7a770a7d..cbe0cc153e 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -20,13 +20,6 @@ py_library( "transform_ops.py", "translate_ops.py", "utils.py", - - "sparse_image_warp.py", - "interpolate_spline.py", - "connected_components.py", - "resampler_ops.py", - "compose_ops.py", - "cutout_ops.py", "gaussian_filter_ops.py", ]), data = [ From cb321aaddb6e872cdbf998d3814b19e6ed0858ba Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 10:27:28 +0530 Subject: [PATCH 33/71] ci check --- tensorflow_addons/image/BUILD | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index cbe0cc153e..66eda0878e 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -133,6 +133,18 @@ py_test( ], ) +py_test( + name = "gaussian_filter_ops_test", + size = "small", + srcs = [ + "gaussian_filter_ops_test.py", + ], + main = "gaussian_filter_ops_test.py", + deps = [ + ":image", + ], +) + py_test( name = "interpolate_spline_test", size = "medium", @@ -205,14 +217,3 @@ py_test( ], ) -py_test( - name = "gaussian_filter_ops_test", - size = "small", - srcs = [ - "gaussian_filter_ops_test.py", - ], - main = "gaussian_filter_ops_test.py", - deps = [ - ":image", - ], -) From 08e763268249318d8bd67305c6ef0e305b1355c5 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 11:18:54 +0530 Subject: [PATCH 34/71] check --- tensorflow_addons/image/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 66eda0878e..c81e741601 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -14,14 +14,14 @@ py_library( "distance_transform.py", "distort_image_ops.py", "filters.py", + "gaussian_filters_op.py", "interpolate_spline.py", "resampler_ops.py", "sparse_image_warp.py", "transform_ops.py", "translate_ops.py", "utils.py", - "gaussian_filter_ops.py", -]), + ]), data = [ ":sparse_image_warp_test_data", "//tensorflow_addons/custom_ops/image:_distort_image_ops.so", From dfb9e9adb74ad0985dc45000ed491338b4fcd518 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 11:22:04 +0530 Subject: [PATCH 35/71] test --- tensorflow_addons/image/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index c81e741601..9cfb43f714 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -14,7 +14,7 @@ py_library( "distance_transform.py", "distort_image_ops.py", "filters.py", - "gaussian_filters_op.py", + "gaussian_filter_ops.py", "interpolate_spline.py", "resampler_ops.py", "sparse_image_warp.py", From 1e6b7c938f27c1f8d6a48f840005086698468b9a Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 11:55:53 +0530 Subject: [PATCH 36/71] check --- tensorflow_addons/image/BUILD | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 9cfb43f714..8743790c0b 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -142,6 +142,7 @@ py_test( main = "gaussian_filter_ops_test.py", deps = [ ":image", + ":PIL", ], ) From 8060fa2fad41ae1ab08478211d5ca89701773be2 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 12:01:58 +0530 Subject: [PATCH 37/71] test --- tensorflow_addons/image/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 8743790c0b..9cfb43f714 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -142,7 +142,6 @@ py_test( main = "gaussian_filter_ops_test.py", deps = [ ":image", - ":PIL", ], ) From 73258f36b02a0c2777690b99b484f9ce06e2579c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 30 Mar 2020 12:32:47 +0530 Subject: [PATCH 38/71] github check --- tensorflow_addons/image/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 9cfb43f714..2801f38c15 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -216,4 +216,3 @@ py_test( ":image", ], ) - From 4b5993617e38889d3a46478efc956716c45141a7 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 31 Mar 2020 17:27:05 +0530 Subject: [PATCH 39/71] github check --- tensorflow_addons/image/gaussian_filter_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 5bf2be97c1..9e3b221ab5 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -""" Tests for gaussian blur. """ +""" Tests for gaussian blur. """ from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur From 07e041fdbea458c509c5fbac3d93bedc4cd25b13 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 2 Apr 2020 08:10:00 +0530 Subject: [PATCH 40/71] ci test --- tensorflow_addons/image/gaussian_filter_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/gaussian_filter_ops_test.py index 9e3b221ab5..1a4359aba9 100644 --- a/tensorflow_addons/image/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/gaussian_filter_ops_test.py @@ -27,7 +27,7 @@ @pytest.mark.usefixtures("maybe_run_functions_eagerly") def test(): test_image_tf = tf.random.uniform( - [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + [2, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 ) gb = gaussian_blur(test_image_tf, 1, 7) From ffe954759e669944c3dfe0cce7c81df90c868db3 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 2 Apr 2020 08:18:52 +0530 Subject: [PATCH 41/71] github check --- tensorflow_addons/image/BUILD | 2 -- 1 file changed, 2 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 4842cb7f8f..c33dee61e1 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -212,8 +212,6 @@ py_test( "utils_test.py", ], main = "utils_test.py", - srcs = glob(["*_test.py"]), - main = "run_all_test.py", deps = [ ":image", ], From 3d8ed07a43b8e1e66d94b28256d057db196352a0 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 6 Apr 2020 09:52:14 +0530 Subject: [PATCH 42/71] github check --- tensorflow_addons/image/BUILD | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 7cfed55175..78619b64f5 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -4,10 +4,6 @@ package(default_visibility = ["//visibility:public"]) py_library( name = "image", -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> 6fa811ba5e0e3492eb1bc089836dbe5f78bb8bf3 srcs = ([ "__init__.py", "color_ops.py", @@ -26,13 +22,6 @@ py_library( "translate_ops.py", "utils.py", ]), -<<<<<<< HEAD -======= - srcs = glob(["*.py"]), ->>>>>>> 1ffb283ca924eb33b43effc7ec2c449a595c1aff -======= - srcs = glob(["*.py"]), ->>>>>>> 6fa811ba5e0e3492eb1bc089836dbe5f78bb8bf3 data = [ ":sparse_image_warp_test_data", "//tensorflow_addons/custom_ops/image:_distort_image_ops.so", @@ -50,10 +39,6 @@ filegroup( py_test( name = "image_test", size = "small", -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> 6fa811ba5e0e3492eb1bc089836dbe5f78bb8bf3 srcs = [ "color_ops_test.py", ], @@ -227,15 +212,8 @@ py_test( "utils_test.py", ], main = "utils_test.py", -<<<<<<< HEAD -======= srcs = glob(["tests/*"]), main = "tests/run_all_test.py", ->>>>>>> 1ffb283ca924eb33b43effc7ec2c449a595c1aff -======= - srcs = glob(["tests/*"]), - main = "tests/run_all_test.py", ->>>>>>> 6fa811ba5e0e3492eb1bc089836dbe5f78bb8bf3 deps = [ ":image", ], From 0e77a2408b1c77a7aaeb3b2fd255a9e05f3b81ec Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 6 Apr 2020 09:54:31 +0530 Subject: [PATCH 43/71] github check --- tensorflow_addons/image/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 78619b64f5..9b822222c6 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -213,7 +213,6 @@ py_test( ], main = "utils_test.py", srcs = glob(["tests/*"]), - main = "tests/run_all_test.py", deps = [ ":image", ], From 2566d0f197793f33621999de6570c11b2f672180 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 6 Apr 2020 10:00:59 +0530 Subject: [PATCH 44/71] github check --- tensorflow_addons/image/BUILD | 193 +----------------- .../{ => tests}/gaussian_filter_ops_test.py | 0 2 files changed, 2 insertions(+), 191 deletions(-) rename tensorflow_addons/image/{ => tests}/gaussian_filter_ops_test.py (100%) diff --git a/tensorflow_addons/image/BUILD b/tensorflow_addons/image/BUILD index 9b822222c6..49464fb06d 100644 --- a/tensorflow_addons/image/BUILD +++ b/tensorflow_addons/image/BUILD @@ -4,24 +4,7 @@ package(default_visibility = ["//visibility:public"]) py_library( name = "image", - srcs = ([ - "__init__.py", - "color_ops.py", - "compose_ops.py", - "connected_components.py", - "cutout_ops.py", - "dense_image_warp.py", - "distance_transform.py", - "distort_image_ops.py", - "filters.py", - "gaussian_filter_ops.py", - "interpolate_spline.py", - "resampler_ops.py", - "sparse_image_warp.py", - "transform_ops.py", - "translate_ops.py", - "utils.py", - ]), + srcs = glob(["*.py"]), data = [ ":sparse_image_warp_test_data", "//tensorflow_addons/custom_ops/image:_distort_image_ops.so", @@ -39,180 +22,8 @@ filegroup( py_test( name = "image_test", size = "small", - srcs = [ - "color_ops_test.py", - ], - main = "color_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "compose_ops_test", - size = "medium", - srcs = [ - "compose_ops_test.py", - ], - main = "compose_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "connected_components_test", - size = "medium", - srcs = [ - "connected_components_test.py", - ], - main = "connected_components_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "cutout_ops_test", - size = "small", - srcs = [ - "cutout_ops_test.py", - ], - main = "cutout_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "dense_image_warp_test", - size = "small", - srcs = [ - "dense_image_warp_test.py", - ], - main = "dense_image_warp_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "distance_transform_ops_test", - size = "small", - srcs = [ - "distance_transform_test.py", - ], - main = "distance_transform_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "distort_image_ops_test", - size = "small", - srcs = [ - "distort_image_ops_test.py", - ], - main = "distort_image_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "filters_test", - size = "medium", - srcs = [ - "filters_test.py", - ], - flaky = True, - main = "filters_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "gaussian_filter_ops_test", - size = "small", - srcs = [ - "gaussian_filter_ops_test.py", - ], - main = "gaussian_filter_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "interpolate_spline_test", - size = "medium", - srcs = [ - "interpolate_spline_test.py", - ], - main = "interpolate_spline_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "resampler_ops_test", - size = "medium", - srcs = [ - "resampler_ops_test.py", - ], - main = "resampler_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "sparse_image_warp_test", - size = "medium", - srcs = [ - "sparse_image_warp_test.py", - ], - main = "sparse_image_warp_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "transform_ops_test", - size = "medium", - srcs = [ - "transform_ops_test.py", - ], - main = "transform_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "translate_ops_test", - size = "medium", - srcs = [ - "translate_ops_test.py", - ], - main = "translate_ops_test.py", - deps = [ - ":image", - ], -) - -py_test( - name = "utils_test", - size = "small", - srcs = [ - "utils_test.py", - ], - main = "utils_test.py", srcs = glob(["tests/*"]), + main = "tests/run_all_test.py", deps = [ ":image", ], diff --git a/tensorflow_addons/image/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py similarity index 100% rename from tensorflow_addons/image/gaussian_filter_ops_test.py rename to tensorflow_addons/image/tests/gaussian_filter_ops_test.py From ee996e9258883d812e7f7f2a2143c526d1538033 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 6 Apr 2020 10:04:58 +0530 Subject: [PATCH 45/71] github check --- tools/install_deps/pytest.txt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tools/install_deps/pytest.txt b/tools/install_deps/pytest.txt index 9dec37eb01..ae9ed3d875 100644 --- a/tools/install_deps/pytest.txt +++ b/tools/install_deps/pytest.txt @@ -3,14 +3,5 @@ pytest-xdist~=1.31 scikit-learn~=0.22 scikit-image~=0.15.0 Pillow~=7.0.0 -<<<<<<< HEAD -<<<<<<< HEAD opencv-python~=4.1.1 -======= tqdm>=4.36.1 ->>>>>>> 1ffb283ca924eb33b43effc7ec2c449a595c1aff -======= -opencv-python~=4.1.1 -tqdm>=4.36.1 - ->>>>>>> 6fa811ba5e0e3492eb1bc089836dbe5f78bb8bf3 From 9039c9143acce13cb46335bf31ea9cca47680127 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 7 Apr 2020 21:36:36 +0530 Subject: [PATCH 46/71] Changed the kernel computing technique --- .../image/gaussian_filter_ops.py | 41 ++++++++----------- .../image/tests/gaussian_filter_ops_test.py | 27 +++--------- tools/install_deps/pytest.txt | 1 - 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 9b47e8f460..5946b25d92 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -15,7 +15,6 @@ """GaussuanBlur Op""" -import numpy as np import tensorflow as tf from tensorflow_addons.utils.types import FloatTensorLike @@ -41,30 +40,24 @@ def gaussian_blur( if sigma == 0: raise ValueError("Sigma should not be zero") - if kSize % 2 == 0: - raise ValueError("kSize should be odd") + gaussian_filter_x = find_kernel(sigma, kSize, axis="x") + gaussian_filter_y = find_kernel(sigma, kSize, axis="y") - gaussianFilter = tf.Variable(tf.zeros(shape=(kSize, kSize), dtype=tf.float64)) - gaussianFilter = findKernel(sigma, kSize, gaussianFilter) - - gaussianKernel = tf.expand_dims(gaussianFilter, axis=2) - gaussianKernel = tf.expand_dims(gaussianKernel, axis=2) - - conv_ops = tf.nn.convolution(input=img, filters=gaussianKernel, padding="SAME") + conv_ops_x = tf.nn.convolution(input=img, filters=gaussian_filter_x, padding="SAME") + conv_ops = tf.nn.convolution( + input=conv_ops_x, filters=gaussian_filter_y, padding="SAME" + ) return conv_ops -def findKernel(sigma, kSize, gaussianFilter): - "This function creates a kernel of size [kSize*kSize]" - rowFilter = tf.Variable(tf.zeros(kSize, dtype=tf.float64), dtype=tf.float64) - for i in range(-kSize // 2, kSize // 2 + 1): - for j in range(-kSize // 2, kSize // 2 + 1): - rowFilter[j + kSize // 2].assign( - 1 - / (2 * np.pi * (sigma) ** 2) - * tf.exp(tf.cast(-(i ** 2 + j ** 2) / (2 * sigma ** 2.00), tf.float64)) - ) - gaussianFilter[i + kSize // 2].assign(rowFilter) - s = tf.math.reduce_sum(gaussianFilter) - gaussianFilter = tf.math.divide(gaussianFilter, s) - return gaussianFilter +def find_kernel(sigma, kSize, axis="x"): + "This function creates a kernel of size [kSize]" + x = tf.range(-kSize // 2 + 1, kSize // 2 + 1) + x = tf.math.square(x, tf.float64) + a = tf.cast(tf.exp(-(x ** 2) / (2 * (sigma ** 2))), tf.float64) + a = a / tf.math.reduce_sum(a) + if axis == "y": + a = tf.reshape(a, [kSize, 1, 1, 1]) + else: + a = tf.reshape(a, [1, kSize, 1, 1]) + return a diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py index 1a4359aba9..1ab5fb79e0 100644 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py @@ -20,35 +20,18 @@ import numpy.core.multiarray from scipy.ndimage import gaussian_filter import tensorflow as tf -import sys import pytest @pytest.mark.usefixtures("maybe_run_functions_eagerly") -def test(): +def test_gaussian_blur(): test_image_tf = tf.random.uniform( - [2, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 ) - - gb = gaussian_blur(test_image_tf, 1, 7) + gb = gaussian_blur(test_image_tf, 1, 5) gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) - - gb2 = gaussian_filter(test_image_cv, 1) - - accuracy = 0 - - for i in range(len(gb1)): - for j in range(len(gb1[0])): - if abs(gb1[i][j] - gb2[i][j]) < 10: - accuracy += 1 - - print(accuracy / 1600) - assert accuracy >= 0.80 - - -if __name__ == "__main__": - sys.exit(pytest.main([__file__])) + gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant", cval=0) + np.testing.assert_allclose(gb2, gb1, 0.5) diff --git a/tools/install_deps/pytest.txt b/tools/install_deps/pytest.txt index ae9ed3d875..26b36d64ae 100644 --- a/tools/install_deps/pytest.txt +++ b/tools/install_deps/pytest.txt @@ -3,5 +3,4 @@ pytest-xdist~=1.31 scikit-learn~=0.22 scikit-image~=0.15.0 Pillow~=7.0.0 -opencv-python~=4.1.1 tqdm>=4.36.1 From cc79bbbd18a54cb7b8f08758164da63e668d5c01 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 7 Apr 2020 21:50:03 +0530 Subject: [PATCH 47/71] github test --- tensorflow_addons/image/tests/gaussian_filter_ops_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py index 1ab5fb79e0..7fdce8de12 100644 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py @@ -34,4 +34,4 @@ def test_gaussian_blur(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant", cval=0) - np.testing.assert_allclose(gb2, gb1, 0.5) + np.testing.assert_allclose(gb2, gb1, 0.6) From c19cbc6b33b430ac7cb3f626e7171fef4685fc67 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 7 Apr 2020 21:52:25 +0530 Subject: [PATCH 48/71] github check --- tensorflow_addons/image/tests/gaussian_filter_ops_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py index 7fdce8de12..1a6b8d517b 100644 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py @@ -34,4 +34,6 @@ def test_gaussian_blur(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant", cval=0) - np.testing.assert_allclose(gb2, gb1, 0.6) + gb1=gb1[:,:]/255 + gb2=gb2[:,:]/255 + np.testing.assert_allclose(gb2, gb1, 0.5) From 6d8b2d26c00a05a8b59b90e9274332f410076027 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 7 Apr 2020 21:54:31 +0530 Subject: [PATCH 49/71] github check --- tensorflow_addons/image/tests/gaussian_filter_ops_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py index 1a6b8d517b..57c9ae875a 100644 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py @@ -34,6 +34,6 @@ def test_gaussian_blur(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant", cval=0) - gb1=gb1[:,:]/255 - gb2=gb2[:,:]/255 + gb1 = gb1[:, :] / 255 + gb2 = gb2[:, :] / 255 np.testing.assert_allclose(gb2, gb1, 0.5) From a59c732d6478ee6fa3d9c8359fffbf2e4154e8a7 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Wed, 8 Apr 2020 00:02:03 +0530 Subject: [PATCH 50/71] github test --- tensorflow_addons/image/gaussian_filter_ops.py | 2 +- tensorflow_addons/image/tests/gaussian_filter_ops_test.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py index 5946b25d92..7d22e586af 100644 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ b/tensorflow_addons/image/gaussian_filter_ops.py @@ -54,7 +54,7 @@ def find_kernel(sigma, kSize, axis="x"): "This function creates a kernel of size [kSize]" x = tf.range(-kSize // 2 + 1, kSize // 2 + 1) x = tf.math.square(x, tf.float64) - a = tf.cast(tf.exp(-(x ** 2) / (2 * (sigma ** 2))), tf.float64) + a = tf.cast(tf.exp(-(x) / (2 * (sigma ** 2))), tf.float64) a = a / tf.math.reduce_sum(a) if axis == "y": a = tf.reshape(a, [kSize, 1, 1, 1]) diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py index 57c9ae875a..1986b7ca88 100644 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py @@ -33,7 +33,9 @@ def test_gaussian_blur(): gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant", cval=0) + gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant") + print(gb1) + print(gb2) gb1 = gb1[:, :] / 255 gb2 = gb2[:, :] / 255 np.testing.assert_allclose(gb2, gb1, 0.5) From 980c3f9d787d40a32b48ecb1c1865364ca2ba8b4 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Sat, 11 Apr 2020 22:28:36 +0530 Subject: [PATCH 51/71] github check --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 1ba502f2c1..db58a26406 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -57,7 +57,7 @@ /tensorflow_addons/image/distance_transform.py @mels630 /tensorflow_addons/image/tests/distance_transform_test.py @mels630 /tensorflow_addons/image/gaussian_filter_ops*.py @ghosalsattam -/tensorflow_addons/image/gaussian_filter_ops_test*.py @ghosalsattam +/tensorflow_addons/image/tests/gaussian_filter_ops_test*.py @ghosalsattam /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq /tensorflow_addons/image/filters.py @mainak431 From 0662ed11dd5e7038c02097fda8e2b923805d83a1 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 20 Apr 2020 21:42:52 +0530 Subject: [PATCH 52/71] Shifted to filters.py --- .github/CODEOWNERS | 6 +- tensorflow_addons/image/__init__.py | 2 +- tensorflow_addons/image/filters.py | 54 ++++++++++++++++ .../image/gaussian_filter_ops.py | 63 ------------------- tensorflow_addons/image/tests/filters_test.py | 44 +++++++++++++ .../image/tests/gaussian_filter_ops_test.py | 41 ------------ 6 files changed, 101 insertions(+), 109 deletions(-) delete mode 100644 tensorflow_addons/image/gaussian_filter_ops.py delete mode 100644 tensorflow_addons/image/tests/gaussian_filter_ops_test.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index db58a26406..afd242bae2 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -56,12 +56,10 @@ /tensorflow_addons/image/tests/dense_image_warp_test.py @windQAQ /tensorflow_addons/image/distance_transform.py @mels630 /tensorflow_addons/image/tests/distance_transform_test.py @mels630 -/tensorflow_addons/image/gaussian_filter_ops*.py @ghosalsattam -/tensorflow_addons/image/tests/gaussian_filter_ops_test*.py @ghosalsattam /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq -/tensorflow_addons/image/filters.py @mainak431 -/tensorflow_addons/image/tests/filters_test.py @mainak431 +/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam +/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam /tensorflow_addons/image/interpolate_spline.py /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index c0fc87e016..fe725a9c84 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -35,4 +35,4 @@ from tensorflow_addons.image.transform_ops import transform from tensorflow_addons.image.translate_ops import translate from tensorflow_addons.image.translate_ops import translate_xy -from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur +from tensorflow_addons.image.filters import gaussian_filter2d diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index cecf85383a..1139ea3f29 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -204,3 +204,57 @@ def median_filter2d( output = tf.cast(median, image.dtype) output = img_utils.from_4D_image(output, original_ndims) return output + + +def gaussian_filter2d( + image: FloatTensorLike, + sigma: FloatTensorLike, + kernel_size: int, + padding: str = "REFLECT", + constant_values: TensorLike = 0, +) -> FloatTensorLike: + """ + This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel + which follows normal distribution then convolves the image with the kernel.Presently it works only on + grayscale images. + Args: + image: A tensor of shape + (batch_size, height, width, channels) + (NHWC), (batch_size, channels, height, width)(NCHW). + sigma:A constant of type float64. It is the standard deviation of the normal distribution. + The more the sigma, the more the blurring effect. + G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) + kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. + A kernel of size [kSize*kSize] is generated. + """ + + if sigma == 0: + raise ValueError("Sigma should not be zero") + if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: + raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") + + gaussian_filter_x = find_kernel(sigma, kernel_size) + gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, kernel_size, 1, 1]) + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float64) + gaussian_filter_y = find_kernel(sigma, kernel_size) + gaussian_filter_y = tf.reshape(gaussian_filter_y, [kernel_size, 1, 1, 1]) + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float64) + image = _pad( + image, (kernel_size, kernel_size), mode=padding, constant_values=constant_values + ) + conv_ops_x = tf.nn.convolution( + input=image, filters=gaussian_filter_x, padding="VALID" + ) + conv_ops = tf.nn.convolution( + input=conv_ops_x, filters=gaussian_filter_y, padding="VALID" + ) + return conv_ops + + +def find_kernel(sigma, kernel_size): + "This function creates a kernel of size [kSize]" + x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1) + x = tf.math.square(x, tf.float64) + a = tf.exp(-(x) / (2 * (sigma ** 2))) + a = a / tf.math.reduce_sum(a) + return a diff --git a/tensorflow_addons/image/gaussian_filter_ops.py b/tensorflow_addons/image/gaussian_filter_ops.py deleted file mode 100644 index 7d22e586af..0000000000 --- a/tensorflow_addons/image/gaussian_filter_ops.py +++ /dev/null @@ -1,63 +0,0 @@ -# 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. -# ============================================================================= -"""GaussuanBlur Op""" - - -import tensorflow as tf -from tensorflow_addons.utils.types import FloatTensorLike - - -def gaussian_blur( - img: FloatTensorLike, sigma: tf.float64, kSize: tf.float64 -) -> FloatTensorLike: - """ - This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel - which follows normal distribution then convolves the image with the kernel.Presently it works only on - grayscale images. - Args: - img: A tensor of shape - (batch_size, height, width, channels) - (NHWC), (batch_size, channels, height, width)(NCHW). - sigma:A constant of type float64. It is the standard deviation of the normal distribution. - The more the sigma, the more the blurring effect. - G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) - kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. - A kernel of size [kSize*kSize] is generated. - """ - - if sigma == 0: - raise ValueError("Sigma should not be zero") - - gaussian_filter_x = find_kernel(sigma, kSize, axis="x") - gaussian_filter_y = find_kernel(sigma, kSize, axis="y") - - conv_ops_x = tf.nn.convolution(input=img, filters=gaussian_filter_x, padding="SAME") - conv_ops = tf.nn.convolution( - input=conv_ops_x, filters=gaussian_filter_y, padding="SAME" - ) - return conv_ops - - -def find_kernel(sigma, kSize, axis="x"): - "This function creates a kernel of size [kSize]" - x = tf.range(-kSize // 2 + 1, kSize // 2 + 1) - x = tf.math.square(x, tf.float64) - a = tf.cast(tf.exp(-(x) / (2 * (sigma ** 2))), tf.float64) - a = a / tf.math.reduce_sum(a) - if axis == "y": - a = tf.reshape(a, [kSize, 1, 1, 1]) - else: - a = tf.reshape(a, [1, kSize, 1, 1]) - return a diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index d3d75ecb61..1f68e07a04 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -18,6 +18,8 @@ import tensorflow as tf from tensorflow_addons.image import mean_filter2d from tensorflow_addons.image import median_filter2d +from tensorflow_addons.image import gaussian_filter2d +from scipy.ndimage import gaussian_filter _dtypes_to_test = { tf.dtypes.uint8, @@ -359,3 +361,45 @@ def test_symmetric_padding_with_3x3_filter_median(image_shape): constant_values=0, expected_plane=expected_plane, ) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_gaussian_filter2d_constant(): + test_image_tf = tf.random.uniform( + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + ) + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="CONSTANT") + gb = gb.numpy() + gb1 = np.resize(gb, (40, 40)) + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [40, 40]) + gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant") + np.testing.assert_allclose(gb2, gb1, 0.05) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_gaussian_filter2d_reflect(): + test_image_tf = tf.random.uniform( + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + ) + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="REFLECT") + gb = gb.numpy() + gb1 = np.resize(gb, (40, 40)) + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [40, 40]) + gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="mirror") + np.testing.assert_allclose(gb2, gb1, 0.05) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_gaussian_filter2d_symmetric(): + test_image_tf = tf.random.uniform( + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + ) + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gb.numpy() + gb1 = np.resize(gb, (40, 40)) + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [40, 40]) + gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="reflect") + np.testing.assert_allclose(gb2, gb1, 0.05) diff --git a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py b/tensorflow_addons/image/tests/gaussian_filter_ops_test.py deleted file mode 100644 index 1986b7ca88..0000000000 --- a/tensorflow_addons/image/tests/gaussian_filter_ops_test.py +++ /dev/null @@ -1,41 +0,0 @@ -# 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. -# ============================================================================== -""" Tests for gaussian blur. """ - - -from tensorflow_addons.image.gaussian_filter_ops import gaussian_blur -import numpy as np -import numpy.core.multiarray -from scipy.ndimage import gaussian_filter -import tensorflow as tf -import pytest - - -@pytest.mark.usefixtures("maybe_run_functions_eagerly") -def test_gaussian_blur(): - test_image_tf = tf.random.uniform( - [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 - ) - gb = gaussian_blur(test_image_tf, 1, 5) - gb = gb.numpy() - gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant") - print(gb1) - print(gb2) - gb1 = gb1[:, :] / 255 - gb2 = gb2[:, :] / 255 - np.testing.assert_allclose(gb2, gb1, 0.5) From 630738eff7d2b6762cde6151d4e2433d30ba80f4 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Mon, 20 Apr 2020 23:10:54 +0530 Subject: [PATCH 53/71] changed codeowners --- .github/CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index afd242bae2..fafc8b4811 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -58,8 +58,8 @@ /tensorflow_addons/image/tests/distance_transform_test.py @mels630 /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq -/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam -/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam +/tensorflow_addons/image/filters.py @mainak431 +/tensorflow_addons/image/tests/filters_test.py @mainak431 /tensorflow_addons/image/interpolate_spline.py /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih From 9733ed1d37027f24caaaa6e51516f06e7edfe806 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 21 Apr 2020 21:21:07 +0530 Subject: [PATCH 54/71] revert codeowners changes --- .github/CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index fafc8b4811..afd242bae2 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -58,8 +58,8 @@ /tensorflow_addons/image/tests/distance_transform_test.py @mels630 /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq -/tensorflow_addons/image/filters.py @mainak431 -/tensorflow_addons/image/tests/filters_test.py @mainak431 +/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam +/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam /tensorflow_addons/image/interpolate_spline.py /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih From 08ec6271dd7b4b18018922cfc3ba2484bc9cebd6 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 21 Apr 2020 21:45:35 +0530 Subject: [PATCH 55/71] changed codeowners --- .github/CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index afd242bae2..aa9f958f30 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -58,8 +58,8 @@ /tensorflow_addons/image/tests/distance_transform_test.py @mels630 /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq -/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam -/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam +/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam +/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam /tensorflow_addons/image/interpolate_spline.py /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih From ab4d65d85b59cdc1ac7c635deddcf15d5e7325a0 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 21 Apr 2020 21:48:36 +0530 Subject: [PATCH 56/71] codeowners --- .github/CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index aa9f958f30..afd242bae2 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -58,8 +58,8 @@ /tensorflow_addons/image/tests/distance_transform_test.py @mels630 /tensorflow_addons/image/distort_image_ops.py @windqaq /tensorflow_addons/image/tests/distort_image_ops_test.py @windqaq -/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam -/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam +/tensorflow_addons/image/filters.py @mainak431 @ghosalsattam +/tensorflow_addons/image/tests/filters_test.py @mainak431 @ghosalsattam /tensorflow_addons/image/interpolate_spline.py /tensorflow_addons/image/tests/interpolate_spline_test.py /tensorflow_addons/image/resampler_ops.py @autoih From 5224755f01db5d2d214c490525173697e3be07bf Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Wed, 22 Apr 2020 22:17:24 +0530 Subject: [PATCH 57/71] Changed TO NHWC format --- tensorflow_addons/image/__init__.py | 2 +- tensorflow_addons/image/filters.py | 51 +++++++++------- tensorflow_addons/image/tests/filters_test.py | 60 ++++++++++++++++++- 3 files changed, 89 insertions(+), 24 deletions(-) diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index fe725a9c84..14cf993e11 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -21,6 +21,7 @@ from tensorflow_addons.image.cutout_ops import cutout from tensorflow_addons.image.dense_image_warp import dense_image_warp from tensorflow_addons.image.distance_transform import euclidean_dist_transform +from tensorflow_addons.image.filters import gaussian_filter2d from tensorflow_addons.image.dense_image_warp import interpolate_bilinear from tensorflow_addons.image.interpolate_spline import interpolate_spline from tensorflow_addons.image.filters import mean_filter2d @@ -35,4 +36,3 @@ from tensorflow_addons.image.transform_ops import transform from tensorflow_addons.image.translate_ops import translate from tensorflow_addons.image.translate_ops import translate_xy -from tensorflow_addons.image.filters import gaussian_filter2d diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 1139ea3f29..9a8ce17077 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -28,12 +28,10 @@ def _pad( constant_values: TensorLike = 0, ) -> tf.Tensor: """Explicitly pad a 4-D image. - Equivalent to the implicit padding method offered in `tf.nn.conv2d` and `tf.nn.depthwise_conv2d`, but supports non-zero, reflect and symmetric padding mode. For the even-sized filter, it pads one more value to the right or the bottom side. - Args: image: A 4-D `Tensor` of shape `[batch_size, height, width, channels]`. filter_shape: A `tuple`/`list` of 2 integers, specifying the height @@ -64,7 +62,6 @@ def mean_filter2d( name: Optional[str] = None, ) -> tf.Tensor: """Perform mean filtering on image(s). - Args: image: Either a 2-D `Tensor` of shape `[height, width]`, a 3-D `Tensor` of shape `[height, width, channels]`, @@ -132,7 +129,6 @@ def median_filter2d( name: Optional[str] = None, ) -> tf.Tensor: """Perform median filtering on image(s). - Args: image: Either a 2-D `Tensor` of shape `[height, width]`, a 3-D `Tensor` of shape `[height, width, channels]`, @@ -209,14 +205,13 @@ def median_filter2d( def gaussian_filter2d( image: FloatTensorLike, sigma: FloatTensorLike, - kernel_size: int, + filter_shape: int, padding: str = "REFLECT", constant_values: TensorLike = 0, ) -> FloatTensorLike: """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel - which follows normal distribution then convolves the image with the kernel.Presently it works only on - grayscale images. + which follows normal distribution then convolves the image with the kernel. It is implemented as 2 1D convolutions. Args: image: A tensor of shape (batch_size, height, width, channels) @@ -224,36 +219,52 @@ def gaussian_filter2d( sigma:A constant of type float64. It is the standard deviation of the normal distribution. The more the sigma, the more the blurring effect. G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) - kSize:It is the kernel-size for the Gaussian Kernel. kSize should be odd. + In 1D, + G(x)=e^(-x**2)/2*sigma**2 + filter_shape:It is the kernel-size for the Gaussian Kernel. kSize should be odd. A kernel of size [kSize*kSize] is generated. + padding:A string. It takes values in ["REFLECT", "CONSTANT", "SYMMETRIC"]. + constant_values:A constant to be used for padding in case of CONSTANT padding. + Returns: + 3D or 4D 'Tensor' of same type float64. + Raises: + Value error if: + 1). Sigma=0 + 2). passing some string other than ["REFLECT", "CONSTANT", "SYMMETRIC"] in padding. """ if sigma == 0: raise ValueError("Sigma should not be zero") if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") + image = img_utils.to_4D_image(image) + channels = tf.shape(image)[3] + + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape, channels) + gaussian_filter_x = tf.repeat(gaussian_filter_x,channels) + gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape, channels, 1]) - gaussian_filter_x = find_kernel(sigma, kernel_size) - gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, kernel_size, 1, 1]) gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float64) - gaussian_filter_y = find_kernel(sigma, kernel_size) - gaussian_filter_y = tf.reshape(gaussian_filter_y, [kernel_size, 1, 1, 1]) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape,channels) + gaussian_filter_y = tf.repeat(gaussian_filter_y,channels) + gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape, 1, channels, 1]) + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float64) image = _pad( - image, (kernel_size, kernel_size), mode=padding, constant_values=constant_values + image, (filter_shape, filter_shape), mode=padding, constant_values=constant_values ) - conv_ops_x = tf.nn.convolution( - input=image, filters=gaussian_filter_x, padding="VALID" + conv_ops_x = tf.nn.depthwise_conv2d( + input=image, filter=gaussian_filter_x, strides=(1,1,1,1), padding="VALID" ) - conv_ops = tf.nn.convolution( - input=conv_ops_x, filters=gaussian_filter_y, padding="VALID" + conv_ops = tf.nn.depthwise_conv2d( + input=conv_ops_x, filter=gaussian_filter_y, strides=(1,1,1,1), padding="VALID" ) return conv_ops -def find_kernel(sigma, kernel_size): - "This function creates a kernel of size [kSize]" - x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1) +def _get_gaussian_kernel(sigma, filter_shape, channels): + "This function creates a kernel of size [filter_shape]" + x = tf.range(-filter_shape // 2 + 1, filter_shape // 2 + 1) x = tf.math.square(x, tf.float64) a = tf.exp(-(x) / (2 * (sigma ** 2))) a = a / tf.math.reduce_sum(a) diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 1f68e07a04..199511360c 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -20,6 +20,7 @@ from tensorflow_addons.image import median_filter2d from tensorflow_addons.image import gaussian_filter2d from scipy.ndimage import gaussian_filter +from skimage.filters import gaussian _dtypes_to_test = { tf.dtypes.uint8, @@ -374,7 +375,7 @@ def test_gaussian_filter2d_constant(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant") - np.testing.assert_allclose(gb2, gb1, 0.05) + np.testing.assert_allclose(gb2, gb1, 0.06) @pytest.mark.usefixtures("maybe_run_functions_eagerly") @@ -388,7 +389,7 @@ def test_gaussian_filter2d_reflect(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="mirror") - np.testing.assert_allclose(gb2, gb1, 0.05) + np.testing.assert_allclose(gb2, gb1, 0.06) @pytest.mark.usefixtures("maybe_run_functions_eagerly") @@ -402,4 +403,57 @@ def test_gaussian_filter2d_symmetric(): test_image_cv = test_image_tf.numpy() test_image_cv = np.resize(test_image_cv, [40, 40]) gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="reflect") - np.testing.assert_allclose(gb2, gb1, 0.05) + np.testing.assert_allclose(gb2, gb1, 0.06) + + +@pytest.mark.parametrize("image_shape", [[2,5,5,3]]) +def test_gaussian_filter2d_batch(image_shape): + test_image_tf = tf.random.uniform( + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + ) + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gb.numpy() + gb1 = np.resize(gb, (40, 40)) + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [40, 40]) + gb2 = gaussian(test_image_cv, 1, truncate=4.6, mode="reflect") + np.testing.assert_allclose(gb2, gb1, 0.06) + + +def test_gaussian_filter2d_channels(): + test_image_tf = tf.constant([[[[0., 0., 0.], + [2., 2., 0.], + [4., 4., 0.], + [6., 6., 0.], + [8., 8., 0.]], + + [[10., 10., 0.], + [12., 12., 0.], + [14., 14., 0.], + [16., 16., 0.], + [18., 18., 0.]], + + [[20., 20., 0.], + [22., 22., 0.], + [24., 24., 0.], + [26., 26., 0.], + [28., 28., 0.]], + + [[30., 30., 0.], + [32., 32., 0.], + [34., 34., 0.], + [36., 36., 0.], + [38., 38., 0.]], + + [[40., 40., 0.], + [42., 42., 0.], + [44., 44., 0.], + [46., 46., 0.], + [48., 48., 0.]]]],dtype=tf.float64) + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gb.numpy() + gb1 = np.resize(gb, (5, 5,3)) + test_image_cv = test_image_tf.numpy() + test_image_cv = np.resize(test_image_cv, [5, 5,3]) + gb2 = gaussian(test_image_cv,sigma=1,mode="reflect",multichannel=True) + np.testing.assert_allclose(gb2, gb1, 0.06) From ce7e229dcec5b1a9e6b0ddd934104106d1888fde Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Wed, 22 Apr 2020 22:20:52 +0530 Subject: [PATCH 58/71] github check --- tensorflow_addons/image/filters.py | 18 +++-- tensorflow_addons/image/tests/filters_test.py | 79 +++++++++++-------- 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 9a8ce17077..ed65e83270 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -241,23 +241,29 @@ def gaussian_filter2d( channels = tf.shape(image)[3] gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape, channels) - gaussian_filter_x = tf.repeat(gaussian_filter_x,channels) + gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape, channels, 1]) gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float64) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape,channels) - gaussian_filter_y = tf.repeat(gaussian_filter_y,channels) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape, channels) + gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape, 1, channels, 1]) gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float64) image = _pad( - image, (filter_shape, filter_shape), mode=padding, constant_values=constant_values + image, + (filter_shape, filter_shape), + mode=padding, + constant_values=constant_values, ) conv_ops_x = tf.nn.depthwise_conv2d( - input=image, filter=gaussian_filter_x, strides=(1,1,1,1), padding="VALID" + input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" ) conv_ops = tf.nn.depthwise_conv2d( - input=conv_ops_x, filter=gaussian_filter_y, strides=(1,1,1,1), padding="VALID" + input=conv_ops_x, + filter=gaussian_filter_y, + strides=(1, 1, 1, 1), + padding="VALID", ) return conv_ops diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 199511360c..38fb19a7a9 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -406,7 +406,7 @@ def test_gaussian_filter2d_symmetric(): np.testing.assert_allclose(gb2, gb1, 0.06) -@pytest.mark.parametrize("image_shape", [[2,5,5,3]]) +@pytest.mark.parametrize("image_shape", [[2, 5, 5, 3]]) def test_gaussian_filter2d_batch(image_shape): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 @@ -421,39 +421,52 @@ def test_gaussian_filter2d_batch(image_shape): def test_gaussian_filter2d_channels(): - test_image_tf = tf.constant([[[[0., 0., 0.], - [2., 2., 0.], - [4., 4., 0.], - [6., 6., 0.], - [8., 8., 0.]], - - [[10., 10., 0.], - [12., 12., 0.], - [14., 14., 0.], - [16., 16., 0.], - [18., 18., 0.]], - - [[20., 20., 0.], - [22., 22., 0.], - [24., 24., 0.], - [26., 26., 0.], - [28., 28., 0.]], - - [[30., 30., 0.], - [32., 32., 0.], - [34., 34., 0.], - [36., 36., 0.], - [38., 38., 0.]], - - [[40., 40., 0.], - [42., 42., 0.], - [44., 44., 0.], - [46., 46., 0.], - [48., 48., 0.]]]],dtype=tf.float64) + test_image_tf = tf.constant( + [ + [ + [ + [0.0, 0.0, 0.0], + [2.0, 2.0, 0.0], + [4.0, 4.0, 0.0], + [6.0, 6.0, 0.0], + [8.0, 8.0, 0.0], + ], + [ + [10.0, 10.0, 0.0], + [12.0, 12.0, 0.0], + [14.0, 14.0, 0.0], + [16.0, 16.0, 0.0], + [18.0, 18.0, 0.0], + ], + [ + [20.0, 20.0, 0.0], + [22.0, 22.0, 0.0], + [24.0, 24.0, 0.0], + [26.0, 26.0, 0.0], + [28.0, 28.0, 0.0], + ], + [ + [30.0, 30.0, 0.0], + [32.0, 32.0, 0.0], + [34.0, 34.0, 0.0], + [36.0, 36.0, 0.0], + [38.0, 38.0, 0.0], + ], + [ + [40.0, 40.0, 0.0], + [42.0, 42.0, 0.0], + [44.0, 44.0, 0.0], + [46.0, 46.0, 0.0], + [48.0, 48.0, 0.0], + ], + ] + ], + dtype=tf.float64, + ) gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") gb = gb.numpy() - gb1 = np.resize(gb, (5, 5,3)) + gb1 = np.resize(gb, (5, 5, 3)) test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [5, 5,3]) - gb2 = gaussian(test_image_cv,sigma=1,mode="reflect",multichannel=True) + test_image_cv = np.resize(test_image_cv, [5, 5, 3]) + gb2 = gaussian(test_image_cv, sigma=1, mode="reflect", multichannel=True) np.testing.assert_allclose(gb2, gb1, 0.06) From f8fe174497be9382ec2fe68e0aedff1c98be0ad4 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Thu, 23 Apr 2020 08:32:12 +0530 Subject: [PATCH 59/71] Update filters.py --- tensorflow_addons/image/filters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index ed65e83270..927883e971 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -221,8 +221,8 @@ def gaussian_filter2d( G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) In 1D, G(x)=e^(-x**2)/2*sigma**2 - filter_shape:It is the kernel-size for the Gaussian Kernel. kSize should be odd. - A kernel of size [kSize*kSize] is generated. + filter_shape:It is the kernel-size for the Gaussian Kernel. + A kernel of size [kernel_shape*kernel_shape] is generated. padding:A string. It takes values in ["REFLECT", "CONSTANT", "SYMMETRIC"]. constant_values:A constant to be used for padding in case of CONSTANT padding. Returns: From 4c1431aa7ca787843fa9c7365ac642a2b6e23e52 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 23 Apr 2020 09:15:40 +0530 Subject: [PATCH 60/71] github check --- tensorflow_addons/image/filters.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index ed65e83270..54a9512770 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -221,8 +221,8 @@ def gaussian_filter2d( G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) In 1D, G(x)=e^(-x**2)/2*sigma**2 - filter_shape:It is the kernel-size for the Gaussian Kernel. kSize should be odd. - A kernel of size [kSize*kSize] is generated. + filter_shape:It is the kernel-size for the Gaussian Kernel. + A kernel of size [filter_shape*filter_shape] is generated. padding:A string. It takes values in ["REFLECT", "CONSTANT", "SYMMETRIC"]. constant_values:A constant to be used for padding in case of CONSTANT padding. Returns: @@ -241,29 +241,23 @@ def gaussian_filter2d( channels = tf.shape(image)[3] gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape, channels) - gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) + gaussian_filter_x = tf.repeat(gaussian_filter_x,channels) gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape, channels, 1]) gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float64) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape, channels) - gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape,channels) + gaussian_filter_y = tf.repeat(gaussian_filter_y,channels) gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape, 1, channels, 1]) gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float64) image = _pad( - image, - (filter_shape, filter_shape), - mode=padding, - constant_values=constant_values, + image, (filter_shape, filter_shape), mode=padding, constant_values=constant_values ) conv_ops_x = tf.nn.depthwise_conv2d( - input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" + input=image, filter=gaussian_filter_x, strides=(1,1,1,1), padding="VALID" ) conv_ops = tf.nn.depthwise_conv2d( - input=conv_ops_x, - filter=gaussian_filter_y, - strides=(1, 1, 1, 1), - padding="VALID", + input=conv_ops_x, filter=gaussian_filter_y, strides=(1,1,1,1), padding="VALID" ) return conv_ops From 31aa403edace59fc9cfa947b86d0e47e036dcb89 Mon Sep 17 00:00:00 2001 From: ghosalsattam <39326440+ghosalsattam@users.noreply.github.com> Date: Thu, 23 Apr 2020 09:24:02 +0530 Subject: [PATCH 61/71] Github Check --- tensorflow_addons/image/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 927883e971..c3ce1f64f8 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -221,7 +221,7 @@ def gaussian_filter2d( G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) In 1D, G(x)=e^(-x**2)/2*sigma**2 - filter_shape:It is the kernel-size for the Gaussian Kernel. + filter_shape:It is the kernel-size for the Gaussian Kernel. A kernel of size [kernel_shape*kernel_shape] is generated. padding:A string. It takes values in ["REFLECT", "CONSTANT", "SYMMETRIC"]. constant_values:A constant to be used for padding in case of CONSTANT padding. From c58e3a354c3359de2d34441cd19b4ffe9207d541 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 23 Apr 2020 11:58:24 +0530 Subject: [PATCH 62/71] new commit --- tensorflow_addons/image/filters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index e001dfd78d..6d6f0980c2 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -244,6 +244,8 @@ def gaussian_filter2d( raise ValueError("Sigma should not be zero") if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") + + image=tf.cast(image,tf.float64) image = img_utils.to_4D_image(image) channels = tf.shape(image)[3] From 2a14a4df3049ae569a2c6b256ff55fc4cfb205ea Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 23 Apr 2020 22:40:13 +0530 Subject: [PATCH 63/71] increased flexibility of kernel_size --- tensorflow_addons/image/filters.py | 45 +++++++++---------- tensorflow_addons/image/tests/filters_test.py | 6 +-- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 9ad07b8b45..02d8b2fbdc 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -205,7 +205,7 @@ def median_filter2d( def gaussian_filter2d( image: FloatTensorLike, sigma: FloatTensorLike, - filter_shape: int, + filter_shape: Union[List[int], Tuple[int]] = [3, 3], padding: str = "REFLECT", constant_values: TensorLike = 0, ) -> FloatTensorLike: @@ -223,15 +223,18 @@ def gaussian_filter2d( In 1D, G(x)=e^(-x**2)/2*sigma**2 - filter_shape:It is the kernel-size for the Gaussian Kernel. - A kernel of size [filter_shape*filter_shape] is generated. - - filter_shape:It is the kernel-size for the Gaussian Kernel. - A kernel of size [kernel_shape*kernel_shape] is generated. + filter_shape:An `integer` or `tuple`/`list` of 2 integers, specifying + the height and width of the 2-D gaussian filter. Can be a single integer + to specify the same value for all spatial dimensions. - padding:A string. It takes values in ["REFLECT", "CONSTANT", "SYMMETRIC"]. + padding:A `string`, one of "REFLECT", "CONSTANT", or "SYMMETRIC". + The type of padding algorithm to use, which is compatible with + `mode` argument in `tf.pad`. For more details, please refer to + https://www.tensorflow.org/api_docs/python/tf/pad. - constant_values:A constant to be used for padding in case of CONSTANT padding. + constant_values:A `scalar`, the pad value to use in "CONSTANT" + padding mode. + name: A name for this operation (optional). Returns: 3D or 4D 'Tensor' of same type float64. Raises: @@ -245,26 +248,22 @@ def gaussian_filter2d( if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") - image = tf.cast(image, tf.float64) + image = tf.cast(image, tf.float32) image = img_utils.to_4D_image(image) channels = tf.shape(image)[3] + filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") - gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape, channels) + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1], channels) gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) - gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape, channels, 1]) + gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape[1], channels, 1]) - gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float64) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape, channels) + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0], channels) gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) - gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape, 1, channels, 1]) - - gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float64) - image = _pad( - image, - (filter_shape, filter_shape), - mode=padding, - constant_values=constant_values, - ) + gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape[0], 1, channels, 1]) + + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) + image = _pad(image, filter_shape, mode=padding, constant_values=constant_values,) conv_ops_x = tf.nn.depthwise_conv2d( input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" ) @@ -280,7 +279,7 @@ def gaussian_filter2d( def _get_gaussian_kernel(sigma, filter_shape, channels): "This function creates a kernel of size [filter_shape]" x = tf.range(-filter_shape // 2 + 1, filter_shape // 2 + 1) - x = tf.math.square(x, tf.float64) + x = tf.math.square(x, tf.float32) a = tf.exp(-(x) / (2 * (sigma ** 2))) a = a / tf.math.reduce_sum(a) return a diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 752dd9f0fd..10dd7c935f 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -381,7 +381,7 @@ def test_gaussian_filter2d_constant(): @pytest.mark.usefixtures("maybe_run_functions_eagerly") def test_gaussian_filter2d_reflect(): test_image_tf = tf.random.uniform( - [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.int32 ) gb = gaussian_filter2d(test_image_tf, 1, 5, padding="REFLECT") gb = gb.numpy() @@ -397,7 +397,7 @@ def test_gaussian_filter2d_symmetric(): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gaussian_filter2d(test_image_tf, 1, (5, 5), padding="SYMMETRIC") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() @@ -461,7 +461,7 @@ def test_gaussian_filter2d_channels(): ], ] ], - dtype=tf.float64, + dtype=tf.float32, ) gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") gb = gb.numpy() From c99f1950de74132db3498ae0d40633a39af404e6 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Fri, 24 Apr 2020 00:19:17 +0530 Subject: [PATCH 64/71] added name parameter for the gaussian blur op --- tensorflow_addons/image/filters.py | 83 ++++++++++--------- tensorflow_addons/image/tests/filters_test.py | 4 +- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 02d8b2fbdc..8d7bef087f 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -202,12 +202,14 @@ def median_filter2d( return output +@tf.function def gaussian_filter2d( image: FloatTensorLike, sigma: FloatTensorLike, filter_shape: Union[List[int], Tuple[int]] = [3, 3], padding: str = "REFLECT", constant_values: TensorLike = 0, + name: Optional[str] = None, ) -> FloatTensorLike: """ This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel @@ -234,7 +236,7 @@ def gaussian_filter2d( constant_values:A `scalar`, the pad value to use in "CONSTANT" padding mode. - name: A name for this operation (optional). + name: A name for this operation (optional). Returns: 3D or 4D 'Tensor' of same type float64. Raises: @@ -243,43 +245,50 @@ def gaussian_filter2d( 2). passing some string other than ["REFLECT", "CONSTANT", "SYMMETRIC"] in padding. """ - if sigma == 0: - raise ValueError("Sigma should not be zero") - if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: - raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") - - image = tf.cast(image, tf.float32) - image = img_utils.to_4D_image(image) - channels = tf.shape(image)[3] - filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") - - gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1], channels) - gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) - gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape[1], channels, 1]) - - gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0], channels) - gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) - gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape[0], 1, channels, 1]) - - gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) - image = _pad(image, filter_shape, mode=padding, constant_values=constant_values,) - conv_ops_x = tf.nn.depthwise_conv2d( - input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" - ) - conv_ops = tf.nn.depthwise_conv2d( - input=conv_ops_x, - filter=gaussian_filter_y, - strides=(1, 1, 1, 1), - padding="VALID", - ) - return conv_ops - - -def _get_gaussian_kernel(sigma, filter_shape, channels): + with tf.name_scope(name or "gaussian_filter2d"): + if sigma == 0: + raise ValueError("Sigma should not be zero") + if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: + raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") + + image = tf.cast(image, tf.float32) + image = img_utils.to_4D_image(image) + channels = tf.shape(image)[3] + filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") + + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1], channels) + gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) + gaussian_filter_x = tf.reshape( + gaussian_filter_x, [1, filter_shape[1], channels, 1] + ) + + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0], channels) + gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) + gaussian_filter_y = tf.reshape( + gaussian_filter_y, [filter_shape[0], 1, channels, 1] + ) + + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) + image = _pad( + image, filter_shape, mode=padding, constant_values=constant_values, + ) + conv_ops_x = tf.nn.depthwise_conv2d( + input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" + ) + conv_ops = tf.nn.depthwise_conv2d( + input=conv_ops_x, + filter=gaussian_filter_y, + strides=(1, 1, 1, 1), + padding="VALID", + ) + return conv_ops + + +def _get_gaussian_kernel(sigma, filter_shape_1d, channels): "This function creates a kernel of size [filter_shape]" - x = tf.range(-filter_shape // 2 + 1, filter_shape // 2 + 1) - x = tf.math.square(x, tf.float32) + x = tf.range(-filter_shape_1d // 2 + 1, filter_shape_1d // 2 + 1) + x = tf.math.square(x) a = tf.exp(-(x) / (2 * (sigma ** 2))) a = a / tf.math.reduce_sum(a) return a diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 10dd7c935f..53f942ff07 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -406,6 +406,7 @@ def test_gaussian_filter2d_symmetric(): np.testing.assert_allclose(gb2, gb1, 0.06) +@pytest.mark.usefixtures("maybe_run_functions_eagerly") @pytest.mark.parametrize("image_shape", [[2, 5, 5, 3]]) def test_gaussian_filter2d_batch(image_shape): test_image_tf = tf.random.uniform( @@ -420,6 +421,7 @@ def test_gaussian_filter2d_batch(image_shape): np.testing.assert_allclose(gb2, gb1, 0.06) +@pytest.mark.usefixtures("maybe_run_functions_eagerly") def test_gaussian_filter2d_channels(): test_image_tf = tf.constant( [ @@ -463,7 +465,7 @@ def test_gaussian_filter2d_channels(): ], dtype=tf.float32, ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC", name="gaussian") gb = gb.numpy() gb1 = np.resize(gb, (5, 5, 3)) test_image_cv = test_image_tf.numpy() From 21ef57ed692e6f24ea2fcb09c7802a7eaad048c1 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Fri, 24 Apr 2020 01:41:54 +0530 Subject: [PATCH 65/71] changed documentation --- tensorflow_addons/image/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 8d7bef087f..c638de0eeb 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -219,7 +219,7 @@ def gaussian_filter2d( (batch_size, height, width, channels) (NHWC), (batch_size, channels, height, width)(NCHW). - sigma:A constant of type float64. It is the standard deviation of the normal distribution. + sigma:A constant of type float32. It is the standard deviation of the normal distribution. The more the sigma, the more the blurring effect. G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) In 1D, From 4f57ca49de0a52fe2bd66db73f29bee1e409a38c Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Fri, 24 Apr 2020 13:00:00 +0530 Subject: [PATCH 66/71] update __init__.py --- tensorflow_addons/image/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/__init__.py b/tensorflow_addons/image/__init__.py index 14cf993e11..15b92a5380 100644 --- a/tensorflow_addons/image/__init__.py +++ b/tensorflow_addons/image/__init__.py @@ -21,9 +21,9 @@ from tensorflow_addons.image.cutout_ops import cutout from tensorflow_addons.image.dense_image_warp import dense_image_warp from tensorflow_addons.image.distance_transform import euclidean_dist_transform -from tensorflow_addons.image.filters import gaussian_filter2d from tensorflow_addons.image.dense_image_warp import interpolate_bilinear from tensorflow_addons.image.interpolate_spline import interpolate_spline +from tensorflow_addons.image.filters import gaussian_filter2d from tensorflow_addons.image.filters import mean_filter2d from tensorflow_addons.image.filters import median_filter2d from tensorflow_addons.image.cutout_ops import random_cutout From a1cf163b9eebf05b5be56b3df4075e4508b44127 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Tue, 28 Apr 2020 15:47:33 +0530 Subject: [PATCH 67/71] Added 2D convolution Along with sequence of 1D convolution --- tensorflow_addons/image/filters.py | 151 +++++++++++------- tensorflow_addons/image/tests/filters_test.py | 10 +- 2 files changed, 97 insertions(+), 64 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index c638de0eeb..262dd9e7d9 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -28,10 +28,12 @@ def _pad( constant_values: TensorLike = 0, ) -> tf.Tensor: """Explicitly pad a 4-D image. + Equivalent to the implicit padding method offered in `tf.nn.conv2d` and `tf.nn.depthwise_conv2d`, but supports non-zero, reflect and symmetric padding mode. For the even-sized filter, it pads one more value to the right or the bottom side. + Args: image: A 4-D `Tensor` of shape `[batch_size, height, width, channels]`. filter_shape: A `tuple`/`list` of 2 integers, specifying the height @@ -62,6 +64,7 @@ def mean_filter2d( name: Optional[str] = None, ) -> tf.Tensor: """Perform mean filtering on image(s). + Args: image: Either a 2-D `Tensor` of shape `[height, width]`, a 3-D `Tensor` of shape `[height, width, channels]`, @@ -129,6 +132,7 @@ def median_filter2d( name: Optional[str] = None, ) -> tf.Tensor: """Perform median filtering on image(s). + Args: image: Either a 2-D `Tensor` of shape `[height, width]`, a 3-D `Tensor` of shape `[height, width, channels]`, @@ -202,51 +206,59 @@ def median_filter2d( return output +def _get_gaussian_kernel(sigma, filter_shape_1d): + "This function creates a kernel of size [filter_shape]" + x = tf.range(-filter_shape_1d // 2 + 1, filter_shape_1d // 2 + 1) + x = tf.math.square(x) + a = tf.exp(-(x) / (2 * (sigma ** 2))) + a = a / tf.math.reduce_sum(a) + return a + + +def _get_gaussian_filter_2d(gaussian_filter_x, gaussian_filter_y): + "Compute 2D Gaussian Kernel" + gaussian_kernel = tf.matmul(gaussian_filter_x, gaussian_filter_y) + return gaussian_kernel + + @tf.function def gaussian_filter2d( image: FloatTensorLike, - sigma: FloatTensorLike, filter_shape: Union[List[int], Tuple[int]] = [3, 3], + sigma: FloatTensorLike = 1, padding: str = "REFLECT", constant_values: TensorLike = 0, name: Optional[str] = None, ) -> FloatTensorLike: - """ - This function is responsible for having Gaussian Blur. It takes the image as input, computes a gaussian-kernel - which follows normal distribution then convolves the image with the kernel. It is implemented as 2 1D convolutions. + """Perform Gaussian Filter. If the program is running on a CPU device, it will be implemented as a sequence of 1d convolution of + gausian kernel (in x- direction and fillowed by in y direction). + In 1-D, G(x)=e**((-x**2)/2*(sigma**2)) + If program is running in GPU, then it is implemented as convolution of image with 2-d gaussian kernel. + Args: - image: A tensor of shape - (batch_size, height, width, channels) - (NHWC), (batch_size, channels, height, width)(NCHW). - - sigma:A constant of type float32. It is the standard deviation of the normal distribution. - The more the sigma, the more the blurring effect. - G(x,y)=1/(2*3.14*sigma**2)e^((x**2+y**2)/2sigma**2) - In 1D, - G(x)=e^(-x**2)/2*sigma**2 - - filter_shape:An `integer` or `tuple`/`list` of 2 integers, specifying - the height and width of the 2-D gaussian filter. Can be a single integer + image: Either a 2-D `Tensor` of shape `[height, width]`, + a 3-D `Tensor` of shape `[height, width, channels]`, + or a 4-D `Tensor` of shape `[batch_size, height, width, channels]`. + filter_shape: An `integer` or `tuple`/`list` of 2 integers, specifying + the height and width of the 2-D median filter. Can be a single integer to specify the same value for all spatial dimensions. - - padding:A `string`, one of "REFLECT", "CONSTANT", or "SYMMETRIC". + sigma: Standard deviation of Gaussian. + padding: A `string`, one of "REFLECT", "CONSTANT", or "SYMMETRIC". The type of padding algorithm to use, which is compatible with `mode` argument in `tf.pad`. For more details, please refer to https://www.tensorflow.org/api_docs/python/tf/pad. - - constant_values:A `scalar`, the pad value to use in "CONSTANT" + constant_values: A `scalar`, the pad value to use in "CONSTANT" padding mode. - name: A name for this operation (optional). + name: A name for this operation (optional). Returns: - 3D or 4D 'Tensor' of same type float64. + 3-D or 4-D `Tensor` of the same dtype as input. Raises: - Value error if: - 1). Sigma=0 - 2). passing some string other than ["REFLECT", "CONSTANT", "SYMMETRIC"] in padding. + ValueError: If `image` is not 2, 3 or 4-dimensional, + if `padding` is other than "REFLECT", "CONSTANT" or "SYMMETRIC", + or if `filter_shape` is invalid or sigma<=0. """ - with tf.name_scope(name or "gaussian_filter2d"): - if sigma == 0: + if sigma <= 0: raise ValueError("Sigma should not be zero") if padding not in ["REFLECT", "CONSTANT", "SYMMETRIC"]: raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") @@ -256,39 +268,60 @@ def gaussian_filter2d( channels = tf.shape(image)[3] filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") - gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1], channels) - gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) - gaussian_filter_x = tf.reshape( - gaussian_filter_x, [1, filter_shape[1], channels, 1] - ) + if tf.test.is_gpu_available(): + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1]) + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) + gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape[1]]) - gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0], channels) - gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) - gaussian_filter_y = tf.reshape( - gaussian_filter_y, [filter_shape[0], 1, channels, 1] - ) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0]) + gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape[0], 1]) + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) - gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) - image = _pad( - image, filter_shape, mode=padding, constant_values=constant_values, - ) - conv_ops_x = tf.nn.depthwise_conv2d( - input=image, filter=gaussian_filter_x, strides=(1, 1, 1, 1), padding="VALID" - ) - conv_ops = tf.nn.depthwise_conv2d( - input=conv_ops_x, - filter=gaussian_filter_y, - strides=(1, 1, 1, 1), - padding="VALID", - ) - return conv_ops + gaussian_filter_2d = tf.matmul(gaussian_filter_y, gaussian_filter_x) + gaussian_filter_2d = tf.repeat(gaussian_filter_2d, channels) + gaussian_filter_2d = tf.reshape( + gaussian_filter_2d, [filter_shape[0], filter_shape[1], channels, 1] + ) + image = _pad( + image, filter_shape, mode=padding, constant_values=constant_values, + ) -def _get_gaussian_kernel(sigma, filter_shape_1d, channels): - "This function creates a kernel of size [filter_shape]" - x = tf.range(-filter_shape_1d // 2 + 1, filter_shape_1d // 2 + 1) - x = tf.math.square(x) - a = tf.exp(-(x) / (2 * (sigma ** 2))) - a = a / tf.math.reduce_sum(a) - return a + conv_ops = tf.nn.depthwise_conv2d( + input=image, + filter=gaussian_filter_2d, + strides=(1, 1, 1, 1), + padding="VALID", + ) + return conv_ops + else: + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1]) + gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) + gaussian_filter_x = tf.reshape( + gaussian_filter_x, [1, filter_shape[1], channels, 1] + ) + + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0]) + gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) + gaussian_filter_y = tf.reshape( + gaussian_filter_y, [filter_shape[0], 1, channels, 1] + ) + + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) + image = _pad( + image, filter_shape, mode=padding, constant_values=constant_values, + ) + conv_ops_x = tf.nn.depthwise_conv2d( + input=image, + filter=gaussian_filter_x, + strides=(1, 1, 1, 1), + padding="VALID", + ) + conv_ops = tf.nn.depthwise_conv2d( + input=conv_ops_x, + filter=gaussian_filter_y, + strides=(1, 1, 1, 1), + padding="VALID", + ) + return conv_ops diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 53f942ff07..259041e1b4 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -369,7 +369,7 @@ def test_gaussian_filter2d_constant(): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="CONSTANT") + gb = gaussian_filter2d(test_image_tf, 5, 1, padding="CONSTANT") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() @@ -383,7 +383,7 @@ def test_gaussian_filter2d_reflect(): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.int32 ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="REFLECT") + gb = gaussian_filter2d(test_image_tf, 5, 1, padding="REFLECT") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() @@ -397,7 +397,7 @@ def test_gaussian_filter2d_symmetric(): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float64 ) - gb = gaussian_filter2d(test_image_tf, 1, (5, 5), padding="SYMMETRIC") + gb = gaussian_filter2d(test_image_tf, (5, 5), 1, padding="SYMMETRIC") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() @@ -412,7 +412,7 @@ def test_gaussian_filter2d_batch(image_shape): test_image_tf = tf.random.uniform( [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float32 ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC") + gb = gaussian_filter2d(test_image_tf, 5, 1, padding="SYMMETRIC") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) test_image_cv = test_image_tf.numpy() @@ -465,7 +465,7 @@ def test_gaussian_filter2d_channels(): ], dtype=tf.float32, ) - gb = gaussian_filter2d(test_image_tf, 1, 5, padding="SYMMETRIC", name="gaussian") + gb = gaussian_filter2d(test_image_tf, 5, 1, padding="SYMMETRIC", name="gaussian") gb = gb.numpy() gb1 = np.resize(gb, (5, 5, 3)) test_image_cv = test_image_tf.numpy() From 97117fdbf8092afa0fdba00761ab69d450f8f8c1 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Wed, 29 Apr 2020 19:39:18 +0530 Subject: [PATCH 68/71] Added experimental_compile --- tensorflow_addons/image/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 262dd9e7d9..decb196c52 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -221,7 +221,7 @@ def _get_gaussian_filter_2d(gaussian_filter_x, gaussian_filter_y): return gaussian_kernel -@tf.function +@tf.function(experimental_compile=True) def gaussian_filter2d( image: FloatTensorLike, filter_shape: Union[List[int], Tuple[int]] = [3, 3], From a8b9566859bfcbcc059896a1a980d15006aaf885 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Wed, 29 Apr 2020 20:11:19 +0530 Subject: [PATCH 69/71] revert --- tensorflow_addons/image/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index decb196c52..262dd9e7d9 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -221,7 +221,7 @@ def _get_gaussian_filter_2d(gaussian_filter_x, gaussian_filter_y): return gaussian_kernel -@tf.function(experimental_compile=True) +@tf.function def gaussian_filter2d( image: FloatTensorLike, filter_shape: Union[List[int], Tuple[int]] = [3, 3], From cd9f5ed0077bceb2f64599711f15b145d0b171dd Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 30 Apr 2020 00:53:22 +0530 Subject: [PATCH 70/71] github check --- tensorflow_addons/image/filters.py | 83 +++++++++--------------------- 1 file changed, 25 insertions(+), 58 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 262dd9e7d9..f4916297c4 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -215,7 +215,7 @@ def _get_gaussian_kernel(sigma, filter_shape_1d): return a -def _get_gaussian_filter_2d(gaussian_filter_x, gaussian_filter_y): +def _get_gaussian_kernel_2d(gaussian_filter_x, gaussian_filter_y): "Compute 2D Gaussian Kernel" gaussian_kernel = tf.matmul(gaussian_filter_x, gaussian_filter_y) return gaussian_kernel @@ -230,10 +230,7 @@ def gaussian_filter2d( constant_values: TensorLike = 0, name: Optional[str] = None, ) -> FloatTensorLike: - """Perform Gaussian Filter. If the program is running on a CPU device, it will be implemented as a sequence of 1d convolution of - gausian kernel (in x- direction and fillowed by in y direction). - In 1-D, G(x)=e**((-x**2)/2*(sigma**2)) - If program is running in GPU, then it is implemented as convolution of image with 2-d gaussian kernel. + """Perform Gaussian Blur. Args: image: Either a 2-D `Tensor` of shape `[height, width]`, @@ -268,60 +265,30 @@ def gaussian_filter2d( channels = tf.shape(image)[3] filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") - if tf.test.is_gpu_available(): - gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1]) - gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) - gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape[1]]) + gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1]) + gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) + gaussian_filter_x = tf.reshape(gaussian_filter_x, [1, filter_shape[1]]) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0]) - gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape[0], 1]) - gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) + gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0]) + gaussian_filter_y = tf.reshape(gaussian_filter_y, [filter_shape[0], 1]) + gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) - gaussian_filter_2d = tf.matmul(gaussian_filter_y, gaussian_filter_x) - gaussian_filter_2d = tf.repeat(gaussian_filter_2d, channels) - gaussian_filter_2d = tf.reshape( - gaussian_filter_2d, [filter_shape[0], filter_shape[1], channels, 1] - ) - - image = _pad( - image, filter_shape, mode=padding, constant_values=constant_values, - ) - - conv_ops = tf.nn.depthwise_conv2d( - input=image, - filter=gaussian_filter_2d, - strides=(1, 1, 1, 1), - padding="VALID", - ) - return conv_ops - else: - gaussian_filter_x = _get_gaussian_kernel(sigma, filter_shape[1]) - gaussian_filter_x = tf.repeat(gaussian_filter_x, channels) - gaussian_filter_x = tf.reshape( - gaussian_filter_x, [1, filter_shape[1], channels, 1] - ) + gaussian_filter_2d = _get_gaussian_kernel_2d( + gaussian_filter_y, gaussian_filter_x + ) + gaussian_filter_2d = tf.repeat(gaussian_filter_2d, channels) + gaussian_filter_2d = tf.reshape( + gaussian_filter_2d, [filter_shape[0], filter_shape[1], channels, 1] + ) - gaussian_filter_x = tf.cast(gaussian_filter_x, tf.float32) - gaussian_filter_y = _get_gaussian_kernel(sigma, filter_shape[0]) - gaussian_filter_y = tf.repeat(gaussian_filter_y, channels) - gaussian_filter_y = tf.reshape( - gaussian_filter_y, [filter_shape[0], 1, channels, 1] - ) + image = _pad( + image, filter_shape, mode=padding, constant_values=constant_values, + ) - gaussian_filter_y = tf.cast(gaussian_filter_y, tf.float32) - image = _pad( - image, filter_shape, mode=padding, constant_values=constant_values, - ) - conv_ops_x = tf.nn.depthwise_conv2d( - input=image, - filter=gaussian_filter_x, - strides=(1, 1, 1, 1), - padding="VALID", - ) - conv_ops = tf.nn.depthwise_conv2d( - input=conv_ops_x, - filter=gaussian_filter_y, - strides=(1, 1, 1, 1), - padding="VALID", - ) - return conv_ops + conv_ops = tf.nn.depthwise_conv2d( + input=image, + filter=gaussian_filter_2d, + strides=(1, 1, 1, 1), + padding="VALID", + ) + return conv_ops From 7b6bf24a2060d04540208a60642586e94fbbc8d4 Mon Sep 17 00:00:00 2001 From: ghosalsattam Date: Thu, 30 Apr 2020 15:09:55 +0530 Subject: [PATCH 71/71] githun check --- tensorflow_addons/image/filters.py | 6 ++-- tensorflow_addons/image/tests/filters_test.py | 33 +++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index f4916297c4..ead32d4840 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -261,6 +261,7 @@ def gaussian_filter2d( raise ValueError("Padding should be REFLECT, CONSTANT, OR SYMMETRIC") 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] filter_shape = keras_utils.normalize_tuple(filter_shape, 2, "filter_shape") @@ -285,10 +286,11 @@ def gaussian_filter2d( image, filter_shape, mode=padding, constant_values=constant_values, ) - conv_ops = tf.nn.depthwise_conv2d( + output = tf.nn.depthwise_conv2d( input=image, filter=gaussian_filter_2d, strides=(1, 1, 1, 1), padding="VALID", ) - return conv_ops + output = img_utils.from_4D_image(output, original_ndims) + return output diff --git a/tensorflow_addons/image/tests/filters_test.py b/tensorflow_addons/image/tests/filters_test.py index 259041e1b4..64723f59b7 100644 --- a/tensorflow_addons/image/tests/filters_test.py +++ b/tensorflow_addons/image/tests/filters_test.py @@ -19,7 +19,6 @@ from tensorflow_addons.image import mean_filter2d from tensorflow_addons.image import median_filter2d from tensorflow_addons.image import gaussian_filter2d -from scipy.ndimage import gaussian_filter from skimage.filters import gaussian _dtypes_to_test = { @@ -372,23 +371,23 @@ def test_gaussian_filter2d_constant(): gb = gaussian_filter2d(test_image_tf, 5, 1, padding="CONSTANT") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="constant") + test_image_np = test_image_tf.numpy() + test_image_np = np.resize(test_image_np, [40, 40]) + gb2 = gaussian(test_image_np, 1, mode="constant") np.testing.assert_allclose(gb2, gb1, 0.06) @pytest.mark.usefixtures("maybe_run_functions_eagerly") def test_gaussian_filter2d_reflect(): test_image_tf = tf.random.uniform( - [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.int32 + [1, 40, 40, 1], minval=0, maxval=255, dtype=tf.float32 ) gb = gaussian_filter2d(test_image_tf, 5, 1, padding="REFLECT") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="mirror") + test_image_np = test_image_tf.numpy() + test_image_np = np.resize(test_image_np, [40, 40]) + gb2 = gaussian(test_image_np, 1, mode="mirror") np.testing.assert_allclose(gb2, gb1, 0.06) @@ -400,9 +399,9 @@ def test_gaussian_filter2d_symmetric(): gb = gaussian_filter2d(test_image_tf, (5, 5), 1, padding="SYMMETRIC") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian_filter(test_image_cv, 1, truncate=4.6, mode="reflect") + test_image_np = test_image_tf.numpy() + test_image_np = np.resize(test_image_np, [40, 40]) + gb2 = gaussian(test_image_np, 1, mode="reflect") np.testing.assert_allclose(gb2, gb1, 0.06) @@ -415,9 +414,9 @@ def test_gaussian_filter2d_batch(image_shape): gb = gaussian_filter2d(test_image_tf, 5, 1, padding="SYMMETRIC") gb = gb.numpy() gb1 = np.resize(gb, (40, 40)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [40, 40]) - gb2 = gaussian(test_image_cv, 1, truncate=4.6, mode="reflect") + test_image_np = test_image_tf.numpy() + test_image_np = np.resize(test_image_np, [40, 40]) + gb2 = gaussian(test_image_np, 1, mode="reflect") np.testing.assert_allclose(gb2, gb1, 0.06) @@ -468,7 +467,7 @@ def test_gaussian_filter2d_channels(): gb = gaussian_filter2d(test_image_tf, 5, 1, padding="SYMMETRIC", name="gaussian") gb = gb.numpy() gb1 = np.resize(gb, (5, 5, 3)) - test_image_cv = test_image_tf.numpy() - test_image_cv = np.resize(test_image_cv, [5, 5, 3]) - gb2 = gaussian(test_image_cv, sigma=1, mode="reflect", multichannel=True) + test_image_np = test_image_tf.numpy() + test_image_np = np.resize(test_image_np, [5, 5, 3]) + gb2 = gaussian(test_image_np, sigma=1, mode="reflect", multichannel=True) np.testing.assert_allclose(gb2, gb1, 0.06)