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

Changed basic_examples to use LightningCLI #6862

Merged
merged 12 commits into from
Apr 15, 2021
Merged

Changed basic_examples to use LightningCLI #6862

merged 12 commits into from
Apr 15, 2021

Conversation

mauvilsa
Copy link
Contributor

@mauvilsa mauvilsa commented Apr 7, 2021

What does this PR do?

Changed autoencoder.py, backbone_image_classifier.py, profiler_example.py and simple_image_classifier.py in pl_examples to use the new LightningCLI. Related to the examples some improvements are also made to LightningCLI that seemed necessary.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Apr 7, 2021

@carmocca An example changed to use LightningCLI. It could be argued that seed_everything and test could become part of LightningCLI. But with the current implementation this could be the example and illustrates how to use a couple of methods.

@carmocca
Copy link
Contributor

carmocca commented Apr 7, 2021

Should the LightningCLI allow {fit,validate,test,tune,predict}? So by default you can do:

python simple_image_classifier.py fit --trainer.max_epochs=50 or python simple_image_classifier.py validate --trainer.limit_val_batches=30

Also yes, seed_everything should be inside before_instantiate_classes by default as the LightningCLI is about simple reproducibility.

And what do you think about LightningCLI returning the trainer, model, and data instances?

trainer, model, data = MyLightningCLI(LitClassifier, MNISTDataModule)

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Apr 7, 2021

Should the LightningCLI allow {fit,validate,test,tune,predict}? So by default you can do:

python simple_image_classifier.py fit --trainer.max_epochs=50 or python simple_image_classifier.py validate --trainer.limit_val_batches=30

At first glance it makes sense. But I have only used lightning for training. I will need to look into {validate,test,tune,predict} to be able to have a more informed opinion about this and be able to propose how to implement it.

Also yes, seed_everything should be inside before_instantiate_classes by default as the LightningCLI is about simple reproducibility.

Should I add in this pull request the seed_everything change in LightningCLI?

And what do you think about LightningCLI returning the trainer, model, and data instances?

trainer, model, data = MyLightningCLI(LitClassifier, MNISTDataModule)

This is not possible. It is a class so it can only return the instance. Anyway the instance has these as attributes so there is no much difference.

@carmocca
Copy link
Contributor

carmocca commented Apr 8, 2021

I will need to look into {validate,test,tune,predict} to be able to have a more informed opinion about this and be able to propose how to implement it.

Awesome!

Should I add in this pull request the seed_everything change in LightningCLI?

Yes, I think that's okay

@carmocca carmocca added this to the 1.3 milestone Apr 8, 2021
@carmocca carmocca added the feature Is an improvement or enhancement label Apr 8, 2021
…he examples.

- Increased minimum required jsonargparse version to 3.9.0.
- Improvements to simple_image_classifier.py example.
- Changed autoencoder.py and backbone_image_classifier.py examples to use LightningCLI.
- Updated pl_examples/test_examples.py so that tests succeed.
@mauvilsa mauvilsa changed the title Changed simple_image_classifier.py to use LightningCLI Changed basic_examples to use LightningCLI Apr 9, 2021
@codecov
Copy link

codecov bot commented Apr 9, 2021

Codecov Report

Merging #6862 (e37e754) into master (f645df5) will decrease coverage by 5%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #6862    +/-   ##
=======================================
- Coverage      92%     87%    -5%     
=======================================
  Files         194     194            
  Lines       12386   12391     +5     
=======================================
- Hits        11414   10770   -644     
- Misses        972    1621   +649     

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Apr 9, 2021

Implemented seed_everything in LightningCLI. Also updated two other examples required so that pl_examples/test_examples.py does not fail and give the same result as before.

- Changed dali_image_classifier.py example to use LightningCLI.
- Simplified use of LightningCLI in autoencoder.py, backbone_image_classifier.py and simple_image_classifier.py.
- Adapted other files related to the changed examples.
@pep8speaks
Copy link

pep8speaks commented Apr 13, 2021

Hello @mauvilsa! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-04-13 17:46:05 UTC

@mauvilsa
Copy link
Contributor Author

From the basic_examples only two have not been updated yet: conv_sequential_example.py and profiler_example.py.

conv_sequential_example.py is a special case because both the datamodule and the model require the same parameter batch_size. I think this could be common so I was thinking of adding a feature to jsonargparse to simplify things. Being required to give in a config file the same value in several locations is redundant and error prone. The idea would be to allow linking arguments so that one value is derived from another. In the conv example the cli would only receive data.batch_size but internally the parser would copy this value to model.batch_size. To configure this could be something like parser.link_arguments('data.batch_size', 'model.batch_size'). What do you think?

@carmocca
Copy link
Contributor

conv_sequential_example is probably going to be removed soon, so feel free to skip on updating it.

In the conv example the cli would only receive data.batch_size but internally the parser would copy this value to model.batch_size. To configure this could be something like parser.link_arguments('data.batch_size', 'model.batch_size'). What do you think?

So this ties into value interpolation. Would something like this be possible?

parser.add_argument("--data.batch_size", default="${model.batch_size}")

@mauvilsa
Copy link
Contributor Author

conv_sequential_example is probably going to be removed soon, so feel free to skip on updating it.

In the conv example the cli would only receive data.batch_size but internally the parser would copy this value to model.batch_size. To configure this could be something like parser.link_arguments('data.batch_size', 'model.batch_size'). What do you think?

So this ties into value interpolation.

It is intended to avoid the need for doing interpolation for cases in which interpolation shouldn't be used because it just adds complexity to the config file even though it is something that should be hard coded. There are other cases in which interpolation is a valid need.

Would something like this be possible?

parser.add_argument("--data.batch_size", default="${model.batch_size}")

No, my idea is not having some special values in default. This would be limiting anyway because this is normally needed when arguments are added automatically from class signatures. It would be configured after the arguments are already added making it possible to still validate using the original type. So it could be running parser.link_arguments('source_key', 'target_key') or even parser.link_arguments('source_key', 'target_key', adapt_function) if the desired link is not just a copy.

@carmocca
Copy link
Contributor

So it could be running parser.link_arguments('source_key', 'target_key') or even parser.link_arguments('source_key', 'target_key', adapt_function) if the desired link is not just a copy.

Would this have the transitive property?

Also, this discussion should continue in the jsonargparse repository for better visibility

@mauvilsa
Copy link
Contributor Author

Also, this discussion should continue in the jsonargparse repository for better visibility

The question relevant for this pull request was how to handle a case like conv_sequential_example.py and just get an opinion if what I was thinking made sense. If we don't update the conv example in this pull request we can skip any further discussion on this. Anyway the same case happens in computer_vision_fine_tuning.py. So this can be further discussed when that example is being updated.

@mauvilsa mauvilsa marked this pull request as ready for review April 13, 2021 17:48
@mauvilsa mauvilsa requested a review from awaelchli as a code owner April 13, 2021 17:48
@Borda Borda requested a review from carmocca April 13, 2021 18:05
@carmocca carmocca added example ready PRs ready to be merged labels Apr 13, 2021
@carmocca carmocca enabled auto-merge (squash) April 15, 2021 14:32
@carmocca carmocca merged commit f852a4f into Lightning-AI:master Apr 15, 2021
@mauvilsa mauvilsa deleted the cli_simple_image_classifier branch May 4, 2021 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants