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

Fix silent fail if network loads wrong weights #1521

Merged
merged 6 commits into from
Aug 26, 2023

Conversation

matt3o
Copy link
Contributor

@matt3o matt3o commented Aug 8, 2023

This is a draft for fixing #1508 . It sets load_strict to False in basic_infer.py, also adds a warning in case load_strict is False but the model does not contain all the necessary keys.
Most importanly this PR helps the creation of new models where one might not even realize that the loaded network does not contain the necessary keys, which is what happened to me.

For default the DeepEdit the warning looks like this:
image

It is a bit hidden, but nevertheless if anyone where to debug the code, this should probably help.

Todo:

  • Is this solution a valid solution for everyone?
  • Add load_strict=False to all existing models (only done for DeepEdit so far)

@matt3o matt3o changed the title Fix silent fail if network loading wrong weights Fix silent fail if network loads wrong weights Aug 8, 2023
@matt3o matt3o force-pushed the Fix_network_loading branch from d82dea1 to c3c9d83 Compare August 8, 2023 08:04
@matt3o matt3o marked this pull request as draft August 14, 2023 12:21
@matt3o
Copy link
Contributor Author

matt3o commented Aug 14, 2023

@tangy5 Can you shortly check if this PR is fine so far? Then I would add self.load_strict = False to all the other models as well

@tangy5
Copy link
Collaborator

tangy5 commented Aug 14, 2023

It looks good to me, Thanks. @SachidanandAlle @diazandr3s , do we have further concerns if we set load_strict to true here by default? Im thinking it should be OK, but I'm not sure about all of our pretrained model weights. Thanks

@SachidanandAlle SachidanandAlle marked this pull request as ready for review August 17, 2023 17:12
@@ -57,7 +57,7 @@ def __init__(
output_label_key: str = "pred",
output_json_key: str = "result",
config: Union[None, Dict[str, Any]] = None,
load_strict: bool = False,
load_strict: bool = True,
Copy link
Collaborator

@SachidanandAlle SachidanandAlle Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
load_strict: bool = True,
load_strict: bool = False,

lets keep the base class value same.. lets not change.. we override it to true in all derived classes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then only the warning?
My idea behind that was that this makes adding new models harder. Which is why I proposed changing the default to True and setting the value to False in all the existing models.
In my case the network would not load any weights at all but MONAILabel would not tell me. The state dict was completely different but I did not even get a warning. I was thus wondering for a few hours why a perfectly fine network wouldn't work in MONAILabel...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case can you verify all sample apps..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you mean that? If you mean that that would imply changing all the current sample apps, then I agree, yes. That's why I was asking as well if this change is desired at all. Imo it is worth the effort since it makes creating new models easier and less prone to weird errors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SachidanandAlle Can you explain what you mean?

Copy link
Collaborator

@SachidanandAlle SachidanandAlle Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep this flag=True, we need to make sure existing pre-trained models can load with this flag.
radiology/pathology and endoscopy apps/models. if some can't then add flag=False for those models for which it fails.

otherwise it will break the release workflows.

Copy link
Contributor Author

@matt3o matt3o Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SachidanandAlle Oh I'm sorry, I now get what you meant initially. What I had planned was to add that flag in the step, when you guys generally approve of the changes, which I was not sure of.
However you already merged the branch now.
Nevertheless I would still add the flag=False for all existing models or do you want to do that based on feedback what breaks?
Not sure if I can execute all the models, so I would simply add it to all current models.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add that today and update this PR / open another one if I can't do that. It might break some preexisting code otherwise.

@SachidanandAlle
Copy link
Collaborator

I will merge this for now.. but better this gets verified for other existing example models/apps

@SachidanandAlle SachidanandAlle merged commit 7208641 into Project-MONAI:main Aug 26, 2023
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

Successfully merging this pull request may close these issues.

3 participants