Skip to content
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

Setting the right anchor size to your model #6838

Closed
1 task done
ShaharSarShalom opened this issue Mar 3, 2022 · 15 comments
Closed
1 task done

Setting the right anchor size to your model #6838

ShaharSarShalom opened this issue Mar 3, 2022 · 15 comments
Labels
question Further information is requested Stale Stale and schedule for closing soon

Comments

@ShaharSarShalom
Copy link

Search before asking

Question

I'm trying to set my model anchor boxes to an optimal value

One source says that an autoanchor calculates the anchor sizes before the model starts and changes the model accordingly.

On the other hand, while running the hyper parameters evaluation (on the same dataset) I get this line:

AutoAnchor: 0.87 anchors/target, 0.167 Best Possible Recall (BPR). Anchors are a poor fit to dataset ⚠️, attempting to improve... AutoAnchor: Running kmeans for 16 anchors on 3570 points... AutoAnchor: Evolving anchors with Genetic Algorithm: fitness = 0.8325: 100% 1000/1000 [00:01<00:00, 643.73it/s] AutoAnchor: thr=0.22: 1.0000 best possible recall, 9.15 anchors past thr AutoAnchor: n=16, img_size=1280, metric_all=0.308/0.832-mean/best, past_thr=0.449-mean: 11,25, 19,16, 40,15, 18,43, 42,35, 24,64, 83,29, 35,89, 79,66, 158,49, 56,143, 115,104, 78,254, 170,136, 375,332, 858,444 AutoAnchor: New anchors saved to model. Update model *.yaml to use these anchors in the future. Image sizes 1280 train, 1280 val

1st question, which anchor sizes shall I use?

2nd question, how do I convert the hyperparameters evaluation output from this-

11,25, 19,16, 40,15, 18,43, 42,35, 24,64, 83,29, 35,89, 79,66, 158,49, 56,143, 115,104, 78,254, 170,136, 375,332, 858,444

To the model file -

`# Parameters
nc: 80 # number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multiple
anchors:

  • [10,13, 16,30, 33,23] # P3/8
  • [30,61, 62,45, 59,119] # P4/16
  • [116,90, 156,198, 373,326] # P5/32`

glenn-jocher thank you for the best repo at github!

Additional

No response

@ShaharSarShalom ShaharSarShalom added the question Further information is requested label Mar 3, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2022

👋 Hello @ShaharSarShalom, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email [email protected].

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 3, 2022

👋 Hello! Thanks for asking about model anchors. YOLOv5 🚀 uses a new Ultralytics algorithm called AutoAnchor for anchor verification and generation before training starts.

Autoanchor will analyse your anchors against your dataset and training settings (like --img-size), and will adjust your anchors as necessary if it determines the original anchors are a poor fit, or if an anchor count was specified in your model.yaml rather than anchor values, i.e.

# Specify anchor count (per layer)
anchors: 3

# --OR-- Specify anchor values manually
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

When generating new anchors, autoanchor first applies a kmeans function against your dataset labels (scaled to your training --img-size), and uses kmeans centroids as initial conditions for a Genetic Evolution (GE) algorithm. The GE algorithm will evolve all anchors for 1000 generations under default settings, using CIoU loss (same regression loss used during training) combined with Best Possible Recall (BPR) as its fitness function.

Notebook example: Open In Colab Open In Kaggle

AutoAnchor Screenshot

No action is required on your part to use autoanchor. If you would like to force manual anchors for any reason, you can skip autoanchor with the --noautoanchor flag:

python train.py --noautoanchor

Good luck 🍀 and let us know if you have any other questions!

@ShaharSarShalom
Copy link
Author

Thank you for your answer

**I'm still confuse whether or not shall I use the anchor output from the hyperparamters (training with evolve flag) **
[and how to use it]

AutoAnchor: n=16, img_size=1280, metric_all=0.308/0.832-mean/best, past_thr=0.449-mean: 11,25, 19,16, 40,15, 18,43, 42,35, 24,64, 83,29, 35,89, 79,66, 158,49, 56,143, 115,104, 78,254, 170,136, 375,332, 858,444 AutoAnchor: New anchors saved to model. Update model *.yaml to use these anchors in the future. Image sizes 1280 train, 1280 val

I assume your intention is to use the naive training script anytime.

thanks

@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Apr 3, 2022
@github-actions github-actions bot closed this as completed Apr 8, 2022
@tbonewmy
Copy link

tbonewmy commented Aug 3, 2022

Hi Sir @glenn-jocher ,
Can you explain the set of numbers in the anchor values a bit? What are these numbers? Are they the size (width and height) of the anchor in pixel?

--OR-- Specify anchor values manually

anchors:

  • [10,13, 16,30, 33,23] # P3/8
  • [30,61, 62,45, 59,119] # P4/16
  • [116,90, 156,198, 373,326] # P5/32

@glenn-jocher
Copy link
Member

@tbonewmy yes that's correct!

@tbonewmy
Copy link

tbonewmy commented Aug 3, 2022

@tbonewmy yes that's correct.

Are they corresponding to the rescaled image size or original image size?

@glenn-jocher
Copy link
Member

glenn-jocher commented Aug 3, 2022

@tbonewmy they correspond to --imgsz images

@tbonewmy
Copy link

tbonewmy commented Aug 4, 2022

- [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

is the 8,16,32 corresponding to the grid size? And P3,P4,P5 the three feature maps?

@glenn-jocher
Copy link
Member

Screen Shot 2022-08-04 at 3 57 52 PM

@NQHuy1905
Copy link

AutoAnchor: n=6, img_size=416, metric_all=0.501/0.794-mean/best, past_thr=0.539-mean: 33,11, 60,15, 43,28, 81,19, 110,27, 170,43

My auto anchor only return 12 values for yolov5n . So what should i change the anchor config, i thought we need at least 18 values for version 4.

@lhztop
Copy link

lhztop commented Apr 16, 2023

AutoAnchor: n=6, img_size=416, metric_all=0.501/0.794-mean/best, past_thr=0.539-mean: 33,11, 60,15, 43,28, 81,19, 110,27, 170,43

My auto anchor only return 12 values for yolov5n . So what should i change the anchor config, i thought we need at least 18 values for version 4.

n=6, width+height=2, 2n=12

@glenn-jocher
Copy link
Member

@lhztop in your example, AutoAnchor has computed 2 sets of 6 anchor box dimensions each, so there should be no issue using the resulting 12 values with the version of YOLOv5 that you are running. Just copy these 12 values into your anchor parameter list in the *.yaml file, and the resulting anchor boxes will be used for training and testing.

@Shadow-Alex
Copy link

Hi, I'm quite confused by the 'saved to model' part. I'm training a model and autoanchor showed up, so when using the trained ckpt for inference and load model in my downstream project, should I modify *.yaml accordingly, or use the old *.yaml ?

@glenn-jocher
Copy link
Member

@Shadow-Alex yes, you should update the *.yaml file with the newly generated anchor values obtained from the autoanchor process. These updated anchor values are saved to the model, and to ensure consistency during inference and when loading the model in your downstream project, it is important to use the updated *.yaml file that includes the new anchor values. Make sure to use the modified *.yaml file for both training and inference to ensure proper anchor box calculations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

6 participants