GenAIHub
Back to Technical
Computer Vision

Convolutional Neural Networks (CNNs)

The backbone of modern Computer Vision. CNNs are specialized neural networks designed to process grid-like data, such as images, by learning spatial hierarchies of features.

How CNNs Work

Unlike standard Neural Networks that treat input pixels as independent, CNNs use **convolution** to preserve spatial relationships. They learn features like edges and textures in early layers, and complex shapes (eyes, wheels) in deeper layers.

Convolution Layer

Applies learnable filters (kernels) to the input image to create feature maps. Essential for detecting patterns.

Pooling Layer

Reduces spatial dimensions (down-sampling), reducing computation and controling overfitting. Max Pooling is most common.

Fully Connected

The final layers that act as a classifier (like a traditional MLP), taking the flattened feature vector to predict the class.

Simple CNN in PyTorch

A classic architecture for MNIST digit classification.

import torch
import torch.nn as nn
import torch.nn.functional as F

class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        # 1 input channel (grayscale), 32 output channels, 3x3 kernel
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.conv2 = nn.Conv2d(32, 64, 3)
        
        # Fully connected layers
        self.fc1 = nn.Linear(64 * 5 * 5, 128)
        self.fc2 = nn.Linear(128, 10) # 10 classes

    def forward(self, x):
        # Convolution -> ReLU -> Pooling
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        
        # Flatten
        x = x.view(-1, 64 * 5 * 5)
        
        # Dense layers
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

model = SimpleCNN()
print(model)

Key Applications

Image Classification

Identifying what is in an image (e.g., Cat vs Dog). Famous models: ResNet, VGG, EfficientNet.

Object Detection

Locating and classifying multiple objects in an image. Famous models: YOLO, R-CNN.

Image Segmentation

Classifying pixels to define precise boundaries of objects. Famous models: U-Net, Mask R-CNN.

Related Topics