Table of contents
1.
Introduction
2.
What is LeNet5?
3.
Architecture of LeNet5
3.1.
First layer
3.2.
Second layer
3.3.
Third layer
3.4.
Fourth layer
3.5.
Fifth layer
3.6.
Sixth layer
3.7.
Seventh layer
4.
Summary of the architecture of LeNet5
5.
Implementation of LeNet5
6.
Advantages and disadvantages of LeNet5
7.
Frequently Asked Questions
7.1.
What is LeNet5 used for?
7.2.
Is Alexnet better than LeNet5?
7.3.
How many layers are there in LeNet5?
7.4.
Why is LeNet5 so in demand?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

LeNet-5 CNN Architecture

Author Tashmit
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

AI has been growing and reducing the gap between the capabilities of humans and machines. The researchers work on various characteristics to make extraordinary things happen. One such area is the domain of deep learning. This field aims to train machines to view the world as humans do. It is constructed and improved with time, primarily over one particular algorithm; a Convolutional Neural Network.

what is lenet5

A Convolutional Neural Network is the Artificial Neural Network most frequently applied to analyze images. This article will discuss LeNet5, one of the Convolution Neural Network applications. So, let us discuss what LeNet5 is.

What is LeNet5?

LeNet5 is one of the applications of the Convolution Neural Network. Yann LeCun developed it in 1989. CNN is a feed-forward neural network whose neurons can respond to the neighboring neurons and performs magnificently in a wide range of image processing. 

Before we understand the architecture of LeNet5, make sure you know the basic terms of Convolution Neural Network to revise this article.

Architecture of LeNet5

LeNet5 is a network made up of 7 layers. It consists of 3 convolution layers, two subsampling layers, and two fully connected layers.

Architecture of LeNet5

As shown in the picture, the input layer is the initial layer. However, this layer is not considered a network layer as nothing is learned at this layer. This layer's only role is to take an image of 32x32 as input and pass it on to the next layer. 

First layer

After this, the first convolution step takes place, and the input image is convoluted to the size of 28x28.

First Layer of LeNet5

Second layer

Next is the subsampling layer, in which the size is reduced to half, i.e., 14x14.

Second Layer of LeNet5

Third layer

In the third layer, convolution occurs again, but this time with 16 filters of 5x5 size. After this layer, the size of the input image is reduced to 10x10x16.

third layer

Fourth layer

The subsampling takes place, and the image size in this step is reduced to 5x5x16. In this layer, the input for the very last function diagram comes from all the remaining function diagrams. 

fourth layer

Fifth layer

This is the final layer of the convolution-subsampling pair.

Fifth Layer of LeNet5

There are 120 filters in this layer with a convolution size of 5x5. After this layer, the feature map is 1x1x120, and the flattened result is 120 values. 

Sixth layer

After the convolution-subsampling, pairs come to the fully connected layer. It consists of 84 nodes with a -1 (for white) and +1 (for black) code.

Sixth Layer of LeNet5

Credit: hackmd.io

It is referred as a layer of convolutional in preference to an ultimately linked layer because the input of LeNet5 becomes large. Its shape stays unchanged, then its output length is more prominent than 1x1, i.e., now no longer a linked layer.  

Seventh layer

The last layer is the output layer, which is also fully connected to 10 nodes.

seventh layer

Each node represents a number between 0 and 9.

Summary of the architecture of LeNet5

Architecture of LeNet5

Implementation of LeNet5

To implement LeNet5, we will work on the Fashion MNIST dataset. 

First, we will import the necessary libraries.

# Importing essential libraries
import tensorflow as tf
from tensorflow.keras import Model, layers
from tensorflow.keras.datasets import fashion_mnist

Next, we will load the Fashion MNIST dataset and divide them into training and testing datasets. Then normalise the dataset.

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

# Preprocess the data
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)

The graphical representation before normalisation is:

Graph of Before Normalisation

The histogram after normalisation would be:

Graph of After Normalisation

Now after creating the training and testing datasets and reshaping them, we will implement the model.

# Defining the LeNet-5 model
class LeNet5(Model):
    def __init__(self):
        super(LeNet5, self).__init__()
        self.conv1 = layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1))
        self.pool1 = layers.MaxPooling2D(pool_size=(2, 2), strides=2)
        self.conv2 = layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu')
        self.pool2 = layers.MaxPooling2D(pool_size=(2, 2), strides=2)
        self.flatten = layers.Flatten()
        self.fc1 = layers.Dense(units=120, activation='relu')
        self.fc2 = layers.Dense(units=84, activation='relu')
        self.fc3 = layers.Dense(units=10, activation='softmax')

    def call(self, inputs):
        x = self.conv1(inputs)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

# Create an instance of the LeNet-5 model
model = LeNet5()

# Compiling the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Training the model
history = model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test))

# Evaluating the model on the test set
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print('Test accuracy:', test_acc)

Output:

output

Advantages and disadvantages of LeNet5

Now, we will discuss the advantages and disadvantages of LeNet5:

  • Initially, Lenet was used for handwritten digit identification. Hence, it was able to outplay all other significant models. The architecture of LeNet5 was relatively easy, with only five layers of Convolution and sub-sampling. 
     
  • Later, two more layers were added to improve its performance. The significant advantage of LeNet-5 is that it consists of a convolution layer with stride two and an Average pooling layer with stride 1. 
     
  • On the contrary, LeNet-5 is not applied widely; instead, AlexNet and VGG-16 Net are used as they consist of deeper layers resulting in accurate classifications.
     
  • Nowadays, most commonly and widely, LeNet-5 is used to detect handwriting and match handwritten cheques by banks.


Check out this problem - Largest Rectangle in Histogram

Frequently Asked Questions

What is LeNet5 used for?

Earlier, LeNet5 was used in banks to detect the handwriting on the cheques. 

Is Alexnet better than LeNet5?

AlexNet consists of 8 more layers than LeNet5, which results in more accurate classification. Therefore, AlexNet is better than LeNet5. 

How many layers are there in LeNet5?

LeNet5 has seven layers: three convolution layers, two pooling layers, and two fully connected layers.

Why is LeNet5 so in demand?

The primary reason behind the popularity of LeNet5 is its simple and straightforward architecture. It is a multi-layer convolution neural network for classifying an image.

Conclusion

In this article, we extensively discussed the LeNet5 topic and its implementation in Python. We hope that this blog has helped you enhance your knowledge regarding LeNet5. You can check out our other blogs, 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja!

Live masterclass