Introduction to Convolutional Neural Networks

Deep Learning with Keras and TensorFlow 11 min min read Updated: Nov 16, 2026 Intermediate
Introduction to Convolutional Neural Networks
Intermediate Topic 1 of 9

Why Not Just Use Dense Layers for Images?

A small 224 x 224 color photo has 150,528 numbers. A dense layer with 1000 neurons would need over 150 million weights. It would be slow, easy to overfit, and it would ignore the fact that nearby pixels belong together. CNNs fix this by looking at small patches and reusing the same weights everywhere.

Images are Tensors

Image typeShape (height, width, channels)
Grayscale 28 x 28(28, 28, 1)
Color 224 x 224(224, 224, 3) for red, green, blue

The Convolution Operation

A filter (or kernel) is a tiny grid of weights, for example 3 x 3. It slides across the image. At each position it multiplies the pixels under it by its weights and adds them up, producing one number. All those numbers form a feature map.

Simple example: Imagine a small magnifying glass that looks for one specific thing, such as a vertical edge. You slide it over the whole picture, and it lights up wherever it finds that edge. A CNN learns many such filters by itself.
python import numpy as np image = np.array([[1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0]]) vertical_edge = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]]) out = np.zeros((3, 3)) for i in range(3): for j in range(3): patch = image[i:i+3, j:j+3] out[i, j] = np.sum(patch * vertical_edge) print(out)
Result

The middle column has large values (3.0) because that is where the bright area meets the dark area: an edge.

Stride and Padding

TermMeaning
StrideHow many pixels the filter moves each step. Stride 2 makes the output smaller
Padding validNo padding, output shrinks
Padding sameAdd zeros around the image so output size stays the same

Pooling

Pooling shrinks the feature map. Max pooling keeps the largest value in each small window (usually 2 x 2). It makes the network faster and less sensitive to the exact position of things.

python import keras from keras import layers x = np.random.rand(1, 28, 28, 1).astype("float32") conv = layers.Conv2D(8, kernel_size=3, activation="relu") pool = layers.MaxPooling2D(pool_size=2) y = conv(x) print(y.shape) # (1, 26, 26, 8) print(pool(y).shape) # (1, 13, 13, 8)

The Typical CNN Recipe

  1. Conv2D + ReLU (find patterns)
  2. MaxPooling2D (shrink)
  3. Repeat the two steps with more filters
  4. Flatten or GlobalAveragePooling
  5. Dense layers for the final answer
  • Early layers see edges
  • Middle layers see textures and shapes
  • Deep layers see object parts

Conclusion

CNNs share small filters across the image, so they need far fewer weights than dense networks and understand local patterns. Next, we will build one.

Quick Summary: Conv2D finds patterns with sliding filters, pooling shrinks the maps, and dense layers decide the final class.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators