93
CV & ML Stanford University 04-Dec-2018 1 Lecture: Computer Vision and Machine Learning Shubhang Desai, Ranjay Krishna, and Juan Carlos Niebles Stanford Vision and Learning Lab

Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

1

Lecture:ComputerVisionandMachineLearning

ShubhangDesai,Ranjay Krishna,andJuanCarlosNieblesStanfordVisionandLearningLab

Page 2: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

2

Today’sagenda

• Reviewofconvolutionsandclassification• Creatingaconvolution-basedclassifier• Overviewofmachinelearning– Neuralnetworks– Gradientdescent– Backprop

• Ourclassifier’sperformance

Page 3: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

3

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

? ?

? ?

* =

f [n,m] ⇤ h[n,m]

Page 4: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

4

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

133 ?

? ?

* =

f [n,m] ⇤ h[n,m]

Page 5: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

5

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

133 75

? ?

* =

f [n,m] ⇤ h[n,m]

Page 6: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

6

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

133 75

100 ?

* =

f [n,m] ⇤ h[n,m]

Page 7: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

7

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

133 75

100 101

* =

f [n,m] ⇤ h[n,m]

Page 8: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

8

Recallconvolutions…

12 3 19

25 10 1

9 7 17

1 2

3 4

133 75

100 101

* =

f [n,m] ⇤ h[n,m]

Page 9: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

9

Recallconvolutions…

1 2

3 4

?* =

12 21

18 31

f [n,m] ⇤ h[n,m]

Page 10: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

10

Recallconvolutions…

1 2

3 4

232* =

12 21

18 31

f [n,m] ⇤ h[n,m]

Page 11: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

11

Whytheyareuseful

Allowustofindinterestinginsights/features fromimages!

0 -½ 0

0 0 0

0 ½ 0

* =

Page 12: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

12

RecallImageClassification…

Classifier “pupper”

Allowustousefeaturestoputimagesincategories!

Featurizer

Page 13: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

13

WaitaMinute…

Convolution=Image->Features

ClassificationAlgorithm=Features->Category

Page 14: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

14

WaitaMinute…

Convolution=Image->Features

ClassificationAlgorithm=Features->Category

Let’sput‘em together!

Page 15: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

15

InSpecific…

Let’sbuildaconvolution-based classificationalgorithmfortheCIFAR-10dataset(10classes,32x32images):

Page 16: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

16

FeatureExtractor

*32x32“Airplane

Filter” =

Page 17: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

17

FeatureExtractor

*32x32“Airplane

Filter” =

“probability”oftheimagebeingan

airplane

Page 18: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

18

FeatureExtractor

*32x32“Airplane

Filter” =

“probability”oftheimagebeingan

airplane”

Page 19: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

19

FeatureExtractor

*32x32“Airplane

Filter” =

“probability”oftheimagebeingan

airplane”

Thisisnotreallyaprobabilitybutascore,becauseitcanbelessthan0andgreaterthan1

Page 20: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

20

FeatureExtractor

*32x32

“AutomobileFilter”

=

“probability”oftheimagebeingan

automobile

Page 21: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

21

FeatureExtractor

*32x32“Bird

Filter” =

“probability”oftheimagebeinga

bird

Page 22: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

22

FeatureExtractor

*32x32“Truck

Filter” =

“probability”oftheimagebeinga

truck

Page 23: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

23

Classifier

𝑐"#$% = argmax()

Page 24: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

24

Classifier

Wepredicttheclassthathasthehighestprobability!

𝑐"#$% = argmax()

Page 25: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

25

TheWholeShebang

Image FeatureExtractor

Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

Page 26: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

26

TheWholeShebang

Image FeatureExtractor

Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

Page 27: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

27

TheWholeShebang

Image FeatureExtractor

Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

Page 28: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

28

Reframingconvolution

1 2

3 4

*

12 21

18 31

Page 29: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

29

Reframingconvolution

1 2

3 4

* =

12 21

18 31

12211831

⋅1234

Page 30: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

30

ReframedFeatureExtractor

*32x32“Airplane

Filter”

Page 31: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

31

ReframedFeatureExtractor

*32x32“Airplane

Filter” =

ImageVector

AirplaneWeightVector

.

Page 32: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

32

NewFeatureExtractorImageVector

AirplaneWeightVector

.=

“probability”oftheimagebeingan

airplane

Page 33: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

33

NewFeatureExtractorImageVector

AutomobileWeightVector

.=

“probability”oftheimagebeingan

automobile

Page 34: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

34

NewFeatureExtractorImageVector

AutomobileWeightVector

.=

“probability”oftheimagebeinga

bird

Page 35: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

35

NewFeatureExtractorImageVector

AutomobileWeightVector

.=

“probability”oftheimagebeinga

truck

Page 36: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

36

NewFeatureExtractor

=X

WeightMatrix

ImageVector

Page 37: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

37

NewFeatureExtractor

𝑊𝑥 = 𝑦0

𝑊: the(10x1024)matrixofweightvectors

𝑥: the(1024x1)imagevector

𝑦0: the(10x1)vectorofclass“probabilities”

Page 38: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

38

NewFeatureExtractor

𝑊𝑥 = 𝑦0

𝑊: the(10x1024)matrixofweightvectors

𝑥: the(1024x1)imagevector

𝑦0: the(10x1)vectorofclass“probabilities”

Thissimplecomputationiscalledafully-connectedlayer!

Page 39: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

39

Aside:Fully-ConnectedNeuralNetworks

Height

Width

Num_Legs

Algorithm

Dog

NotDog

𝑦0𝑥

Page 40: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

40

Aside:Fully-ConnectedNeuralNetworks

Height

Width

Num_Legs

Dog

NotDog

𝑦0𝑥

Page 41: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

41

Aside:Fully-ConnectedNeuralNetworks

Height

Width

Num_Legs

𝑤?

Dog

NotDog

𝑤@

𝑤A𝑤B

𝑤C

𝑤D

𝑦0𝑥

Page 42: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

42

Aside:Fully-ConnectedNeuralNetworks

𝑤A 𝑤@ 𝑤?𝑤B 𝑤C 𝑤D ⋅ =

𝑦0𝑥𝑊

𝑊𝑥 = 𝑦0

Page 43: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

43

Aside:Fully-ConnectedNeuralNetworks

”Fully-Connected” ”NeuralNetwork”

Everynodeisconnectedtoeveryothernode

Kinda lookslikeaneuron!

Page 44: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

44

NewFeatureExtractor

𝑊𝑥 = 𝑦0

𝑊: the(10x1024)matrixofweightvectors

𝑥: the(1024x1)imagevector

𝑦0: the(10x1)vectorofclass“probabilities”

Page 45: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

45

NewFeatureExtractor

𝑊𝑥 = 𝑦0

𝑊: the(10x1024)matrixofweightvectors

𝑥: the(1024x1)imagevector

𝑦0: the(10x1)vectorofclass“probabilities”?

Page 46: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

46

ClassProbabilityVector

• Musthavevaluesbetween0and1

• Mustsumto1

• There’snoguaranteeeitherrequirementissatisfied!

𝑦0 = 𝑊𝑥

Page 47: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

47

Softmax Function

1

-3

𝑎

Softmax:𝑎 𝑥 E =$FG

∑ $FI�I

Page 48: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

48

Softmax Function

1

-3

𝑎

Softmax:𝑎 𝑥 E =$FG

∑ $FI�I

0.98

0.02

𝑆𝑀(𝑎)

Page 49: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

49

ClassProbabilityVector

• Musthavevaluesbetween0and1

• Mustsumto1

𝑦0 = 𝑊𝑥

Page 50: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

50

ClassProbabilityVector

• Musthavevaluesbetween0and1

• Mustsumto1

𝑦0 = 𝑆𝑀(𝑊𝑥)

Page 51: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

51

Systemsofar…

• Featureextractor:

• Classifier:

𝑐"#$% = argmax(𝑦0)

y0 = 𝑆𝑀(𝑊𝑥)

Page 52: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

52

Systemsofar…

• Featureextractor:

• Classifier:

𝑐"#$% = argmax(𝑦0)

y0 = 𝑆𝑀(𝑊𝑥)

Page 53: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

53

Systemsofar…

• Featureextractor:

• Classifier:

𝑐"#$% = argmax(𝑦0)

y0 = 𝑆𝑀(𝑊𝑥)

Page 54: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

54

Usingthelabel

Let’scompareourpredictionwiththerealanswer!Foreachimage,wehavethelabel𝑦 whichtellsusthetrueclass:

0000010000

Xy

Dogclassindex

Page 55: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

55

KeyInsight:

Wewant:

argmax 𝑦0 = argmax 𝑦

Page 56: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

56

KeyInsight:

Wewant:

argmax 𝑦0 = argmax 𝑦

Whichwecanaccomplishby:

𝑊∗ = argminR

−Tlog(𝑝X)�

Y,[

Page 57: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

57

KeyInsight:

Wewant:

argmax 𝑦0 = argmax 𝑦

Whichwecanaccomplishby:

𝑊∗ = argminR

−Tlog(𝑝X)�

Y,[Where𝑝X istheprobabilityofthetrueclassin𝑦0

Page 58: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

58

Cross-EntropyLoss

Ourlossfunctionrepresentshowbadwearecurrentlydoing:

𝐿 = −log(𝑝X)

Page 59: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

59

Cross-EntropyLoss

Ourlossfunctionrepresentshowbadwearecurrentlydoing:

𝐿 = −log(𝑝X)

Examples:

𝑝X = 0 → 𝐿 = − log 0 = ∞𝑝X = 0.1 → 𝐿 = − log 0.1 = 2.3𝑝X = 0.9 → 𝐿 = − log 0.9 = 0.1

𝑝X = 1 → 𝐿 = − log 1 = 0

Page 60: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

60

Cross-EntropyLoss

Ourlossfunctionrepresentshowbadwearecurrentlydoing:

𝐿 = −log(𝑝X)

Examples:

𝑝X = 0 → 𝐿 = − log 0 = ∞𝑝X = 0.1 → 𝐿 = − log 0.1 = 2.3𝑝X = 0.9 → 𝐿 = − log 0.9 = 0.1

𝑝X = 1 → 𝐿 = − log 1 = 0

Thelargertheloss,theworseourprediction.WewanttominimizeL!

Page 61: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

61

MinimizingLoss

𝑤

𝐿

Page 62: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

62

MinimizingLoss

𝑤

𝐿

Page 63: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

63

MinimizingLoss

𝑤

𝐿

Page 64: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

64

MinimizingLoss

𝑤

𝐿

Page 65: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

65

MinimizingLoss

𝑤

𝐿

Page 66: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

66

MinimizingLoss

𝑤

𝐿

Page 67: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

67

MinimizingLoss

𝑤

𝐿

Page 68: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

68

MinimizingLoss

𝑤

𝐿

Page 69: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

69

MinimizingLoss

𝑤

𝐿

Page 70: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

70

GradientDescentPseudocode

for i in {0,…,num_epochs}:for x, y in data:

𝑦0 = 𝑆𝑀 𝑊𝑥𝐿 = 𝐶𝐸 𝑦0, 𝑦%c%R =? ? ?

𝑊 ≔ 𝑊 − 𝛼 %c%R

Page 71: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

71

GettingtheGradient

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

Page 72: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

72

GettingtheGradient

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧 (𝑥)

Page 73: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

73

GettingtheGradient

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 = (𝑆𝑀(𝑧) − 𝑦)(𝑥)

Page 74: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

74

GettingtheGradient

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 = (𝑆𝑀(𝑧) − 𝑦)(𝑥i)

Page 75: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

75

GettingtheGradient

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 = (𝑆𝑀(𝑧) − 𝑦)(𝑥i)

Page 76: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

76

WhatisBackprop?

𝑊

𝑥

𝑦

×

𝑆𝐶𝐸

𝑧

𝐿

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

Page 77: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

77

WhatisBackprop?

𝑊

𝑥

𝑦

×

𝑆𝐶𝐸

𝑧

𝐿

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

Page 78: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

78

WhatisBackprop?

𝑊

𝑥

𝑦

×

𝑆𝐶𝐸

𝑧

𝐿

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

𝑆𝑀(𝑧) − 𝑦

𝑥

Page 79: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

79

WhatisBackprop?

𝑊

𝑥

𝑦

×

𝑆𝐶𝐸

𝑧

𝐿

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

𝑆𝑀(𝑧) − 𝑦

𝑥

Whencomputationsaretreatedasnodes,allderivativesdependonlyoninputstothatnode.

Page 80: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

80

WhatisBackprop?

𝑊

𝑥

𝑦

×

𝑆𝐶𝐸

𝑧

𝐿

𝑧 = 𝑊𝑥𝐿 = 𝑆𝐶𝐸 𝑧, 𝑦

𝑑𝐿𝑑𝑊 =

𝑑𝐿𝑑𝑧

𝑑𝑧𝑑𝑊

𝑆𝑀(𝑧) − 𝑦

𝑥

Whencomputationsaretreatedasnodes,allderivativesdependonlyoninputstothatnode.

cacheX = {x}

cacheSCE = {z, y}

So,wecancachetheinitialcomputationandreuse!

Page 81: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

81

Backprop-FriendlyCode

class FullyConnected:def __init__(self):

self.cache = {}

def forward(self, W, x):self.cache[‘x’] = x

return np.dot(W, x)

def backward(self, dout):x = self.cache[‘x’]

return np.matmul(dout, x.T)

class SCELoss:def __init__(self):

self.cache = {}

def forward(self, z, y):self.cache[‘z’] = zself.cache[‘y’] = y

return tf.sce(z, y)

def backward(self):z = self.cache[‘z’]y = self.cache[‘y’]

return tf.sm(z) - y

Page 82: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

82

GradientDescentPseudocode(Updated)

for i in {0,…,num_epochs}:for x, y in data:

𝑦0 = 𝑆𝑀 𝑊𝑥𝐿 = 𝐶𝐸 𝑦0, 𝑦%c%R =? ? ?

𝑊 ≔ 𝑊 − 𝛼 %c%R

Page 83: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

83

GradientDescentPseudocode(Updated)

for i in {0,…,num_epochs}:for x, y in data:

𝑦0 = 𝑆𝑀 𝑊𝑥𝐿 = 𝐶𝐸 𝑦0, 𝑦%c%R = backprop(L)

𝑊 ≔𝑊 − 𝛼 %c%R

Page 84: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

84

GradientDescentPseudocode(Updated)

for i in {0,…,num_epochs}:for x, y in data:

𝑦0 = 𝑆𝑀 𝑊𝑥𝐿 = 𝐶𝐸 𝑦0, 𝑦%c%R = backprop(L)

𝑊 ≔𝑊 − 𝛼 %c%R

Page 85: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

85

OurClassificationSystem

Image FeatureExtractor

Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

Page 86: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

86

OurClassificationSystem(modified)

InputImage Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

InputLabel

𝑦 𝐶𝐸 𝐿LossValueLossFunction

FeatureExtractor

Page 87: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

87

OurClassificationSystem(modified)

InputImage Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

InputLabel

𝑦 𝐶𝐸 𝐿LossValueLossFunction

FeatureExtractor

Page 88: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

88

OurClassificationSystem(modified)

InputImage Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

InputLabel

𝑦 𝐶𝐸 𝐿LossValueLossFunction

FeatureExtractor

Page 89: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

89

OurClassificationSystem(modified)

InputImage Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

InputLabel

𝑦 𝐶𝐸 𝐿LossValueLossFunction

FeatureExtractor

3)Usinggradientdescent!

Page 90: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

90

OurClassificationSystem(modified)

InputImage Prediction𝑦0 Classifier

𝑎𝑟𝑔𝑚𝑎𝑥 𝑐"#$%

ClassificationOutput

InputLabel

𝑦 𝐶𝐸 𝐿LossValueLossFunction

FeatureExtractor

3)Usinggradientdescent!

Page 91: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

91

OurSystem’sPerformance

• ~40%accuracyonCIFAR-10test– Bestclass:Truck(~60%)–Worstclass:Horse(~16%)

• Checkoutthemodelat:https://tinyurl.com/cifar10

• Whataboutthefilters?Whatdotheylooklike?

Page 92: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

92

VisualizingtheFilters

Page 93: Lecture: Computer Vision and Machine Learningvision.stanford.edu/teaching/cs131_fall1819/files/19_cv_and_ml.pdf · L Stanford University 04-c-2018 11 Why they are useful Allow us

CV & ML

Stanford University

04-Dec-2018

93

NextTime…

Buildingastrongerconvolution-basedfeatureextractor

Historyofdeeplearning+computervision(ConvolutionalNeuralNets!)

ApplicationsofCNNs