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

infer.py doesn't work, variable referenced before assignment #52

Open
PT0X0E opened this issue Jul 8, 2022 · 3 comments
Open

infer.py doesn't work, variable referenced before assignment #52

PT0X0E opened this issue Jul 8, 2022 · 3 comments

Comments

@PT0X0E
Copy link

PT0X0E commented Jul 8, 2022

Hi, thanks for the work!
But I think there is an obvious bug at line 56 in src_files/models/utils/factory.py:
model.load_state_dict(state[key], strict=True)
The variable key in else branch is used without definition. So when load_head argument is True, which True in infer.py, the code doesn't work.

@BigBuffa1o
Copy link

Move the if else loop out of the load head loop will solve this problem.

        if 'model' in state:
            key = 'model'
        else:
            key = 'state_dict'
        if not load_head:
            filtered_dict = {k: v for k, v in state[key].items() if
                             (k in model.state_dict() and 'head.fc' not in k)}
            model.load_state_dict(filtered_dict, strict=False)
        else:
            model.load_state_dict(state[key], strict=True)

@yangyangtiaoguo
Copy link

Move the if else loop out of the load head loop will solve this problem.

        if 'model' in state:
            key = 'model'
        else:
            key = 'state_dict'
        if not load_head:
            filtered_dict = {k: v for k, v in state[key].items() if
                             (k in model.state_dict() and 'head.fc' not in k)}
            model.load_state_dict(filtered_dict, strict=False)
        else:
            model.load_state_dict(state[key], strict=True)

Still does not work, model loading failed.
RuntimeError: Error(s) in loading state_dict for TResNet:

@KoSweet
Copy link

KoSweet commented Oct 2, 2023

The solution works for me. Thanks

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

No branches or pull requests

4 participants