Phase 6: Computer Vision
Module 15: CNN Architectures
What You Will Learn
In this module, you will learn:
- Evolution of CNN Architectures
- LeNet
- AlexNet
- VGG
- GoogLeNet (Inception)
- ResNet
- DenseNet
- MobileNet
- EfficientNet
- Transfer Learning
- CIFAR-10 Classification Project
Evolution of CNN Architectures
Computer Vision has evolved dramatically over the years.
1LeNet (1998) 2 │ 3 ▼ 4AlexNet (2012) 5 │ 6 ▼ 7VGG (2014) 8 │ 9 ▼ 10GoogLeNet (2014) 11 │ 12 ▼ 13ResNet (2015) 14 │ 15 ▼ 16DenseNet (2017) 17 │ 18 ▼ 19MobileNet (2017) 20 │ 21 ▼ 22EfficientNet (2019)
Each architecture solved limitations of previous CNNs.
CNN Architecture Comparison
| Model | Year | Main Idea | Parameters |
|---|---|---|---|
| LeNet | 1998 | First CNN | ~60K |
| AlexNet | 2012 | Deep CNN + ReLU | ~60M |
| VGG16 | 2014 | Small 3×3 filters | ~138M |
| GoogLeNet | 2014 | Inception modules | ~7M |
| ResNet50 | 2015 | Residual learning | ~25M |
| DenseNet121 | 2017 | Dense connections | ~8M |
| MobileNetV2 | 2018 | Depthwise convolutions | ~3.5M |
| EfficientNet-B0 | 2019 | Compound scaling | ~5.3M |
1. LeNet
Introduction
LeNet was introduced by Yann LeCun in 1998.
It was designed for handwritten digit recognition (MNIST).
It became the foundation of modern CNNs.
LeNet Architecture
132×32 Image 2 │ 3Conv 4 │ 5Average Pool 6 │ 7Conv 8 │ 9Average Pool 10 │ 11Flatten 12 │ 13FC 14 │ 15FC 16 │ 17Output
LeNet Implementation
1import torch 2import torch.nn as nn 3 4class LeNet(nn.Module): 5 6 def __init__(self): 7 super().__init__() 8 9 self.features = nn.Sequential( 10 11 nn.Conv2d(1,6,5), 12 13 nn.Tanh(), 14 15 nn.AvgPool2d(2), 16 17 nn.Conv2d(6,16,5), 18 19 nn.Tanh(), 20 21 nn.AvgPool2d(2) 22 ) 23 24 self.classifier = nn.Sequential( 25 26 nn.Flatten(), 27 28 nn.Linear(16*5*5,120), 29 30 nn.Tanh(), 31 32 nn.Linear(120,84), 33 34 nn.Tanh(), 35 36 nn.Linear(84,10) 37 ) 38 39 def forward(self,x): 40 41 x = self.features(x) 42 43 return self.classifier(x)
Advantages
- Simple architecture
- Very fast
- Excellent for MNIST
Limitations
- Shallow network
- Poor performance on complex datasets
2. AlexNet
Introduction
AlexNet won the ImageNet 2012 competition.
It proved that deep CNNs could outperform traditional computer vision methods.
Major innovations:
- ReLU activation
- Dropout
- GPU training
- Data augmentation
AlexNet Architecture
1Input 2 │ 3Conv 4 │ 5ReLU 6 │ 7MaxPool 8 │ 9Conv 10 │ 11MaxPool 12 │ 13Conv 14 │ 15Conv 16 │ 17Conv 18 │ 19FC 20 │ 21FC 22 │ 23Output
Load AlexNet
1from torchvision.models import alexnet 2 3model = alexnet(weights=None) 4 5print(model)
Pretrained version:
1from torchvision.models import ( 2 alexnet, 3 AlexNet_Weights 4) 5 6model = alexnet( 7 weights=AlexNet_Weights.DEFAULT 8)
Advantages
- Introduced ReLU
- Better feature extraction
- GPU-friendly
3. VGG
Introduction
VGG demonstrated that stacking many small 3×3 convolution filters improves performance.
Popular variants:
- VGG11
- VGG13
- VGG16
- VGG19
VGG16 Architecture
1Input 2 │ 33×3 Conv 4 │ 53×3 Conv 6 │ 7MaxPool 8 │ 9Repeated 10 │ 11FC 12 │ 13Output
Load VGG16
1from torchvision.models import ( 2 vgg16, 3 VGG16_Weights 4) 5 6model = vgg16( 7 weights=VGG16_Weights.DEFAULT 8)
Advantages
- Easy to understand
- Strong feature extractor
Limitations
- 138 million parameters
- High memory usage
4. GoogLeNet (Inception)
Introduction
GoogLeNet introduced the Inception Module, allowing multiple convolution sizes to run in parallel.
Inception Module
1 Input 2 │ 3 ┌────────┼────────┐ 4 │ │ │ 51×1 3×3 5×5 6 │ │ │ 7 └────────┼────────┘ 8 │ 9 Concatenate
Load GoogLeNet
1from torchvision.models import ( 2 googlenet, 3 GoogLeNet_Weights 4) 5 6model = googlenet( 7 weights=GoogLeNet_Weights.DEFAULT 8)
Advantages
- Much fewer parameters
- Faster than VGG
- Multi-scale feature extraction
5. ResNet
Introduction
Deep networks often suffer from the vanishing gradient problem.
ResNet introduced Residual Connections (Skip Connections).
Residual Block
1Input 2 │ 3Conv 4 │ 5Conv 6 │ 7 +───────────────+ 8 │ │ 9 └──── Add ◄─────┘ 10 │ 11 Output
Instead of learning:
1Output = F(x)
ResNet learns:
1Output = F(x) + x
Load ResNet18
1from torchvision.models import ( 2 resnet18, 3 ResNet18_Weights 4) 5 6model = resnet18( 7 weights=ResNet18_Weights.DEFAULT 8)
Load ResNet50
1from torchvision.models import ( 2 resnet50, 3 ResNet50_Weights 4) 5 6model = resnet50( 7 weights=ResNet50_Weights.DEFAULT 8)
Advantages
- Easy to train deep networks
- High accuracy
- Most widely used CNN
6. DenseNet
Introduction
DenseNet connects every layer to every subsequent layer.
Instead of:
1Layer1 → Layer2 → Layer3
DenseNet uses:
1Layer1 ─────► Layer2 2 3 │ │ 4 5 ▼ ▼ 6 7Layer3 ◄──────┘
Each layer receives feature maps from all previous layers.
Load DenseNet121
1from torchvision.models import ( 2 densenet121, 3 DenseNet121_Weights 4) 5 6model = densenet121( 7 weights=DenseNet121_Weights.DEFAULT 8)
Advantages
- Better feature reuse
- Fewer parameters than ResNet
- Strong gradient flow
7. MobileNet
Introduction
MobileNet is designed for mobile and embedded devices.
Main innovation:
Depthwise Separable Convolution
Instead of one expensive convolution:
1Standard Conv
MobileNet performs:
1Depthwise Conv 2 3↓ 4 5Pointwise Conv
This greatly reduces computation.
Load MobileNetV2
1from torchvision.models import ( 2 mobilenet_v2, 3 MobileNet_V2_Weights 4) 5 6model = mobilenet_v2( 7 weights=MobileNet_V2_Weights.DEFAULT 8)
Advantages
- Lightweight
- Fast inference
- Ideal for mobile deployment
8. EfficientNet
Introduction
EfficientNet introduced Compound Scaling.
Instead of increasing only depth or width, it scales:
- Depth
- Width
- Image Resolution
Together.
Compound Scaling
1Increase 2 3Depth 4 5+ 6 7Width 8 9+ 10 11Resolution 12 13↓ 14 15Better Accuracy
Load EfficientNet-B0
1from torchvision.models import ( 2 efficientnet_b0, 3 EfficientNet_B0_Weights 4) 5 6model = efficientnet_b0( 7 weights=EfficientNet_B0_Weights.DEFAULT 8)
Advantages
- Excellent accuracy
- Efficient computation
- Modern state-of-the-art baseline
Transfer Learning
Instead of training from scratch, use pretrained models.
Replace the classifier for your dataset.
Example:
1import torch.nn as nn 2from torchvision.models import ( 3 resnet18, 4 ResNet18_Weights 5) 6 7model = resnet18( 8 weights=ResNet18_Weights.DEFAULT 9) 10 11model.fc = nn.Linear( 12 model.fc.in_features, 13 10 14)
Freeze Feature Extractor
1for param in model.parameters(): 2 param.requires_grad = False 3 4model.fc = nn.Linear( 5 model.fc.in_features, 6 10 7)
Only the final layer will be trained.
Practice Project
CIFAR-10 Classification
Step 1: Load Dataset
1from torchvision.datasets import CIFAR10 2from torchvision import transforms 3from torch.utils.data import DataLoader 4 5transform = transforms.Compose([ 6 transforms.ToTensor(), 7 transforms.Normalize( 8 (0.5,0.5,0.5), 9 (0.5,0.5,0.5) 10 ) 11]) 12 13train_dataset = CIFAR10( 14 root="data", 15 train=True, 16 download=True, 17 transform=transform 18) 19 20test_dataset = CIFAR10( 21 root="data", 22 train=False, 23 download=True, 24 transform=transform 25) 26 27train_loader = DataLoader( 28 train_dataset, 29 batch_size=64, 30 shuffle=True 31) 32 33test_loader = DataLoader( 34 test_dataset, 35 batch_size=64 36)
Step 2: Load ResNet18
1import torch.nn as nn 2from torchvision.models import ( 3 resnet18, 4 ResNet18_Weights 5) 6 7model = resnet18( 8 weights=ResNet18_Weights.DEFAULT 9) 10 11model.fc = nn.Linear( 12 model.fc.in_features, 13 10 14)
Step 3: Loss & Optimizer
1import torch.optim as optim 2 3criterion = nn.CrossEntropyLoss() 4 5optimizer = optim.Adam( 6 model.parameters(), 7 lr=0.001 8)
Step 4: Training Loop
1device = "cuda" 2 3model.to(device) 4 5for images, labels in train_loader: 6 7 images = images.to(device) 8 9 labels = labels.to(device) 10 11 optimizer.zero_grad() 12 13 outputs = model(images) 14 15 loss = criterion(outputs, labels) 16 17 loss.backward() 18 19 optimizer.step() 20 21 break
Step 5: Save Model
1import torch 2 3torch.save( 4 model.state_dict(), 5 "resnet18_cifar10.pth" 6)
Architecture Comparison
| Model | Speed | Accuracy | Parameters | Best Use |
|---|---|---|---|---|
| LeNet | ⭐⭐⭐⭐⭐ | ⭐⭐ | Very Low | MNIST |
| AlexNet | ⭐⭐⭐ | ⭐⭐⭐ | High | Learning CNNs |
| VGG16 | ⭐⭐ | ⭐⭐⭐⭐ | Very High | Feature extraction |
| GoogLeNet | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | Low | Efficient classification |
| ResNet50 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Medium | General vision tasks |
| DenseNet121 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Low | Medical imaging |
| MobileNetV2 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | Very Low | Mobile devices |
| EfficientNet-B0 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Low | Production systems |
Choosing the Right CNN
| Scenario | Recommended Model |
|---|---|
| Learning CNNs | LeNet, AlexNet |
| Small dataset | ResNet18 |
| High accuracy | EfficientNet |
| Resource-constrained devices | MobileNet |
| Medical imaging | DenseNet |
| Image classification | ResNet50 |
| Transfer learning | ResNet18, EfficientNet-B0 |
Best Practices
- Start with pretrained models instead of training from scratch.
- Use transfer learning for small or medium-sized datasets.
- Fine-tune only the classifier first, then optionally unfreeze deeper layers.
- Normalize images using the statistics expected by the pretrained model.
- Use ResNet18 or MobileNetV2 for quick experimentation.
- Choose EfficientNet when you need a strong balance between speed and accuracy.
- Use MobileNet for edge devices and mobile applications.
- Monitor validation accuracy and save the best-performing model.
Module Summary
In this module, you learned:
- ✅ The evolution of CNN architectures from LeNet to EfficientNet.
- ✅ How AlexNet popularized deep CNNs using ReLU, Dropout, and GPU training.
- ✅ Why VGG uses stacked 3×3 convolution layers.
- ✅ How GoogLeNet extracts multi-scale features with Inception modules.
- ✅ How ResNet uses residual (skip) connections to train very deep networks.
- ✅ How DenseNet improves feature reuse through dense connectivity.
- ✅ How MobileNet achieves efficient inference using depthwise separable convolutions.
- ✅ How EfficientNet balances depth, width, and resolution through compound scaling.
- ✅ How to apply transfer learning with pretrained models.
- ✅ How to build a complete CIFAR-10 image classification project using ResNet18.
After completing this module, you'll have a strong understanding of the most influential CNN architectures and be prepared to explore advanced computer vision topics such as Object Detection (YOLO, Faster R-CNN), Image Segmentation (U-Net, DeepLabV3), and Vision Transformers (ViT) in PyTorch.