Computer Vision

Computer Vision enables machines to interpret and understand visual information from the world. At its core is the Convolutional Neural Network (CNN) β€” an architecture specifically designed to process grid-like data such as images.

Detect

Find objects and their locations in an image

Classify

Assign a category label to an entire image

Segment

Label every pixel with a class (semantic segmentation)

The Convolution Operation

A convolution slides a small filter (kernel) across an image, computing a dot product at each position. This produces a feature map β€” a compact representation that highlights edges, textures, or patterns the filter was designed to detect.

Input (5Γ—5)

1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
0 0 1 1 0
0 1 1 0 0

Filter (3Γ—3)

1 0 1
0 1 0
1 0 1

Feature Map (3Γ—3)

4 3 4
2 4 3
2 3 4

import numpy as np
from scipy.signal import convolve2d

image = np.array([
    [1,1,1,0,0],
    [0,1,1,1,0],
    [0,0,1,1,1],
    [0,0,1,1,0],
    [0,1,1,0,0]
])

# Edge-detection filter (Sobel-like)
kernel = np.array([
    [1, 0, 1],
    [0, 1, 0],
    [1, 0, 1]
])

feature_map = convolve2d(image, kernel, mode='valid')
print(feature_map)

Convolution Step-by-Step

7Γ—7 Input
3Γ—3 Kernel
5Γ—5 Output
Press Play or step through to see the dot-product computation at each position.
Step 0 / 25

Click cells to edit Β· Choose a kernel Β· Step through to see the math

CNN Architecture

A CNN stacks several types of layers. Each layer learns progressively more abstract features β€” from edges in early layers to shapes and objects in later layers.

C

Conv2D

Applies learnable filters to extract local features. Defined by filter size (e.g. 3Γ—3) and number of filters.

P

MaxPooling2D

Downsamples the feature map by keeping the maximum value in each window β€” reduces computation and adds translation invariance.

D

Dense (Fully Connected)

After flattening, dense layers combine all features for final classification.

Dr

Dropout

Randomly zeros out a fraction of neurons during training to prevent overfitting.

Image Classification β€” Cats vs Dogs

A binary CNN classifier trained on 64Γ—64 RGB images. Achieves strong accuracy on the Cats vs. Dogs dataset using data augmentation and early stopping.

Build the Model

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv2D, MaxPool2D, BatchNormalization,
                                     Flatten, Dense, Dropout)

def build_cnn(input_shape=(64, 64, 3)):
    model = Sequential([
        Conv2D(32, (3,3), activation='relu', input_shape=input_shape),
        BatchNormalization(),
        MaxPool2D(2, 2),

        Conv2D(64, (3,3), activation='relu'),
        BatchNormalization(),
        MaxPool2D(2, 2),

        Conv2D(128, (3,3), activation='relu'),
        MaxPool2D(2, 2),

        Flatten(),
        Dense(256, activation='relu'),
        Dropout(0.5),
        Dense(1, activation='sigmoid')   # binary output
    ])
    model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model

model = build_cnn()
model.summary()

Train with Data Augmentation

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping

datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    horizontal_flip=True,
    zoom_range=0.2,
    validation_split=0.2
)

train_gen = datagen.flow_from_directory(
    'data/', target_size=(64,64), batch_size=32,
    class_mode='binary', subset='training'
)
val_gen = datagen.flow_from_directory(
    'data/', target_size=(64,64), batch_size=32,
    class_mode='binary', subset='validation'
)

early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

history = model.fit(train_gen, validation_data=val_gen,
                    epochs=30, callbacks=[early_stop])

94% Accuracy

A deeper version of this CNN achieves 94% validation accuracy on an image classification task β€” see the full notebook in the 5 Computer Vision/ folder.

Live Demo β€” Colour Classifier

A CNN trained on 64Γ—64 colour swatches classifies any RGB colour into one of 10 categories. Pick a colour below β€” the model fills a virtual 64Γ—64 image with that colour, runs it through the CNN entirely in your browser, and returns the predicted class.

Model trained with python scripts/export_colour_cnn.py β€” see 5 Computer Vision/Colour_CNN.ipynb for the full training walkthrough.

What colour is this?

ONNX
Click to change
β€”

Knowledge Check

Test your understanding β€” pick the best answer, then click Check.

1. What does a MaxPool layer do?

2. Why do CNNs outperform fully-connected networks on images?

3. What is the purpose of padding in a convolution?