Anchor boxes are important parameters of deep learning object detectors such as Faster R-CNN and YOLO v2. The shape, scale, and number of anchor boxes impact the efficiency and accuracy of the detectors.

**Load Training Data**

Load the vehicle dataset, which contains 295 images and associated box labels.

Add the full path to the local vehicle data folder.

Display the data set summary.

Variables:
imageFilename: 295×1 cell array of character vectors
vehicle: 295×1 cell

**Visualize Ground Truth Box Distribution**

Visualize the labeled boxes to better understand the range of object sizes present in the data set.

Combine all the ground truth boxes into one array.

Plot the box area versus the box aspect ratio.

The plot shows a few groups of objects that are of similar size and shape, However, because the groups are spread out, manually choosing anchor boxes is difficult. A better way to estimate anchor boxes is to use a clustering algorithm that can group similar boxes together using a meaningful metric.

**Estimate Anchor Boxes**

Estimate anchor boxes from training data using the `estimateAnchorBoxes`

function, which uses the intersection-over-union (IoU) distance metric.

A distance metric based on IoU is invariant to the size of boxes, unlike the Euclidean distance metric, which produces larger errors as the box sizes increase [1]. In addition, using an IoU distance metric leads to boxes of similar aspect ratios and sizes being clustered together, which results in anchor box estimates that fit the data.

Create a `boxLabelDatastore`

using the ground truth boxes in the vehicle data set. If the preprocessing step for training an object detector involves resizing of the images, use `transform`

and `bboxresize`

to resize the bounding boxes in the `boxLabelDatastore`

before estimating the anchor boxes.

Select the number of anchors and estimate the anchor boxes using `estimateAnchorBoxes`

function.

anchorBoxes = *5×2*
21 27
87 116
67 92
43 61
86 105

Choosing the number of anchors is another training hyperparameter that requires careful selection using empirical analysis. One quality measure for judging the estimated anchor boxes is the mean IoU of the boxes in each cluster. The `estimateAnchorBoxes`

function uses a *k*-means clustering algorithm with the IoU distance metric to calculate the overlap using the equation, `1 - `

`bboxOverlapRatio`

`(allBoxes,boxInCluster)`

.

The mean IoU value greater than 0.5 ensures that the anchor boxes overlap well with the boxes in the training data. Increasing the number of anchors can improve the mean IoU measure. However, using more anchor boxes in an object detector can also increase the computation cost and lead to overfitting, which results in poor detector performance.

Sweep over a range of values and plot the mean IoU versus number of anchor boxes to measure the trade-off between number of anchors and mean IoU.

Using two anchor boxes results in a mean IoU value greater than 0.65, and using more than 7 anchor boxes yields only marginal improvement in mean IoU value. Given these results, the next step is to train and evaluate multiple object detectors using values between 2 and 6. This empirical analysis helps determine the number of anchor boxes required to satisfy application performance requirements, such as detection speed, or accuracy.