Beginner Basics

A Guide to Convolutional Neural Networks: How AI Makes Sense of Images

CNNs process images with local filters, stride, padding, ReLU, and pooling, sharing parameters to learn spatial features and classify images.

A Guide to Convolutional Neural Networks: How AI Makes Sense of Images

Key Takeaways

  • CNNs process images as spatial grids instead of flattened vectors, preserving local relationships and avoiding the huge cost of dense networks on raw pixels.
  • Small learnable filters slide across images to detect edges, shapes, and higher-level visual patterns, while parameter sharing keeps the model compact.
  • Stride, padding, ReLU, and pooling control feature-map size, add nonlinearity, and reduce computation while preserving important visual signals.
  • CNNs are trained by backpropagation to turn learned features into class probabilities, as shown by a simple MNIST example with high accuracy and relatively few parameters.

Meta description: Learn how CNNs use filters, stride, padding, ReLU and pooling to turn pixel grids into image predictions like a cat, with MNIST code basics.

A Guide to Convolutional Neural Networks: How AI Makes Sense of Images

Tags: CNNs, computer vision, image classification, MNIST, deep learning basics

Featured image: Abstract friendly illustration of a small filter sliding across a pixel grid, revealing cat-like shapes; no real people, logos or text.

For readers learning computer vision, this explainer shows how Convolutional Neural Networks (CNNs) turn pixel grids into predictions such as "that's a cat". A single photo contains tens of thousands of numbers, and neighboring pixels are not independent: what matters is often where things are relative to each other, not raw values. CNNs respect image structure and support image classification, face recognition, object detection, medical image analysis and self-driving systems.

Why ordinary neural networks struggle with images

A computer does not see a cat. It sees numbers: a grid of pixels, each with an intensity. Grayscale has one number per pixel; color has one grid each for red, green and blue, stacked, so shape is height x width x 3. A 28x28 grayscale image uses 0 for black and 255 for white; a sample grid may show 0, 20, 50, then 80, 120, 200, then 255, 180, 90.

A normal neural network flattens this into one long list and loses neighborhood. For a 100x100 color image, that is 30,000 inputs. With 1,000 neurons in the first dense layer, it needs 30 million weights. The cost is huge, invites overfitting (Article #5), wastes computation, discards spatial relationships and scales badly: doubling image size multiplies cost again.

A Guide to Convolutional Neural Networks: How AI Makes Sense of Images

How 3x3 filters learn visual structure

A CNN is a neural network designed to automatically learn useful features from images. It replaces dense wiring with filters, also called kernels, that scan local patches. The terms filter and kernel are often used interchangeably. A filter is a small grid of learnable weights, like ordinary network weights. Common sizes are 3x3 and 5x5. A filter multiplies element-wise with a patch and sums the result into one number; sliding it creates a feature map. A 2x2 filter over a 3x3 image gives a 2x2 map; the first step is 1x1 + 2x0 + 4x0 + 5x1 = 6. Each number asks how strongly the patch matches the filter's pattern.

A vertical-edge filter uses 1, 0, -1 in each row: it responds where left is bright and right is dark, and about zero on flat patches. Other filters catch horizontal edges, corners or textures. Stacking layers builds complexity: Layer 1 edges, Layer 2 shapes, Layer 3 parts such as an eye or wheel, Layer 4 objects such as a face or car.

Each filter learns a feature. A 3x3 RGB filter has 3x3x3 = 27 weights plus a bias and is reused across the image, called parameter sharing. A 32-filter layer needs 896 parameters, not millions.

Stride, padding, ReLU and pooling

Stride sets movement: 1 shifts one pixel, 2 shifts two; larger stride shrinks output. Without padding, output size is (i - k) / s + 1, rounded down; for a 7-pixel-wide input and 3x3 kernel at stride 1, it gives 5. Padding adds zeros around the border: valid padding does not add border and lets output shrink; same padding keeps spatial dimensions. With padding p, size is (i - k + 2p) / s + 1. Padding also helps edge pixels, which otherwise get covered fewer times.

A Guide to Convolutional Neural Networks: How AI Makes Sense of Images

After convolution, ReLU applies max(0, x): -5 -> 0, 2 -> 2, 8 -> 8. It adds non-linearity, prevents stacked layers from collapsing into one linear operation, and is cheap; Leaky ReLU avoids dead neurons with a small negative slope. One filter gives one feature map; 32 filters give 32 maps stacked like channels.

Pooling shrinks maps: max pooling keeps the largest value in a block, so [1 3; 2 5] becomes 5; average pooling keeps the mean. Pooling reduces computation, keeps strong signals, tolerates small shifts and helps overfitting.

Training, MNIST example and what comes next

A typical CNN repeats convolution, ReLU and pooling, then flattens final feature maps into one vector for fully connected layers, which weigh the evidence, because those layers expect a one-dimensional list. Softmax turns scores into probabilities that add to 1; a model may be 92% sure it sees a cat. Filters start random and are trained by backpropagation and gradient descent; edge detectors emerge because they reduce loss, not because they were programmed.

A small Keras Sequential CNN for MNIST handwritten digits shows load, normalize, build, train, evaluate, predict. It starts with Input(shape=(28, 28, 1)), loads training data shaped (60000, 28, 28, 1), normalizes train and test by dividing by 255.0, then uses Conv2D(32, 3, activation="relu"), MaxPooling2D(2), Conv2D(64, 3, activation="relu"), MaxPooling2D(2), Flatten to 1600, Dense(64, activation="relu"), and Dense(10, activation="softmax") for 10 classes. Sizes go 28x28 to 26x26x32, 13x13x32, 11x11x64, 5x5x64. It compiles with optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], trains 5 epochs with validation_split=0.1, evaluates x_test and predicts the first digit using probs.argmax(). Each 3x3 convolution trims 2 pixels, each pooling halves size. The model has about 122,000 parameters, tiny compared with a dense network on raw pixels, and typically reaches roughly 98 to 99% accuracy on MNIST after a few epochs.

CNNs already support face recognition on phones, number plate recognition, X-ray and scan analysis, object detection in cameras and vehicles, image search and handwriting recognition. The explainer was originally published by Towards AI on Medium. For readers, the takeaway is that CNNs excel at spatial patterns. The next question is order: an image can be read in any order, but sentences, stock prices over time and event sequences cannot, so models that handle sequence become relevant.

Comments (0)

  1. No comments yet. Be the first to share what worked for you.

Leave a comment

Comments are reviewed before they appear. Your email address is not published.