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

Unexpected key(s) in state_ dict: "epoch", "best_acc", "state_dict". #387

Closed
shanpriya3 opened this issue May 29, 2024 · 3 comments
Closed

Comments

@shanpriya3
Copy link

shanpriya3 commented May 29, 2024

Same as this issue(#71 (comment) trained my model using custom dataset and when I run test.py, I am getting the following error:
Running,
python test.py
--in_channels=1
--out_channels=3
--space_x=1.0
--space_y=1.0
--space_z=1.0
--feature_size=32
--infer_overlap=0.5
--data_dir="/cluster/home/shan/ves/research-contributions/UNETR/BTCV/dataset/us/"
--pretrained_dir="/cluster/home/shan/ves/research-contributions/UNETR/BTCV/runs/unetrmonai_test/"
--pretrained_model_name="model.pt"
--saved_checkpoint=ckpt
produces the following error: Unexpected key(s) in state_dict: "epoch", "best_acc", "state_dict".

How to solve this? Thanks!

@ericspod
Copy link
Member

I think the model.pt file you have contains information in addition to the weights for the model. The weights are under the key "state_dict" in this file, so you can extract them yourself and save to another file:

import torch
m=torch.load("model.pt")
torch.save(m["state_dict"],"model1.pt")

Something like this should work then you can try loading the new file.

@shanpriya3
Copy link
Author

Thanks. It is working!

@ericspod
Copy link
Member

No problem, closing as completed then.

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

2 participants