New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
howto: calculating custom anchors for YOLOv4-tiny #7856
Comments
Yes, there is random initialization in the k-means++ approach https://en.wikipedia.org/wiki/K-means%2B%2B When you run command You see:
So you can run it several times and choose the anchors with the highest IoU.
Try to use this command with flag
Check that the anchors cover most of the points evenly.
Yes, masks of anchors are 0-based. So try
instead of
It was a mistake in yolov3-tiny and yolov4-tiny versions. Which we shouldn't fix in the default models, beacause we ahve many of pre-trained models with these masks.
Actually you should use:
The order of the [yolo] layers is different in different models. |
It is not related to anchors, but it can slightly improve accuracy if you use pre-trained weights for training. Line 198 in 005513a So it will freeze all layers before this one for the first 800 iterations, so randomly initialized layers will not produce random gradient and will not destroy information in the pre-trained weights. After 800 iterations, these layers will already be trained and will not contain random weights. Or for very large models you can try to train darknet/cfg/yolov4-p5-frozen.cfg Line 1698 in 005513a
with pre trained weigths https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-p5.conv.232 In this case |
@AlexeyAB - thanks for posting these details. I too am trying to customize anchors for a custom yolov4-tiny model. I'm still a little confused... what should @stephanecharette set the masks to for lines 227 and 278 above? |
@AlexeyAB sorry but you have not answered to the most critical (and misterious) part of Stephane question:
Yolo doesn't respect his own rules? Another question, does changing the network size affect the "theoretical" 30x30 and 60x60 limits? |
There is detailed answer for this question: #7856 (comment) Actually you should use:
In general yes. |
Try to keep this rule. |
ok thank you for those explanations, It's the first time I read about the rewritten_box values in relation to Anchors...
|
So is this combination the best Did you try And new cfg-file/pre-trained weights:
Does it solve Out of memory issue, or does it increase accuracy? Line 216 in d669680
|
@AlexeyAB
I tried yolov4 csp / scaled in december/january but it was not yet ok... |
EMA is a custom version of SWA https://pytorch.org/blog/pytorch-1.6-now-includes-stochastic-weight-averaging/
You can try to train this model: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-csp-x-swish.cfg with this pre-trained weights https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp-x-swish.conv.192 |
ok, will try |
@arnaud-nt2i
|
ok that seems fair, nothing replaces old good long trainings ! |
Hi @arnaud-nt2i
Isn't Batch_normalise should be either 0 or 1? |
@cpsu00 see batchnorm_layer.c |
Oh, I didn't notice that. Thanks! |
Does that mean maks won't change if I use a pre-trained model? If I change it to [3,4,5], [0,1,2], I can't use the pre-trained model. Okay? |
Of course, do not change the masks or anchors on pre-trained models! This only works if you are training your own custom network. |
ok thank you ! |
How should I change the |
@stephanecharette @AlexeyAB I have trained two Yolov4 models. one using resolution 416x 416 and the other 512x512. however, the model with 512x512 has a lower mAP than the 416x416. It is confusing for me that it should be the opposite? the input images were all equal size 1008 x 1008. any help will be appreciated. anchor generated at 416: 10, 10, 18, 8, 8, 18, 12, 12, 14, 14, 16, 15, 18, 18, 21, 21, 25, 25 ...............90.52% IOU |
Hi @MrGolden1 if you want to increase the anchor box size you should also increase the detection heads of yolo. see Yolov4-tiny-3l.cfg |
@stephanecharette @AlexeyAB the other question I have is what if I forget the resolution I used for training but I have the weight file, is there any way to know the training time resolution from the weight file? |
The image size is irrelevant and ignored by Darknet. The only size that matters is the width and height in the cfg. Your images could be 999999x999999 and Darknet will still resize the images to match the network dimensions. |
Thanks for your reply @stephanecharette okay let's forget about image size. whatever image size I have given, isn't my model trained with network resolution (width and height in the cfg) size 512x512 give better results than 416x416? in my case the 512 x 512 .cfg gives very low mAP than the 416 x 416. I am very confused |
I think you're right. recalculating the anchor frame will result in a decrease in the overall accuracy of the model. My experiment is consistent with yours, and the default anchor works best. Although I do not know why this is the case, it is possible from my analysis that the anchors of K-means + + clustering do not cover all scales, most likely because the target box in the dataset is fixed in one scale. |
@AlexeyAB I know about this line in the readme:
I've avoided the topic of re-calculating anchors for the past few years. But people ask on the Discord server, and truth is, I'd like to know how to do it as well! :) Every time I try to do it, the results are worse than the default anchors, so I assume that I'm doing it wrong and I'm not enough of an expert.
Say we use this license plate project as an example: https://github.com/stephanecharette/DarkPlate
The default anchors in YOLOv4-tiny is this:
I know the anchor-calculating code has a bit of randomness in it, so every time I run it I get slightly different results. For my 416x416 YOLOv4-tiny config file, I run this command:
The results I get look like one of these lines:
First thing I do is pick one of the anchor lines I list above. (They're all very similar, off by just a few pixels.)
Let say we use this line for our example:
anchors = 12, 19, 24, 37, 41, 75, 95, 38, 148, 85, 245, 151
Then I look for each
[yolo]
section in the .cfg file and replace theanchors = ...
line with one we selected above.Lastly comes these instructions:
Considering the default anchors are
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
, the default YOLOv4-tiny.cfg has 2 YOLO sections with these masks and anchors:Lines 226-228:
And lines 277-279:
Are the anchors zero-based, or one-based? I assume zero-based, so line 227 refers to:
And line 278 refers to:
Is it intentional that mask index #3 (
81,82
) is referenced in both YOLO sections, or is that a typo? Should the mask be1,2,3
and4,5,6
or0,1,2
and3,4,5
?And just as importantly, how do we reconcile this statement:
From what I can see, the 1st [YOLO] section has anchors 81,82, 135,169, and 344,319, all of which are larger than 30x30, not smaller.
And even in the 2nd [YOLO] section, only the very first anchor of
23,27
would be smaller than 30x30, so I'm very confused.But even without understanding all of this, I went ahead and trained 2 networks, one with the default anchors and the other with some new custom anchors. I did not change the mask, only the
anchors = ...
line. (See attached .cfg file.)This is the chart.png when I train with the default YOLOv4-tiny anchors:
And this is the chart.png file when I use the custom anchors:
Can you help clear up the confusion and various questions?
DarkPlate.cfg.txt
The text was updated successfully, but these errors were encountered: