puhuk
puhuk

Reputation: 484

YOLO anchor box

As I read the YOLO paper it says it makes anchor box with K-means. However, when I see the code implementing this, it seems to fix anchor size as below. I hope you describe what it exactly means or point out my misunderstanding with this.

Thanks, and regards

[yolo] 
mask = 6,7,8
***anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326***

Upvotes: 3

Views: 7489

Answers (2)

iv67
iv67

Reputation: 4151

As stated by other answer, the anchor boxes value in cfg file is only the initial value, later it will be resized to the closest predicted object. And you can generate your own anchor boxes using K-means as stated in other answer.

Here's the important thing, the initial value will be resized. Refer to this explanation by AlexeyAB. https://github.com/pjreddie/darknet/issues/568

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):

darknet/src/yolo_layer.c

Lines 88 to 89 in 6f6e475

 b.w = exp(x[index + 2*stride]) * biases[2*n]   / w;   
 b.h = exp(x[index + 3*stride]) * biases[2*n+1] / h;  

x[...] - outputs of the neural network

biases[...] - anchors

b.w and b.h result width and height of bounded box that will be showed on the result image

Thus, the network should not predict the final size of the object, but should only adjust the size of the nearest anchor to the size of the object.

In Yolo v3 anchors (width, height) - are sizes of objects on the image that resized to the network size (width= and height= in the cfg-file).

In Yolo v2 anchors (width, height) - are sizes of objects relative to the final feature map (32 times smaller than in Yolo v3 for default cfg-files).

Upvotes: 2

nick
nick

Reputation: 463

Anchor is like a default bounding box for a cell. It is composed of width and height for each anchor.

anchors = anchor1_width, anchor1_height, anchor2_width, anchor2_height, ..., anchorN_width, anchorN_height

You can generate your own anchors using this code if you are training yolov3 https://github.com/pjreddie/darknet/issues/597#issuecomment-377370922

for yolov2 https://github.com/AlexeyAB/darknet/blob/master/scripts/gen_anchors.py

after you have generated your own anchors, replace default ones with yours in .cfg file

Upvotes: 2

Related Questions