Course Content
CNNs in PyTorch
Implementing convolutional networks using PyTorch
Introduction
Convolutional Neural Networks (CNNs) are highly effective for image classification and recognition tasks. In this tutorial, you will learn how to build, train, and evaluate a CNN using PyTorch on the CIFAR-10 dataset.
1οΈβ£ Install and Import Dependencies
First, ensure PyTorch and torchvision are installed:
pip install torch torchvision
Now import the required libraries:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
2οΈβ£ Prepare the Dataset
Using CIFAR-10 for colored image classification:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
3οΈβ£ Define the CNN Model
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.fc1 = nn.Linear(64 * 8 * 8, 128)
self.fc2 = nn.Linear(128, 10)
self.relu = nn.ReLU()
def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = self.pool(self.relu(self.conv2(x)))
x = x.view(-1, 64 * 8 * 8)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
net = SimpleCNN()
4οΈβ£ Define Loss Function and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)
5οΈβ£ Training the Model
epochs = 10
for epoch in range(epochs):
running_loss = 0.0
for images, labels in trainloader:
optimizer.zero_grad()
outputs = net(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch+1}, Loss: {running_loss/len(trainloader):.4f}")
print("Training completed!")
6οΈβ£ Evaluating the Model
correct = 0
total = 0
with torch.no_grad():
for images, labels in testloader:
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Test Accuracy: {100 * correct / total:.2f}%")
Conclusion
β
You have successfully built, trained, and evaluated a CNN using PyTorch.
β
You now understand how to prepare datasets, define models, train, and assess performance for image classification tasks using CNNs.
Whatβs Next?
β
Experiment with deeper architectures, adding dropout and batch normalization.
β
Visualize feature maps to understand what your CNN is learning.
β
Explore advanced CNN architectures like ResNet on superml.org
.
Join the SuperML Community to share your CNN projects and receive peer feedback.
Happy Building with PyTorch! π