Diving into Deep Learning with PyTorch: Creating Your First Neural Network for Image Recognition

Diving into Deep Learning with PyTorch: Creating Your First Neural Network for Image Recognition

Introduction

Deep learning is a powerful subset of machine learning that uses algorithms inspired by the structure and function of the brain called artificial neural networks. One of the most popular frameworks for deep learning in Python is PyTorch, developed by Facebook’s AI Research lab. This post will guide you through creating your first neural network with PyTorch, specifically designed for image recognition tasks.

Why PyTorch?

  • Ease of use: PyTorch offers an intuitive interface and is easier to learn for beginners due to its straightforward API and dynamic computational graph.
  • Flexibility: It provides flexibility as you can modify the graphs on the go, which is great for experiments.
  • Strong Community: Being backed by Facebook and a robust community, PyTorch is well-maintained and constantly evolving.

Setting Up

Before diving into the coding part, ensure you have Python installed. Then, install PyTorch by running the following command:

pip install torch torchvision

Creating the Dataset

For this tutorial, we’ll use the CIFAR-10 dataset, which consists of 60,000 32×32 color images in 10 classes, with 6,000 images per class. PyTorch conveniently packages this dataset and provides utility functions to load it in a handy manner.

import torch
from torchvision import datasets, transforms

# Transform the data into a tensor and normalize it
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# Load the CIFAR-10 dataset
trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True)

Building the Neural Network

Now let’s build our neural network. We will use a simple architecture with two convolutional layers followed by two fully connected layers.

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

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # Convolutional layers
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # Fully connected layers
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = Net()

Training the Model

The next step is to train our neural network on the CIFAR-10 dataset. We will use a simple gradient descent with momentum to optimize our model.

import torch.optim as optim

# Define Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Training loop
for epoch in range(2): # loop over the dataset multiple times
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # Get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # Zero the parameter gradients
        optimizer.zero_grad()

        # Forward + backward + optimize
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # Print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # Print every 2000 mini-batches
            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
            running_loss = 0.0

Conclusion

You’ve just built and trained your first image recognition neural network using PyTorch! Although the network structure used in this tutorial is quite simple, it lays the groundwork for diving deeper into more complex architectures and problems. With PyTorch, experimenting and iterating models is straightforward, giving you the power to tackle a myriad of machine learning tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *