Introduction to Convolution Neural Network
Introduction to Convolution Neural Network
The data is fed into the model and output from each layer is
obtained from the above step is called feedforward, we then
calculate the error using an error function, some common error
functions are cross-entropy, square loss error, etc. The error
function measures how well the network is performing. After that,
we backpropagate into the model by calculating the derivatives.
This step is called Backpropagation which basically is used to
minimize the loss.
CNN architecture
Convolutional Neural Network consists of multiple layers like the
input layer, Convolutional layer, Pooling layer, and fully connected
layers.
Now imagine taking a small patch of this image and running a small
neural network, called a filter or kernel( A filter, or kernel, in a CNN is
a small matrix of weights that slides over the input data (such as an
image), performs element-wise multiplication with the part of the input
it is currently on, and then sums up all the results into a single output
pixel) on it, with say, K outputs and representing them vertically.
Now slide that neural network across the whole image, as a result,
we will get another image with different widths, heights, and
depths. Instead of just R, G, and B channels now we have more
channels but lesser width and height. This operation is
called Convolution. If the patch size is the same as that of the
image it will be a regular neural network. Because of this small
patch, we have fewer weights.
Step:
import the necessary libraries
set the parameter
define the kernel
Load the image and plot it.
Reformat the image
Apply convolution layer operation and plot the output
image.
Apply activation layer operation and plot the output image.
Apply pooling layer operation and plot the output image.
Python3
import numpy as np
import tensorflow as tf
plt.rc('figure', autolayout=True)
plt.rc('image', cmap='magma')
[-1, 8, -1],
])
image = tf.io.read_file('Ganesh.jpg')
image = tf.io.decode_jpeg(image, channels=1)
img = tf.squeeze(image).numpy()
plt.figure(figsize=(5, 5))
plt.imshow(img, cmap='gray')
plt.axis('off')
plt.show();
# Reformat
conv_fn = tf.nn.conv2d
image_filter = conv_fn(
input=image,
filters=kernel,
strides=1, # or (1, 1)
padding='SAME',
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(
tf.squeeze(image_filter)
plt.axis('off')
plt.title('Convolution')
# activation layer
relu_fn = tf.nn.relu
# Image detection
image_detect = relu_fn(image_filter)
plt.subplot(1, 3, 2)
plt.imshow(
tf.squeeze(image_detect)
plt.axis('off')
plt.title('Activation')
# Pooling layer
pool = tf.nn.pool
image_condense = pool(input=image_detect,
window_shape=(2, 2),
pooling_type='MAX',
strides=(2, 2),
padding='SAME',
plt.subplot(1, 3, 3)
plt.imshow(tf.squeeze(image_condense))
plt.axis('off')
plt.title('Pooling')
plt.show()
Output:
Original Grayscale image
Output