Convolutional Neural Networks Image Processing Feature Extraction

# Convolutional Neural Networks: Image Processing & Feature Extraction

## Introduction & Motivation

CNNs exploit spatial structure via convolutional filters sliding over images, reducing parameters vs. fully-connected layers. Share weights across space; learn hierarchical features (edges→textures→objects). State-of-the-art for image classification, detection, segmentation.

Motivation: Images have spatial locality and translation invariance. Fully-connected wasteful. Convolutions capture local patterns; pooling provides invariance. Hierarchical architecture mirrors visual cortex.

Applications: Image classification (ImageNet), object detection (YOLO), semantic segmentation, face recognition, medical imaging.

---

## Core Concepts & Theory

### Convolution Operation

Slide filter across image; compute dot product. Output: feature map.
$$ ext{Output}[i,j] = \sum_k \sum_l ext{Filter}[k,l] imes ext{Input}[i+k,j+l]$$

### Pooling

Downsample via max/average pooling; reduce spatial dimensions, increase invariance.

---

## Mathematical Formulation

Convolution with bias and activation:
$$\mathbf{y} = \sigma(\mathbf{W} * \mathbf{x} + \mathbf{b})$$

where * is convolution, \sigma is activation (ReLU).

Pooling (max):
$$y_{ij} = \max_{k,l \in ext{window}} x_{i+k,j+l}$$

---

## Advanced Theory & Extensions

### Batch Normalization

Normalize activations per batch; stabilizes training, enables higher learning rates.

### Residual Connections

Skip connections bypass layers; mitigate vanishing gradients in deep networks (ResNet).

### Depthwise Separable Convolutions

Reduce parameters via group convolutions; enable mobile deployment (MobileNet).

---

## Computational Considerations

Training: O(n imes c_{in} imes k^2 imes h imes w) for n samples, k×k filters, h×w spatial dims.

Inference: Same; often accelerated via cuDNN.

---

## Practical Implementation Strategies

### Filter Size

3×3 most common; 1×1 for dimensionality reduction, 5×5+ rarely used.

### Stride & Padding

Stride=1, padding='same' preserve spatial dims; stride>1 reduces dimensions.

### Depth (channels)

32, 64, 128, 256... double at each pooling layer.

---

## Benchmark Datasets & Evaluation

ImageNet: 1000 classes, 1.2M images. Metric: top-1 accuracy, top-5 accuracy.

CIFAR-10: 10 classes, 60K images. Simpler benchmark.

---

## Key Challenges & Limitations

### Computational Cost

High-res images expensive. Solutions: downsampling, efficient architectures (MobileNet).

### Data Requirements

CNNs need large datasets (100K+ images). Transfer learning critical.

---

## Hyperparameter Tuning

Filter sizes, num_filters, stride, padding, batch_norm, dropout rates.

---

## Real-World Applications & Case Studies

Medical Imaging: CT/MRI diagnosis via CNNs.

Autonomous Driving: Object detection via CNNs.

---

## Integration with Other Methods

CNN + RNN → Image captioning.

CNN + Attention → Focus on relevant regions.

---

## Summary & Key Takeaways

CNNs exploit spatial structure via convolutions and pooling, enabling efficient, hierarchical feature learning for images.

Principles:
1. Convolutions capture local spatial patterns.
2. Pooling provides translation invariance.
3. Hierarchical architecture learns from simple to complex.
4. Batch norm stabilizes training.
5. Skip connections enable very deep networks.

---

---

## Appendix: Practical Labs

### Lab 1: Simple CNN

import torch
import torch.nn as nn
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

class SimpleCNN(nn.Module):
 def __init__(self):
 super().__init__()
 self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
 self.pool = nn.MaxPool2d(2, 2)
 self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
 self.fc = nn.Linear(64 * 7 * 7, 10)
 
 def forward(self, x):
 x = self.pool(torch.relu(self.conv1(x)))
 x = self.pool(torch.relu(self.conv2(x)))
 x = x.view(x.size(0), -1)
 return self.fc(x)

model = SimpleCNN()
X = torch.randn(4, 1, 28, 28)
out = model(X)

print(f"Output shape: {out.shape}")
assert out.shape == (4, 10), "Should have 10 classes"
print("✓ Simple CNN working")

if __name__ == "__main__":
 print("Lab 1: Simple CNN - PASSED")

### Lab 2: Conv Filter Visualization

import torch
import torch.nn as nn

conv = nn.Conv2d(1, 16, kernel_size=3, padding=1)
X = torch.randn(1, 1, 28, 28)
output = conv(X)

print(f"Input shape: {X.shape}, Output shape: {output.shape}")
print(f"Filter parameters: {conv.weight.shape}")

assert output.shape == (1, 16, 28, 28), "Correct output shape"
assert conv.weight.shape == (16, 1, 3, 3), "Correct filter shape"
print("✓ Conv filter visualization working")

if __name__ == "__main__":
 print("Lab 2: Conv Filters - PASSED")

### Lab 3: Pooling Effect

import torch
import torch.nn as nn

X = torch.randn(1, 1, 28, 28)

max_pool = nn.MaxPool2d(2, 2)
avg_pool = nn.AvgPool2d(2, 2)

max_out = max_pool(X)
avg_out = avg_pool(X)

print(f"Max pool output: {max_out.shape}")
print(f"Avg pool output: {avg_out.shape}")

assert max_out.shape == (1, 1, 14, 14), "MaxPool reduces by 2"
assert avg_out.shape == (1, 1, 14, 14), "AvgPool reduces by 2"
print("✓ Pooling effect working")

if __name__ == "__main__":
 print("Lab 3: Pooling - PASSED")

### Lab 4: Batch Norm

import torch
import torch.nn as nn

class CNNWithBatchNorm(nn.Module):
 def __init__(self):
 super().__init__()
 self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
 self.bn1 = nn.BatchNorm2d(32)
 self.pool = nn.MaxPool2d(2, 2)
 
 def forward(self, x):
 x = self.conv1(x)
 x = self.bn1(x)
 x = torch.relu(x)
 x = self.pool(x)
 return x

model = CNNWithBatchNorm()
X = torch.randn(8, 1, 28, 28)
out = model(X)

print(f"Output with batch norm: {out.shape}")
assert out.shape == (8, 32, 14, 14), "Correct shape"
print("✓ Batch norm working")

if __name__ == "__main__":
 print("Lab 4: Batch Norm - PASSED")

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account