3개의 training 예시와, 3개의 클래스가 존재하고, f(x, W) = Wx를 만족한다.
Loss function
SVM loss
(x_i, y_i) 가 있을 때, x_i가 image이고, y_i가 label일 때
s_ j : 잘못된 label의 score
s_y_i : 제대로 된 label의 score
1 : safety margin
1) 고양이 사진일 때 SVM loss
max(0, 5.1-3.2+1) + max(0, -1.7-3.2+1) = max(0, 2.9) + max(0, -3.9) = 2.9 + 0 = 2.9
2) 자동차 사진일 때 SVM loss
max(0, 1.3-4.9+1) + max(0, 2.0-4.9+1) = max(0, -2.6) + max(0, -1.9) = 0 + 0 = 0
3) 개구리 사진일 때 SVM loss
max(0, 2.2-(-3.1)+1) + max(0, 2.5-(-3.1)+1) = max(0, 6.3) + max(0,6.6) = 6.3+6.6 = 12.9
최종 loss 값 : (2.9 + 0 + 12.9) / 3 = 5.3
만약 j=y_1도 포함하면?
SVM loss function의 code
def L_i_vectorized(x, y, W):
scores = W.dot(x)
margins = np.maximum(0, scores - scores[y] + 1)
margins[y] = 0
loss_i = np.sum(margins)
return loss_i
여기서 문제점은 0으로 만드는 W(weight) 값이 unique 하지 않다.
-> regularization 도입
이때 마지막에 추가된 부분을 test를 조정하기 위해 추가하였다.
Softmax Classifier (Multinomial Logistic Regression)
ex.
이때 cat의 계산법은 다음과 같이 실행된다.
다음 예시와 같이 SVM과 Softmax와 비교를 진행하면 다음과 같이 결과가 나온다.
Optimization
현재 W 값 : [0.34, -1.11, 0.78, 0.12, 0.55, 2.81, -3.1, -1.5, 0.33, ...] => loss : 1.25347
h가 0.0001일 때, gradient dW:
[-2.5, 0.6, 0, ...]
def eval_numerical_gradient(f, x):
fx = f(x)
grad = np.zeros(x.shape)
h = 0.00001
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
ix = it.multi_index
old_value = x[ix]
x[ix] = old_value + h
fxh = f(x)
x[ix] = old_value
grad[ix] = (fxh - fx) / h
it.iternext()
return grad
- 대략적인 정보가 나온다.
- 평가하기에 느리다.
Gradient Descent
full-batch gradient descent
while True:
weights_grad = evaluate_gradient(loss_fun, data, weights)
weights += - step_size * weights_grad
Mini-batch Gradient Descent
while True:
data_batch = sample_training_data(data, 256)
weights_grad = evaluate_gradient(loss_fun, data_batch, weights)
weights += - step_size * weights_grad
현실에서 실질적으로 사용됨
training set 중에 일부만 활용
일반적으로 mini-batch는 32/64/128을 사용
AlexNet은 256을 사용함
learning weight는 적절히 설정해야 한다.
Feature 추출 방법
Image Features
feature을 추출한 다음 linear classifier 적용
feature 추출 방식은 Color Histogram
Color Histogram
모든 픽셀의 컬러를 파악하고 각각 컬러에 해당하는 bin이 몇개인지 파악을 한다.
Histogram of Oriented Gradients
방향을 9가지로 분류해 9가지 bin에 몇개가 속하는지 추출한다.
Bag of Words
기존에는 feature 추출을 사람이 해줘야 했는데 새로운 방식에서는 이미지를 function에 넣어주면 결과값을 얻을 수 있음
'AI > CS231n' 카테고리의 다른 글
[CS231n] Lecutre 5. Convolutional Neural Networks (0) | 2024.07.12 |
---|---|
[CS231n] Lecture 4. Introduction to Neural Networks (1) | 2024.07.12 |
[CS231n] Lecture 2. Image Classification (0) | 2024.07.09 |