cs5670: computer vision - cornell university · 2018. 4. 12. · k-nearest neighbors: summary •in...

51
Image Classification CS5670: Computer Vision Noah Snavely Slides from Fei-Fei Li, Justin Johnson, Serena Yeung http://vision.stanford.edu/teaching/cs231n/

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Image Classification

CS5670: Computer VisionNoah Snavely

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

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

Page 2: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Announcements

• Project 4 to be released shortly

• Vote for Project 3 artifacts!

– Deadline: midnight tonight

Page 3: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Today

• Image classification pipeline

• Training, validation, testing

• Nearest neighbor classification

• Linear classification

• Score function and loss function

• Building up to CNNs for learning– Next 2-4 lectures on deep learning

Page 4: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Image Classification: A core task in Computer Vision

• Assume given set of discrete labels, e.g. {cat, dog, cow, apple, tomato, truck, … }

Page 5: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Image classification demo

https://cloud.google.com/vision/See also: https://aws.amazon.com/rekognition/https://www.clarifai.com/https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/

Page 6: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Image Classification

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 7: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Image Classification: Problem

Page 8: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 9: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Slide credit: Fei-Fei Li & Justin Johnson & Serena Yeung

Page 10: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Data-driven approach

• Collect a database of images with labels

• Use ML to train an image classifier

• Evaluate the classifier on test images

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 11: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Data-driven approach

• Collect a database of images with labels

• Use ML to train an image classifier

• Evaluate the classifier on test images

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 12: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 13: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 14: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Classifiers

• Nearest Neighbor

• kNN (“k-Nearest Neighbors”)

• Linear Classifier

• SVM (Support Vector Machine)

• …

Page 15: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

First: Nearest Neighbor (NN) Classifier

• Train

– Remember all training images and their labels

• Predict

– Find the closest (most similar) training image

– Predict its label as the true label

Page 16: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

CIFAR-10 and NN results

Page 17: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

CIFAR-10 and NN results

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 18: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

k-nearest neighbor

• Find the k closest points from training data

• Take majority vote from K closest points

Page 19: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 20: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 21: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

How to find the most similar training image? What is the distance metric?

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 22: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Choice of distance metric

• Hyperparameter

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 23: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 24: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Demo: http://vision.stanford.edu/teaching/cs231n-demos/knn/

Page 25: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Visualization: L2 distance

Page 26: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Hyperparameters

• What is the best distance to use?

• What is the best value of k to use?

• These are hyperparameters: choices about the algorithm that we set rather than learn

• How do we set them?

– One option: try them all and see what works best

Page 27: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 28: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 29: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 30: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 31: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 32: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 33: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 34: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Recap: How to pick hyperparameters?

• Methodology

– Train and test

– Train, validate, test

• Train for original model

• Validate to find hyperparameters

• Test to understand generalizability

Page 35: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

kNN -- Complexity and Storage

• N training images, M test images

• Training: O(1)

• Testing: O(MN)

• Hmm…

– Normally need the opposite

– Slow training (ok), fast testing (necessary)

Page 36: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 37: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

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

• The K-Nearest Neighbors classifier predicts labels based on nearest training examples

• Distance metric and K are hyperparameters

• Choose hyperparameters using the validation set; only run on the test set once at the very end!

Page 38: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Linear classifiers

Page 39: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Score function

Slides from Andrej Karpathy and Fei-Fei Li

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

Page 40: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Score function: f

Page 41: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Parametric approach: Linear classifier

Page 42: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Parametric approach: Linear classifier

Page 43: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Linear Classifier

Page 44: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 45: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Interpretation: Template matching

Page 46: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

Geometric Interpretation

Page 47: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must

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 will come back to this

later

Page 48: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 49: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 50: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must
Page 51: CS5670: Computer Vision - Cornell University · 2018. 4. 12. · k-Nearest Neighbors: Summary •In image classification we start with a training set of images and labels, and must