This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Adds a template task and docs #306
Merged
Merged
Changes from 28 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
f5e3c49
Initial commit
ethanwharris 4cccd79
Updates
ethanwharris 838e40c
Updates
ethanwharris 49de5a0
Merge branch 'master' into feature/template
ethanwharris 7735349
Updates
ethanwharris ce2108f
Remove template README
ethanwharris 04b3b96
Fixes
ethanwharris f79f909
Updates
ethanwharris 9834a47
Add examples
ethanwharris 53a1ba2
Updates
ethanwharris 2694f46
Updates
ethanwharris 28b5eec
Updates
ethanwharris c552635
Updates
ethanwharris 65f9bdd
Add tests
ethanwharris cc3001a
Updates
ethanwharris b4102f0
Merge branch 'master' into feature/template
ethanwharris 4ae69fa
Fixes
ethanwharris 3bcf221
A fix
ethanwharris eb7c3e4
Fixes
ethanwharris 839c99a
More tests
ethanwharris e2df1ee
Merge branch 'master' into feature/template
ethanwharris afe8142
Updates
ethanwharris bee8bdd
Fix
ethanwharris 382c2cb
Merge branch 'master' into feature/template
ethanwharris 3a24117
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] acd302e
Update docs/source/reference/template.rst
ethanwharris 907c927
Respond to comments
ethanwharris 0af0d28
updates
ethanwharris 9a2be0e
Update docs/source/template/data.rst
ethanwharris 9740580
Update docs/source/template/data.rst
ethanwharris b6d57a2
Update docs/source/template/data.rst
ethanwharris 084eb6e
Merge branch 'master' into feature/template
ethanwharris e390d32
Update docs/source/template/model.rst
ethanwharris 166dd4d
Updates
ethanwharris 2f52577
Merge branch 'feature/template' of https://github.com/PyTorchLightnin…
ethanwharris 0c0780c
Updates
ethanwharris 3fdba4a
Fixes
ethanwharris fa6ba79
Updates
ethanwharris 7b201b3
Updates
ethanwharris 96df2c2
Updates
ethanwharris 9a9cfd4
Fixes
ethanwharris fe2cff7
Fixes
ethanwharris 8167884
Fix
ethanwharris ad976f4
Add backbones
ethanwharris fecb316
Add backbones
ethanwharris b4d952c
Updates
ethanwharris c7b7806
Updates
ethanwharris 23f2f20
Updates
ethanwharris ba83757
Fixes
ethanwharris 1c43ec9
Add links
ethanwharris 5aa7cf5
Fixes
ethanwharris 4d35762
Simplify
ethanwharris 36a6538
Update CHANGELOG.md
ethanwharris 17085fb
Merge branch 'master' into feature/template
mergify[bot] 4550e04
Update docs/source/template/optional.rst
ethanwharris 5ebc71e
Update docs/source/template/optional.rst
ethanwharris 71de79c
Update docs/source/template/task.rst
ethanwharris 0850333
Updates
ethanwharris 4fd0344
Updates
ethanwharris c21e816
Updates
ethanwharris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
.. _contributing_backbones: | ||
|
||
************* | ||
The Backbones | ||
************* | ||
|
||
Now that you've got a way of loading data, you should implement some backbones to use with your :class:`~flash.core.model.Task`. | ||
Create a :any:`FlashRegistry <registry>` to use with your :class:`~flash.core.model.Task` in `backbones.py <https://github.com/PyTorchLightning/lightning-flash/blob/master/flash/template/classification/backbones.py>`_. | ||
|
||
The registry allows you to register backbones for your task that can be selected by the user. | ||
The backbones can come from anywhere as long as you can register a function that loads the backbone. | ||
Furthermore, the user can add their own models to the existing backbones, without having to write their own :class:`~flash.core.model.Task`! | ||
|
||
You can create a registry like this: | ||
|
||
.. code-block:: python | ||
|
||
TEMPLATE_BACKBONES = FlashRegistry("backbones") | ||
|
||
Let's add a simple MLP backbone to our registry. | ||
We'll create the backbone and return it along with the output size (so that we can create the model head in our :class:`~flash.core.model.Task`). | ||
Here's the code: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/backbones.py | ||
:language: python | ||
:pyobject: load_mlp_128 | ||
|
||
Here's another example with a slightly more complex model: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/backbones.py | ||
:language: python | ||
:pyobject: load_mlp_128_256 | ||
|
||
More Examples | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_____________ | ||
|
||
Here's a more advanced example, which adds ``SimCLR`` to the ``IMAGE_CLASSIFIER_BACKBONES``, from `flash/image/backbones.py <https://github.com/PyTorchLightning/lightning-flash/blob/master/flash/image/backbones.py>`_: | ||
|
||
.. literalinclude:: ../../../flash/image/backbones.py | ||
:language: python | ||
:pyobject: load_simclr_imagenet | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
------ | ||
|
||
Once you've got some data and some backbones, :ref:`implement your task! <contributing_task>` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
.. _contributing_task: | ||
|
||
******** | ||
The Task | ||
******** | ||
|
||
Once you've implemented a Flash :class:`~flash.core.data.data_module.DataModule` and some backbones, you should implement your :class:`~flash.core.model.Task` in `model.py <https://github.com/PyTorchLightning/lightning-flash/blob/master/flash/template/classification/model.py>`_. | ||
The :class:`~flash.core.model.Task` is responsible for: setting up the backbone, performing the forward pass of the model, and calculating the loss and any metrics. | ||
Remember that, under the hood, the Flash :class:`~flash.core.model.Task` is simply a :any:`pytorch_lightning:lightning_module` with some helpful defaults. | ||
|
||
Task | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
^^^^ | ||
You can override the base :class:`~flash.core.model.Task` or any of the existing :class:`~flash.core.model.Task` implementations. | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
For example, in our scikit-learn example, we can just override :class:`~flash.core.classification.ClassificationTask` which provides good defaults for classification. | ||
|
||
You should attach your backbones registry as a class attribute like this: | ||
|
||
.. code-block:: python | ||
|
||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
backbones: FlashRegistry = TEMPLATE_BACKBONES | ||
|
||
In the :meth:`~flash.core.model.Task.__init__`, you will need to configure defaults for the: | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- loss function | ||
- optimizer | ||
- metrics | ||
- backbone / model | ||
|
||
You will also need to create the backbone from the registry and create the model head. | ||
Here's the code: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/model.py | ||
:language: python | ||
:dedent: 4 | ||
:pyobject: TemplateSKLearnClassifier.__init__ | ||
|
||
You should also override the ``{train,val,test,predict}_step`` methods. | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The default ``{train,val,test,predict}_step`` implementations in :class:`~flash.core.model.Task` expect a tuple containing the input and target, and should be suitable for most applications. | ||
ethanwharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
In our template example, we just extract the input and target from the input mapping and forward them to the ``super`` methods. | ||
Here's the code for the ``training_step``: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/model.py | ||
:language: python | ||
:dedent: 4 | ||
:pyobject: TemplateSKLearnClassifier.training_step | ||
|
||
We use the same code for the ``validation_step`` and ``test_step``. | ||
For ``predict_step`` we don't need the targets, so our code looks like this: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/model.py | ||
:language: python | ||
:dedent: 4 | ||
:pyobject: TemplateSKLearnClassifier.predict_step | ||
|
||
.. note:: You can completely replace the ``{train,val,test,predict}_step`` methods (that is, without a call to ``super``) if you need more custom behaviour for your :class:`~flash.core.model.Task` at a particular stage. | ||
|
||
Finally, we use our backbone and head in a custom forward pass: | ||
|
||
.. literalinclude:: ../../../flash/template/classification/model.py | ||
:language: python | ||
:dedent: 4 | ||
:pyobject: TemplateSKLearnClassifier.forward | ||
|
||
------ | ||
|
||
Now that you've got your task, take a look at some :ref:`optional advanced features you can add <contributing_optional>` or go ahead and :ref:`create some examples showing your task in action! <contributing_examples>` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include link to images to make your description better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just tabular data, so I'm not sure what images we would show here