research-contributions icon indicating copy to clipboard operation
research-contributions copied to clipboard

Training model cannot load properly

Open SiruLiu645 opened this issue 2 years ago • 5 comments

I use a heart CT dataset to train this model,and I save this model when loss decrease to 0.7. However, I cannot load this pretrained model when I run test.py. image Then I changed the code "model_dict = torch.load(pretrained_pth)" to "model_dict = torch.load(pretrained_pth)["state_dict"]", it came to another problem: image

SiruLiu645 avatar Oct 11 '23 02:10 SiruLiu645

yes, I have the same problem!!! Do you have a solution now?

taishanglaojunya avatar Jun 10 '24 07:06 taishanglaojunya

Hi both, sorry for the lateness of reply. I think the latter case when you're loading from the "state_dict" key is the correct thing to do, however the model that you've instantiated has different constructor arguments from the model that these weights were saved from. You'll need to figure out what the parameters were exactly and be sure your class is instantiated correctly, if your weights are very old it's possible the class definition has changed and you may have to get the old version of the code from a previous MONAI release on Github and copy that into your project.

ericspod avatar Jun 10 '24 11:06 ericspod

谢谢你的提示!

Eric Kerfoot @.***> 于2024年6月10日周一 19:42写道:

Hi both, sorry for the lateness of reply. I think the latter case when you're loading from the "state_dict" key is the correct thing to do, however the model that you've instantiated has different constructor arguments from the model that these weights were saved from. You'll need to figure out what the parameters were exactly and be sure your class is instantiated correctly, if your weights are very old it's possible the class definition has changed and you may have to get the old version of the code from a previous MONAI release on Github and copy that into your project.

— Reply to this email directly, view it on GitHub https://github.com/Project-MONAI/research-contributions/issues/322#issuecomment-2158118277, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDN4R34OE7NQB46IDWASUTTZGWGMFAVCNFSM6AAAAABJBXKC5WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJYGEYTQMRXG4 . You are receiving this because you commented.Message ID: @.***>

taishanglaojunya avatar Jul 08 '24 11:07 taishanglaojunya

I use a heart CT dataset to train this model,and I save this model when loss decrease to 0.7. However, I cannot load this pretrained model when I run test.py. image Then I changed the code "model_dict = torch.load(pretrained_pth)" to "model_dict = torch.load(pretrained_pth)["state_dict"]", it came to another problem: image

How did you solve it in the end?

Yikol avatar Mar 12 '25 05:03 Yikol

yes, I have the same problem!!! Do you have a solution now?

How did you solve it in the end?

Yikol avatar Mar 12 '25 05:03 Yikol