[cs231n] 2. Image Classification - NN, KNN, Linear Classification
Image Classification
์ปดํจํฐ๋ ์ฌ๋์ฒ๋ผ ์ด๋ฏธ์ง ์์ฒด๋ฅผ ์ธ์ํ๋ ๊ฒ์ด ์๋, ์ซ์, ์ฆ ํฝ์ ๊ฐ์ผ๋ก ์ธ์ํ๋ค !
๋ค์ํ ์ด๋ฏธ์ง๋ค์ ๋ถ๋ฅ๋ฅผ ์ํด์ ์๋ง์ ๋ณํ์ ๋ํด์ robustํ ์๊ณ ๋ฆฌ์ฆ์ด ํ์ํ๋ค.
Data-driven Image Classification (๋ฐ์ดํฐ ๊ธฐ๋ฐ ์ด๋ฏธ์ง ๋ถ๋ฅ)
๊ธฐ์กด์ rule-based appraoch๋ ๋ค์ํ ํด๋์ค๋ค์ ์ ์ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค.
ex) ๊ณ ์์ด๋ ๊ท, ๋, ์ฝ๊ฐ '์กด์ฌํ๋ค' -> ์ด๋ฅผ ํตํด ๊ณ ์์ด์์ ํ๋จ
but, ๊ณ ์์ด๋ ์ข ๋ฅ์ ๋ฐ๋ผ ๋ค๋ฅด๋ค.
์ฌ๋์ด ์ง์ ๋ง๋๋ ๊ฒ์ด ์๋, ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ชจ๋ธ์ ๋ง๋ค์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์!
1. Collect a (large numbers of) dataset of images and labels
2. Use Machine Learning to train a classifier
-> ๋ค์ํ ๊ฐ์ฒด ์ธ์ํ ์ ์๋ ๋ชจ๋ธ ๋ง๋ค๊ธฐ
3. Evaluate the classifier on new images
NN (Nearest Neighborhood)
- train data๋ฅผ ์ ์ฅํ ํ, ์์ธก ๋จ๊ณ์์ ํฌ์ ๋ ์ด๋ฏธ์ง์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ์ดํฐ์ ๋ ์ด๋ธ์ ์์ธกํ๋ ๋ฐฉ๋ฒ
1. memorize all data and labels
2. predict the (new images') label to find the most similar training image
์ค๋ฅธ์ชฝ์ ๋ณด๋ฉด train image ์ค test image์ ์ ์ฌํ ์์ผ๋ก ์ ๋ ฌ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
* ๊ทธ๋ ๋ค๋ฉด, ์ด๋ฏธ์ง์ ์ด๋ฏธ์ง์ ๊ฐ๊น์ด ์ ๋(distance)๋ ์ด๋ป๊ฒ ๊ตฌํ ์ ์์๊น?
(+) ๊ฑฐ๋ฆฌ ๊ณ์ฐ์ ๋์ผํ ์์น ๊ฐ์์ rgb๋ฅผ ๋ํ๋ด๋ pixel๊ฐ์ผ๋ก
# L1 ์์
import numpy as np
class NearestNeighbor:
def __init__(self):
pass
# ๋จ์ง Memorize training data (simple)
def train(self, X, y):
""" X is N x D where each row is an example.
Y is 1-dim of size N """
# the nearest neightbor classifier simply remembers all the training data
self.Xtr = X
self.ytr = y
def predict(self, X):
""" X is N x D where each row is an example we wish to predict label for """
num_test = X.shape[0]
# lets make sure that the output type matches the input type
Ypred = np.zeros(num_test, dtype=self.ytr.dtype)
# loop over all test rows
for i in xrange(num_test):
# find the nearest training image to the i'th test image
# using the L1-distance (sum of absolute value differences)
distances = np.sum(np.abs(self.Xtr - X[i, :]), axis=1)
min_index = np.argmin(distances) # get the Index with the smallest distance
Ypred[i] = self.ytr[min_index] # predict the label of teh nearest example
return Ypred
KNN (K-nearest neighbor)
- NN์ ๋จ ํ๋์ label์์๋ง prediction์ ๊ณ ๋ คํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ์ด ๋จ์ด์ง๋ค.
ํ ์คํธ(์์ธก) ๋จ๊ณ์์ input๊ณผ ๊ฐ์ฅ ๊ฐ๊น์ด ์ด์์ ์ด k๊ฐ ์ฐพ๊ณ , ๊ฐ์ฅ ๋น๋ฒํ๊ฒ ๋์จ ๋ ์ด๋ธ๋ก ์์ธกํ๋ ๋ฐฉ๋ฒ
(voting: ์ด๋ ๊ฒ ์ฌ๋ฌ ๊ฐ๋ก๋ถํฐ ๊ฐ์ฅ ๋น๋ฒํ ๋์จ ๊ฒ์ (ํฌํ) ์์ธก ๊ฒฐ๊ณผ๋ก ํ๋ ์์ )
KNN์ ์ด์์น์ ๋ ๋๊ฐํ๋ค.
unseen data์ ๋ํ ์ฑ๋ฅ(generalization)์ด ๋ ๋๋ค!
(๋จธ์ ๋ฌ๋์ ๋ชฉ์ : unseen data๋ฅผ ์ฌ๋ฐ๊ฒ ์์ธกํ๋ ๊ฒ)
* ์ต์ ์ ๋ชจ๋ธ์ ์ฐพ๊ธฐ ์ํด์ ?
ํ์ดํผ ํ๋ผ๋ฏธํฐ (hyperparameter)์ ์กฐ์ ํ์!
KNN์์๋ k์ distance function
- ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ๋ฉฐ ๊ฐ์ฅ ์ข์ ๊ฒ ์ฐพ๊ธฐ
but, hyperparameter๋ฅผ ์ฐพ๋๋ฐ test set์ ์ฌ์ฉํ ์ ์์
- test set์ ์ฑ๋ฅ ํ๊ฐํ๋ ๋ถ๋ถ์์๋ง ์ฌ์ฉ๋์ด์ผ ํจ!
- training set์ ์ผ๋ถ๋ฅผ hyperparameter tuning์ ์ํด ๋ถํ ํ์ฌ validation set์ ์์ํ๋ค.
- NN์ ์ด๋ฏธ์ง ๋ถ๋ฅ์ ์ข์ ์ฑ๋ฅ์ ๋ณด์ด์ง ์๋ ๊ฒฝํฅ์ด ์๋ค.
Why?
1. prediction is very slow
2. ํฝ์ ๋ค ๊ฐ์ difference๊ฐ ์ด๋ฏธ์ง์ ์ ์ฌ์ฑ(or ์ฐจ์ด)๋ฅผ ์ ๋ํ๋ด์ง ๋ชปํจ
3. Curse of dimensionality (์ฐจ์์ ์ ์ฃผ)
- ์๊ณ ๋ฆฌ์ฆ์ ์ ๋์ํ๊ฒ ํ๋ ค๋ฉด ๊ธฐํ ๊ธ์์ (์ฐจ์์ ์ ๊ณฑ)์ธ ์์ training data๊ฐ ํ์ํ๋ฐ,
์ค์ ์ด๋ ๋ถ๊ฐ๋ฅํ๊ณ ์ง์์น์ data์ ์ฑ๋ฅ์ ๋งค์ฐ ๋จ์ด์ง
Linear Classifier
Image Classification์ ์์ด KNN๋ณด๋ค ์ข ๋ ๊ฐ๋ ฅํ ์ ๊ทผ๋ฒ
Parametric Approach
f(x, W) = Wx + b
- f์ ์ ๋ ฅ๊ฐ: x, W
- x: ์ ๋ ฅ ๋ฐ์ดํฐ
- W: ํ๋ผ๋ฏธํฐ
- b(bias): unseen data๋ฅผ ๋ถ๋ฅํ๊ธฐ ์ํด ๋์์ ์ค
- ๋ฐ์ดํฐ์ ๋ ๋ฆฝ์ ์ผ๋ก ์กด์ฌ -> input data์ ๋ ๋ฆฝ์ ์ผ๋ก ๋ง๋ฌ
๊ฐ์ฅ simpleํ ๋ฐฉ๋ฒ์ x์ W๋ฅผ ๊ณฑํด์ฃผ๋ ๊ฒ
- ํ๋ ฌ ๊ณฑ์ (๋ด์ )
์์ผ๋ก W(ํ๋ผ๋ฏธํฐ)์ ๊ฐ์ ๊ฐ์ ์์ผ๋๊ฐ๋ฉฐ ์ข์ ์ฑ๋ฅ์ ๋ด๋ ๋ถ๋ฅ๊ธฐ๋ฅผ ๋ง๋ค๋๋ก ํ ๊ฒ์ด๋ค.
Reference
- https://www.youtube.com/watch?v=OoUX-nOEjG0
- https://3months.tistory.com/512
- https://worthpreading.tistory.com/46