Sitemap
Muhammadsufian

Muhammadsufian

Understanding Convolutional Neural Networks (CNN): A Complete Beginner’s Guide to Image Classification and Deep Learning

14 min readJul 19, 2026

--

Learn the complete CNN workflow from input images to backpropagation in the simplest way possible. In this guide, we’ll explore the complete CNN workflow from image preprocessing and feature extraction to backpropagation, prediction, transfer learning, and beyond in a simple, beginner-friendly way.

Press enter or click to view image in full size

What is a Convolutional Neural Network (CNN)?

A Convolutional Neural Network (CNN) is a deep learning model specifically designed to process images.

Unlike traditional machine learning algorithms that rely on manually created features, a CNN automatically learns the important features directly from images.

For example, if you’re building a model to recognize cats and dogs, you don’t have to tell the CNN what ears, eyes, tails, or whiskers look like.

Instead, the CNN learns these patterns by itself during training. This ability to automatically learn visual features is what makes CNNs so powerful.

The Complete CNN Workflow

Press enter or click to view image in full size
Press enter or click to view image in full size

This entire process repeats many times during training until the CNN learns to make accurate predictions.

Now let’s understand each stage.

Step 1: Dataset

Every CNN begins with a dataset.

A dataset is simply a collection of labeled images that the CNN will learn from.

For example:

Image             Label
🐱 Cat
🐶 Dog
🐴 Horse

The labels tell the CNN the correct answer so it can compare its predictions and gradually improve. The more diverse and high-quality your dataset is, the better your CNN can learn.

Step 2: Image Preprocessing

Raw images cannot usually be fed directly into a CNN.

They first need to be prepared so that every image has the same format.

Common preprocessing steps include:

  • Resize every image to the same dimensions (for example, 128 × 128)
  • Convert the image into pixel values
  • Normalize pixel values from 0–255 to 0–1

The workflow becomes:

Press enter or click to view image in full size

Step 3: Input Layer

The Input Layer is the entry point of the CNN.

It doesn’t perform any calculations. Its only job is to receive the preprocessed image and pass it to the first convolution layer.

You can think of it as the front door of the CNN.

Step 4: Convolution Layer

The Convolution Layer is the heart of every CNN.

This is where feature extraction begins.

Instead of looking at the entire image at once, the CNN uses small filters (also called kernels) that slide across the image.

As each filter moves, it searches for specific visual patterns such as:

  • Edges
  • Lines
  • Corners
  • Curves
  • Simple textures

In deeper layers, these simple features combine into more meaningful features like:

  • Eyes
  • Nose
  • Ears
  • Wheels
  • Faces

Eventually, the CNN learns enough features to recognize complete objects.

Initially, these filters are random. During training, the CNN gradually learns the best filter values for detecting useful patterns.

This is one of the most important things to remember:

Filters are not manually designed. They are learned automatically during training.

Step 5: ReLU Activation Function

After convolution, the feature map passes through an activation function called ReLU (Rectified Linear Unit).

Its job is simple.

  • Positive values stay the same.
  • Negative values become zero.
Press enter or click to view image in full size

Mathematically:

ReLU(x)

If x > 0 → x

If x ≤ 0 → 0

ReLU helps the CNN focus on meaningful information while removing unnecessary negative values.

Without an activation function, CNNs would struggle to learn complex patterns.

Step 6: Pooling Layer

After important features are detected, the Pooling Layer reduces the size of the feature map.

This makes the CNN:

  • Faster
  • More memory efficient
  • Less likely to overfit

The most common pooling method is Max Pooling.

Instead of keeping every value, Max Pooling keeps only the largest value from a small region.

Press enter or click to view image in full size

For example:

2 5
1 4

After Max Pooling:

5

The important information remains while unnecessary details are removed.

Step 8: Flatten Layer

After multiple rounds of feature extraction, the CNN has produced many feature maps.

However, the Dense Layer cannot work directly with these 2D feature maps. The Flatten Layer converts them into a single one-dimensional vector.

For example:

Press enter or click to view image in full size
Feature Maps

↓

Flatten

↓

1D Feature Vector

No learning happens here.

Flatten simply changes the data format so the next layer can use it.

Step 9: Dense (Fully Connected) Layer

The Dense Layer acts like the decision-making part of the CNN.

It receives all the extracted features and determines which class the image most likely belongs to.

For example, if the extracted features include:

  • Pointed ears
  • Whiskers
  • Cat-shaped face
Press enter or click to view image in full size

The Dense Layer starts assigning higher confidence to the “Cat” class.

This layer uses weights and biases, which are also learned during training.

Step 10: Output Layer

The Output Layer produces the final prediction.

Suppose your model recognizes three animals.

The output might look like this:

Cat     = 0.82
Dog = 0.12
Horse = 0.06

For multi-class classification, CNNs commonly use the Softmax activation function.

Softmax converts the output scores into probabilities that sum to 100%.

The class with the highest probability becomes the final prediction.

For example:

Cat = 82%

Final prediction:

This image is a Cat.

What Happens After Prediction?

Making a prediction is not the end of training.

The CNN now needs to check whether its prediction was correct.

This is done using a Loss Function.

Understanding the Loss Function

The loss function measures how wrong the CNN’s prediction is.

Think of it as the CNN’s report card.

If the prediction is close to the correct answer:

  • Loss is low

If the prediction is far from the correct answer:

  • Loss is high

During training, the goal is simple:

Reduce the loss as much as possible

Press enter or click to view image in full size

In TensorFlow/Keras, the loss function is usually defined when compiling the model:

model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"]
)

Here:

  • loss="categorical_crossentropy" measures how incorrect the model's prediction is.
  • optimizer="adam" will later use this loss to improve the model.

Backpropagation: How the CNN Learns from Its Mistakes

Now comes the learning phase.

After calculating the loss, the CNN asks:

“Which filters, weights, and biases caused this error?”

Backpropagation works backward through the network and calculates how much each learnable parameter contributed to the mistake.

Instead of guessing, it mathematically computes the necessary corrections.

Press enter or click to view image in full size

In TensorFlow/Keras, this process happens automatically during training you don’t manually write the backpropagation equations. The framework calculates the gradients and updates the model internally.

What Does the Optimizer Do?

Backpropagation finds what needs to change, but it doesn’t actually make the changes.

That’s the job of the optimizer.

An optimizer (such as Adam) uses the information from backpropagation to update the model’s learnable parameters.

It improves:

  • Convolution filters
  • Dense layer weights
  • Biases

These updates help the CNN make better predictions in the next training iteration.

The complete learning cycle becomes:

Press enter or click to view image in full size

Types of Optimizers

There are several optimization algorithms available in deep learning. Although they all have the same goal — reducing the loss by updating the model’s parameters — they use different strategies to decide how much and how quickly to update the weights.

Some of the most commonly used optimizers are:

1. Stochastic Gradient Descent (SGD)

SGD updates the model’s parameters using one training example (or a small batch) at a time.

It is simple, widely used, and gives you more control over the learning process. However, it may require careful tuning of the learning rate and often converges more slowly than newer optimizers.

Best for: Large-scale machine learning and when you want more control over training.

2. Adam (Adaptive Moment Estimation)

Adam is one of the most popular optimizers in deep learning because it combines the advantages of Momentum and RMSProp.

It automatically adjusts the learning rate for each parameter, allowing the model to converge faster and more efficiently.

Because of its speed and strong performance, Adam is the default optimizer in many CNN projects.

Best for: Most deep learning applications, especially CNNs.

3. RMSProp (Root Mean Square Propagation)

RMSProp adapts the learning rate for each parameter based on recent gradients.

This helps the optimizer perform well on problems where gradients change frequently.

Best for: Recurrent Neural Networks (RNNs) and deep learning tasks with varying gradients.

4. Adagrad (Adaptive Gradient Algorithm)

Adagrad adjusts the learning rate individually for every parameter.

Parameters that receive frequent updates get smaller learning rates, while infrequent parameters receive larger updates.

Although effective for sparse data, its learning rate can become very small over time, causing training to slow down.

Best for: Sparse datasets, such as text and recommendation systems.

5. Adadelta

Adadelta was introduced to overcome Adagrad’s problem of continuously decreasing learning rates.

Instead of accumulating all previous gradients, it uses a moving window of recent gradients, allowing learning to continue effectively throughout training.

What Happens After Training? Understanding CNN Prediction on New Unseen Images

In the previous section, we learned how a CNN is trained using forward propagation, loss calculation, backpropagation, and an optimizer.

But what happens after training is complete?

When a new unseen image is given to the CNN, does it learn again or simply make a prediction?

Let’s find out.

Training vs Prediction

A CNN works in two phases:

  • Training Phase — The model learns from labeled images.
  • Prediction (Inference) Phase — The model uses its learned knowledge to classify new images.

The key difference is:

  • Training = Learning
  • Prediction = Using what was learned

Unlike training, prediction does not involve learning or updating the model.

What is a Trained CNN?

A common misconception is that a trained CNN memorizes all the training images.

That’s not true.

A trained CNN simply means the model has learned the best values for its filters, weights, and biases, and these learned parameters are stored inside the model.

These learned parameters include:

  • Convolution Filters (Kernels):Detect features like edges, textures, eyes, and ears.
  • Weights :Determine how important each feature is for making predictions.
  • Biases :Help neurons make better decisions.

Instead of memorizing images, the CNN remembers the patterns it learned from them.

Step 1: Preprocess the New Image

Before prediction, the new image must be prepared exactly like the training images.

Original Image
↓
Resize
↓
Convert to Pixel Matrix
↓
Normalize
↓
Ready for CNN

Step 2: The Image Passes Through the CNN

Even after training, the new image still passes through the same CNN architecture.

Press enter or click to view image in full size

Why? Because the CNN must extract features from the new image and compare them with the patterns it learned during training.

The layers don’t learn anymore they simply apply their learned filters, weights, and biases to make a prediction

What Happens During Prediction?

During prediction, the CNN performs only a forward pass.

Get Muhammadsufian’s stories in your inbox

Join Medium for free to get updates from this writer.

Unlike training:

  • ❌ No loss calculation
  • ❌ No backpropagation
  • ❌ No optimizer
  • ❌ No weight updates

The model simply uses its learned knowledge to classify the new image.

inal Prediction

After the image passes through all the layers, the Output Layer generates a score for each class.

For example:

Cat    = 0.95
Dog = 0.03
Horse = 0.02

The Softmax activation function converts these scores into probabilities.

The class with the highest probability becomes the final prediction.

Cat = 95%

Final Prediction: This image is a Cat.

Training vs Prediction

Press enter or click to view image in full size

Understanding Learning Rate in CNN: Why It Matters During Training

By now, we have learned the complete CNN training process.

One important question still remains:

When the optimizer updates the model’s parameters, how much should they change?

The answer is determined by the Learning Rate.

What is Learning Rate?

The Learning Rate is a hyperparameter that controls how much the optimizer updates the model’s filters, weights, and biases during each training step.

Think of it as the step size the optimizer takes while trying to reduce the loss.

Instead of changing the parameters randomly, the optimizer adjusts them by an amount determined by the learning rate.

In simple words:

Learning Rate decides how big or small each parameter update should be during training.

Press enter or click to view image in full size

Why Do We Need a Learning Rate?

After backpropagation calculates the gradients, the optimizer knows which direction to move to reduce the loss.

However, it still needs to know:

“How far should I move?”

That’s exactly what the learning rate decides.

Without a learning rate, the optimizer wouldn’t know whether to make a tiny adjustment or a large one.

Learning Rate as a Step Size

Imagine you’re standing on a mountain and trying to reach the lowest point in the valley.

Each step you take represents a parameter update.

The learning rate determines the size of each step.

  • Small learning rate → Small steps
  • Large learning rate → Large steps

The goal is to reach the lowest point, where the model has the minimum possible loss

What Happens If the Learning Rate Is Too High?

If the learning rate is very high, the optimizer takes huge steps.

Instead of moving smoothly toward the minimum loss, it may jump over the best solution again and again.

As a result:

  • The model may fail to converge.
  • The loss may keep increasing or fluctuate.
  • Training becomes unstable.

Think of it like trying to stop at a destination while driving too fast — you keep overshooting it.

What Happens If the Learning Rate Is Too Low?

If the learning rate is too small, the optimizer makes only tiny adjustments.

The model still learns, but the learning process becomes very slow.

This leads to:

  • Slow convergence
  • Longer training time
  • More epochs needed to achieve good performance

It’s like walking toward your destination using very small steps.

Eventually, you’ll get there — but it takes much longer.

Finding the Right Learning Rate

An ideal learning rate should strike a balance.

It should be:

  • Large enough to help the model learn efficiently.
  • Small enough to avoid overshooting the optimal solution.

Choosing a suitable learning rate helps the CNN train faster while maintaining stable and accurate learning.

Learning Rate in TensorFlow/Keras

When using TensorFlow or Keras, the learning rate is usually specified while creating the optimizer.

For example:

from tensorflow.keras.optimizers import Adam

optimizer = Adam(learning_rate=0.001)

Or directly while compiling the model:

model.compile(
optimizer=Adam(learning_rate=0.001),
loss="categorical_crossentropy",
metrics=["accuracy"]
)

Here:

  • Adam is the optimizer.
  • 0.001 is the learning rate.

This means Adam will update the model’s filters, weights, and biases using a step size of 0.001 during every training iteration.

 Optimizer Typical Learning Rate
--------- --------------------
Adam 0.001
SGD 0.01
RMSProp 0.001

Understanding Gradient Descent: How CNN Finds the Best Solution

What is Gradient Descent?

Explain:

  • Definition of Gradient Descent
  • Why it is needed after backpropagation
  • The goal of minimizing the loss function
  • Simple intuition (finding the lowest point in a valley)

Example flow:

Press enter or click to view image in full size

Gradient Descent is the mathematical technique that helps the optimizer move toward lower loss by following the gradients calculated during backpropagation.

How Does Gradient Descent Work?

Explain step by step:

  1. CNN makes a prediction.
  2. Loss is calculated.
  3. Backpropagation computes the gradients.
  4. Gradient Descent determines the direction that reduces the loss.
  5. The optimizer updates the filters, weights, and biases.
  6. The process repeats until the loss becomes as small as possible.

Types of Gradient Descent

Briefly explain the three main types.

Batch Gradient Descent

  • Uses the entire training dataset before updating the parameters.
  • Stable updates but slower on large datasets.

Stochastic Gradient Descent (SGD)

  • Updates parameters after every single training example.
  • Faster but updates are noisier.

Mini-Batch Gradient Descent

  • Uses a small batch of images (e.g., 32 or 64) before updating the parameters.
  • Combines the advantages of Batch and SGD.
  • Most commonly used in modern deep learning and CNN training.
Press enter or click to view image in full size

Understanding Overfitting and Underfitting in CNN

Training a CNN isn’t just about achieving high accuracy on the training dataset. The real goal is to make accurate predictions on new unseen images.

However, this doesn’t always happen. Sometimes the model learns too much, while other times it doesn’t learn enough. These situations are called Overfitting and Underfitting.

Underfitting

Underfitting occurs when the CNN fails to learn the important patterns in the training data.

As a result, the model performs poorly on both the training data and new unseen data.

Causes:

  • Model is too simple.
  • Not enough training.
  • Learning rate is too high.
  • Insufficient features.

Overfitting

Overfitting happens when the CNN memorizes the training data instead of learning general patterns.

It performs very well on the training dataset but poorly on new unseen images.

Causes:

  • Too many training epochs.
  • Small training dataset.
  • Complex model.
  • No regularization.

How to Reduce Overfitting

Some common techniques include:

  • Dropout
  • Data Augmentation
  • Regularization
  • Early Stopping
  • Using more training data

Understanding Padding

During convolution, filters reduce the size of the feature map.

Padding solves this problem by adding extra pixels (usually zeros) around the image border before convolution.

Why is Padding Needed?

  • Preserves image information at the edges.
  • Prevents the feature map from shrinking too quickly.
  • Allows deeper CNNs to retain more useful features.

Understanding Stride

Stride determines how many pixels the filter moves each time while sliding over the image.

For example:

  • Stride = 1 → Filter moves one pixel at a time, capturing more details and producing a larger feature map.
  • Stride = 2 → Filter moves two pixels at a time, producing a smaller feature map with less computation.

In short:

  • Small stride → More detail, larger output.
  • Large stride → Less detail, smaller output

Understanding Batch Normalization

As a CNN becomes deeper, training can become slower and unstable.

Batch Normalization helps by normalizing the outputs of a layer before passing them to the next layer.

Benefits

  • Faster training
  • More stable learning
  • Allows higher learning rates
  • Often improves model accuracy

It is commonly placed after the Convolution layer and before the activation function.

Understanding Dropout

Dropout is a regularization technique used to reduce overfitting.

During training, it randomly turns off some neurons, forcing the remaining neurons to learn independently instead of relying on each other.

Benefits

  • Reduces overfitting
  • Improves generalization
  • Creates a more robust model

Note: Dropout is used only during training, not during prediction.

Understanding Transfer Learning

Training a CNN from scratch requires a large dataset and significant computational power.

Transfer Learning solves this problem by using a pre-trained CNN that has already learned useful image features from millions of images.

Instead of training an entirely new model, we reuse the learned knowledge and fine-tune it for our own dataset.

Popular pre-trained CNN models include:

  • ResNet
  • VGGNet
  • MobileNet
  • EfficientNet
  • Inception

Transfer Learning is one of the most widely used techniques in real-world deep learning because it saves time, requires less data, and often achieves excellent performance.

Image Segmentation

Image Classification identifies what the object is.

Object Detection identifies what and where the object is.

Image Segmentation goes even further by classifying every pixel in the image.

Instead of drawing a box around a cat, segmentation identifies the exact pixels that belong to the cat.

Popular segmentation models include:

  • U-Net
  • Mask R-CNN

Segmentation is commonly used in medical imaging, satellite image analysis, autonomous driving, and image editing.

Conclusion

Convolutional Neural Networks have revolutionized computer vision by enabling machines to automatically learn features from images without manual feature engineering. Throughout this guide, we’ve covered the complete CNN pipeline, including data preprocessing, convolution, activation functions, pooling, dense layers, forward propagation, loss calculation, backpropagation, optimizers, learning rate, gradient descent, and prediction on unseen images.

We also explored important concepts like overfitting, padding, stride, batch normalization, dropout, transfer learning, object detection, and image segmentation. Together, these concepts form the foundation of modern deep learning for computer vision.

With this knowledge, you now have a solid understanding of how CNNs learn, improve, and make predictions. Whether you’re preparing for interviews, building your first image classification model, or continuing your deep learning journey, mastering these fundamentals will make learning advanced CNN architectures like LeNet, AlexNet, VGGNet, ResNet, MobileNet, and EfficientNet much easier. Keep practicing, experiment with real datasets, and you’ll be well on your way to becoming proficient in deep learning and computer vision.

--

--