Convolutional Neural Networks (CNNs)
Convolutional Neural Networks (CNNs)
Convolutional neural networks are a specialized type of neural network built specifically to work with image data, though they have since found use in other areas like audio and video processing too. Unlike a basic feedforward network, which treats every input pixel as an independent number with no relationship to its neighbors, a CNN is designed to recognize that pixels close to each other often form meaningful patterns, like edges, textures, and shapes.
This makes CNNs dramatically more efficient and accurate for visual tasks. Instead of learning a separate weight for every single pixel in an image, which would be computationally enormous and prone to overfitting, a CNN uses small, reusable filters that scan across the image looking for specific patterns.
How Machines See Images
To a computer, an image is not a picture at all. It is a grid of numbers, where each number represents the brightness or color intensity of a single pixel. A grayscale image is represented as a single grid of values, while a color image typically has three separate grids, one each for red, green, and blue, stacked on top of each other.
This numerical representation is exactly what a CNN works with. There is no inherent understanding of what an eye, a wheel, or a leaf looks like until the network has been trained to recognize the pixel patterns associated with those objects. Early layers in a CNN tend to pick up very basic visual elements like edges and color transitions, while deeper layers combine those basic elements into more recognizable shapes and eventually full objects.
Filters, Pooling, and Feature Maps
1. Filters (Kernel)
A filter, also called a kernel, is a small grid of numbers that slides across an image, performing a mathematical operation called convolution at each position.
A CNN typically uses multiple filters in each layer, with each filter trained to detect a different pattern.
2. Feature Maps
As a filter moves across the image, it produces a feature map, which is a new grid of values.
The feature map highlights where a particular pattern, such as a vertical edge or a curve, appears most strongly in the original image.
3. Pooling
Pooling is a separate operation that comes after convolution, and its job is to shrink the size of the feature map while keeping the most important information.
The most common type, max pooling, looks at small regions of the feature map and keeps only the highest value in each region, discarding the rest.
This reduces the amount of computation needed in later layers and also makes the network more tolerant of small shifts or distortions in the image, since the exact position of a feature matters less once pooling has been applied.
Building a CNN from Scratch
Step 1: Add Convolutional and Pooling Layers
The CNN begins by stacking convolutional layers followed by pooling layers to start processing the input image.
Step 2: Extract Features
As the network becomes deeper, it gradually learns more meaningful features from the image:
- The image size becomes smaller after each pooling operation.
- The number of detected features increases.
- Early layers identify simple patterns such as edges and textures, while deeper layers recognize more complex shapes and objects.
Step 3: Flatten the Feature Maps
After multiple rounds of convolution and pooling, the resulting feature maps are flattened into a one-dimensional vector so they can be processed by dense layers.
Step 4: Add Fully Connected Layers
The flattened feature vector is then passed through one or more fully connected (dense) layers, where the extracted features are combined and analyzed to prepare for the final prediction.
Step 5: Make the Final Prediction
The output layer uses the processed features to make the final prediction, such as classifying the image into its correct category.
Step 6: Build the CNN in Keras
Frameworks like Keras provide ready-made convolutional and pooling layers, allowing an entire CNN architecture to be built with just a few lines of code.










