cs231n

cs231n Assignment 1: Q5 (HOG, HSV 추출 사용)

츄츄츄츄츄츄츄 2023. 2. 6. 00:14

과제링크:Assignment 1 (cs231n.github.io)

 

Assignment 1

This assignment is due on Friday, April 15 2022 at 11:59pm PST. Starter code containing Colab notebooks can be downloaded here. Setup Please familiarize yourself with the recommended workflow before starting the assignment. You should also watch the Colab

cs231n.github.io

 

내 풀이 링크: https://github.com/lionkingchuchu/cs231n.git

 

GitHub - lionkingchuchu/cs231n: cs231n Spring 2022 Assignment

cs231n Spring 2022 Assignment. Contribute to lionkingchuchu/cs231n development by creating an account on GitHub.

github.com

 

이번 과제는 기존의 분류에서는 데이터를 각 이미지의 RGB값에 대한 픽셀 데이터 (0,255) 형식으로 분류하였지만, 이미지 분류에는 Histogram of Oriented Gradients (HOG) 와 RGB가 아닌 HSV를 이용한 데이터로 전환하여 분류를 하는 방식이다. HOG는 이미지의 gradient, 즉 이미지에 있는 물체의 선들에 대한 정보를 갖고 있다. HOG 데이터는 이미지의 색상 대신 영역의 우세한 픽셀 변화를 통해 선, 모서리 등의 표현을 찾아내는데 사용된다. HSV는 기존의 RGB로 표현한 색을 Hue (색상) Saturation (채도) Value (명도)로 표현한 것이다. 

 

먼저 SVM모델을 사용해 위의 HOG, HSV를 추출한 데이터를 분류 하는 코드를 짜 보았다.

# Use the validation set to tune the learning rate and regularization strength

from cs231n.classifiers.linear_classifier import LinearSVM

learning_rates = [1e-9, 1e-8, 1e-7]
regularization_strengths = [5e4, 5e5, 5e6]

results = {}
best_val = -1
best_svm = None

# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

for lr in learning_rates:
  for reg in regularization_strengths:
    model = LinearSVM()
    model.train(X_train_feats,y_train, learning_rate = lr, reg = reg, num_iters = 2000)
    y_train_pred = model.predict(X_train_feats)
    y_val_pred = model.predict(X_val_feats)

    train_accuracy = np.mean((y_train_pred == y_train))
    val_accuracy = np.mean((y_val_pred == y_val))

    if val_accuracy > best_val:
      best_val = val_accuracy
      best_svm = model
    results[(lr,reg)] = (train_accuracy, val_accuracy)

# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print('lr %e reg %e train accuracy: %f val accuracy: %f' % (
                lr, reg, train_accuracy, val_accuracy))
    
print('best validation accuracy achieved: %f' % best_val)

HOG, HSV두개의 feature를 추출한 데이터로 SVM model을 사용해 분류 해 보았다.

기존의 SVM모델을 사용헀을때의 최고 validation accuracy는 0.383이었던것에 반해 정확도가 높아진 것을 알 수 있었다. 

HOG, HSV데이터를 SVM model을 사용했을때의 오분류 예시들

Inline question 1:

Describe the misclassification results that you see. Do they make sense?

YourAnswer:I could find out misclassification with two chunks: manmade objects that have straight lines such as car, ship, truck, plane are mostly misclassified each other, and animals with curvy lines are misclassified each other. I think the use of HOG data helped a little than just using a RGB value.

 

여기서 다음 문제는 위의 오분류 데이터 예시들의 대한 설명이다. 확실히 HOG 를 추가로 이용하니깐 직선이 많이 사용되는 사람이 만든 물체들 (자동차, 배, 트럭, 비행기) 들이 서로 오분류 되는 것을 알 수 있고, 곡선이 많은 동물들 끼리 오분류 되는 것을 알 수 있다. 기존의 RGB 색상 값만 이용했을때 보다 HOG, HSV를 추가로 이용한 것이 분류에 도움이 되었을 것이다.

 

from cs231n.classifiers.fc_net import TwoLayerNet
from cs231n.solver import Solver

input_dim = X_train_feats.shape[1]
hidden_dim = 500
num_classes = 10

data = {
    'X_train': X_train_feats, 
    'y_train': y_train, 
    'X_val': X_val_feats, 
    'y_val': y_val, 
    'X_test': X_test_feats, 
    'y_test': y_test, 
}


best_net = None

# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
best_accuracy = -1

learning_rates = [1, 0.1,1e-2]
regularization_strengths = [0., 0.1]

for lr in learning_rates:
  for reg in regularization_strengths:
    net = TwoLayerNet(input_dim, hidden_dim, num_classes, reg = reg)
    solver = Solver(net,data,optim_config={'learning_rate': lr}, num_epochs = 10, verbose = False)
    solver.train()
    accuracy = solver.check_accuracy(data['X_val'],data['y_val'])
    print('learning rate:', lr, 'regularization strength:', reg, 'val accuracy:', accuracy)
    if accuracy > best_accuracy:
      best_accuracy = accuracy
      best_net = net

print('best accuracy:', best_accuracy)

# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

최대 정확도 0.595로 꽤 높은 값을 보여준다

다음으로 이전에 만든 TwoLayerNet을 통해 cross validation을 사용하며 분류하는 과정이다. 생각보다 꽤 높은 정확도를 보여주는 것을 알 수 있다. 이후 Test accuracy도 0.595로 기존의 TwoLayerNet보다 높은 정확도를 보여주고, 이후에 Assignment 2에서 나올 FullyconnectedNet에서 기존 RGB값을 이용한 데이터의 최대 점수보다 높은 정확도를 보여주어서 놀라웠다. 확실히 HOG, HSV의 feature을 추출해서 사용하는 것이 정확도를 높이는데 도움을 준다.