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:

alt

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
Collecting package metadata (current_repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.8.2 latest version: 4.8.3 Please update conda by running $ conda update -n base conda ## Package Plan ## environment location: /srv/conda/envs/notebook added / updated specs: - cpuonly - numpy - pandas - pytorch - torchvision The following packages will be downloaded: package | build ---------------------------|----------------- blas-2.15 | mkl 10 KB conda-forge ca-certificates-2020.4.5.2 | hecda079_0 147 KB conda-forge certifi-2020.4.5.2 | py37hc8dfbb8_0 152 KB conda-forge cpuonly-1.0 | 0 2 KB pytorch freetype-2.10.2 | he06d7ca_0 905 KB conda-forge intel-openmp-2020.1 | 217 780 KB defaults jpeg-9d | h516909a_0 266 KB conda-forge libblas-3.8.0 | 15_mkl 10 KB conda-forge libcblas-3.8.0 | 15_mkl 10 KB conda-forge libgfortran-ng-7.5.0 | hdf63c60_6 1.7 MB conda-forge liblapack-3.8.0 | 15_mkl 10 KB conda-forge liblapacke-3.8.0 | 15_mkl 10 KB conda-forge libpng-1.6.37 | hed695b0_1 308 KB conda-forge libtiff-4.1.0 | hc7e4089_6 668 KB conda-forge libwebp-base-1.1.0 | h516909a_3 845 KB conda-forge lz4-c-1.9.2 | he1b5a44_1 226 KB conda-forge mkl-2020.1 | 217 129.0 MB defaults ninja-1.10.0 | hc9558a2_0 1.9 MB conda-forge numpy-1.18.5 | py37h8960a57_0 5.1 MB conda-forge olefile-0.46 | py_0 31 KB conda-forge pandas-1.0.4 | py37h0da4684_0 10.1 MB conda-forge pillow-7.1.2 | py37h718be6c_0 658 KB conda-forge pytorch-1.5.0 | py3.7_cpu_0 90.5 MB pytorch pytz-2020.1 | pyh9f0ad1d_0 227 KB conda-forge torchvision-0.6.0 | py37_cpu 11.0 MB pytorch zstd-1.4.4 | h6597ccf_3 991 KB conda-forge ------------------------------------------------------------ Total: 255.3 MB The following NEW packages will be INSTALLED: blas conda-forge/linux-64::blas-2.15-mkl cpuonly pytorch/noarch::cpuonly-1.0-0 freetype conda-forge/linux-64::freetype-2.10.2-he06d7ca_0 intel-openmp pkgs/main/linux-64::intel-openmp-2020.1-217 jpeg conda-forge/linux-64::jpeg-9d-h516909a_0 libblas conda-forge/linux-64::libblas-3.8.0-15_mkl libcblas conda-forge/linux-64::libcblas-3.8.0-15_mkl libgfortran-ng conda-forge/linux-64::libgfortran-ng-7.5.0-hdf63c60_6 liblapack conda-forge/linux-64::liblapack-3.8.0-15_mkl liblapacke conda-forge/linux-64::liblapacke-3.8.0-15_mkl libpng conda-forge/linux-64::libpng-1.6.37-hed695b0_1 libtiff conda-forge/linux-64::libtiff-4.1.0-hc7e4089_6 libwebp-base conda-forge/linux-64::libwebp-base-1.1.0-h516909a_3 lz4-c conda-forge/linux-64::lz4-c-1.9.2-he1b5a44_1 mkl pkgs/main/linux-64::mkl-2020.1-217 ninja conda-forge/linux-64::ninja-1.10.0-hc9558a2_0 numpy conda-forge/linux-64::numpy-1.18.5-py37h8960a57_0 olefile conda-forge/noarch::olefile-0.46-py_0 pandas conda-forge/linux-64::pandas-1.0.4-py37h0da4684_0 pillow conda-forge/linux-64::pillow-7.1.2-py37h718be6c_0 pytorch pytorch/linux-64::pytorch-1.5.0-py3.7_cpu_0 pytz conda-forge/noarch::pytz-2020.1-pyh9f0ad1d_0 torchvision pytorch/linux-64::torchvision-0.6.0-py37_cpu zstd conda-forge/linux-64::zstd-1.4.4-h6597ccf_3 The following packages will be UPDATED: ca-certificates 2020.4.5.1-hecc5488_0 --> 2020.4.5.2-hecda079_0 certifi 2020.4.5.1-py37hc8dfbb8_0 --> 2020.4.5.2-py37hc8dfbb8_0 Downloading and Extracting Packages lz4-c-1.9.2 | 226 KB | ##################################### | 100% libtiff-4.1.0 | 668 KB | ##################################### | 100% libwebp-base-1.1.0 | 845 KB | ##################################### | 100% liblapacke-3.8.0 | 10 KB | ##################################### | 100% numpy-1.18.5 | 5.1 MB | ##################################### | 100% olefile-0.46 | 31 KB | ##################################### | 100% intel-openmp-2020.1 | 780 KB | ##################################### | 100% pillow-7.1.2 | 658 KB | ##################################### | 100% pytorch-1.5.0 | 90.5 MB | ##################################### | 100% libblas-3.8.0 | 10 KB | ##################################### | 100% certifi-2020.4.5.2 | 152 KB | ##################################### | 100% libgfortran-ng-7.5.0 | 1.7 MB | ##################################### | 100% blas-2.15 | 10 KB | ##################################### | 100% zstd-1.4.4 | 991 KB | ##################################### | 100% libcblas-3.8.0 | 10 KB | ##################################### | 100% mkl-2020.1 | 129.0 MB | ##################################### | 100% libpng-1.6.37 | 308 KB | ##################################### | 100% liblapack-3.8.0 | 10 KB | ##################################### | 100% pandas-1.0.4 | 10.1 MB | ##################################### | 100% pytz-2020.1 | 227 KB | ##################################### | 100% torchvision-0.6.0 | 11.0 MB | ##################################### | 100% ca-certificates-2020 | 147 KB | ##################################### | 100% cpuonly-1.0 | 2 KB | ##################################### | 100% freetype-2.10.2 | 905 KB | ##################################### | 100% jpeg-9d | 266 KB | ##################################### | 100% ninja-1.10.0 | 1.9 MB | ##################################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done
import os
import torch
import torchvision
import tarfile
from torchvision.datasets.utils import download_url
from torch.utils.data import random_split
print("Modules Imported")
Modules Imported