ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.33k stars 16.24k forks source link

Setting the right anchor size to your model #6838

Closed ShaharSarShalom closed 2 years ago

ShaharSarShalom commented 2 years ago

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:

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

Additional

No response

github-actions[bot] commented 2 years ago

πŸ‘‹ 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 support@ultralytics.com.

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 commented 2 years ago

πŸ‘‹ 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 commented 2 years ago

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[bot] commented 2 years ago

πŸ‘‹ 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 ⭐!

tbonewmy commented 2 years ago

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:

glenn-jocher commented 2 years ago

@tbonewmy yes that's correct!

tbonewmy commented 2 years ago

@tbonewmy yes that's correct.

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

glenn-jocher commented 2 years ago

@tbonewmy they correspond to --imgsz images

tbonewmy commented 2 years ago
- [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 commented 2 years ago
Screen Shot 2022-08-04 at 3 57 52 PM
NQHuy1905 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

@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.