Darknet: How to get anchor value in YoloV3 ?

Created on 29 Mar 2018  路  20Comments  路  Source: AlexeyAB/darknet

./darknet detector calc_anchors -num_of_clusters 9 -width 13 -heigh 13

let obtained anchors:
1,2 3,4 5,6 7,8 9,10, 11,12 13,14 15,16 17,18
then
cluster 1,2,3 => scale:x1
cluster 4,5,6=> scale: x2
cluster 7,8,9=> scale: x4

(1,2) (3,4) (5,6): *32 *1
(7,8) (9,10) (11,12): *32 *2
(13,14) (15,16) (17,18): *32 *4
is that right?

question

Most helpful comment

@hacunamatada Hi,

For Yolo v3 you should use -width -height with the same values as width and height in your cfg-file.

For example ./darknet detector calc_anchors -num_of_clusters 9 -width 416 -heigh 416

And then you should copy all the same 9 anchors (18 values) to each of 3 [yolo]-layers in your cfg-file.

All 20 comments

@hacunamatada Hi,

For Yolo v3 you should use -width -height with the same values as width and height in your cfg-file.

For example ./darknet detector calc_anchors -num_of_clusters 9 -width 416 -heigh 416

And then you should copy all the same 9 anchors (18 values) to each of 3 [yolo]-layers in your cfg-file.

I appreciate your reply.
one more question:
when training random=1, can i use these anchors from 416?
network size varies from 320 to 608. what anchor should i use?

@AlexeyAB I want to know in yolov3 these anchors are produced on the VOC original resolution or the width=416 and heigh=416 in cfg file? Your gen_anchors.py compute different anchors on the same dataset. which is the fo yolov3?

@bubulv

  • gen_anchors.py is for Yolo v2.
  • for Yolo v3 use: ./darknet detector calc_anchors data/voc.data -num_of_clusters 9 -width 416 -heigh 416

anchors calculated for width=416 and heigh=416 in cfg file, and for specified dataset: voc.data or coco.data or obj.data...

@hacunamatada Use width and height from cfg-file, even if you use random=1 for training.

@AlexeyAB So, if my test image's size is 320*320 , these anchors is also suitable for it? I want to train yolov3 on my dataset , did change the anchors can improve the map? Did you have a try?

@bubulv Yes, it usually improve the mAP. Anchors is independent of image size.

@AlexeyAB ./darknet detector calc_anchors -num_of_clusters 9 -width 416 -heigh 416
I use this command produce some anchors, but I don't know which one is for the mask=0,1,2 or which is for mask =3,4,5...
just use the default results from this command?

@bubulv Use the same 9 anchors for each of 3 yolo-layers.

recalculate anchors for your dataset for width and height from cfg-file: darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -heigh 416 then set the same 9 anchors in each of 3 [yolo]-layers in your cfg-file

More: https://github.com/AlexeyAB/darknet#how-to-improve-object-detection

@AlexeyAB It's no use..... I ues these new anchors for my dataset to train yolov3, but after some items, the avg is nan..
image
I use the default anchors in yolov3-voc.cfg is ok.

@bubulv

  • Did you changes anchors in cfg in 3 places?
  • Can you show your anchors?
  • What command did you use to calculate anchors?

@AlexeyAB yes, I replaces all the anchors in my cfg.
I ues the command is:
./darknet detector calc_anchors cfg/voc.data -num_of_clusters 9 -width 4116 -heigh 416

anchors for my dataset is : 92.2932,185.0346, 54.5689,88.6081, 64.1705,118.6016, 217.2153,333.1541, 118.0744,225.9878, 22.6399,36.7393, 40.0209,63.6680, 135.2745,291.8580, 80.4109,148.1382

@AlexeyAB
image

I think you also need to order the anchors (ascending)

I do it, but no use. The author say 9 anchors to 3scals , I am confused about it. The default anchors in yolov3.cfg some one is not order by size....

@bubulv
random=0, Try training at the smallest resolution possible.
for example, 320x320 or 352x352

Rounding the anchor values solved this for me.
From your example:
92.2932,185.0346, 54.5689,88.6081, 64.1705,118.6016, 217.2153,333.1541, 118.0744,225.9878, 22.6399,36.7393, 40.0209,63.6680, 135.2745,291.8580, 80.4109,148.1382

change to->
92,185, 55,89, 64,119, 217,333, 118,226, 23,37, 40,64, 135,292, 80,148

I added automatic ordering of anchors in the last commits.
You can update your code from this repository and recompile.

My images are of size 480x360(only one, 2000x1500(1000 nos), 1400x1050 (2400), 1916x1078(800), 1398x1048(30), 1400x788(1300), 1360x765(800), 960x540(210)

After calculation anchors fot image size 832x832, the output was as below
my question is should I use recalculated anchors or default used in yolo cfg file

darknet$ ./darknet detector calc_anchors data/obj.data -num_of_clusters 9 -width 832 -height 832 -show

num_of_clusters = 9, width = 832, height = 832
read labels from 6471 images
loaded image: 893 box: 66237

Wrong label: data/obj/0000293_03401_d_0000939.txt - j = 129, x = 0.742279, y = 0.488889, width = 0.002206, height = 0.000000
loaded image: 5253 box: 277416

Wrong label: data/obj/9999985_00000_d_0000020.txt - j = 11, x = 0.437857, y = 0.150476, width = 0.002857, height = 0.000000
loaded image: 6331 box: 342695

Wrong label: data/obj/9999999_00590_d_0000267.txt - j = 88, x = 0.275000, y = 0.276000, width = 0.005000, height = 0.000000
loaded image: 6471 box: 353550
all loaded.

calculating k-means++ ...

iterations = 164

avg IoU = 65.24 %

Saving anchors to the file: anchors.txt
anchors = 5, 11, 15, 15, 9, 27, 27, 24, 19, 46, 48, 41, 34, 79, 77, 81, 113,155
Unable to init server: Could not connect: Connection refused

@shantanuctech

It is better to use default anchors in your case, because calculated anchors are too small for the correspond layers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yumin-Sun-00 picture Yumin-Sun-00  路  3Comments

jasleen137 picture jasleen137  路  3Comments

yongcong1415 picture yongcong1415  路  3Comments

qianyunw picture qianyunw  路  3Comments

siddharth2395 picture siddharth2395  路  3Comments