Many detectors, from some one-stage designs to the proposal step of many two-stage ones, start from a fixed set of template boxes called anchors, laid out across the image at set positions and sizes. For each training photo, before the loss is computed, every anchor is given a job by comparing it with that photo's true object boxes using IoU (the area two boxes share divided by the total area they cover between them).
Step 1: thresholds. For each anchor, find the object it overlaps best: the highest IoU, with the lower object index winning a tie. Then:
high, the anchor is responsible for that object. Its label is the object's index.low, the anchor is background. Its label is -1.-2, which means "ignore".Step 2: no object left behind. A small or awkwardly placed object might not reach high with any anchor. So go through the objects in order. For each one, find the anchor that overlaps it best (the lowest anchor index wins a tie). If that IoU is greater than 0, set that anchor's label to this object's index, replacing whatever label it already has, even another object's. When two objects pick the same anchor, the later object's label is the one that stays.
Task: write match_anchors(anchors, objects, high, low).
[x_min, y_min, x_max, y_max], with x_min < x_max and y_min < y_max.low <= high.anchors.