AI Convolutional Neural Networks

Convolutional neural networks are built specifically for grid-like data such as images, scanning small filters across the input to detect local patterns like edges, textures, and shapes.

Why Plain Networks Struggle With Images

A fully-connected network treats every input as an independent number and connects it to every neuron in the next layer. Fed a raw image this way, even a small 100x100 pixel picture has 10,000 pixel values, each needing its own connection to every neuron in the next layer - the weight count explodes, and worse, the network has no built-in sense that pixels sitting next to each other are related. A convolutional neural network (CNN) fixes both problems by processing the image in small local patches instead of all at once.

The Convolution Operation

A convolutional layer slides a small grid of learnable weights, called a filter (or kernel), across the input image, a few pixels at a time. At each position it multiplies the filter's weights against the pixels underneath it and sums the result into a single number. Doing this across the whole image produces a feature map that lights up wherever the pattern the filter has learned to detect - an edge, a corner, a splash of color - appears in the image. Because the same small filter is reused at every position, the network needs far fewer weights than a fully-connected layer, and it can recognize a pattern no matter where it appears in the image.

Example

import numpy as np

# A tiny 5x5 "image" and a 3x3 filter that responds to vertical edges
image = np.array([
    [0, 0, 1, 1, 1],
    [0, 0, 1, 1, 1],
    [0, 0, 1, 1, 1],
    [0, 0, 1, 1, 1],
    [0, 0, 1, 1, 1],
])
vertical_edge_filter = np.array([
    [1, 0, -1],
    [1, 0, -1],
    [1, 0, -1],
])

# Slide the filter across every 3x3 patch and record the match strength
feature_map = np.zeros((3, 3))
for row in range(3):
    for col in range(3):
        patch = image[row:row+3, col:col+3]
        feature_map[row, col] = np.sum(patch * vertical_edge_filter)

print("Feature map (high values mark the vertical edge):")
print(feature_map)

Stacking Filters: From Edges to Objects

A single convolutional layer usually contains many different filters, each learning to detect a different simple pattern. Stacking several convolutional layers lets the network build the same kind of hierarchy deep learning relies on generally: early layers detect edges and colors, middle layers combine those into textures and simple shapes, and deeper layers combine shapes into recognizable parts and whole objects.

Pooling: Shrinking the Map Without Losing the Signal

Between convolutional layers, CNNs typically add a pooling layer, most commonly max pooling, which slides a small window over the feature map and keeps only the strongest value in each window. This shrinks the size of the data flowing through the network (reducing computation) and makes the detected features slightly more tolerant to the pattern shifting by a pixel or two.

  • Input image (a grid of pixel values)
  • Convolutional layer + activation function (detects local patterns)
  • Pooling layer (shrinks the feature map, keeps the strongest signals)
  • Repeat convolution + pooling several times to build up more abstract features
  • Flatten into a single list of numbers
  • One or more fully-connected layers to combine features into a final decision
  • Output layer (e.g. softmax over possible classes)
Use CaseWhy a CNN Fits
Photo classificationDetects visual patterns regardless of where they appear in the frame
Medical image analysisPicks up on local textures and shapes in scans, like tumors or fractures
Object detectionLocates and labels multiple objects within a single image
Handwriting recognitionRecognizes stroke patterns and shapes that form characters
Note: CNNs work well for any data laid out on a grid, not just photographs - spectrograms of audio, satellite data, and even some board-game states are commonly fed through convolutional layers too.