Beginners Guide to Convolutional Neural Network with Implementation in Python

Deepanshi Last Updated : 30 Sep, 2024
7 min read

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

Introduction to CNN

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:

  • It is too much computation for an ANN model to train large-size images and different types of image channels.
  • The next disadvantage is that it is unable to capture all the information from an image whereas a CNN model can capture the spatial dependencies of the image.
  • Another reason is that ANN is sensitive to the location of the object in the image i.e if the location or place of the same object changes, it will not be able to classify properly.

Components of CNN

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:

  • Input layer
  • Convolution layer + Activation function
  • Pooling layer
  • Fully Connected Layer
Convolutional Neural Network with Implementation in Python layers

    Source: https://learnopencv.com/image-classification-using-convolutional-neural-networks-in-keras/

Let’s learn about each layer in detail.

Input layer

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.

Convolutional Neural Network with Implementation in Python layers

Convolution Layer

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.

Convolutional Neural Network with Implementation in Python conv. layer

Understanding the Feature Map

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.

Convolutional Neural Network with Implementation in Python conv layer 2

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.

layer 3

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.

layer 3

Pooling Layer

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.

Pooling layer

Fully Connected Layer

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.

Fully connected layer

                       Source: https://developersbreach.com/convolution-neural-network-deep-learning/

How to Implement CNN in Python?

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.

  1. #importing the required libraries

    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

  2. #loading data

    (X_train,y_train) , (X_test,y_test)=mnist.load_data()

  3. #reshaping 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))

  4. #checking the shape after reshaping

    print(X_train.shape)
    print(X_test.shape)

  5. #normalizing the pixel values

    X_train=X_train/255
    X_test=X_test/255

  6. #defining model

    model=Sequential()model=Sequential()

  7. #adding convolution layer

    model.add(Conv2D(32,(3,3),activation=’relu’,input_shape=(28,28,1)))

  8. #adding pooling layer

    model.add(MaxPool2D(2,2))

  9. #adding fully connected layer

    model.add(Flatten())
    model.add(Dense(100,activation=’relu’))

  10. #adding output layer

    model.add(Dense(10,activation=’softmax’))

  11. #compiling the model

    model.compile(loss=’sparse_categorical_crossentropy’,optimizer=’adam’,metrics=[‘accuracy’])

  12. #fitting the model

    model.fit(X_train,y_train,epochs=10)

Output:

CNN in python Convolutional Neural Network with Implementation in Python
#evaluting the model
model.evaluate(X_test,y_test)
output

Conclusion

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.

About the Author

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.

Frequently Asked Questions

Q1. What is CNN in Python?

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.

Q2. What are the 4 types of CNN?

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).

Q3. What is tensorflow in python?

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.

Responses From Readers

Clear

Van
Van

this is an exquisite blog, thank you.

saley
saley

This is so useful. I am a beginner in this area. thank you so much

Ahamadullah Khan
Ahamadullah Khan

In a very simple way, explained CNN so beautifully and effectively. Excellent!

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details