Train ConvNet using CIFAR10

Training an image classifier

We will do the following steps in order:

  1. Load and normalizing the CIFAR10 training and test datasets using torchvision

  2. Define a Convolutional Neural Network

  3. Define a loss function

  4. Train the network on the training data

  5. Test the network on the test data

Data Loading

import torch
import torchvision
import torchvision.transforms as transforms

The output of torchvision datasets are PILImage images of range [0, 1]. We transform them to Tensors of normalized range [-1, 1].

Download General Dataset

Use Downloaded General Dataset

Change the option download=False, and set the path (root)where data is stored.

User Defined Dataset

Load and Show images(tensor, color)

Define Model

Define Loss function and Optimization

Train the network

Save model

Test the network

Show some ground truth of test data

Overall accuracy

Evaluate each class

numpy.squeeze() function is used when we want to remove single-dimensional entries from the shape of an array.

Exercise

  • Try increasing the width of your network (argument 2 of

    the first nn.Conv2d, and argument 1 of the second nn.Conv2d

    they need to be the same number), see what kind of speedup you get.

  • Build a MNIST Convnet

Last updated

Was this helpful?