Anchor boxes interpretation

Hello!
What do the numbers in anchors tell us? Why are there 6 values? And what are the P3/8, P4/16 and P5/32?

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

Best regards
Sara Larsson

1 Like

@Sara980710 :wave: Hello! Thanks for asking about model anchors. Each row in the YAML corresponds to an output layer, i.e. P3 is for small objects and has 3 anchors of width,height 10,13, etc.

YOLOv5 :rocket: 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

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:

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

1 Like

Thanks for the answer! Are P3, P4 and P5 then used in different parts/layers of the FPN structure since it seem like there are 3 levels of the FPN, or are they all present in the feature maps throughout the FPN?

P3 just refers to a 2 ** 3 stride, can apply to any layer in any part of the model.

1 Like

Am I correct in thinking that more anchors could increase recall?

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
    I have a question like how to find these anchor boxes values? and what exact these values represent. As for my understanding, first is width, second is height and what other values? How can I find my own anchor based on my own custom dataset for --noautoanchor.