cs5670: computer vision - cornell university · 2018-04-16 · announcements •project 4 (stereo)...

60
Loss functions for image classification CS5670: Computer Vision Noah Snavely Slides adapted from Fei-Fei Li, Justin Johnson, Serena Yeung http://vision.stanford.edu/teaching/cs231n/

Upload: others

Post on 21-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Loss functions for image classification

CS5670: Computer VisionNoah Snavely

Slides adapted from Fei-Fei Li, Justin Johnson, Serena Yeung

http://vision.stanford.edu/teaching/cs231n/

Page 2: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Readings

• Image classification:

– http://cs231n.github.io/classification/

• Linear classification and loss functions:

– http://cs231n.github.io/linear-classify/

• Optimization

– http://cs231n.github.io/optimization-1/

– http://cs231n.github.io/optimization-2/

Page 3: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Announcements

• Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm

– To be done in groups of two

• Project 3 voting results

Page 4: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Third Place

Page 5: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Arpit Sheth and John Draikiwicz

Page 6: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Second Place

Page 7: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Elena Zhizhimontova and Jared Wong

Page 8: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

First Place

Page 9: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Sarah Le Cam and Yunie Mao

Page 10: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Recap

• Image classification

• Difficult to hand-code, so we learn from data

• Train on training data, set hyperparameters using validation data, (at the end) test on test data

{“llama” : 0.93, “car” : 0.01}

Page 11: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Recap

• Learning methods

– k-Nearest Neighbors

– Linear classification

• Classifier outputs a score function giving a score to each class

• Today: how do we define how good a classifier is based on the training data?

Page 12: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Linear classification

Output scores

Page 13: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 14: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Loss function, cost/objective function

• Given ground truth labels (yi), scores f(xi, W)

– how unhappy are we with the scores?

• Loss function or objective/cost function measures unhappiness

• During training, want to find the parameters W that minimizes the loss function

Page 15: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Simpler example: binary classification

• Two classes (e.g., “cat” and “not cat”)

– AKA “positive” and “negative” classes

cat not cat

Page 16: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Linear classifiers• Find linear function (hyperplane) to separate

positive and negative examples

0:negative

0:positive

b

b

ii

ii

wxx

wxx

Which hyperplane is best?We need a loss function to

decide

Page 17: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

What is a good loss function?

• One possibility

– Number of misclassified examples

– Problems: discrete, can’t break ties

– We want the loss to lead to good generalization

Loss: 2 Loss: 0 Loss: 0

Page 18: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Idea: support vector machines (SVMs)

• Find hyperplane that maximizes the margin between the positive and negative examples

C. Burges, A Tutorial on Support Vector Machines for Pattern Recognition, Data Mining and Knowledge Discovery, 1998

Page 19: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Support vector machines

• Find hyperplane that maximizes the margin between the positive and negative examples

1:1)(negative

1:1)( positive

by

by

iii

iii

wxx

wxx

MarginSupport vectors

Distance between point and hyperplane: ||||

||

w

wx bi

For support vectors, 1 bi wx

Therefore, the margin is 2 / ||w||

Page 20: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Bias Trick

• From now on, we will use W to mean both weights and bias

Page 21: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

What if classes aren’t linearly separable?

• Margin doesn’t exist, so we can’t maximize it!

• Alternative: hinge loss

• Total loss: sum of hinge losses over training set

plot of max(0, 1 - x)Recall: for positive examples

for negative examples

Page 22: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Loss function for more than 2 classes

• Given ground truth labels (yi) and scores f(xi, W)

– how unhappy are you with the scores?

Page 23: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Intuition: generalization of hinge loss

Page 24: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 25: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 26: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 27: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 28: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Loss function: interpretation

Page 29: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 30: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 31: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 32: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 33: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 34: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 35: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Multi-class SVM loss

Page 36: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 37: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 38: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 39: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 40: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 41: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 42: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 43: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 44: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 45: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 46: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 47: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 48: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 49: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 50: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 51: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two
Page 52: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Summary

• Have score function and loss function

– Will generalize the score function

• Find W and b to minimize loss

– Minimize loss using gradient descent

• Next: CNNs

Page 53: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Summary

Page 54: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Other loss functions

• Scores are not very intuitive

• Softmax classifier

– Score function is same

– Intuitive output: normalized class probabilities

– Extension of logistic regression to multiple classes

Page 55: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Softmax classifier

Interpretation: squashes values into range 0 to 1

Page 56: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Cross-entropy loss

Page 57: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Aside: Loss function interpretation

• Probability– Maximum Likelihood Estimation (MLE)

– Regularization is Maximum a posteriori (MAP) estimation

• Cross-entropy H– p is true distribution (1 for the correct class), q is

estimated

– Softmax classifier minimizes cross-entropy

– Minimizes the KL divergence (Kullback-Leibler) between the distribution: distance between p and q

Page 58: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

SVM vs. Softmax

Page 59: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Summary

• Have score function and loss function

– Will generalize the score function

• Find W and b to minimize loss

– SVM vs. Softmax

• Comparable in performance

• SVM satisfies margins, softmax optimizes probabilities

Page 60: CS5670: Computer Vision - Cornell University · 2018-04-16 · Announcements •Project 4 (Stereo) is out, due Thursday, April 26, 2018, by 11:59pm –To be done in groups of two

Next: Gradient Descent