ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.48k stars 16.08k forks source link

Anchors scales and aspect ratios. #6103

Closed myasser63 closed 2 years ago

myasser63 commented 2 years ago

Search before asking

Question

I want to understand more about anchors for yolov5s. what are the scales and aspect ratios used to generate the default anchor in yolov5s. Thanks in advance.


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

Additional

No response

glenn-jocher commented 2 years ago

@myasser63 👋 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!

iumyx2612 commented 2 years ago

@myasser63 👋 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!

I have 3 questions:

  1. In YOLOv2, Redmon introduced clustering box dimension, which also uses kmeans to find best fit box on the dataset. In YOLOv5, this is further enhanced by applying Genetic Evolution (GE) algorithm right? Can I have the link to the Genetic Evolution algorithm?
  2. If I want to algorithm to pick good anchors for me every time, can I simply don't specify anchors in the config file?
  3. Can you further explain the comments like # P3/8, # P4/16, # P5/32 @glenn-jocher
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 ⭐!