CNN, Data Handling and MINST dataset
In part one we learned basics of pytorch and built a simple linear model. In this part we will build a CNN model, learn how to handle data and use the MINST dataset. ### Convolutional Neural Networks (CNNs) CNNs are a type of neural network that are particularly well-suited for image data. They use convolutional layers to automatically and adaptively learn spatial hierarchies of features from input images. This makes them very effective for tasks such as image classification, object detection, and more.
A Quick Overview of CNNs and Data Handling in PyTorch
Convolutional Neural Networks (CNNs) are powerful tools for image-related tasks, leveraging convolutional layers to learn spatial hierarchies of features. They reduce computational complexity through parameter sharing and process images hierarchically, making them robust to spatial variations.
Key Components of CNNs:
- Convolutional Layer (
nn.Conv2d): Detects local features using kernels, strides, and padding. It processes input channels and outputs feature maps. - Pooling Layer (
nn.MaxPool2d): Downsamples feature maps, reducing parameters and enhancing translational invariance. - Fully Connected Layer (
nn.Linear): Flattens high-level features for final classification or regression tasks.
What are CNNs and Why Use Them?
Convolutional Neural Networks (CNNs) are a specialized kind of neural network designed for processing data with a grid-like topology — like images. Unlike traditional fully connected layers, CNNs use convolutional layers that scan small regions (kernels) over the input, capturing important features such as edges, textures, and patterns.
Key benefits of CNNs include:
-
Parameter sharing: The same kernel (filter) is applied across the entire image, drastically reducing the number of parameters compared to fully connected layers.
-
Spatial hierarchy: CNNs can learn hierarchical features from simple edges to complex objects as you go deeper.
-
Robustness: Pooling layers reduce spatial size, making the model more resilient to small shifts or distortions in the image.
CNN Layers: nn.Conv2d and nn.MaxPool2d
Concept Explanation:
- Convolutional Layers (
nn.Conv2d): The core building block of CNNs. They apply a convolution operation to the input, sliding a small filter (kernel) over the input data. This helps capture local patterns like edges, textures, etc.- Key idea: Parameter sharing (the same filter is used across different parts of the input image), which reduces the number of parameters compared to fully connected layers on images.
- Input: Typically a 4D tensor of shape
(N, C_in, H_in, W_in)where:N: Batch sizeC_in: Number of input channels (e.g., 3 for RGB images, 1 for grayscale)H_in: Height of the input feature mapW_in: Width of the input feature map
- Output: A 4D tensor of shape
(N, C_out, H_out, W_out)whereC_outis the number of output channels (number of filters used).H_outandW_outdepend on kernel size, stride, and padding.
Key nn.Conv2d Parameters:
in_channels (int): Number of channels in the input image.out_channels (int): Number of channels produced by the convolution (number of filters).kernel_size (int or tuple): Size of the convolving kernel.stride (int or tuple, optional): Stride of the convolution. Default: 1.padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0.dilation (int or tuple, optional): Spacing between kernel elements. Default: 1.groups (int, optional): Number of blocked connections from input channels to output channels (for depthwise separable convolutions, etc.). Default: 1.bias (bool, optional): IfTrue, adds a learnable bias to the output. Default:True.
Example:
import torch
import torch.nn as nn
conv_layer = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 32, 32) # Batch of 1 RGB image 32x32
output = conv_layer(input_tensor)
print(output.shape) # Output shape: (1, 16, 32, 32)
- Pooling Layers (
nn.MaxPool2d): Used to reduce the spatial dimensions (height and width) of the feature maps. This helps reduce computation, control overfitting, and makes the network more robust to variations in the position of features.- Max Pooling: Takes the maximum value from a small window (kernel) as it slides over the feature map.
- Input: Typically a 4D tensor from a convolutional layer.
- Output: A 4D tensor with reduced
HandWdimensions. The number of channelsCremains the same.
Key nn.MaxPool2d Parameters:
kernel_size (int or tuple): The size of the window to take a max over.stride (int or tuple, optional): The stride of the window. Default value iskernel_size.padding (int or tuple, optional): Implicit zero padding to be added on both sides. Default: 0.dilation (int or tuple, optional): Controls the spacing between the kernel points. Default: 1.return_indices (bool, optional): IfTrue, will return the max indices along with the outputs. Useful forMaxUnpool2d. Default:False.ceil_mode (bool, optional): WhenTrue, will useceilinstead offloorto compute the output shape. Default:False.
Example:
pool_layer = nn.MaxPool2d(kernel_size=2, stride=2)
pooled_output = pool_layer(output)
print(pooled_output.shape) # Shape: (1, 16, 16, 16)
Data Handling in PyTorch:
Efficient data handling is crucial for training CNNs. PyTorch provides tools like torchvision.transforms for preprocessing, torch.utils.data.Dataset for custom datasets, and torch.utils.data.DataLoader for batching and shuffling data.
- Transforms: Convert images to tensors, resize them, and normalize values for effective training.
- Dataset: Subclass
Datasetto load and preprocess data samples. - DataLoader: Wraps datasets for batching, shuffling, and parallel loading.
These components streamline the process of building and training CNNs for tasks like image classification and gaze tracking.
Example of Data Preprocessing with torchvision.transforms:
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize((<HEIGHT>, <WIDTH>)),# Replace <HEIGHT> and <WIDTH> with desired dimensions
transforms.ToTensor(),# Converts image to tensor
transforms.Normalize(mean=[<MEAN_R>, <MEAN_G>, <MEAN_B>],# Replace with dataset-specific mean values
std=[<STD_R>, <STD_G>, <STD_B>])# Replace with dataset-specific std deviations
])
Custom Dataset and DataLoader
To work with your own images or data, subclass torch.utils.data.Dataset and implement:
-
__len__: Returns dataset size. -
__getitem__: Returns a single sample (image and label), applying transforms.
The DataLoader wraps the dataset and provides batching, shuffling, and parallel loading.
Mini Project: Simple CNN on MNIST
Put it all together by building a simple CNN to classify handwritten digits using the MNIST dataset.
- Load MNIST dataset with transforms (ToTensor, Normalize).
- Define a CNN with Conv2d, ReLU, MaxPool layers.
- Train with CrossEntropyLoss and Adam optimizer.
- Evaluate accuracy on the test set.
Resources: