Deep Learning with CNN Techniques
Deep Learning with CNN Techniques
Module 2
• Improving Deep Neural Networks:
• Convolutional Neural Network, Transfer learning
Techniques,
• Variants of CNN: DenseNet, PixelNet, ResNet, AlexNet,
• Sequence Modelling: Recurrent Neural Network and
its variants - Long Short Term Memory (LSTM), Gated
Recurrent Unit (GRU).
11/11/2025
Mr. Pakruddin B, Assit. Prof., PSCS 1
Convolution Neural Network
11/11/2025 2
Mr. Pakruddin B, Assit. Prof., PSCS
Some Fundamentals of Image
Processing
Images are represented as matrix of pixel values.
Can be in gray-scale or RGB
11/11/2025 3
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 4
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 5
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 6
Mr. Pakruddin B, Assit. Prof., PSCS
Why not ANN?????
• Due to the large number of parameters to be learned by the model
• For an input image of size 28 x 28, the input layer has :
• [Link] inputs : 28 x 28 x 3
• No. of weights : 28 x 28 x 3 x no. of hidden neurons in next layer
• Hence the model has to learn many parameters in subsequent layers
11/11/2025 7
Mr. Pakruddin B, Assit. Prof., PSCS
CNN
• CNN are widely used tools in DL
• Frequently used with image data sets
• Also work on other inputs such as texts, signals and other continuous responses
• It has input layer, many hidden layers and an output layer.
• The hidden layers include feature learning and classification layers
• Feature learning is done using a set of operations like convolution using a
kernel(filter), ReLu and pooling
11/11/2025 8
Mr. Pakruddin B, Assit. Prof., PSCS
• Convolution puts the image through a set of filters(kernels). Each filter activates
certain features in the image, relevant to the learning task.
• Ex: if the model has to detect faces in the image, there may be filters for edge
detection, shape detection, etc moving through the image to capture those
significant images for face detection
• Rectified linear unit (ReLU) allows for faster and more effective training by
mapping negative values to zero and maintaining positive values.
• These three operations are repeated over tens or hundreds of layers, with each
layer learning to detect different features.
11/11/2025 9
Mr. Pakruddin B, Assit. Prof., PSCS
• Classification Layers: After feature detection, the architecture of a CNN shifts to
classification.
• The next-to-last layer is a fully connected layer (FC) that outputs a vector of K
dimensions where K is the number of classes that the network will be able to
predict.
• This vector contains the probabilities for each class of any image being classified.
• The final layer of the CNN architecture uses a softmax function to provide the
classification output
11/11/2025 10
Mr. Pakruddin B, Assit. Prof., PSCS
CNN
11/11/2025 11
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 12
Mr. Pakruddin B, Assit. Prof., PSCS
• Convolution of an image with different filters can perform operations such
as edge detection, blur and sharpen by applying filters
• Some common filters are
11/11/2025 13
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 14
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 15
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 16
Mr. Pakruddin B, Assit. Prof., PSCS
Details of Convolution layer
A DNN trained to recognise characters, should be able to recognise different
renditions of a character
Following are different renditions of ‘x’ and ‘o’
11/11/2025 17
Mr. Pakruddin B, Assit. Prof., PSCS
Reference image of ‘x’ Input image of ‘x’
• A location invariant model will be able to identify whether the features of the
reference image are present in the input image or not, rather than where they are
present.
• Convolution operation helps to build such location invariant models.
• Small patches of the reference image(filters) are mixed with the nearby locations in
the input image to see whether there is a match.
11/11/2025 18
Mr. Pakruddin B, Assit. Prof., PSCS
• Each convolution filter represents a feature of interest and the CNN algorithm
finds which features comprise the reference image(alphabet of interest)
• Convolution has 4 steps :
• Line up the feature(filter) and the input image
• Multiply the corresponding pixels of the filter and the input image
• Find the sum of these products.
• Divide this sum by the total no. of pixels.
11/11/2025 19
Mr. Pakruddin B, Assit. Prof., PSCS
• Place this final value at the centre of the filtered image
11/11/2025 20
Mr. Pakruddin B, Assit. Prof., PSCS
• Move the feature(filter) to every other position in the image to see how match
the feature matches with the image
• The number of steps the filter takes to move on the image is the stride.
• With a stride = 1, the final feature map is
• The output signal strength is not dependent on where the features are located, but
simply whether the features are present. Hence, an alphabet could be sitting in
different positions and the Convolutional Neural Network algorithm would still be
able to recognize it.
• Image of size nxn and filter of size fxf will result in convolved image of size n-f+1 x
n-f+1
11/11/2025 21
Mr. Pakruddin B, Assit. Prof., PSCS
[Link]/convolutional-neural-networks-explained-
9cc5188c4939#:~:text=A%20Convolutional%20Neural%20Network%2C%
20also,binary%20representation%20of%20visual%20data
.
11/11/2025 22
Mr. Pakruddin B, Assit. Prof., PSCS
• Stride – no. of pixel shifts taken by a filter when it moves
on the input image
11/11/2025 23
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 24
Mr. Pakruddin B, Assit. Prof., PSCS
We have seen that convolving an input of 6 X 6 dimension with a 3 X 3
filter results in 4 X 4 output. We can generalize it and say that if the
input is n X n and the filter size is f X f, then the output size will be (n-
f+1) X (n-f+1):
•Input: n X n
•Filter size: f X f
•Output: (n-f+1) X (n-f+1)
There are primarily two disadvantages here:
[Link] time we apply a convolutional operation, the size of the image
shrinks
[Link] present in the corner of the image are used only a few number
of times during convolution as compared to the central pixels. Hence,
we do not focus too much on the corners since that can lead to
information loss
11/11/2025 25
Mr. Pakruddin B, Assit. Prof., PSCS
Padding
To overcome these issues, we can pad the image with an additional border,
i.e., we add one pixel all around the edges. This means that the input will be
an 8 X 8 matrix (instead of a 6 X 6 matrix). Applying convolution of 3 X 3 on it
will result in a 6 X 6 matrix which is the original shape of the image. This is
where padding comes to the fore:
•Input: n X n Padding: p Filter size: f X f
•Output: (n+2p-f+1) X (n+2p-f+1)
There are two common choices for padding:
[Link]: It means no padding. If we are using valid padding, the output will
be (n-f+1) X (n-f+1)
[Link]: Here, we apply padding so that the output size is the same as the
input size, i.e., n+2p-f+1 = n , So, p = (f-1)/2
11/11/2025 26
Mr. Pakruddin B, Assit. Prof., PSCS
Padding
the optimal amount of zero padding (in terms of test accuracy) is somewhere between
Pad = 0 and the pad that causes the output and input to have the same width and height.
Goodfellow et al.
11/11/2025 27
Mr. Pakruddin B, Assit. Prof., PSCS
Stride
Defines how much the filter moves in the convolution
11/11/2025 28
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 29
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 30
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 31
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 32
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 33
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 34
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 35
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 36
Mr. Pakruddin B, Assit. Prof., PSCS
Convolutions Over Volume
Suppose, instead of a 2-D image, we have a 3-D input image of shape 6
X 6 X 3. How will we apply convolution on this image? We will use a 3 X
3 X 3 filter instead of a 3 X 3 filter. Let’s look at an example:
•Input: 6 X 6 X 3
•Filter: 3 X 3 X 3
The dimensions above represent the height, width and channels in the
input and filter. Keep in mind that the number of channels in the
input and filter should be same. This will result in an output of 4 X
4.
Since there are three channels in the input, the filter will consequently also
have three channels. After convolution, the output shape is a 4 X 4 matrix.
So, the first element of the output is the sum of the element-wise product
of the first 27 values from the input (9 values from each channel) and the
27 values from the filter. After that we convolve over the entire image.
11/11/2025 37
Mr. Pakruddin B, Assit. Prof., PSCS
Generalized dimensions can be given as:
•Input: n X n X nc
•Filter: f X f X nc
•Padding: p
•Stride: s
•Output: [(n+2p-f)/s+1] X [(n+2p-f)/s+1] X nc’
Here, nc is the number of channels in the input and filter, while
nc’ is the number of filters.
11/11/2025 38
Mr. Pakruddin B, Assit. Prof., PSCS
Pooling
11/11/2025 39
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 40
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 41
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 42
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 43
Mr. Pakruddin B, Assit. Prof., PSCS
RELU
• ReLu is applied to the convolution outputs.
• The main aim is to remove all the negative values from the convolution. All
the positive values remain the same but all the negative values get changed
to zero
11/11/2025 44
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 45
Mr. Pakruddin B, Assit. Prof., PSCS
RELU for all Feature Maps
11/11/2025 46
Mr. Pakruddin B, Assit. Prof., PSCS
POOLING
• Pooling layers section would reduce the number of parameters
when the images are too large.
• Spatial pooling also called subsampling or downsampling which
reduces the dimensionality of each map but retains important
information.
• Spatial pooling can be of different types: 1. Max Pooling 2.
Average Pooling 3. Sum Pooling
• Max pooling takes the largest element from the rectified feature
map.
• Taking average of all elements in the feature map is average
pooling.
• Sum of all elements in the feature map call as sum pooling.
11/11/2025 47
Mr. Pakruddin B, Assit. Prof., PSCS
POOLING
11/11/2025 48
Mr. Pakruddin B, Assit. Prof., PSCS
Pooling for the first rectified feature map with
window size = 2; stride = 2
11/11/2025 49
Mr. Pakruddin B, Assit. Prof., PSCS
Pooling for all the rectified feature maps with
window size = 2; stride = 2
11/11/2025 50
Mr. Pakruddin B, Assit. Prof., PSCS
Stacking the Layers
11/11/2025 51
Mr. Pakruddin B, Assit. Prof., PSCS
Adding another Layer
11/11/2025 52
Mr. Pakruddin B, Assit. Prof., PSCS
Stacking the Layers
We can reduce the image size from 4 x 4 to something lesser by stacking the layers.
Repeat the operations convolution, ReLU and Pooling after first pass
11/11/2025 53
Mr. Pakruddin B, Assit. Prof., PSCS
Adding another Layer
11/11/2025 54
Mr. Pakruddin B, Assit. Prof., PSCS
Fully Connected Layer
• The last layers in the network are fully connected, meaning that neurons of preceding
layers are connected to every neuron in subsequent layers.
• This mimics high level reasoning where all possible pathways from the input to output
are considered.
• Fully connected layer is the final layer where the classification actually happens.
• Here we take our filtered and shrinked images and put them into one single list –
Flattening
11/11/2025 55
Mr. Pakruddin B, Assit. Prof., PSCS
Classification?
• When the input image is ‘X’, there will be some element(1st, 4th, 5th, 10th and
11th values )in the flattened vector that will be high
• For image ‘O’ there will be some element in the vector that will be high.
• The concept is similar for the other alphabets as well
11/11/2025 56
Mr. Pakruddin B, Assit. Prof., PSCS
Prediction?
• we have a 12 element vector below, obtained after passing the input image
of a random letter through all the layers of our network.
• Predict it as ‘x’ or ‘o’
11/11/2025 57
Mr. Pakruddin B, Assit. Prof., PSCS
Predict
11/11/2025 58
Mr. Pakruddin B, Assit. Prof., PSCS
Typical CNN Architecture
11/11/2025 59
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 60
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 61
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 62
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 63
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 64
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 65
Mr. Pakruddin B, Assit. Prof., PSCS
output_shape = [Link]((input_shape - pool_size) / strides) + 1
(when input_shape >= pool_size)
11/11/2025 66
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 67
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 68
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 69
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 70
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 71
Mr. Pakruddin B, Assit. Prof., PSCS
11/11/2025 72
Mr. Pakruddin B, Assit. Prof., PSCS
[Link]
[Link]
11/11/2025 73
Mr. Pakruddin B, Assit. Prof., PSCS
Why ConvNet should be Deep?
Rob Fergus,
NIPS 2013
For reporting error rates, a model predicts the top 5 most likely labels. The 'top-
5' error rate is the fraction of test images for which the correct label is amongst
this top 5, and the 'top-1' error rate is the fraction of test images for which the
correct label is the one judged most likely by the model
11/11/2025 74
Mr. Pakruddin B, Assit. Prof., PSCS
Why ConvNet should be Deep?
11/11/2025 75
Mr. Pakruddin B, Assit. Prof., PSCS
Why ConvNet should be Deep?
11/11/2025 76
Mr. Pakruddin B, Assit. Prof., PSCS
Why ConvNet should be Deep?
11/11/2025 77
Mr. Pakruddin B, Assit. Prof., PSCS
Why ConvNet should be Deep?
CNN code
[Link]
=sharing
11/11/2025 78
Mr. Pakruddin B, Assit. Prof., PSCS
Understanding and Calculating the number of Parameters in Convolution Neural
Networks (CNNs)
11/11/2025 79
Mr. Pakruddin B, Assit. Prof., PSCS
1. The first input layer has no parameters. Input layer has nothing to learn, at
it’s core, what it does is just provide the input image’s shape. So no learnable parameters
here. Thus number of parameters = 0.
7. Parameters in the Seventh FC4 layer is: ((current layer c*previous layer
p)+1*c) = 84*120+1* 84 = 10164.
8. The Eighth Softmax layer has ((current layer c*previous layer p)+1*c)
parameters = 10*84+1*10 = 850.
Most important feature of RNN is Hidden state, which remembers some information about a
sequence.
RNN has a “memory” which remembers all information about what has been calculated in the
previous day.
It uses the same parameters for each input as it performs the same task on all the inputs or
hidden layers to produce the output.
This reduces the complexity of parameters, unlike other neural networks
One to Many
One to Many is a kind of RNN architecture is applied
in situations that give multiple output for a single
input.
• These “Gates” control which information from the “distant path” should be passed
through the network to update the current cell state.
• The most commonly used variants of RNN which are capable of remembering long term
dependencies using “gated cell” is the
LSTM (Long Short Term Memory) and GRU(gated recurrent unit).
• The “gates” perform different tensor operations to decide which information can be
removed/added to the current hidden state.
The tanh activation is used to help regulate the values flowing through the network.
It squishes values to always be between -1 and 1.
RNNs involve less computational resources
But works well only for shorter sequences
The cell state, helps information from the earlier time steps to make it’s way to
later time steps, reducing the effects of short-term memory. As the cell state
goes on its journey, information get’s added or removed to the cell state via
gates.
The gates are different neural networks that decide which information is
allowed on the cell state. The gates can learn what information is relevant to
keep or forget during training.
The network can learn which data is not important therefore can be forgotten
or which data is important to keep using Sigmoid.
Three different gates regulate information flow in an LSTM cell. A forget gate,
input gate, and output gate.
Forget Gate:
This gate decides what information should be thrown away or kept.
Information from the previous hidden state and information from the
current input is passed through the sigmoid function.
Values come out between 0 and 1.
The closer to 0 means to forget, and the closer to 1 means to keep.
Then we take the output from the input gate and do a pointwise addition which
updates the cell state to new values that the neural network finds relevant.
The “input gate” along with a tanh and a sigmoid function decides what new inputs are
added to the network
The “output gate” updates the cell state, using the outputs from the previous two gates.
The tanh and sigmoid layers decide which part of the cell state are to be output to the
hidden state.
🔹 Summary of Values at tt
The GRU is the newer generation of Recurrent Neural networks and is pretty similar
to an LSTM.
GRU’s got rid of the cell state and used the hidden state to transfer information.
It also only has two gates, a reset gate and update gate.
When x_t is plugged into the network unit, it is multiplied by its own weight W(z).
The same goes for h_(t-1) which holds the information for the previous t-1 units and
is multiplied by its own weight U(z). Both results are added together and a sigmoid
activation function is applied to squash the result between 0 and 1. Following the
above schema, we have:
Essentially, this gate is used from the model to decide how much of the past
information to forget. To calculate it, we use:
This formula is the same as the one for the update gate. The difference comes in
the weights and the gate’s usage, which will see in a bit. The schema below
shows where the reset gate is:
• Multiply the input x_t with a weight W and h_(t-1) with a weight U.
• Calculate the Hadamard (element-wise) product between the reset
gate r_t and Uh_(t-1). That will determine what to remove from the previous time
steps. Let’s say we have a sentiment analysis problem for determining one’s opinion
about a book from a review he wrote.
To determine the overall level of satisfaction from the book we only need the last part
of the review. In that case as the neural network approaches to the end of the text it
will learn to assign r_t vector close to 0, washing out the past and focusing only on
the last sentences.
We do an element-wise
multiplication of h_(t-1) —
blue line and r_t — orange
line and then sum the result —
pink line with the input x_t —
purple line. Finally, tanh is
used to produce h’_t — bright
green line.
As the last step, the network needs to calculate h_t — vector which holds information
for the current unit and passes it down to the network. In order to do that the update
gate is needed. It determines what to collect from the current memory content
— h’_t and what from the previous steps — h_(t-1). That is done as follows:
Let’s bring up the example about the book review. This time, the most relevant
information is positioned in the beginning of the text. The model can learn to set
the vector z_t close to 1 and keep a majority of the previous information.
Since z_t will be close to 1 at this time step, 1-z_t will be close to 0 which will ignore
big portion of the current content (in this case the last part of the review which
explains the book plot) which is irrelevant for our prediction.
Thank You