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

Image Classification using Logistic Regression in PyTorch

Part 3 of "PyTorch: Zero to GANs"

This post is the third 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 this tutorial, we'll use our existing knowledge of PyTorch and linear regression to solve a very different kind of problem: image classification. We'll use the famous MNIST Handwritten Digits Database as our training dataset. It consists of 28px by 28px grayscale images of handwritten digits (0 to 9), along with labels for each image indicating which digit it represents. Here are some sample images from the dataset:

mnist-sample

System setup

This tutorial takes a code-first approach towards learning PyTorch, and you should try to follow along by running and experimenting with the code yourself. The easiest way to start executing this notebook is to click the "Run" button at the top of this page, and select "Run on Kaggle". This will run the notebook on Kaggle, a free online service for running Jupyter notebooks (you might need to create an account).

Running on your computer locally

(Skip this if you're running on Kaggle) To run this notebook locally, clone this notebook, install the required dependencies using conda, and start Jupyter by running the following commands on the terminal / Conda prompt:

pip install jovian --upgrade # Install the jovian library jovian clone aakashns/03-logistic-regression # Download notebook & dependencies cd 03-logistic-regression # Enter the created directory conda create -n 03-logistic-regression python=3.8 # Create an environment conda activate 03-logistic-regression # Activate virtual env jupyter notebook # Start Jupyter

You can find the notebook_id by cliking the Clone button at the top of this page on Jovian. For a more detailed explanation of the above steps, check out the System setup section in the first notebook.

Exploring the Data

We begin by importing torch and torchvision. torchvision contains some utilities for working with image data. It also contains helper classes to automatically download and import popular datasets like MNIST.

# Uncomment and run the commands below if imports fail
!conda install numpy 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.7.10 latest version: 4.9.0 Please update conda by running $ conda update -n base conda ## Package Plan ## environment location: /srv/conda/envs/notebook added / updated specs: - cpuonly - numpy - pytorch - torchvision The following packages will be downloaded: package | build ---------------------------|----------------- blas-1.0 | mkl 6 KB defaults ca-certificates-2020.6.20 | hecda079_0 145 KB conda-forge certifi-2020.6.20 | py37he5f6b98_2 151 KB conda-forge cpuonly-1.0 | 0 2 KB pytorch freetype-2.10.4 | h7ca028e_0 912 KB conda-forge intel-openmp-2020.2 | 254 786 KB defaults jpeg-9d | h36c2ea0_0 264 KB conda-forge libblas-3.9.0 |1_h6e990d7_netlib 176 KB conda-forge libcblas-3.9.0 |3_h893e4fe_netlib 54 KB conda-forge libgfortran-ng-7.5.0 | hae1eefd_17 22 KB conda-forge libgfortran4-7.5.0 | hae1eefd_17 1.3 MB conda-forge liblapack-3.9.0 |3_h893e4fe_netlib 2.9 MB conda-forge libpng-1.6.37 | h21135ba_2 306 KB conda-forge libtiff-4.0.9 | h648cc4a_1002 566 KB conda-forge libuv-1.40.0 | hd18ef5c_0 920 KB conda-forge mkl-2020.2 | 256 138.3 MB defaults ninja-1.10.1 | hfc4b9b4_2 1.9 MB conda-forge numpy-1.19.4 | py37h7e9df27_1 5.2 MB conda-forge olefile-0.46 | pyh9f0ad1d_1 32 KB conda-forge openssl-1.1.1h | h516909a_0 2.1 MB conda-forge pillow-5.4.1 |py37h00a061d_1000 605 KB conda-forge python_abi-3.7 | 1_cp37m 4 KB conda-forge pytorch-1.7.0 | py3.7_cpu_0 59.5 MB pytorch torchvision-0.8.1 | py37_cpu 17.2 MB pytorch typing_extensions-3.7.4.3 | py_0 25 KB conda-forge ------------------------------------------------------------ Total: 233.3 MB The following NEW packages will be INSTALLED: blas pkgs/main/linux-64::blas-1.0-mkl cpuonly pytorch/noarch::cpuonly-1.0-0 freetype conda-forge/linux-64::freetype-2.10.4-h7ca028e_0 intel-openmp pkgs/main/linux-64::intel-openmp-2020.2-254 jpeg conda-forge/linux-64::jpeg-9d-h36c2ea0_0 libblas conda-forge/linux-64::libblas-3.9.0-1_h6e990d7_netlib libcblas conda-forge/linux-64::libcblas-3.9.0-3_h893e4fe_netlib libgfortran-ng conda-forge/linux-64::libgfortran-ng-7.5.0-hae1eefd_17 libgfortran4 conda-forge/linux-64::libgfortran4-7.5.0-hae1eefd_17 liblapack conda-forge/linux-64::liblapack-3.9.0-3_h893e4fe_netlib libpng conda-forge/linux-64::libpng-1.6.37-h21135ba_2 libtiff conda-forge/linux-64::libtiff-4.0.9-h648cc4a_1002 libuv conda-forge/linux-64::libuv-1.40.0-hd18ef5c_0 mkl pkgs/main/linux-64::mkl-2020.2-256 ninja conda-forge/linux-64::ninja-1.10.1-hfc4b9b4_2 numpy conda-forge/linux-64::numpy-1.19.4-py37h7e9df27_1 olefile conda-forge/noarch::olefile-0.46-pyh9f0ad1d_1 pillow conda-forge/linux-64::pillow-5.4.1-py37h00a061d_1000 python_abi conda-forge/linux-64::python_abi-3.7-1_cp37m pytorch pytorch/linux-64::pytorch-1.7.0-py3.7_cpu_0 torchvision pytorch/linux-64::torchvision-0.8.1-py37_cpu typing_extensions conda-forge/noarch::typing_extensions-3.7.4.3-py_0 The following packages will be UPDATED: ca-certificates 2019.6.16-hecc5488_0 --> 2020.6.20-hecda079_0 certifi 2019.6.16-py37_1 --> 2020.6.20-py37he5f6b98_2 openssl 1.1.1c-h516909a_0 --> 1.1.1h-h516909a_0 Downloading and Extracting Packages intel-openmp-2020.2 | 786 KB | ##################################### | 100% python_abi-3.7 | 4 KB | ##################################### | 100% libpng-1.6.37 | 306 KB | ##################################### | 100% libgfortran4-7.5.0 | 1.3 MB | ##################################### | 100% libblas-3.9.0 | 176 KB | ##################################### | 100% libtiff-4.0.9 | 566 KB | ##################################### | 100% pytorch-1.7.0 | 59.5 MB | ##################################### | 100% olefile-0.46 | 32 KB | ##################################### | 100% typing_extensions-3. | 25 KB | ##################################### | 100% libcblas-3.9.0 | 54 KB | ##################################### | 100% numpy-1.19.4 | 5.2 MB | ##################################### | 100% mkl-2020.2 | 138.3 MB | ##################################### | 100% ninja-1.10.1 | 1.9 MB | ##################################### | 100% libuv-1.40.0 | 920 KB | ##################################### | 100% pillow-5.4.1 | 605 KB | ##################################### | 100% blas-1.0 | 6 KB | ##################################### | 100% jpeg-9d | 264 KB | ##################################### | 100% openssl-1.1.1h | 2.1 MB | ##################################### | 100% torchvision-0.8.1 | 17.2 MB | ##################################### | 100% ca-certificates-2020 | 145 KB | ##################################### | 100% libgfortran-ng-7.5.0 | 22 KB | ##################################### | 100% certifi-2020.6.20 | 151 KB | ##################################### | 100% cpuonly-1.0 | 2 KB | ##################################### | 100% liblapack-3.9.0 | 2.9 MB | ##################################### | 100% freetype-2.10.4 | 912 KB | ##################################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done