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

KeyError: 'model.24.anchor_grid' with v6.0 #5379

Closed
deepsworld opened this issue Oct 28, 2021 · 5 comments
Closed

KeyError: 'model.24.anchor_grid' with v6.0 #5379

deepsworld opened this issue Oct 28, 2021 · 5 comments
Labels
question Further information is requested

Comments

@deepsworld
Copy link
Contributor

deepsworld commented Oct 28, 2021

❔Question

There is an additional key called model.24.anchor_grid in yolov5-v6.0 checkpoints which does not exist in the model loaded with hub.load(pretrained=False). I am not sure if this is intended or I am missing any argument. I appreciate your help.

Additional context

Minimal code to reproduce the error

import torch
from torch import hub
# pytorch 1.9 workaround 
torch.hub._validate_not_a_forked_repo=lambda a,b,c: True

def github_release_url(owner, project, tag, name):
    return f"https://github.com/{owner}/{project}/releases/download/{tag}/{name}"

tag = 'v6.0'
chkpt = 'yolov5x'
model = hub.load(f'ultralytics/yolov5:{tag}', chkpt, False, 3, 80, force_reload=True, autoshape=False)
url = github_release_url('ultralytics', 'yolov5', tag, f'{chkpt}.pt')
chkpt = hub.load_state_dict_from_url(url,
                                     model_dir=None,
                                     map_location=torch.device('cpu'))
state_dict = chkpt['model'].float().state_dict()

try:
    state_dict = { k: v for k, v in state_dict.items() if model.state_dict()[k].shape == v.shape }
except Exception as e:
    raise e
@deepsworld deepsworld added the question Further information is requested label Oct 28, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Oct 28, 2021

👋 Hello @deepsworld, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@deepsworld
Copy link
Contributor Author

deepsworld commented Oct 29, 2021

NOTE: The version v5.0 when loaded in similar fashion works fine.
@glenn-jocher Is there a reason the register buffer for anchor grid was removed from the Detect module and only added when pretrained is True. Here: https://github.com/ultralytics/yolov5/blob/v5.0/models/yolo.py#L37

@glenn-jocher
Copy link
Member

glenn-jocher commented Oct 30, 2021

@deepsworld yes this change was introduced in OpenCV DNN PR #4833, along with backward compatibility updates to allow previous version models to load and run correctly.

@Wangwang99999
Copy link

I don't see any answer to this question. Why there is no 'model.24.anchor_grid' in trained model, but it do exist in pretrained models.

@glenn-jocher
Copy link
Member

@Wangwang99999 architecture may update over time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants