We have learned about the Artificial Neural network and its application in the last few articles. This blog will be all about another Deep Learning model which is the Convolutional Neural Network. As always, this will be a beginner’s guide written in a way that newcomers to the data science field can easily understand the concept. So, keep reading! 🙂
This article was published as a part of the Data Science Blogathon
Convolutional Neural Network is a Deep Learning algorithm specially designed for working with Images and videos. It takes images as inputs, extracts and learns the features of the image, and classifies them based on the learned features.
This algorithm is inspired by the working of a part of the human brain which is the Visual Cortex. The visual Cortex is a part of the human brain which is responsible for processing visual information from the outside world. It consists of various layers, with each layer serving a specific function by extracting information from the image or visual. Ultimately, all the information gathered from each layer combines to interpret or classify the image or visual.
Similarly, CNN utilizes various filters, with each filter extracting specific information from the image, such as edges and different shapes (vertical, horizontal, round). These extracted features combine to help identify the image.
Now, the question here can be: Why can’t we use Artificial Neural Networks for the same purpose? This is because there are some disadvantages with ANN:
The CNN model works in two steps: feature extraction and Classification
Feature extraction is a phase where various filters and layers apply to the images to extract information and features. Once this process is complete, the extracted data moves to the next phase, classification, where it is classified based on the target variable of the problem.
A typical CNN model looks like this:
Source: https://learnopencv.com/image-classification-using-convolutional-neural-networks-in-keras/
Let’s learn about each layer in detail.
As the name says, it’s our input image and can be Grayscale or RGB. Every image is made up of pixels that range from 0 to 255. We need to normalize them i.e convert the range between 0 to 1 before passing it to the model.
Below is the example of an input image of size 4*4 and has 3 channels i.e RGB and pixel values.
The convolution layer applies the filter to the input image to extract or detect its features. The filter processes the image multiple times, creating a feature map that aids in classifying the input image. Let’s understand this with the help of an example. For simplicity, we will take a 2D input image with normalized pixels.
In the above figure, we have an input image of size 6*6 and applied a filter of 3*3 on it to detect some features. In this example, we have applied only one filter but in practice, many such filters are applied to extract information from the image.
The result of applying the filter to the image is that we get a Feature Map of 4*4 which has some information about the input image. Many such feature maps are generated in practical applications.
Let’s get into some maths behind getting the feature map in the above image.
As presented in the above figure, in the first step, the filter applies to the green highlighted part of the image. The pixel values of the image multiply with the filter values (as shown in the figure using lines) and then sum up to produce the final value.
In the next step, shift the filter by one column, as illustrated in the figure below. This movement to the next column or row is known as stride. In this example, we take a stride of 1, meaning we shift by one column.
Similarly, the filter passes over the entire image and we get our final Feature Map. Once we get the feature map, an activation function is applied to it for introducing nonlinearity.
A point to note here is that the Feature map we get is smaller than the size of our image. As we increase the value of stride the size of the feature map decreases.
The pooling layer follows the convolutional layer and reduces the dimensions of the feature map, helping to preserve important information and features of the input image while also reducing computation time.
Using pooling, a lower resolution version of input is created that still contains the large or important elements of the input image.
The most common types of Pooling are Max Pooling and Average Pooling. The below figure shows how Max Pooling works. Using the Feature map which we got from the above example to apply Pooling. Here we are using a Pooling layer of size 2*2 with a stride of 2.
The process takes the maximum value from each highlighted area, resulting in a new version of the input image with a size of 2×2. After applying pooling, the dimension of the feature map reduces.
Till now we have performed the Feature Extraction steps, now comes the Classification part. The Fully connected layer (as we have in ANN) is used for classifying the input image into a label. This layer connects the information extracted from the previous steps (i.e Convolution layer and Pooling layers) to the output layer and eventually classifies the input into the desired label.
The complete process of a CNN model can be seen in the below image.
Source: https://developersbreach.com/convolution-neural-network-deep-learning/
We will be using the Mnist Digit classification dataset which we used in the last blog of Practical Implementation of ANN. Please refer to that first for a better understanding of the application of CNN.
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
(X_train,y_train) , (X_test,y_test)=mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1))
print(X_train.shape)
print(X_test.shape)
X_train=X_train/255
X_test=X_test/255
model=Sequential()model=Sequential()
model.add(Conv2D(32,(3,3),activation=’relu’,input_shape=(28,28,1)))
model.add(MaxPool2D(2,2))
model.add(Flatten())
model.add(Dense(100,activation=’relu’))
model.add(Dense(10,activation=’softmax’))
model.compile(loss=’sparse_categorical_crossentropy’,optimizer=’adam’,metrics=[‘accuracy’])
model.fit(X_train,y_train,epochs=10)
Output:
#evaluting the model model.evaluate(X_test,y_test)
This blog covers some important elements of CNN, but many topics remain, such as padding, data augmentation, and more details on stride. Since deep learning is a vast and ever-evolving field, I will discuss these topics in future blogs. I hope you found this article helpful and worth your time investing on.
In the next few blogs, you can expect a detailed implementation of CNN with explanations and concepts like Data augmentation and Hyperparameter tuning.
I am Deepanshi Dhingra currently working as a Data Science Researcher, and possess knowledge of Analytics, Exploratory Data Analysis, Machine Learning, and Deep Learning. Feel free to content with me on LinkedIn for any feedback and suggestions.
A. A Convolutional Neural Network (CNN) is a type of deep neural network used for image recognition and classification tasks in machine learning. Python libraries like TensorFlow, Keras, PyTorch, and Caffe provide pre-built CNN architectures and tools for building and training them on specific datasets.
A. The four common types of Convolutional Neural Networks (CNNs) are LeNet, AlexNet, VGGNet, and ResNet. LeNet is the first CNN architecture designed for handwritten digit recognition. In contrast, AlexNet, VGGNet, and ResNet are deep CNNs that achieved top performance in the ImageNet Large Scale Visual Recognition Challenge (ILSVRC).
A. TensorFlow is an open-source machine learning and artificial intelligence library developed by Google Brain Team. It is written in Python and provides high-level APIs like Keras, as well as low-level APIs, for building and training machine learning models. TensorFlow also offers tools for data preprocessing, visualization, and distributed computing.
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.