Convolutional Neural Net — A practice in nutshell
This article is not about What is CNN? or Technical insights on CNN architecture? This article is for those who already have some knowledge about CNN and looking for CNN as an implementation perspective.
In the recent years, AI has witnessed tremendous efforts that takes the digital machines to the inference level. One of the major tasks that we human do in our every moment is looking the environment, scanning the objects associated, and recognizing them in order to take some meaningful decisions or actions.
This is really a very oblivious process for a common men, not involved in neuro medical science or computer science. But, actually it is not that obvious process. Technically it is very complex process that too require previous knowledge with us. Like, when I say An Apple, your brain immediately pops up several images and incidents that your have experienced with an apple. These is happening because all of you are aware about this fruits and thousands of times you have associated the name apple with its image, that is too with numerous features of several types of apple.
To make this happen with our digital machines, we have a excellent deep learning algorithm named Convolutional Neural Network or ConvNet or CNN .
The connectivity patterns of CNN is inspired by the organization of visual cortex of the Human brain. The CNN was first introduced by Yann LeCun.
On a first glimpse the CNN architecture seems horrible, but the good news is, it is not. We will see how to step wise implement CNN and fit your data to it in order to get predictions that you want.
The are majorly three steps for any deep learning system implementation.
- preparing your data for particular architecture
- Creating a model
- Training and inferencing with the model
In this article, I am only focusing on CNN model creation. We are going to implement the following architecture. The model architecture is given to give you ease in step wise implementation.
To begin with, we will import some important modules and methods from Keras library.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (
Dense,
Conv2D,
MaxPool2D,
Flatten,
Dropout,
BatchNormalization,
)
In next, we will define a Sequential model. Which is a model having plain stack of layers. For more information about Sequential layer visit.
model = Sequential()
Now, we will define max pooling layer followed by our 2D convolutional layer.
model.add(Conv2D(75, (3, 3), strides=1, padding="same", activation="relu", input_shape=(28, 28, 1)))
model.add(BatchNormalization())
model.add(MaxPool2D((2, 2), strides=2, padding="same"))
This early convolutions detects simple features such as lines. Here, 75 refers to the number of filters that will be learned. (3, 3) is a size of these filters. strides argument refers to the step size that the filter will take as it passes over the image. Padding helps to keep the same size output image as of input image.
Like normalizing our inputs, batch normalization scales the values in the hidden layers to improve training.
Max pooling takes an input image and shrinks it to lower resolution by pooling the maximum value from the given window.
Next, we will repeat the same process for remaining required number of Conv2D, Maxpool2D and Dense layers.
model.add(Conv2D(50, (3, 3), strides=1, padding="same", activation="relu"))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(MaxPool2D((2, 2), strides=2, padding="same"))
model.add(Conv2D(25, (3, 3), strides=1, padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPool2D((2, 2), strides=2, padding="same"))
Dropout is a technique for preventing overfitting by randomly selecting neurons and turning them of, so that they can not give their participation in either forward or backward pass. The value of drop out denotes, how many neurons you want to drop.
Dense layer is deeply connect neural network layer that perform the following operation:
output = activation(dot(input, weight_data) + bias)
Now the last step of our model is to take the output. This will be performed using Flattening the values.
model.add(Flatten())
model.add(Dense(units=512, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(units=num_classes, activation="softmax"))
Flatten() takes the output of one layer which is having the multidimensional form and convert it into one-dimensional array. This is called feature vector and this will be connected to the last layer used for final classification.
So, these is it. Isn't it really simple?
Bonus: you can visualize your model that you have just designed using model.summary() method.
model.summary()
This method prints the string summary of your network.
Thanks for upgrading yourself. Share your views via comments.