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

This tutorial covers the following topics:

  • Downloading an image dataset from web URL
  • Understanding convolution and pooling layers
  • Creating a convolutional neural network (CNN) using PyTorch
  • Training a CNN from scratch and monitoring performance
  • Underfitting, overfitting and how to overcome them

Exploring the CIFAR100 Dataset

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 CIFAR100 dataset, which consists of 60000 32x32 px colour images in 100 classes. Here are some sample images from the dataset:

alt
# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder / Windows (No GPU)
# !pip install numpy matplotlib torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Linux / Windows (GPU)
# pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
 
# MacOS (NO GPU)
# !pip install numpy matplotlib torch torchvision torchaudio
# to get URL from fastai
# !pip install -Uqq fastai
# from fastai.vision.all import *


# type "URLs."" then select the dataset from the dropdown to get URL links for the datasets below
# URLs.FOOD
# URLs.CUB_200_2011
# URLs.CALTECH_101
# URLs.IMAGENETTE

# other datasets from fastai
#dataset_url = "https://s3.amazonaws.com/fast-ai-imageclas/food-101.tgz"
#dataset_url = "https://s3.amazonaws.com/fast-ai-imageclas/CUB_200_2011.tgz"
#dataset_url = "https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz"
#dataset_url = "https://s3.amazonaws.com/fast-ai-imageclas/caltech_101.tgz"
#import os
import torch
import torchvision
#import tarfile
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torchvision.datasets import CIFAR100
#from torchvision.datasets.utils import download_url
#from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor
from torch.utils.data import random_split
from torchvision.utils import make_grid
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

matplotlib.rcParams['figure.facecolor'] = '#ffffff'

#from torch.utils.data.dataloader import DataLoader