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 get our own anchors? #2585

Closed
BrianKenobi opened this issue Mar 24, 2021 · 5 comments
Closed

How to get our own anchors? #2585

BrianKenobi opened this issue Mar 24, 2021 · 5 comments
Labels
Stale Stale and schedule for closing soon

Comments

@BrianKenobi
Copy link

BrianKenobi commented Mar 24, 2021

I have the same problem as issue #2394. Without autoanchor, I can't get my recall more than 0.663. So I am thinking about how to get my own data's anchor so I can have a better result. Thanks.

well that's an improvement at least. Though I see your BPR is only 0.65, which means that your recall and mAP will never be higher than 0.65. You should try to supply your own manual anchors in your yolov5s.yaml file if possible to better match your data:

# anchors
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32

Originally posted by @glenn-jocher in #2394 (comment)

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 24, 2021

@BrianKenobi YOLOv5 anchors are saved as Detect() layer attributes on model creation, and updated as necessary by autoanchor before training starts. Their exact location is here:

yolov5/models/yolo.py

Lines 34 to 35 in fab5085

self.register_buffer('anchors', a) # shape(nl,na,2)
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)

You can examine the anchors of any trained YOLOv5 model like this:

Input

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False)  # hub model
# -- or --
model = torch.load('yolov5s.pt')['model']  # local model

# Anchors
m = model.model[-1]  # Detect() layer
print(m.anchor_grid.squeeze())  # print anchors

Output

#           x     y
tensor([[[ 10.,  13.],
         [ 16.,  30.],
         [ 33.,  23.]],  # P3/8-small

        [[ 30.,  61.],
         [ 62.,  45.],
         [ 59., 119.]],  # P4/16-medium

        [[116.,  90.],
         [156., 198.],
         [373., 326.]]], dtype=torch.float16)  # P5/32-large

@BrianKenobi
Copy link
Author

BrianKenobi commented Mar 25, 2021

@glenn-jocher thank you!
in the line :

model = torch.load('yolov5s.pt')['model'] # local model

I'm not sure what should I put in ['model'].
So I only change 'yolov5s.pt' to my trained YoloV5x model 'best.pt'.
And the result is "ModuleNotFoundError: No module named 'models'"

anchor

anchor result

@BrianKenobi
Copy link
Author

BrianKenobi commented Mar 25, 2021

I solved the problem by put the code in the YoloV5 folder. Thank you @glenn-jocher again!

But as the result, I still get the default anchor.
I am wondering how can I calculate my own data's anchor because using default anchors I won't get my recall higher than 0.663.
anchor result

@glenn-jocher
Copy link
Member

@BrianKenobi yes this is true. YOLOv5 models must either be loaded within the yolov5/ directory or via PyTorch Hub to enabled access to required module imports. To load from anywhere via PyTorch Hub:

model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='best.pt')  # custom model

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Apr 25, 2021
@github-actions github-actions bot closed this as completed May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

2 participants