Resources from the book

Anchor Calculator

Section 21.4 · Calculate dataset-specific anchors for your YOLO configuration.

Section 21.4.1

Purpose

Calculate custom anchors for a YOLO configuration using the bounding-box sizes in your training dataset.

Use 21.2 · Bounding Box Visualizer first to review the approximate number and distribution of box-size groups, and 21.3 · Grid / Stride Visualizer when evaluating detection-layer strides.

Section 21.4.2

Why It Exists

Anchors that reflect your dataset’s box sizes provide a better starting point than poorly matched anchors. The book describes this as an important training step: a network can still train with poor anchors, but they can waste iterations and weaken the result.

This is a lightweight alternative to the standard Darknet anchor calculator. Use whichever calculator consistently produces anchors that perform well in your training workflow.

Section 21.4.3

Input

Use an exported YOLO / Darknet training dataset. All source images must have the same resolution. The script finds the first readable PNG, JPG, or JPEG to determine that resolution, then recursively collects annotations from folders named labels.

class_id  center_x  center_y  width  height
0         0.50      0.50      0.25   0.20

Width and height are normalized. The calculator converts them to source-image pixel dimensions before clustering. It ignores empty files and skips lines without exactly five values. Five-value lines must be numeric, with valid positive box dimensions.

Anchors depend on box size, not class name or class ID. All collected boxes are considered together.

Before you run it

Use lowercase labels folder names and .txt extensions. The script assumes a common source resolution; it does not verify every image. Choose a positive anchor count no larger than the number of collected boxes.

Review 21.1 · Data Structure & Python Setup →

Section 21.4.4

Output

The program prints the anchors and two clustering diagnostics in the terminal. The book shows this example:

anchors=19,24,67,83
Average IoU: 0.7204
Iterations until convergence: 3

Illustrative book output; your dataset and settings determine the actual values.

OutputMeaning
anchors=19,24,67,83Two width/height pairs: (19, 24) and (67, 83), in pixels at the requested network resolution. Anchors are sorted by increasing width.
Average IoUThe mean best-match overlap between each box and its closest anchor shape, assuming shared centers. Values closer to 1 indicate a closer shape fit.
IterationsClustering iterations performed. Assignments stabilize when each box remains assigned to the same cluster.

IoU is intersection area divided by union area. The diagnostic is calculated using the unrounded clusters in source-image coordinates; the printed anchors are scaled and rounded for your network.

The script prints results without editing your configuration. Review the anchor count and your configuration’s head/mask arrangement before applying the values. Average IoU describes anchor fit; it is not a model accuracy score.

The guide behind the codeFrom the page
to the plant floor.
Practical Guide to AI Computer Vision Integration in Industrial ManufacturingExplore the Book