In [1]:
import tensorflow as tf
import numpy as np
from skimage import data as imgdata
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
In [2]:
img = imgdata.camera().astype(np.float32)
img
Out[2]:
In [3]:
plt.plot(img)
plt.show()
plt.imshow(img)
plt.show()
plt.imshow(img, cmap = "gray")
plt.show()
In [4]:
img.shape
Out[4]:
To perform 2D convolution with TensorFlow we need our images to be four-dimensional.
The resulting image from this convolution would have images with the following parameters :
The number of images, width, height, channels
In [5]:
img4d = tf.reshape(img,[1,img.shape[0],img.shape[1],1])
print(img4d)
print(img4d.eval())
In [6]:
# using a Gaussian Kernel
mean = 0.0
sigma = 1.0
x = tf.linspace(-5.0,5.0,100)
z = (tf.exp(tf.neg((tf.pow(x-mean,2.0) /
2.0 * tf.pow(sigma , 2.0)))) * (1.0 / sigma*tf.sqrt(tf.multiply(2.0 , 3.1415))))
onedsize = z.get_shape().as_list()[0] #here we get the shape for the array of values created
twodz = tf.multiply(tf.reshape(z,[onedsize,1]),tf.reshape(z,[1,onedsize]))
# we multiply z with its transpose to get a n by n matrix
fourdz = tf.reshape(twodz, [onedsize,onedsize,1,1]) #this is a kernel
print(fourdz.get_shape().as_list())
lets use this Gaussian Kernel to convolve an image
In [7]:
convolve = tf.nn.conv2d(img4d,fourdz,strides=[1,1,1,1],padding="SAME")
In [8]:
res = convolve.eval()
print(res.shape)
We have to change the image back to a two-dimensional array
In [9]:
print("Original Image Value DIstribution")
plt.plot(img)
plt.show()
print("Convolved Image Value Distribution")
plt.plot(np.squeeze(res))
plt.show()
plt.imshow(np.squeeze(res),cmap='gray')
plt.show()