0% found this document useful (0 votes)
54 views5 pages

Pink

This document discusses training a classifier on the CIFAR10 dataset using PyTorch. It loads and normalizes the CIFAR10 training and test datasets using torchvision. It then defines a convolutional neural network, loss function, trains the network on the training data, and tests it on the test data. It shows examples of loading and transforming the CIFAR10 data into tensors for use in PyTorch.

Uploaded by

Hua-Chien Chang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views5 pages

Pink

This document discusses training a classifier on the CIFAR10 dataset using PyTorch. It loads and normalizes the CIFAR10 training and test datasets using torchvision. It then defines a convolutional neural network, loss function, trains the network on the training data, and tests it on the test data. It shows examples of loading and transforming the CIFAR10 data into tensors for use in PyTorch.

Uploaded by

Hua-Chien Chang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

id,label

0,0
1,4
2,4
3,2
jal x1, T_N # call T(n')=T(n//2)

slli x8, x8, 2 # 4T(n//2)


add x8, x8, x7
add x8, x8, x7
addi sp, sp, 24
lw x1, 0(sp)
lw x10, 8(sp)
lw x7, 16(sp)

jalr x0, 0(x1)

################################################################################

__start:
# Prints msg0
addi a0, x0, 4
la a1, msg0
ecall
# Prints msg1
addi a0, x0, 4
23,4
24,6
25,1
26,8
27,2
jal x1, T_N # call T(n')=T(n//2)

slli x8, x8, 2 # 4T(n//2)


add x8, x8, x7
add x8, x8, x7
addi sp, sp, 24
lw x1, 0(sp)
lw x10, 8(sp)
lw x7, 16(sp)

jalr x0, 0(x1)

################################################################################

__start:
# Prints msg0
addi a0, x0, 4
la a1, msg0
ecall
# Prints msg1
addi a0, x0, 4
46,3
47,5
48,3
49,3
50,5
51,1
52,6
53,6
54,9
55,9
56,3
57,7
58,3
59,4
60,6
jal x1, T_N # call T(n')=T(n//2)

slli x8, x8, 2 # 4T(n//2)


add x8, x8, x7
add x8, x8, x7
addi sp, sp, 24
lw x1, 0(sp)
lw x10, 8(sp)
lw x7, 16(sp)

jalr x0, 0(x1)

################################################################################

__start:
# Prints msg0
addi a0, x0, 4
la a1, msg0
ecall
# Prints msg1
addi a0, x0, 4
78,5
79,3
80,3
81,9
82,2
83,9
84,0
85,7
86,9
87,7
88,9
89,6
90,3
91,2
92,9
93,8
94,1
95,9
213202302230..153615156

123
123
02
30
.
0.
0
.
0.
jal x1, T_N # call T(n')=T(n//2)
slli x8, x8, 2 # 4T(n//2)
add x8, x8, x7
add x8, x8, x7
addi sp, sp, 24
lw x1, TRAINING A CLASSIFIER
This is it. You have seen how to define neural networks, compute loss and make
updates to the weights of the network.

Now you might be thinking,

What about data?


Generally, when you have to deal with image, text, audio or video data, you can use
standard python packages that load data into a numpy array. Then you can convert
this array into a torch.*Tensor.

For images, packages such as Pillow, OpenCV are useful


For audio, packages such as scipy and librosa
For text, either raw Python or Cython based loading, or NLTK and SpaCy are useful
Specifically for vision, we have created a package called torchvision, that has
data loaders for common datasets such as Imagenet, CIFAR10, MNIST, etc. and data
transformers for images, viz., torchvision.datasets and
torch.utils.data.DataLoader.

This provides a huge convenience and avoids writing boilerplate code.

For this tutorial, we will use the CIFAR10 dataset. It has the classes: ‘airplane’,
‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. The
images in CIFAR-10 are of size 3x32x32, i.e. 3-channel color images of 32x32 pixels
in size.

cifar10
cifar10

Training an image classifier


We will do the following steps in order:

Load and normalizing the CIFAR10 training and test datasets using torchvision
Define a Convolutional Neural Network
Define a loss function
Train the network on the training data
Test the network on the test data
1. Loading and normalizing CIFAR10
Using torchvision, it’s extremely easy to load CIFAR10.

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]. .. note:

If running on Windows and you get a BrokenPipeError, try setting


the num_worker of torch.utils.data.DataLoader() to 0.
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,


download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,


download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',


'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
Out:

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to
./data/cifar-10-python.tar.gz
Extracting ./data/cifar-10-python.tar.gz to ./data
Files already downloaded and verified
Let us show some of the training images, for fun.

import matplotlib.pyplot as plt


import numpy as np

# functions to show an image

def imshow(img):
img = img / 2 + 0.5 # unnormalize
lw x1, 0(sp)
lw x10, 8(sp)
lw x7, 16(sp)

jalr x0, 0(x1)

################################################################################

__start:TRAINING A CLASSIFIER
This is it. You have seen how to define neural networks, compute loss and make
updates to the weights of the network.

Now you might be thinking,

What about data?


Generally, when you have to deal with image, text, audio or video data, you can use
standard python packages that load data into a numpy array. Then you can convert
this array into a torch.*Tensor.

For images, packages such as Pillow, OpenCV are useful


For audio, packages such as scipy and librosa
For text, either raw Python or Cython based loading, or NLTK and SpaCy are useful
Specifically for vision, we have created a package called torchvision, that has
data loaders for common datasets such as Imagenet, CIFAR10, MNIST, etc. and data
transformers for images, viz., torchvision.datasets and
torch.utils.data.DataLoader.

This provides a huge convenience and avoids writing boilerplate code.

For this tutorial, we will use the CIFAR10 dataset. It has the classes: ‘airplane’,
‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. The
images in CIFAR-10 are of size 3x32x32, i.e. 3-channel color images of 32x32 pixels
in size.
cifar10
cifar10

Training an image classifier


We will do the following steps in order:

Load and normalizing the CIFAR10 training and test datasets using torchvision
Define a Convolutional Neural Network
Define a loss function
Train the network on the training data
Test the network on the test data
1. Loading and normalizing CIFAR10
Using torchvision, it’s extremely easy to load CIFAR10.

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]. .. note:

If running on Windows and you get a BrokenPipeError, try setting


the num_worker of torch.utils.data.DataLoader() to 0.
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,


download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,


download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',


'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
Out:

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to
./data/cifar-10-python.tar.gz
Extracting ./data/cifar-10-python.tar.gz to ./data
Files already downloaded and verified
Let us show some of the training images, for fun.

import matplotlib.pyplot as plt


import numpy as np

# functions to show an image

def imshow(img):
img = img / 2 + 0.5 # unnormalize

You might also like