Master how CNNs detect edges and patterns. Priya at MedScan: edge detection kernels find boundaries in medical scans, pooling reduces size, feature maps reveal patterns.
🎨 Edge Detection🖼️ Feature Maps📦 Pooling🔧 Intermediate📖 ~25 min
Lesson 12 of Deep Learning Track · CIFAR-10 dataset
🏥
Real-world scenario
Priya at MedScan Diagnostics (Bangalore): detect tumor boundaries in CT scans using CNN filters
Priya is analyzing CT scans at MedScan Diagnostics in Bangalore. She wants to train a CNN to detect tumors by learning to recognize edges and patterns in images. The problem: how does a CNN actually "see"? Fully-connected networks treat images as flat vectors, losing 2D structure. Convolution solves this: small filters slide across the image, detecting edges (low-level), corners (mid-level), and tumor shapes (high-level). Pooling compresses feature maps, reducing computation while preserving important patterns. Understanding convolution is the foundation of computer vision—from medical imaging to autonomous driving.
32x32
CIFAR-10 Image Size
3
Color Channels (RGB)
10
Classes
What is convolution? A filter sliding across an image
Core idea: A convolution applies a small filter (kernel) to patches of an image. The filter slides left-to-right, top-to-bottom, computing the dot product between filter weights and image pixels at each position. This produces a feature map: a smaller 2D grid of activations.
Why convolution? It exploits locality: nearby pixels are more correlated than distant ones. A 3x3 filter focuses on local patches, learning edges, textures, and patterns. Stacking convolutions builds a hierarchy: layer 1 detects edges (simple), layer 2 detects corners (intermediate), layer 3 detects objects (high-level).
Biological intuition: The brain's visual cortex works similarly—neurons respond to local patterns (edges at specific angles, colors, orientations) before combining them into objects.
Convolution math: kernel dot product
A convolution operation: slide a 3x3 kernel over a 5x5 image, computing dot products. At each position, multiply kernel values by image pixels, sum them, and place the result in the output feature map.
convolution_manual.pyImplement convolution from scratch
Convolution operation: 3x3 kernel sliding over 5x5 image, computing dot products at each position.
Edge detection filters: Sobel, vertical and horizontal
Common edge detection kernels exploit patterns. Sobel filters detect edges in specific directions. A vertical edge detector activates on vertical boundaries (changes left-to-right). A horizontal detector activates on horizontal boundaries (changes top-to-bottom).
edge_detection.pyApply Sobel filters to detect edges in images
Sobel edge detection on CIFAR-10 image. Vertical and horizontal filters reveal boundaries and textures.
Feature maps from Conv2d: learned filters in PyTorch
Hand-designed filters like Sobel are useful, but CNNs learn filters automatically via backprop. A Conv2d layer has many filters (e.g., 32, 64) that collectively extract diverse features: edges at different angles, textures, corners, etc. Each filter produces one feature map (channel).
learned_filters.pyTrain a Conv2d layer and visualize learned filters
Feature maps from Conv2d layer. 32 learned filters produce 32 channels; each channel detects different patterns in CIFAR-10 images.
Pooling: reduce spatial size, preserve features
After convolution, feature maps are the same size as input (with padding). Pooling downsamples: it divides the feature map into non-overlapping regions (e.g., 2x2) and summarizes each region. MaxPool takes the maximum value; AvgPool takes the average. This reduces computation, prevents overfitting, and helps the network learn hierarchical features.
Pooling on CIFAR-10. MaxPool2d (left) preserves strongest activations; AvgPool2d (right) smooths them. Both reduce size 4x while retaining important features.
Stride and padding: control convolution size
Stride: How far the kernel moves each step. Stride=1 (default) moves 1 pixel. Stride=2 moves 2 pixels, downsampling 2x. Higher stride = smaller output.
Padding: Add border pixels (usually zeros) to input. Padding=1 adds a 1-pixel border, preserving spatial size. Without padding, (5x5) input with 3x3 kernel gives 3x3 output. With padding=1, output is still 5x5.
Output size formula: Out_size = (In_size - K + 2P) / S + 1, where K=kernel, P=padding, S=stride.
Every CNN (ResNet, VGG, MobileNet, diffusion models) relies on convolution. Priya's medical imaging system at MedScan uses Conv2d layers with learned filters to detect tumors. Early layers learn simple features (edges), mid-layers learn complex shapes, final layers classify. Modern vision transformers replace convolution with attention, but convolution remains the standard for images due to parameter efficiency and translation invariance. Understanding convolution—filters, feature maps, pooling—is essential for architecting and debugging vision models.
3D convolution across space + time. Stride-1 captures fine motion; stride-2 compresses temporal info. Pooling across frames reduces redundancy.
🎯 Mini Challenge: design a conv-pool pipeline
Task: Given a 64x64 input image, design a sequence of Conv2d and MaxPool2d layers to reduce it to 8x8 while learning 128 features. Choose kernel sizes, padding, stride, and pool sizes. Write the PyTorch code and verify output shape.
Hint 1: Each MaxPool2d with stride=2 cuts spatial size in half. 64 → 32 → 16 → 8 requires 3 pooling layers.
Hint 2: Use Conv2d with padding=1 to preserve size, then pool to downsample. E.g., Conv2d(in_ch, out_ch, 3, padding=1), then MaxPool2d(2, 2).
A filter (kernel) slides across an image, computing dot products between filter weights and image patches. Each position produces one value in the output feature map. It detects local patterns: edges, textures, colors.
MaxPool takes the maximum value from each region (preserves strong activations). AvgPool averages values (smooths). MaxPool is more aggressive, often better for feature detection. AvgPool is gentler, sometimes used when gradual pooling is desired.
Without padding, output shrinks: 5x5 image with 3x3 kernel → 3x3 output. Padding=1 adds a 1-pixel border, preserving size: 5x5 → 5x5. This prevents progressive size reduction in deep networks and ensures border pixels are processed equally.
Pooling (MaxPool) is cheap: just comparisons or averages. Convolution is expensive: it's matrix multiplication. Pooling with stride=2 reduces spatial dimensions 4x, slashing downstream convolution cost. Strategic pooling (after feature extraction) balances computational efficiency with representation quality.
Layer 1 learns simple features (edges at different angles, colors). Layer 2 combines layer-1 outputs to detect corners, textures (mid-level). Layer 3 detects objects (eyes, wheels). This hierarchy emerges from convolution + pooling: each layer has a larger receptive field, seeing a larger context. Pooling helps: early pooling preserves details, later pooling encodes global structure.
Quiz: 5 questions to test your knowledge
Question 1 of 5
A 3x3 kernel slides over a 5x5 image with no padding. What's the output feature map size?
Question 2 of 5
MaxPool2d with 2x2 kernel and stride=2 on a 32x32 feature map produces?
Question 3 of 5
What does padding=1 do in convolution?
Question 4 of 5
Which pooling operation preserves strong feature activations?
Question 5 of 5
What is the benefit of stride=2 in convolution?
0
/5
Excellent! You understand convolution fundamentals.
Cheatsheet: convolution operations at a glance
Operation
Purpose
Key Parameters
Output Effect
Conv2d
Extract features via convolution
out_channels, kernel_size, padding, stride
Produces feature maps (same spatial size if padding)
Without padding=1, 64x64 → Conv (3x3) → 62x62 → Conv → 60x60 ... → tiny. Always use padding=1 with 3x3 kernels to maintain size. Formula: padding = (kernel_size - 1) / 2.
Mistake 2: Using stride > 1 without understanding size reduction
Stride=2 halves spatial size: 64x64 → 32x32. Often desired after feature extraction, but can lose detail if applied too early. Use stride=1 for detail-preserving layers, stride=2 for downsampling layers.
Mistake 3: Excessive pooling on small images
CIFAR-10 is 32x32. Three 2x2 MaxPool layers → 4x4 remaining. Too small. Limit pooling: often 1-2 pools max. Alternatively, use stride=2 convolution instead of pooling.
Mistake 4: Not monitoring feature map statistics during training
Large activations (mean >> 1) cause saturation; small activations (mean << 0.1) cause dead neurons. Use batch normalization after convolution, or scale activations to mean~1, std~0.1 via proper initialization.
Key takeaways
Convolution is the workhorse of computer vision. Priya's CNN at MedScan learns to detect tumor boundaries by stacking convolutions and pooling—early layers detect simple edges (from Sobel-like filters), mid-layers combine them into corners and textures, final layers identify tumors.
Convolution = sliding kernel: Dot product between filter and image patches. Detects local patterns. Multiple filters → multiple feature maps.
Feature maps: Outputs of Conv2d. Each channel is one filter's response. Visualizing them reveals what the network "sees".
Pooling downsamples: MaxPool preserves strong activations (good for edges). AvgPool smooths (good for dense features). Both reduce computation 4x for stride=2.
Padding prevents shrinkage: Without padding, images shrink. Padding=1 with 3x3 kernels maintains size. Essential for deep networks.
Stride controls downsampling: Stride=2 halves spatial size without pooling. Useful for learnable downsampling.