Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Fixed the tutorial for Windows - #483

Closed
StijnvWijn wants to merge 1 commit into
Project-MONAI:mainfrom
StijnvWijn:482-SPADE-VAE-tutorial-not-working
Closed

StijnvWijn wants to merge 1 commit into
Project-MONAI:mainfrom
StijnvWijn:482-SPADE-VAE-tutorial-not-working

Conversation

@StijnvWijn

Copy link
Copy Markdown
Contributor

The following things were changed to get it working on Windows again:

  1. Wrapped the train and data preparation loop in a main() function
  2. Moved one_hot(), picture_results() and feature_loss() functions outside of the main() function
  3. Added a _masked_area() function to replace the lambda function in the Lambdad transform, as I got an error when I ran it with the lambda function.

@marksgraham

Copy link
Copy Markdown
Collaborator

@ericspod do you have any thoughts here? I've not experience running on windows, i note none of the MONAI tutorials make use of a main() so I wonder if this is necessary for windows compatibility?

@StijnvWijn

Copy link
Copy Markdown
Contributor Author

AFAIK the main() is not necessary for Windows in all cases, but the moment you need to use multiprocessing, like a dataloader with workers > 1, you need an if statement like this:
if name == "main":
main()
It is described in detail in the Pytorch documentation

@marksgraham

Copy link
Copy Markdown
Collaborator

Hmm, I think in general the tutorials are meant to give an overall example of how to run the code which users can then take and build upon locally if they want more performant versions. So it might be fine to just set workers = 1 for compatibility with windows here, rather than reconfiguring the tutorials. But I'll let eric weight in here as he has more experience with how things are done on MONAI core :)

@StijnvWijn

Copy link
Copy Markdown
Contributor Author

The same issues are occurring when I try to use the LDM tutorial python file, so if you want, I can make a separate PR for that one as well.

@marksgraham

Copy link
Copy Markdown
Collaborator

@ericspod can we get your input on how we should handle these tutorials not working on windows out the box? does MONAI core have an approach?

@ericspod

Copy link
Copy Markdown
Member

Hi @marksgraham @StijnvWijn There's two things going on here. We're generating the tutorial script files from the notebooks so they lack the main function for that reason, that isn't happening in the main tutorials repo so those script files can have a main function guarded with if __name__ == "__main__":. This is best practice that I'd recommend always regardless of the process spawn issue you're encountering.

The second thing is that for those tutorials or ours here in GenerativeModels the fix for notebooks is much harder, so we have said that Windows users need to use 0 workers. We haven't been consistent with putting that in the code (like here) but it's what works. Script files should be fine still, it's just notebooks that would have an issue using spawn semantics.

So for us here if we're create a script file independently of a notebook we should follow what Python requires for Windows, ie. the guarded main function so that process spawning works. If it's a notebook we use multiple workers but write a comment to state the Windows issue since we still want to generate script files from these. Does this seem sensible?

@marksgraham

Copy link
Copy Markdown
Collaborator

Given we have tied our scripts to our notebooks with jupytext there will be no easy way to reconfigure all of the .py tutorials without affecting the notebooks. So @StijnvWijn I think the best fix we could do for now would involve adding a comment, either in each tutorial or in the README, discussing the changes that would need to be made to get these to work on windows.

Once we move these tutorials over to MONAI we might decouple notebooks and python files and we could fix this more generally.

@StijnvWijn

Copy link
Copy Markdown
Contributor Author

Hmm yeah, I also do not see a way that we can fix it right now. A workaround has been mentioned in the Jupytext issue 592, but that seems like a very hacky solution which is not really desirable.

I will close this issue for now.

@StijnvWijn StijnvWijn closed this Apr 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants