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 to interpret autoanchor log and improve more BPR #7118

Closed YoungjaeDev closed 2 years ago

YoungjaeDev commented 2 years ago

Search before asking

Question

Hello, I train with custom dataset, and it has various data size. Therefore, I set it to 4 while using the annotation of anchor from hyp because I wanted to customize it and get higher mAP performance. But I have a question about the autoanchor log.

AutoAnchor: 0.48 anchors/target, 0.114 Best Possible Recall (BPR). Anchors are a poor fit to dataset ⚠️, attempting to improve...
AutoAnchor: WARNING: Extremely small objects found: 256 of 129021 labels are < 3 pixels in size
AutoAnchor: Running kmeans for 12 anchors on 129018 points...
AutoAnchor: Evolving anchors with Genetic Algorithm: fitness = 0.7203: 100%|██████████| 1000/1000 [00:06<00:00, 154.38it/s]                                                                    
AutoAnchor: thr=0.25: 0.9962 best possible recall, 5.13 anchors past thr
AutoAnchor: n=12, img_size=960, metric_all=0.266/0.721-mean/best, past_thr=0.468-mean: 21,14, 40,27, 86,24, 57,54, 131,52, 94,143, 238,79, 229,171, 526,118, 305,442, 664,274, 794,574
AutoAnchor: Done ✅ (optional: update model *.yaml to use these anchors in the future)
  1. what is Evolving anchors with Genetic Algorithm: fitness (0.7203)? Is 0.7203 an appropriate value?
  2. thr=0.25, Does thr mean the iou threshold of gt and anchor?
  3. What is past_ptr? So it's confusing what the value of 5.13 anchor is also available. In the end, it is questionable whether anchor should be reset to 5.
  4. In the end, it seems to be going to 4 anchor now, how do you improve more than the present when you look at the log?

Thank you

Additional

No response

github-actions[bot] commented 2 years ago

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

@youngjae-avikus 👋 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!

YoungjaeDev commented 2 years ago

@glenn-jocher Thank you. Additionally, I would like to know my log Autoanchor 5.13 anchors past thr. Does it mean that BPR is 0.99 when set to Anchor 5?

glenn-jocher commented 2 years ago

@youngjae-avikus anchors past threshold counts the number of anchors that match a label on average for your dataset.

YoungjaeDev commented 2 years ago

@glenn-jocher When I read the code related check_anchor, I don't understand how it comes out because I don't understand evolve side, but in conclusion, did past-threshold say 5 as an anchor that overfits my dataset? Or does 5 mean 5anchors/target? Thank you

glenn-jocher commented 2 years ago

5 anchors per target

YoungjaeDev commented 2 years ago

@glenn-jocher

The image data changes when fine-tuning, but the number of anchor boxes doesn't have to be the same (eg. coco) as before, so it's better to turn on the anchor option, is it right?

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 ⭐!

glenn-jocher commented 11 months ago

@youngjae-avikus yes, that's correct. It's often helpful to tune the anchor options when fine-tuning on a custom dataset with different characteristics than the original dataset. This can help optimize performance for your specific use case.