Lab 2-Image-Classification-Using-NNs
Lab 2-Image-Classification-Using-NNs
Classification
Duration: 2 hours
Tools:
• Jupyter Notebook
• IDE: PyCharm==2024.2.3 (or any IDE of your choice)
• Python: 3.12
• Libraries:
o PyTorch==2.4.0
o TorchVision==0.19.0
o Matplotlib==3.9.2
Learning Objectives:
• Understand the basic architecture of a neural network.
• Load and explore the CIFAR-10 dataset.
• Implement and train a neural network, individualized by your QMUL ID.
• Verify machine learning concepts such as accuracy, loss, and evaluation metrics
by running predefined code.
Lab Outline:
In this lab, you will implement a simple neural network model to classify images from
the CIFAR-10 dataset. The task will be individualized based on your QMUL ID to ensure
unique configurations for each student.
• The CIFAR-10 dataset consists of 60,000 32x32 color images categorized into 10
classes (airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks).
• The dataset is divided into 50,000 training images and 10,000 testing images.
• You will load the CIFAR-10 dataset using PyTorch’s built-in torchvision library.
Step-by-step Instructions:
1. Open the provided Jupyter Notebook.
2. Load and explore the CIFAR-10 dataset using the following code:
You will implement a neural network model to classify images from the CIFAR-10
dataset. However, certain parts of the task will be individualized based on your QMUL
ID. Follow the instructions carefully to ensure your model’s configuration is unique.
Step 1: Dataset Split Based on Your QMUL ID
You will use the last digit of your QMUL ID to define the training-validation split:
• If your ID ends in 0-4: use a 70-30 split (70% training, 30% validation).
• If your ID ends in 5-9: use an 80-20 split (80% training, 20% validation).
Code:
# DataLoaders
from torch.utils.data import DataLoader
batch_size = 32 + last_digit_of_id # Batch size is 32 +
last digit of your QMUL ID
train_loader = DataLoader(train_dataset,
batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset,
batch_size=batch_size, shuffle=False)
Code:
import torch
import torch.optim as optim
Expected Output: For training with around 100 epochs, it may take 0.5~1 hour to finish.
You may see a lower accuracy, especially for the validation accuracy, due to the lower
number of epochs or the used simple neural network model, etc. If you are interested,
you can find more advanced open-sourced codes to test and improve the performance.
In this case, it may require a long training time on the CPU-based device.
Code:
# Training loop
train_losses = []
train_accuracies = []
val_accuracies = []
running_loss += loss.item()
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
# Validation step
model.eval()
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in val_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
train_losses.append(running_loss)
train_accuracies.append(train_accuracy)
val_accuracies.append(val_accuracy)
Task 3: Visualizing and Analyzing the Results
Visualize the results of the training and validation process. Generate the following plots
using Matplotlib:
• Training Loss vs. Epochs.
• Training and Validation Accuracy vs. Epochs.
# Plot Loss
plt.figure()
plt.plot(range(1, num_epochs + 1), train_losses,
label="Training Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Training Loss")
plt.legend()
plt.show()
# Plot Accuracy
plt.figure()
plt.plot(range(1, num_epochs + 1), train_accuracies,
label="Training Accuracy")
plt.plot(range(1, num_epochs + 1), val_accuracies,
label="Validation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Training and Validation Accuracy")
plt.legend()
plt.show()