Convolutional Neural Networks (CNNs) form the foundation of modern computer vision. Unlike fully connected neural networks that evaluate flat 1D vectors, CNNs preserve spatial 2D relationships in images by sliding small weight matrices (kernels) across raw pixels to extract abstract visual features like edges, textures, and shapes.
1. Summary & Key Takeaways
- 2D Discrete Convolution: Computes the dot product between a kernel and localized pixel regions:
- Sobel Edge Detection Filters: Highlights high-frequency spatial intensity gradients corresponding to object borders.
- Feature Map Dimensions: For an input of size , kernel size , stride , and padding :
- Translation Invariance: Convolution filters recognize visual patterns regardless of where they appear in the frame.
2. Interactive 2D Convolution Playground
Use the interactive visualizer below to slide a Sobel Edge or Sharpen filter kernel across a pixel grid and build the resulting feature map!
Click Slide 3x3 Kernel 1 Step to watch elementwise multiplication sum values get written into the destination output feature map.
CNN 2D Convolution & Feature MapsComputer Vision Filter
Slide a 3x3 Filter Kernel across the input image pixel grid to build the output feature map.
# 2D Convolution in Python (PyTorch / NumPy)
import torch
import torch.nn as nn
# Define 2D Convolutional Layer
conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=0)
# Sobel Edge Filter Weights
sobel_kernel = torch.tensor([
[[-1., -2., -1.],
[ 0., 0., 0.],
[ 1., 2., 1.]]
]).unsqueeze(0)
conv.weight = nn.Parameter(sobel_kernel)
# Forward pass: Input Image -> Output Feature Map
# output = conv(input_tensor)3. Mathematical Foundations & Sobel Edge Kernels
1. Sobel Horizontal Edge Filter ()
Detects horizontal pixel intensity transitions:
2. Sobel Vertical Edge Filter ()
Detects vertical pixel intensity transitions:
Combined Edge Magnitude ()
4. CNN Architecture Layer Comparison
| Layer Type | Purpose | Primary Operation | Output Dimension Change |
|---|---|---|---|
| Convolutional Layer | Feature Extraction | Sliding Kernel Dot Product | Reduces by (if ) |
| Pooling Layer (MaxPool) | Spatial Downsampling | Max/Average Window Pooling | Cuts dimensions in half () |
| Fully Connected (Dense) | Final Classification | Matrix Multiplication + Softmax | Flattens into class logits |