AI Notes
Master CNN architecture for artificial intelligence. Learn convolution operation, pooling, filters, image classification, PyTorch implementation. Technical interview 2024.
Introduction
Convolutional Neural Networks (CNNs) are specialized neural architectures designed for processing grid-structured data, particularly images. Instead of connecting every input pixel to every neuron (which would require billions of parameters for a simple photograph), CNNs use small learnable filters that slide across the image, detecting local patterns like edges, textures, and shapes. This design mirrors how the human visual cortex processes visual information through receptive fields of increasing complexity.
The Convolution Operation
The core operation in a CNN is convolution: sliding a small filter (kernel) across the input and computing dot products at each position.
CNN Architecture Components
Convolutional Layers
Each convolutional layer applies multiple filters to detect different features:
| Layer specification | Conv2D(filters=32, kernel_size=3, stride=1, padding='same') |
| - filters | Number of different patterns to detect |
| - kernel_size | Size of sliding window (3×3 is most common) |
| - stride | How many pixels to skip between positions |
| - padding | 'same' keeps output size = input size |
| Example | 3 channels × 3 × 3 × 32 + 32 = 896 parameters |
| vs fully connected | 224×224×3 × 1000 = 150,528,000 parameters! |
Pooling Layers
Pooling reduces spatial dimensions while retaining important features:
| Input | Output: |
| [4 2 1 3] | [4 5] |
| Effect | Reduces 4×4 to 2×2 (75% fewer values) |
| Benefit | Translation invariance (small shifts don't change output) |
Fully Connected Layers
After convolutional and pooling layers extract features, fully connected layers perform classification:
Complete CNN Architecture Example
| Input | 224×224×3 (RGB image) |
| Conv(64, 3×3) + ReLU | 224×224×64 (detect edges) |
| Conv(64, 3×3) + ReLU | 224×224×64 |
| MaxPool(2×2) | 112×112×64 |
| Conv(128, 3×3) + ReLU | 112×112×128 (detect textures) |
| Conv(128, 3×3) + ReLU | 112×112×128 |
| MaxPool(2×2) | 56×56×128 |
| Conv(256, 3×3) + ReLU | 56×56×256 (detect patterns) |
| Conv(256, 3×3) + ReLU | 56×56×256 |
| MaxPool(2×2) | 28×28×256 |
| Flatten | 200,704 values |
| Dense(1000) + Softmax | class probabilities |
Key CNN Design Principles
Local Connectivity: Each neuron connects only to a small region of the input (its receptive field). A 3×3 filter looks at only 9 pixels at a time, but deeper layers have larger effective receptive fields.
Weight Sharing: The same filter weights are used at every spatial position. A vertical edge detector works the same whether the edge is in the top-left or bottom-right of the image.
Hierarchical Features: Early layers learn simple features (edges, colors), middle layers combine these into textures and parts, and deep layers recognize complete objects.
| After Conv1 (3×3) | Each neuron sees 3×3 = 9 pixels |
| After Conv2 (3×3) | Each neuron sees 5×5 = 25 pixels |
| After Conv3 (3×3) | Each neuron sees 7×7 = 49 pixels |
| After Pool | Receptive field doubles |
Famous CNN Architectures
LeNet-5 (1998): Yann LeCun's pioneer for digit recognition. Two conv layers + two FC layers. Proved CNNs work for practical problems.
AlexNet (2012): Won ImageNet competition, igniting the deep learning revolution. Used ReLU, dropout, and GPU training. 8 layers, 60M parameters.
VGGNet (2014): Showed that deeper is better when using small 3×3 filters consistently. VGG-16 has 16 layers, 138M parameters.
ResNet (2015): Introduced skip connections allowing training of 152+ layer networks. Solved degradation problem where deeper networks performed worse.
Worked Example: Edge Detection
A CNN automatically learns filters similar to classical edge detectors:
Data Augmentation for CNNs
Since CNNs need large datasets, data augmentation creates variations of training images:
Transfer Learning
Pre-trained CNNs (trained on ImageNet with millions of images) can be adapted to new tasks with limited data:
Summary
CNNs transformed computer vision by learning hierarchical visual features automatically through convolution, pooling, and backpropagation. Their key innovations of local connectivity, weight sharing, and spatial hierarchy make them parameter-efficient and highly effective for image tasks. Modern architectures like ResNet enable training extremely deep networks, while transfer learning allows adapting powerful pre-trained models to new problems with minimal data. Understanding CNNs is essential for any work involving visual data in AI.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Convolutional Neural Networks (CNN) - Complete AI Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, deep, learning, convolutional, neural
Related Artificial Intelligence Topics