Project-MONAI / research-contributions

Implementations of recent research prototypes/demonstrations using MONAI.
https://monai.io/
Apache License 2.0
995 stars 328 forks source link

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

Closed shanpriya3 closed 3 months ago

shanpriya3 commented 3 months ago

Same as this issue(https://github.com/Project-MONAI/research-contributions/issues/71#issuecomment-1172886171_):I 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 commented 3 months ago

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 commented 3 months ago

Thanks. It is working!

ericspod commented 3 months ago

No problem, closing as completed then.