Administrative: Piazza
SCPD students: Use your @stanford.edu address to register for Piazza; contact
scpd-customerservice@stanford.edu for help.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 2 April 6, 2017
Administrative: Assignment 1
Out tonight, due 4/18 11:59pm
- K-Nearest Neighbor
- Linear classifiers: SVM, Softmax
- Two-layer neural network
- Image features
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 3 April 6, 2017
Administrative: Python + Numpy
http://cs231n.github.io/python-numpy-tutorial/
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 4 April 6, 2017
Administrative: Google Cloud
http://cs231n.github.io/gce-tutorial/
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 5 April 6, 2017
Image Classification: A core task in Computer Vision
cat
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 6 April 6, 2017
The Problem: Semantic Gap
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 7 April 6, 2017
Challenges: Viewpoint variation
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 8 April 6, 2017
Challenges: Illumination
This image is CC0 1.0 public domain This image is CC0 1.0 public domain This image is CC0 1.0 public domain This image is CC0 1.0 public domain
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 9 April 6, 2017
Challenges: Deformation
This image by Umberto Salvagnin This image by sare bear is This image by Tom Thai is
This image by Umberto Salvagnin
is licensed under CC-BY 2.0 licensed under CC-BY 2.0 licensed under CC-BY 2.0
is licensed under CC-BY 2.0
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 10 April 6, 2017
Challenges: Occlusion
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 11 April 6, 2017
Challenges: Background Clutter
This image is CC0 1.0 public domain This image is CC0 1.0 public domain
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 12 April 6, 2017
Challenges: Intraclass variation
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 13 April 6, 2017
An image classifier
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 14 April 6, 2017
Attempts have been made
?
John Canny, A Computational Approach to Edge Detection, IEEE TPAMI 1986
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 15 April 6, 2017
Data-Driven Approach
1. Collect a dataset of images and labels
2. Use Machine Learning to train a classifier
3. Evaluate the classifier on new images
Example training set
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 16 April 6, 2017
First classifier: Nearest Neighbor
Memorize all
data and labels
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 17 April 6, 2017
Example Dataset: CIFAR10
10 classes
50,000 training images
10,000 testing images
Alex Krizhevsky, Learning Multiple Layers of Features from Tiny Images, Technical Report, 2009.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 18 April 6, 2017
Example Dataset: CIFAR10
10 classes
50,000 training images
10,000 testing images Test images and nearest neighbors
Alex Krizhevsky, Learning Multiple Layers of Features from Tiny Images, Technical Report, 2009.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 19 April 6, 2017
Distance Metric to compare images
L1 distance:
add
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 20 April 6, 2017
Nearest Neighbor classifier
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 21 April 6, 2017
Nearest Neighbor classifier
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 22 April 6, 2017
Nearest Neighbor classifier
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 23 April 6, 2017
Nearest Neighbor classifier
Q: With N examples,
how fast are training
and prediction?
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 24 April 6, 2017
Nearest Neighbor classifier
Q: With N examples,
how fast are training
and prediction?
A: Train O(1),
predict O(N)
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 25 April 6, 2017
Nearest Neighbor classifier
Q: With N examples,
how fast are training
and prediction?
A: Train O(1),
predict O(N)
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 26 April 6, 2017
What does this look like?
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 27 April 6, 2017
K-Nearest Neighbors
Instead of copying label from nearest neighbor,
take majority vote from K closest points
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 28 April 6, 2017
What does this look like?
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 29 April 6, 2017
What does this look like?
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 30 April 6, 2017
K-Nearest Neighbors: Distance Metric
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 31 April 6, 2017
K-Nearest Neighbors: Distance Metric
K=1 K=1
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 32 April 6, 2017
K-Nearest Neighbors: Demo Time
http://vision.stanford.edu/teaching/cs231n-demos/knn/
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 33 April 6, 2017
Hyperparameters
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 34 April 6, 2017
Hyperparameters
Very problem-dependent.
Must try them all out and see what works best.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 35 April 6, 2017
Setting Hyperparameters
Idea #1: Choose hyperparameters
that work best on the data
Your Dataset
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 36 April 6, 2017
Setting Hyperparameters
Idea #1: Choose hyperparameters BAD: K = 1 always works
that work best on the data perfectly on training data
Your Dataset
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 37 April 6, 2017
Setting Hyperparameters
Idea #1: Choose hyperparameters BAD: K = 1 always works
that work best on the data perfectly on training data
Your Dataset
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 38 April 6, 2017
Setting Hyperparameters
Idea #1: Choose hyperparameters BAD: K = 1 always works
that work best on the data perfectly on training data
Your Dataset
Idea #2: Split data into train and test, choose BAD: No idea how algorithm
hyperparameters that work best on test data will perform on new data
train test
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 39 April 6, 2017
Setting Hyperparameters
Idea #1: Choose hyperparameters BAD: K = 1 always works
that work best on the data perfectly on training data
Your Dataset
Idea #2: Split data into train and test, choose BAD: No idea how algorithm
hyperparameters that work best on test data will perform on new data
train test
Idea #3: Split data into train, val, and test; choose Better!
hyperparameters on val and evaluate on test
train validation test
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 40 April 6, 2017
Setting Hyperparameters
Your Dataset
Useful for small datasets, but not used too frequently in deep learning
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 41 April 6, 2017
Setting Hyperparameters Example of
5-fold cross-validation
for the value of k.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 42 April 6, 2017
k-Nearest Neighbor on images never used.
Original image is
CC0 public domain (all 3 images have same L2 distance to the one on the left)
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 43 April 6, 2017
k-Nearest Neighbor on images never used.
Dimensions = 3
- Curse of dimensionality Points = 43
Dimensions = 2
Points = 42
Dimensions = 1
Points = 4
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 44 April 6, 2017
K-Nearest Neighbors: Summary
In Image classification we start with a training set of images and labels, and
must predict labels on the test set
Choose hyperparameters using the validation set; only run on the test set once at
the very end!
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 45 April 6, 2017
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 46 April 6, 2017
Neural Network
Linear
classifiers
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 47 April 6, 2017
Two young girls are Boy is doing backflip
playing with lego toy. on wakeboard
Construction worker in
Man in black shirt orange safety vest is
is playing guitar. working on road.
Karpathy and Fei-Fei, Deep Visual-Semantic Alignments for Generating Image Descriptions, CVPR 2015
Figures copyright IEEE, 2015. Reproduced for educational purposes.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 48 April 6, 2017
Two young girls are Boy is doing backflip
playing with lego toy. on wakeboard
Construction worker in
Man in black shirt orange safety vest is
is playing guitar. working on road.
Karpathy and Fei-Fei, Deep Visual-Semantic Alignments for Generating Image Descriptions, CVPR 2015
Figures copyright IEEE, 2015. Reproduced for educational purposes.
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 49 April 6, 2017
Recall CIFAR10
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 50 April 6, 2017
Parametric Approach
Image
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 51 April 6, 2017
Parametric Approach: Linear Classifier
Image
f(x,W) = Wx
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 52 April 6, 2017
Parametric Approach: Linear Classifier
3072x1
Image
f(x,W) = Wx
10x1 10x3072
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 53 April 6, 2017
Parametric Approach: Linear Classifier
3072x1
Image
f(x,W) = Wx + b 10x1
10x1 10x3072
10 numbers giving
f(x,W) class scores
Array of 32x32x3 numbers
(3072 numbers total)
W
parameters
or weights
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 54 April 6, 2017
Example with an image with 4 pixels, and 3 classes (cat/dog/ship)
56
0.2 -0.5 0.1 2.0 1.1 -96.8 Cat score
56 231
231
24 2
1.5 1.3 2.1 0.0
24
+ 3.2
= 437.9 Dog score
f(x,W) = Wx + b
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 56 April 6, 2017
Interpreting a Linear Classifier
f(x,W) = Wx + b
Example trained weights
of a linear classifier
trained on CIFAR-10:
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 57 April 6, 2017
Interpreting a Linear Classifier
f(x,W) = Wx + b
Plot created using Wolfram Cloud Cat image by Nikita is licensed under CC-BY 2.0
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 58 April 6, 2017
Hard cases for a linear classifier
Class 1: Class 1: Class 1:
number of pixels > 0 odd 1 <= L2 norm <= 2 Three modes
Class 2: Class 2: Class 2:
number of pixels > 0 even Everything else Everything else
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 59 April 6, 2017
So far: Defined a (linear) score function f(x,W) = Wx + b
Example class
scores for 3
images for
-3.45 -0.51 3.42
some W: -8.87 6.04 4.64
0.09 5.31 2.65
2.9 -4.22 5.1
How can we tell 4.48 -4.19 2.64
whether this W 8.02 3.58 5.55
3.78 4.49 -4.34
is good or bad? 1.06 -4.37 -1.5
Cat image by Nikita is licensed under CC-BY 2.0
-0.36 -2.09 -4.79
Car image is CC0 1.0 public domain
Frog image is in the public domain -0.72 -2.93 6.14
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 60 April 6, 2017
f(x,W) = Wx + b
Coming up:
(quantifying what it means to
- Loss function have a good W)
(start with random W and find a
- Optimization W that minimizes the loss)
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 2 - 61 April 6, 2017