Learn practical skills, build real-world projects, and advance your career

Image Classification using Convolutional Neural Networks in PyTorch

Part 5 of "PyTorch: Zero to GANs"

This post is the fifth in a series of tutorials on building deep learning models with PyTorch, an open source neural networks library. Check out the full series:

  1. PyTorch Basics: Tensors & Gradients
  2. Linear Regression & Gradient Descent
  3. Image Classfication using Logistic Regression
  4. Training Deep Neural Networks on a GPU
  5. Image Classification using Convolutional Neural Networks
  6. Data Augmentation, Regularization and ResNets
  7. Generating Images using Generative Adverserial Networks

In the previous tutorial, we trained a feedfoward neural networks with a single hidden layer to classify handwritten digits from the MNIST dataset with over 97% accuracy. For this tutorial, we'll use the CIFAR10 dataset, which consists of 60000 32x32 px colour images in 10 classes. Here are some sample images from the dataset:


System Setup

This notebook is hosted on Jovian.ml, a platform for sharing data science projects. If you want to follow along and run the code as you read, you can choose the "Run on Kaggle" option from the "Run" dropdown above.

Otherwise, to run the code on your machine, you can clone the notebook, install the required dependencies using conda, and start Jupyter by running the following commands:

pip install jovian --upgrade           # Install the jovian library 
jovian clone 05-cifar10-cnn            # Download notebook & dependencies
cd 05-cifar10-cnn                      # Enter the created directory 
conda create -n 05-cifar10-cnn         # Create virtual env
conda activate 05-cifar10-cnn          # Activate virtual env
conda install jupyter                  # Install Jupyter
jupyter notebook                       # Start Jupyter

On older versions of conda, you might need to run source activate 05-cifar10-cnn to activate the environment. For a more detailed explanation of the above steps, check out the System setup section in the first notebook.

Before you start executing the code below, you may want to clear the cell outputs by selecting "Kernel > Restart and Clear Output" from the Jupyter notebook menu bar, to avoid confus

We begin by importing the required modules & libraries.

# Uncomment and run the commands below if imports fail
# !conda install numpy pandas pytorch torchvision cpuonly -c pytorch -y
# !pip install matplotlib --upgrade --quiet
import os
import torch
import torchvision
import tarfile
from torchvision.datasets.utils import download_url
from torch.utils.data import random_split