top of page
Gradient With Circle
Image by Nick Morrison

Insights Across Technology, Software, and AI

Discover articles across technology, software, and AI. From core concepts to modern tech and practical implementations.

CNNs in Deep Learning: From Convolution Operations to Image Processing

  • 21 minutes ago
  • 8 min read

Convolutional Neural Networks (CNNs) are one of the most important neural network architectures in deep learning, particularly for tasks involving images and other grid-like data. Unlike traditional fully connected neural networks, CNNs are designed to preserve the spatial structure of their inputs. This allows them to learn patterns such as edges, textures, shapes, and increasingly complex visual features directly from data.


CNNs have become a fundamental technology behind image classification, object detection, facial recognition, medical image analysis, autonomous systems, and many other computer vision applications. Their effectiveness comes largely from the convolution operation, which allows a network to extract meaningful features while using significantly fewer parameters than a fully connected architecture.


Understanding CNNs therefore requires more than knowing their layer structure. It involves looking at how visual information is represented, transformed, and progressively refined as it moves through a network. In this blog, we will examine the fundamental operations and architectural principles that enable CNNs to learn from images, how these processes contribute to hierarchical feature representation, and how the resulting models can be applied to practical image-processing and computer vision tasks.


end-to-end Convolutional Neural Network (CNN) pipeline, from raw pixel input through feature extraction to final image classification.

What is Convolution in CNNs in Deep Learning?

The convolution operation is the central mathematical operation behind a Convolutional Neural Network. Its purpose is to extract local patterns from an input by applying a small matrix called a kernel or filter across different regions of the input.

Consider an input image represented as a matrix:

Input =
[ 1  2  3
  4  5  6
  7  8  9 ]

A convolution kernel is typically much smaller than the input. For example:

Kernel =
[ 1  0
  0 -1 ]

The kernel moves across the input, and at each position, corresponding values are multiplied and summed. The resulting values form a new matrix called a feature map.

Mathematically, a simplified 2D convolution can be expressed as:


2D Spatial Convolution operation

where X represents the input, K represents the kernel, and Y represents the resulting feature map. In practice, deep learning frameworks generally implement cross-correlation rather than the mathematically flipped version of convolution. However, the operation is conventionally still called convolution in the context of CNNs.


The important idea is that the kernel acts as a pattern detector. Different kernels can respond strongly to different visual structures, such as horizontal edges, vertical edges, corners, or textures. During training, CNNs learn the values of these filters automatically. Instead of manually designing image-processing filters, the network discovers useful feature detectors by minimizing its training loss.


We can translate the 2D cross-correlation equation into Python using NumPy. Building this operation from scratch clarifies how convolutional layers extract spatial features from raw image tensors without relying on black-box framework abstractions.

def cross_correlation_2d(X, K):
    
    H_x, W_x = X.shape
    H_k, W_k = K.shape

    # Output dimensions for 'valid' correlation (no padding)
    H_y = H_x - H_k + 1
    W_y = W_x - W_k + 1

    Y = np.zeros((H_y, W_y))

    # Calculate Y(i, j)
    for i in range(H_y):
        for j in range(W_y):

            # Extract patch from X matching kernel size
            patch = X[i : i + H_k, j : j + W_k]

            # Element-wise multiplication summation
            Y[i, j] = np.sum(patch * K)

    return Y

While writing explicit Python loops is a great way to build intuition, it isn't how deep learning frameworks operate in production. Heavyweight libraries like PyTorch and TensorFlow bypass slow loops entirely, using parallel GPU execution (CUDA) or memory tricks like im2col to run these calculations in milliseconds.


How CNN Filters Extract Features

The power of CNNs comes from hierarchical feature extraction.

The first convolutional layers generally learn relatively simple patterns. These can include edges, gradients, corners, and basic textures. Deeper layers combine these simpler patterns into increasingly complex representations. For example, an image-processing CNN might develop a hierarchy similar to:


CNN - Feature Extraction Hierarchy

This hierarchy is one of the major differences between CNNs and traditional image-processing techniques. A conventional image-processing pipeline might require manually designed operations to detect edges, contours, or textures. A CNN instead learns the feature extraction process from training data.


The receptive field also becomes important as the network gets deeper. A neuron in an early convolutional layer observes a small region of the input image. As more convolutional layers are stacked, deeper neurons indirectly incorporate information from larger portions of the original image.


This allows the network to move from local information toward broader semantic representations.


CNN Architecture and Its Core Layers

A typical CNN architecture contains several types of layers that work together to transform raw image data into useful representations. A simplified CNN can be represented as:


Architecture CNN

1. Convolutional Layer

The convolutional layer performs feature extraction. It applies multiple filters to the input and produces corresponding feature maps.

If a convolutional layer contains 32 filters, it can learn 32 different patterns from its input. The output therefore contains multiple feature maps representing different learned features.

The basic convolution operation can be written as:


Z = X * K + b


where X is the input, K is the learned kernel, b is the bias, and Z is the resulting feature map before activation. The number of filters, kernel size, stride, and padding are important architectural parameters.


2. Activation Function

After convolution, an activation function introduces non-linearity into the network. One of the most commonly used activation functions in CNNs is ReLU:


ReLU ( x ) = max ( 0 , x )


ReLU replaces negative values with zero while preserving positive values.

Without nonlinear activation functions, stacking multiple convolutional layers would not provide the same expressive power because the overall transformation would remain essentially linear.


3. Pooling Layer

Pooling reduces the spatial dimensions of feature maps. This can lower computational requirements while helping the network focus on important features.

Max pooling is a common approach. For a pooling region, the largest value is selected.

Average pooling instead calculates the average value within the selected region.

Pooling can also provide a degree of spatial robustness. Small changes in the exact position of a feature may have less influence on the resulting representation.

Modern CNN architectures do not always rely heavily on traditional pooling layers. Some architectures use strided convolutions or other downsampling mechanisms instead.


4. Flattening and Fully Connected Layers

After several convolution and downsampling operations, the resulting feature maps are transformed into a one-dimensional representation. This operation is known as flattening.

For classification tasks, the flattened representation can then be passed to fully connected layers. These layers combine the extracted features and produce the final prediction.

For a classification problem with C classes, the final layer commonly produces C output values. A softmax function can then convert these values into class probabilities:


softmax function

The predicted class is generally the one associated with the highest probability.


How CNNs Process Images

The image-processing capability of CNNs comes from the repeated transformation of the input representation. Suppose an RGB image has dimensions (224 × 224 × 3). The three channels correspond to red, green, and blue. A convolutional layer applies learned filters across these channels. The resulting feature maps contain information about patterns detected by the filters. As the image moves through deeper layers, its representation changes significantly. The early representation is closely related to pixel-level information. Deeper representations become increasingly abstract.


Feature Hierarchy in Convolutional Neural Networks (CNNs)

This process allows CNNs to identify meaningful structures without requiring every feature to be explicitly programmed. Another important property is local connectivity. A convolutional neuron does not initially connect to every pixel in the image. Instead, it examines a limited region determined by the kernel size. This significantly reduces the number of parameters compared with a fully connected network.


CNNs also use parameter sharing. The same filter is applied across different spatial locations of an image. Consequently, a feature detector learned in one part of an image can also detect the same feature elsewhere. These two properties, local connectivity and shared parameters—are fundamental to the efficiency of CNN architectures.


Stride and Padding in Convolution

Two important parameters control how convolution changes the spatial dimensions of an image: stride and padding. Stride determines how far the kernel moves during each operation. With a stride of 1, the filter moves one pixel at a time. With a stride of 2, it moves two pixels at a time, reducing the spatial dimensions of the output more aggressively.

Padding adds additional values, commonly zeros, around the boundary of the input.

Without padding, repeated convolution operations can cause feature maps to become progressively smaller.


For an input of size N × N, kernel size K × K, padding P, and stride S, the output spatial dimension can be calculated as:


Output Size = ((N + 2P - K) / S) + 1

This equation is useful when designing CNN architectures because it allows us to determine how the dimensions of feature maps change after convolution.


Training Convolution Neural Networks (CNN)

CNNs learn their filters through backpropagation and gradient-based optimization.

During training, an image is passed through the network to produce a prediction. The prediction is compared with the target using a loss function.

The network then calculates gradients of the loss with respect to its parameters. An optimization algorithm such as stochastic gradient descent or Adam updates the parameters to reduce the loss. Every training step follows a continuous cycle of prediction, evaluation, and adjustment:


  1. Forward Pass: An input image flows through the convolutional, activation, and pooling layers. The network applies its current filter values and outputs a prediction.

  2. Loss Calculation: A loss function compares this prediction to the actual target label, turning the network's mistake into a concrete numerical error score.

  3. Backpropagation: The network works backward from the output layer to trace how much each individual filter weight contributed to that error.

  4. Gradient Calculation: Calculus determines the exact direction and magnitude each weight needs to shift to lower the error.

  5. Parameter Update: An optimizer like Stochastic Gradient Descent (SGD) or Adam nudges the weights slightly in the right directionAfter many iterations, the convolutional filters gradually learn representations that are useful for the target task.


This is one reason CNNs generally require substantial training data and computational resources for complex computer vision problems.


Practical Python Implementation

CNNs can be implemented using deep learning frameworks such as TensorFlow and PyTorch. A simple CNN architecture in TensorFlow can be defined as follows:

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Flatten, Dense

model = Sequential([
    Conv2D(32, (3, 3), activation="relu",
           input_shape=(224, 224, 3)),
    MaxPooling2D((2, 2)),

    Conv2D(64, (3, 3), activation="relu"),
    MaxPooling2D((2, 2)),

    Flatten(),
    Dense(128, activation="relu"),
    Dense(10, activation="softmax")
])

model.summary()

For a hands-on implementation, our existing guides cover several important CNN architectures and image-classification workflows using Python.


1. Implementing VGG on CIFAR-10

Our guide on Implementing VGG on CIFAR-10 Dataset in Python walks through the implementation of the VGG architecture for image classification using the CIFAR-10 dataset. It demonstrates how VGG-style networks progressively extract hierarchical visual features and can be trained on a benchmark image dataset.


This is a useful next step after understanding the fundamentals of convolutional layers because VGG provides a clear example of how convolution and pooling layers can be stacked to build a deeper CNN architecture.


2. Implementing AlexNet with PyTorch

For another classic CNN architecture, Implementing AlexNet with PyTorch’s torchvision in Python using CIFAR-10 Dataset explores how AlexNet can be implemented using PyTorch and torchvision.


The guide covers loading a pre-trained AlexNet model, using it for feature extraction, fine-tuning the architecture for a specific task, and applying it to the CIFAR-10 dataset. It provides a practical perspective on transfer learning and shows how a pre-trained CNN can be adapted to a new image-classification problem.


3. Neural Networks for CIFAR-10 with TensorFlow

If you want to implement image classification using TensorFlow, Implementing Neural Networks for Image Classification on the CIFAR-10 Dataset Using TensorFlow in Python provides another practical reference.


The guide demonstrates how neural networks can be developed and trained for image classification using Python and TensorFlow, making it useful for connecting the theoretical concepts of CNNs with an actual deep learning workflow.


Together, these implementations provide practical examples of how CNN architectures can be used with Python frameworks such as TensorFlow and PyTorch. They also demonstrate an important progression in computer vision: understanding the underlying convolution operation first, then working with established architectures such as VGG and AlexNet, and finally applying these models to image-classification datasets.


Conclusion

CNNs in deep learning provide an efficient way to learn visual representations directly from image data. Their foundation lies in the convolution operation, where learnable filters scan across an input and extract local patterns.

By combining convolutional layers, nonlinear activation functions, pooling or other downsampling methods, and classification layers, CNNs transform raw pixels into increasingly meaningful representations. Early layers tend to capture low-level patterns, while deeper layers learn more complex structures.

The combination of local connectivity, parameter sharing, hierarchical feature extraction, and gradient-based learning makes CNNs particularly effective for image processing and computer vision.

Although newer architectures continue to expand the possibilities of visual learning, CNNs remain an essential concept for understanding deep learning-based image processing. Learning how convolution, filters, feature maps, stride, padding, pooling, and backpropagation work provides a strong foundation for understanding modern computer vision systems.

Get in touch for customized mentorship, research and freelance solutions tailored to your needs.

bottom of page