Head vs breakz

[인공지능]- One shot learning with Siamese Networks using keras - Harshall lamba - 3 본문

Head/인공지능

[인공지능]- One shot learning with Siamese Networks using keras - Harshall lamba - 3

headbreakz 2020. 2. 29. 11:14

One shot learning with Siamese Networks using keras - Harshall lamba

https://towardsdatascience.com/one-shot-learning-with-siamese-networks-using-keras-17f34e75bb3d

를 공부하기 위해 번역과 정리 하는 글입니다.

Base Line 1 — Nearest Neighbor Model

간단한 baseline 모델을 만들고 그 결과를 만들려고하는 복잡한 모델과 비교하는 것이 좋다. 첫번째 기본 모델은 Nearest Neighbor model이다.

벡터 X와 다른 벡터들 간의 L2 distance를 비교하여, 거리가 가장 작은 벡터를 확인한다. 거리가 가까울수록 유사성이 높기 떄문에, 그 벡터가 가장 유사성이 최대인 벡터이다. 그러나 우리의 경우 벡터가 아닌 gray scale images가 있기 때문에 행렬을 Flatten을 통해 평면화 한 후 , L2 distance를 계산하면 된다.

N-way one shot learning과 비슷하게, 반복하여 모든 경우에 대해 평균 예측 점수를 계산한다.

  • nearest neighbor approach 의 코드
def nearest_neighbour_correct(pairs,targets):
    """returns 1 if nearest neighbour gets the correct answer for a one-shot task
        given by (pairs, targets)"""
    L2_distances = np.zeros_like(targets)
    for i in range(len(targets)):
        L2_distances[i] = np.sum(np.sqrt(pairs[0][i]**2 - pairs[1][i]**2))
    if np.argmin(L2_distances) == np.argmax(targets):
        return 1
    return 0


  def test_nn_accuracy(N_ways,n_trials):
    """Returns accuracy of NN approach """
    print("Evaluating nearest neighbour on {} unique {} way one-shot learning tasks ...".format(n_trials,N_ways))
    n_right = 0

    for i in range(n_trials):
        pairs,targets = make_oneshot_task(N_ways,"val")
        correct = nearest_neighbour_correct(pairs,targets)
        n_right += correct
    return 100.0 * n_right / n_trials

Test Results and Inference

N- way 테스트는 N = 1,3,5 ....19 까지 실험을 하였다. 각 테스트 마다 50번 실행 하였으며, 평균 정확도가 계산되었다. 총 4 가지 모델을 비교한 그래프이다. Siamese(val set), Siamese(train set), Nearest neighbour , Random guessing

Siamese model은 Raddom과 Nearest Neighbor model보다 뛰어났고, train과 test 세트의 결과에는 over fitting이 발생하였다.

Conclusion

over fitting을 하지 않기 위해 하이퍼 파라미터 값을 고쳐야 한다. 또한 N-way testing에서 N 값을 늘려가며 더욱 엄격한 테스트를 할 수 있었다.

참조 & 도움

https://towardsdatascience.com/one-shot-learning-with-siamese-networks-using-keras-17f34e75bb3d

Comments