import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
Tensorflow has 1D,2D and 3D convolutions. The dimension of CNN represents the kernel dimension. Eg, For 2D Conv, we have a 2D kernel
The shape of 2D kernel is (H,W,C,NumFilters) and bias is (NumFilters,)
tf.layers.conv2d(
inputs, #Should be 4D (B,H,W,C)
filters, #Specify num filters
kernel_size, #2D
strides=(1, 1),
padding='valid', #VALID : No zero padding; SAME:Zero padding
data_format='channels_last',
dilation_rate=(1, 1),
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None,
reuse=None
)
The filters determine the operation that is done on the image
im = Image.open('pics/car.jpg').convert("L")
im
input_im = tf.constant(im)
input_im = tf.expand_dims(input_im,0)
input_im = tf.expand_dims(input_im,-1)
input_im = tf.image.convert_image_dtype(input_im,tf.float32)
input_im.shape
Check the shape of Kernel and bias
W : (H,W,C,N)
b : (N,)
conv2d = tf.layers.conv2d(input_im,filters=15,kernel_size=(4,4),strides=(1,1))
tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope=conv2d.name.split('/')[0])
outline_kernel = np.array([[-1.,-1.,-1.],[-1.,8.,-1.],[-1.,-1.,-1.]])
outline_kernel = outline_kernel.reshape(3,3,1,1)
#outline_kernel = np.concatenate((outline_kernel,outline_kernel,outline_kernel),axis=2)
print(outline_kernel.shape)
outline_kernel
kernel_init = tf.initializers.constant(outline_kernel)
conv2d = tf.layers.conv2d(input_im,filters=1,kernel_size=(3,3),strides=(1,1),kernel_initializer=kernel_init)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
o = sess.run(conv2d)
o.shape
Conv output
plt.imshow(o[0,:,:,0],cmap='gray') #o[0].reshape(373,-1) also works
Filter
outline_kernel.shape
plt.imshow(outline_kernel[:,:,0,0])
emboss_kernel = np.array([[-2.,-1.,0.],[-1,1,1],[0,1,2]])
emboss_kernel = emboss_kernel.reshape(3,3,1,1)
kernel_init = tf.initializers.constant(emboss_kernel)
conv2d = tf.layers.conv2d(input_im,filters=1,kernel_size=(3,3),strides=(2,2),kernel_initializer=kernel_init)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
o = sess.run(conv2d)
plt.imshow(o[0,:,:,0],cmap='gray')
plt.imshow(emboss_kernel[:,:,0,0])
Use it when you have the kernels and bias as explicit tensorflow variables (ie, obtained from other models)
#W --> [filter H, filter W, Channel In, Channel Out]
#b --> [Channel Out,]
#x --> [Batch, Im H, Im W, Channel In]
W = tf.Variable(tf.random_normal((5,5,3,6)))
b = tf.Variable(tf.ones((6,)))
input_x = tf.random_normal((1,10,10,3))
#stride = [batch,H,W,channel]
conv_out = tf.nn.conv2d(input_x,W,strides=[1,2,2,1],padding='SAME') + b
conv_out