본문 바로가기

About/Pytorch

[Pytorch] Inception v3를 이용한 동물 이미지 분류 학습하기(하이퍼 파라미터 및 Optimizer 비교)

Image Classifier 하이퍼 파라미터 비교 Kaggle에서 얻은 이미지 데이터를 Inception v3 모델을 이용하여 학습시키고 분류한다. 모델의 하이퍼 파라미터를 변경하며 학습하였을 때 모델의 분류 정확도를 비교해보자.

 

사용한 모델은 inception v3 모델입니다.

inception v3 모델

사용한 데이터는 다양한 동물의 이미지 사진이다. (분류 클래스 : ['butterfly', 'cow', 'dog', 'elephant', 'goat', 'hen', 'horse', 'spyder'] )

학습에 사용한 이미지

이미지 출처
https://www.kaggle.com/kdnishanth/animal-classification

 

Animal Classification

 

www.kaggle.com

 


SGD, Adam 비교

model = inception v3
batchsize = 200, learning rate = 0.005, epoch = 15
Train images = 15587, Test Images = 4017
 

  • SGD Optimizer
더보기

[1, 81] loss: 1.824
Accuracy of the network on the 4017 test images: 41 %
[2, 81] loss: 1.542
Accuracy of the network on the 4017 test images: 48 %
[3, 81] loss: 1.328
Accuracy of the network on the 4017 test images: 55 %
[4, 81] loss: 1.172
Accuracy of the network on the 4017 test images: 59 %
[5, 81] loss: 1.035
Accuracy of the network on the 4017 test images: 63 %
[6, 81] loss: 0.962
Accuracy of the network on the 4017 test images: 65 %
[7, 81] loss: 0.879
Accuracy of the network on the 4017 test images: 66 %
[8, 81] loss: 0.812
Accuracy of the network on the 4017 test images: 68 %
[9, 81] loss: 0.754
Accuracy of the network on the 4017 test images: 68 %
[10, 81] loss: 0.672
Accuracy of the network on the 4017 test images: 71 %
[11, 81] loss: 0.625
Accuracy of the network on the 4017 test images: 71 %
[12, 81] loss: 0.567
Accuracy of the network on the 4017 test images: 72 %
[13, 81] loss: 0.505
Accuracy of the network on the 4017 test images: 71 %
[14, 81] loss: 0.460
Accuracy of the network on the 4017 test images: 73 %
[15, 81] loss: 0.395
Accuracy of the network on the 4017 test images: 74 %

Min loss = 0.395, Max Accuracy : 74.55%

 

  • Adam Optimizer
더보기


[1, 81] loss: 1.556
Accuracy of the network on the 4017 test images: 53 %
[2, 81] loss: 1.278
Accuracy of the network on the 4017 test images: 57 %
[3, 81] loss: 1.033
Accuracy of the network on the 4017 test images: 63 %
[4, 81] loss: 0.888
Accuracy of the network on the 4017 test images: 69 %
[5, 81] loss: 0.756
Accuracy of the network on the 4017 test images: 72 %
[6, 81] loss: 0.651
Accuracy of the network on the 4017 test images: 74 %
[7, 81] loss: 0.594
Accuracy of the network on the 4017 test images: 73 %
[8, 81] loss: 0.516
Accuracy of the network on the 4017 test images: 76 %
[9, 81] loss: 0.464
Accuracy of the network on the 4017 test images: 76 %
[10, 81] loss: 0.372
Accuracy of the network on the 4017 test images: 77 %
[11, 81] loss: 0.283
Accuracy of the network on the 4017 test images: 78 %
[12, 81] loss: 0.212
Accuracy of the network on the 4017 test images: 77 %
[13, 81] loss: 0.166
Accuracy of the network on the 4017 test images: 80 %
[14, 81] loss: 0.124
Accuracy of the network on the 4017 test images: 79 %
[15, 81] loss: 0.074
Accuracy of the network on the 4017 test images: 80 %

 

 

Min loss = 0.074, Max Accuracy : 80.68%

loss 그래프
acc 그래프

확실히 SGD 보다는 Adam 의 학습속도가 훨씬 빠르게 나타난다.


이미지 정규화 적용

 

model = inception v3
batchsize = 200, learning rate = 0.005, epoch = 15, optimizer = Adam
Train images = 15587, Test Images = 4017
Image Normalize([0.485, 0.456, 0.496], [0.229, 0.224, 0.225]

 

- Image 정규화한 경우 이미지 

학습 이미지 정규화 적용


정규화된 값으로 이미지가 표현되기 때문에 이상하게 나타난다.

 

Train Adam Optimizer(Image Normalize([0.485, 0.456, 0.496], [0.229, 0.224, 0.225]

더보기


[1, 81] loss: 1.631
Accuracy of the network on the 4017 test images: 54 %
[2, 81] loss: 1.161
Accuracy of the network on the 4017 test images: 62 %
[3, 81] loss: 1.109
Accuracy of the network on the 4017 test images: 61 %
[4, 81] loss: 1.143
Accuracy of the network on the 4017 test images: 50 %
[5, 81] loss: 1.145
Accuracy of the network on the 4017 test images: 65 %
[6, 81] loss: 0.825
Accuracy of the network on the 4017 test images: 68 %
[7, 81] loss: 0.709
Accuracy of the network on the 4017 test images: 71 %
[8, 81] loss: 0.597
Accuracy of the network on the 4017 test images: 72 %
[9, 81] loss: 0.520
Accuracy of the network on the 4017 test images: 74 %
[10, 81] loss: 0.412
Accuracy of the network on the 4017 test images: 74 %
[11, 81] loss: 0.319
Accuracy of the network on the 4017 test images: 76 %
[12, 81] loss: 0.255
Accuracy of the network on the 4017 test images: 75 %
[13, 81] loss: 0.193
Accuracy of the network on the 4017 test images: 75 %
[14, 81] loss: 0.135
Accuracy of the network on the 4017 test images: 76 %
[15, 81] loss: 0.090
Accuracy of the network on the 4017 test images: 77 %

Min loss = 0.09, Max Accuracy = 77.54%

 

 

loss 그래프
acc 그래프

정규화 하였을 때도 꽤 괜찮은 성능을 보였다. 내가 가진 데이터셋의 평균, 표준편차 값으로 정규화한다면 성능을 더 높일 수 있을 것 같다.

 


learning rate 별 비교

model = inception v3
batchsize = 200, optimizer = Adam, epoch = 15
Train images = 15587, Test Images = 4017

learning rate = 0.001

더보기

*Train Adam Optimizer(0.001)*
[1, 81] loss: 1.256
Accuracy of the network on the 4017 test images: 65 %
[2, 81] loss: 0.828
Accuracy of the network on the 4017 test images: 72 %
[3, 81] loss: 0.645
Accuracy of the network on the 4017 test images: 76 %
[4, 81] loss: 0.509
Accuracy of the network on the 4017 test images: 76 %
[5, 81] loss: 0.373
Accuracy of the network on the 4017 test images: 79 %
[6, 81] loss: 0.286
Accuracy of the network on the 4017 test images: 79 %
[7, 81] loss: 0.209
Accuracy of the network on the 4017 test images: 80 %
[8, 81] loss: 0.170
Accuracy of the network on the 4017 test images: 79 %
[9, 81] loss: 0.121
Accuracy of the network on the 4017 test images: 81 %
[10, 81] loss: 0.069
Accuracy of the network on the 4017 test images: 81 %
[11, 81] loss: 0.047
Accuracy of the network on the 4017 test images: 82 %
[12, 81] loss: 0.062
Accuracy of the network on the 4017 test images: 81 %
[13, 81] loss: 0.077
Accuracy of the network on the 4017 test images: 81 %
[14, 81] loss: 0.044
Accuracy of the network on the 4017 test images: 82 %
[15, 81] loss: 0.023
Accuracy of the network on the 4017 test images: 82 %

Min loss = 0.023, Max Accuracy = 82.79%

 

learning rate = 0.005

더보기

*Train Adam Optimizer*(0.005)
[1, 81] loss: 1.512
Accuracy of the network on the 4017 test images: 56 %
[2, 81] loss: 1.293
Accuracy of the network on the 4017 test images: 58 %
[3, 81] loss: 1.096
Accuracy of the network on the 4017 test images: 61 %
[4, 81] loss: 0.970
Accuracy of the network on the 4017 test images: 68 %
[5, 81] loss: 0.788
Accuracy of the network on the 4017 test images: 69 %
[6, 81] loss: 0.682
Accuracy of the network on the 4017 test images: 73 %
[7, 81] loss: 0.609
Accuracy of the network on the 4017 test images: 73 %
[8, 81] loss: 0.543
Accuracy of the network on the 4017 test images: 74 %
[9, 81] loss: 0.460
Accuracy of the network on the 4017 test images: 74 %
[10, 81] loss: 0.377
Accuracy of the network on the 4017 test images: 78 %
[11, 81] loss: 0.266
Accuracy of the network on the 4017 test images: 76 %
[12, 81] loss: 0.197
Accuracy of the network on the 4017 test images: 77 %
[13, 81] loss: 0.140
Accuracy of the network on the 4017 test images: 78 %
[14, 81] loss: 0.107
Accuracy of the network on the 4017 test images: 78 %
[15, 81] loss: 0.082
Accuracy of the network on the 4017 test images: 79 %

Min loss = 0.082, Max Accuracy = 79.21%

 

learning rate = 0.01

 

더보기

Train Adam Optimizer*(0.01)
[1, 81] loss: 1.906
Accuracy of the network on the 4017 test images: 38 %
[2, 81] loss: 1.613
Accuracy of the network on the 4017 test images: 44 %
[3, 81] loss: 1.725
Accuracy of the network on the 4017 test images: 43 %
[4, 81] loss: 1.486
Accuracy of the network on the 4017 test images: 43 %
[5, 81] loss: 1.439
Accuracy of the network on the 4017 test images: 55 %
[6, 81] loss: 1.234
Accuracy of the network on the 4017 test images: 59 %
[7, 81] loss: 1.118
Accuracy of the network on the 4017 test images: 61 %
[8, 81] loss: 0.962
Accuracy of the network on the 4017 test images: 65 %
[9, 81] loss: 0.808
Accuracy of the network on the 4017 test images: 68 %
[10, 81] loss: 0.705
Accuracy of the network on the 4017 test images: 71 %
[11, 81] loss: 0.618
Accuracy of the network on the 4017 test images: 71 %
[12, 81] loss: 0.515
Accuracy of the network on the 4017 test images: 71 %
[13, 81] loss: 0.437
Accuracy of the network on the 4017 test images: 71 %
[14, 81] loss: 0.328
Accuracy of the network on the 4017 test images: 73 %
[15, 81] loss: 0.256
Accuracy of the network on the 4017 test images: 73

Min loss = 0.256, Max Accuracy = 73.88%

learning rate 별 loss 그래프
learning rate별 acc 그래프

 

Learning Rate 별로 생각보다 큰 학습 차이를 보인다. lr=0.001인 경우 가장 학습이 잘되었다. 무조건 0.001인 경우가 좋은 것이 아니라 데이터, 모델 등에 따라 적절한 learning rate 값을 찾아서 사용하는 것이 학습에 도움이 된다.

 


inception v3 모델을 이용하여 동물 이미지 분류기를 학습시키며 각 하이퍼파라미터, Optimizer 를 변경해가며 정확도를 비교해보았습니다.