Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use anchors to make predictions in object detection 🔥 #1

Open
hotcouscous1 opened this issue Aug 20, 2022 · 0 comments
Open
Labels
good first issue Good for newcomers

Comments

@hotcouscous1
Copy link
Owner

Anchor-based detection models have been mainly developed over four series.

  • Faster R-CNN
  • YOLO
  • RetinaNet
  • SSD

Each anchor-based detection model series has a different manners of creating anchor priors, creating anchors, and combining them with the model's regression predictions.
Despite slight variations from model to model, models included in a series basically share the same manners.

Now let's see the examples of how Anchor-Maker gives its anchors to models 🔥

YOLO

img_size = 416
anchor_sizes = [
    [(10, 13), (16, 30), (33, 23)], 
    [(30, 61), (62, 45), (59, 119)], 
    [(116, 90), (156, 198), (373, 326)]
]
strides = [8, 16, 32]
anchors = yolo_anchors(img_size, anchor_sizes, strides)

for i, s in enumerate(strides):
    pred[i][..., :2] = torch.sigmoid(pred[i][..., :2]) * s + anchors[i][..., :2]
    pred[i][..., 2:4] = torch.exp(pred[i][..., 2:4]) * anchors[i][..., 2:]

Here, we should note that in YOLO, unlike the others, anchors and model predictions are a list of tensors on each stride, not tensors flattened over given strides.

RetinaNet

img_size = 608
anchor_sizes = [32, 64, 128, 256, 512]
anchor_scales = [1, 2**(1/3), 2**(2/3)]
aspect_ratios = [(2**(1/2), 2**(-1/2)), (1, 1), (2**(-1/2), 2**(1/2))]
strides = [8, 16, 32, 64, 128]

anchors = retinanet_anchors(img_size, anchor_sizes, anchor_scales, aspect_ratios, strides)

pred[..., :2] = anchors[..., :2] + (pred[..., :2] * anchors[..., 2:])
pred[..., 2:4] = torch.exp(pred[..., 2:4]) * anchors[..., 2:]

SSD

img_size = 300
anchor_sizes = [21, 45, 99, 153, 207, 261]
upper_sizes = [45, 99, 153, 207, 261, 315]
strides = [8, 16, 32, 64, 100, 300]
num_anchors = [4, 6, 6, 6, 4, 4]

anchors = ssd_anchors(img_size, anchor_sizes, upper_sizes, strides, num_anchors)

pred[..., :2] = anchors[..., :2] + (center_variance * pred[..., :2] * anchors[..., 2:])
pred[..., 2:4] = torch.exp(size_variance * pred[..., 2:4]) * anchors[..., 2:]

Of course, you can freely generate anchors. For example, if you want to create anchor priors in RetinaNet way, but combine anchors and predictions in YOLO style, you can write

anchor_priors = retinanet_anchor_priors(anchor_sizes, anchor_scales, aspect_ratios, strides)
anchors = Anchor_Maker(anchor_priors, strides, center=False, clamp=False, relative=False)(img_size)

pred[..., :2] = torch.sigmoid(pred[..., :2]) * s + anchors[..., :2]
pred[..., 2:4] = torch.exp(pred[..., 2:4]) * anchors[..., 2:]

Note that the important thing is that anchor priors and anchors can be generated seperately. The above cases are merely examples that are generally used.

@hotcouscous1 hotcouscous1 added the good first issue Good for newcomers label Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant