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

How are anchors updated during training? #10201

Closed AlessandroMondin closed 1 year ago

AlessandroMondin commented 1 year ago

Search before asking

Question

I am trying to understand the loss function and I noticed that the anchors are only defined in the ComputeLoss class with self.anchors = m.anchors (here) and I could'n find nor in this class nor in the train.py anything related to updating such anchors.

My question is, where are anchors updated during training?

Additional

No response

github-actions[bot] commented 1 year ago

👋 Hello @AlessandroMondin, 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

YOLOv5 CI

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

glenn-jocher commented 1 year ago

@AlessandroMondin 👋 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

Screenshot 2022-03-24 at 10 58 28

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

For more details on AutoAnchor see: https://github.com/ultralytics/yolov5/blob/master/utils/autoanchor.py

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

AlessandroMondin commented 1 year ago

Hi Glen! Thanks for sharing! Therefore after the initial research for best anchors with auto-anchors, these are fixed during yolo training and are not treated as yolo parameters (modified with back-propagation?

glenn-jocher commented 1 year ago

@AlessandroMondin yes that's correct, anchors are not modified once training starts, they do not have a gradient.

AlessandroMondin commented 1 year ago

Thanks a lot Glenn! Doubts solved 👍

glenn-jocher commented 1 year ago

@AlessandroMondin I tried making anchors learnable parameters once, but did not get good results this way.

bot66 commented 1 year ago

@glenn-jocher Hi Glenn, I wonder yolov5*.yaml default anchor value is based on the training image size of 640, so if I set the training image size to 1280, does it make any sense if I scale all the anchor width and height to 2 times larger? Or even bigger image size, say:5120, then scaling the anchors is a must since the build_targets will reject anchors that are smaller than gt bbox after multiplying the anchor_t which is 4.

I don't know if this is correct.

Yosu26 commented 5 months ago

@glenn-jocher Hi Glenn, Is this autoanchor a good thing or not?

glenn-jocher commented 5 months ago

@Yosu26 Hey there! 🚀 AutoAnchor is definitely a good thing. It helps ensure that the anchors are well-suited to your specific dataset, potentially improving model accuracy and performance right from the start of training. It automatically adjusts if it detects that your current anchors are not an optimal fit, without requiring manual recalibration. So, yes, it's designed to make your life easier and your model more effective! 😊