블로그 이미지
devtang
Instagram : @taebr0 devtaehyeong@gmail.com

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2020. 3. 20. 15:15 AI/YOLO

항상 YOLO 학습때 anchor 값이 정확히 무엇인지 몰라서 찾아봤는데 

https://github.com/pjreddie/darknet/issues/568

 

Can someone clarify the anchor box concept used in Yolo? · Issue #568 · pjreddie/darknet

I know this might be too simple for many of you. But I can not seem to find a good literature illustrating clearly and definitely for the idea and concept of anchor box in Yolo (V1,V2, andV3). Thanks!

github.com

여기서 AlexeyAB , YOLO 버전의 제작자중 한명이 댓글을 남겼습니다.

 

Anchors are initial sizes (width, height) some of which (the closest to the object size) will be resized to the object size - using some outputs from the neural network (final feature map):

 

Anchor는 초기의 크기(너비, 높이) 이며 그 중 일부 (개체 크기에 가장 가까운)는 신경망 (최종 기능 맵)의 일부 출력을 사용하여 개체 크기로 크기가 조정됩니다.

 

yolo_layer.c - get_yolo_box 함수

 

  b.x = (i + x[index + 0*stride]) / lw;
  b.y = (j + x[index + 1*stride]) / lh;
  b.w = exp(x[index + 2*stride]) * biases[2*n]   / w;
  b.h = exp(x[index + 3*stride]) * biases[2*n+1] / h;

darknet 폴더안에 yolo_layer.c 파일중 get_yolo_box 함수 안에 다음과 같은 코드가 있습니다.

대부분의 경계 상자에는 특정 높이 너비 비율이 있는데 YOLO는 경계 상자를 직접 예측하는 대신 특정 높이 너비 비율을 가진 미리 결정된 상자 세트에서 대상을 예측합니다. 이때 미리 정해진 상자 세트가 앵커 입니다.

 

따라서 학습시에 yolov3.cfg 파일을 가져와서 다음과 같이 수정합니다.

 

max_batches 는 자신의 classes * 4000 로 설정하였습니다. (저는 클래스가 6개이므로 24000)

 

그리고 cfg파일에 맨아래쪽으로 내려가면

[convolutional]
size=1
stride=1
pad=1
filters=33
activation=linear


[yolo]
mask = 0,1,2
anchors = 11, 21,  31, 62,  43, 80,  53,103,  65,125,  78,155,  51,287, 106,202,  76,350
classes=6
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

여기서 [yolo]부분에 classes 를 각 자신이 설정한 classes 개수로 설정하고 [convolutional] 파트에 filters 수는 yoloV3 기준 (classes + 5) * 3 해준 값을 설정해줍니다. (저는 클래스가 6이므로 (6+5) * 3 = 33 해줬습니다.)

 

조금더 올려보면 마찬가지로 [yolo] 가 두개나 더 있습니다.

[yolo]와 바로 그위에 [convolutional] 를 모두 수정해줍니다.

[convolutional]
size=1
stride=1
pad=1
filters=33
activation=linear

[yolo]
mask = 6,7,8
anchors = 11, 21,  31, 62,  43, 80,  53,103,  65,125,  78,155,  51,287, 106,202,  76,350
classes=6
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

[convolutional]
size=1
stride=1
pad=1
filters=33
activation=linear

[yolo]
mask = 3,4,5
anchors = 11, 21,  31, 62,  43, 80,  53,103,  65,125,  78,155,  51,287, 106,202,  76,350
classes=6
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

자이제 anchors 값도 설정해줘야하는데 각자 anchors 값이 다르므로 명령어를 통해 가져옵니다.

darknet.exe detector calc_anchors data/obj.data data/yolo-obj.cfg -num_of_clusters 9 -width 416 -height 416

obj.data , cfg 파일은 본인의 경로에 맞게 설정해주시고 width 와 heights 역시 본인의 cfg 파일에 설정한 크기대로 설정해주시면 됩니다. 저는 416 416으로 맞췄습니다.

코드를 실행해주면 평균 IoU % 값이 뜨고 anchors값이 txt로 저장이됩니다.

 

 

이후에 동일하게 설정한 cfg파일을 사용하여 학습 진행해 주면 됩니다.

 

 

posted by devtang