Anchor creation in fastai docs repository

I am looking at the anchor creation code here:

def create_anchors(sizes, ratios, scales, flatten=True):
    "Create anchor of `sizes`, `ratios` and `scales`."
    aspects = [[[s*math.sqrt(r), s*math.sqrt(1/r)] for s in scales] for r in ratios]
    aspects = torch.tensor(aspects).view(-1,2)
    anchors = []
    for h,w in sizes:
        #4 here to have the anchors overlap.
        sized_aspects = 4 * (aspects * torch.tensor([2/h,2/w])).unsqueeze(0)
        base_grid = create_grid((h,w)).unsqueeze(1)
        n,a = base_grid.size(0),aspects.size(0)
        ancs =[base_grid.expand(n,a,2), sized_aspects.expand(n,a,2)], 2)
    return[anc.view(-1,4) for anc in anchors],0) if flatten else anchors

I am unable to understand the use of 4 here. Isn’t that just scaling all the given scales with 4? Why is that important?

The 4 is there so that each anchor is four times bigger than the corresponding square in the grid. This is the size of the corresponding receptive field.
This is also why the scales should be adjusted if the image is not 512 pixels of size minimum (otherwise the anchors will all be too big). RetinaNet paper uses a size of 600 minimum.

Just confirming, that it is equivalent to having scales = 4 * np.array([1,2**(1/3), 2**(2/3)]) instead of scales = [1,2**(1/3), 2**(2/3)] and not having the 4 in the create anchors code?

Yeah, I was also struggling to get good iou matches when using image size of 300. Which is why I was trying to see what the underlying problem was.

It’s completely equivalent, yes.

1 Like