Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Re-write examples (#496)
Browse files Browse the repository at this point in the history
* Initial commit

* Update example tests

* Update example tests

* Small fix

* Small fix

* Small fix

* Fixes

* Fixes

* Try something

* Try something

* Try something

* Try something

* Try something

* Try something

* Some docs refactor

* Updates

* Add tabular

* refactor and add text classification

* Add multi label text

* Updates

* Add summarization

* Update translation

* Add video

* Updates

* Updates

* Update API references

* Fix some links

* Merge image and video

* Merge image and video
  • Loading branch information
ethanwharris authored Jul 7, 2021
1 parent fb08447 commit 5d98cde
Show file tree
Hide file tree
Showing 75 changed files with 714 additions and 1,803 deletions.
10 changes: 10 additions & 0 deletions docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "!layout.html" %}
<link rel="canonical" href="{{ theme_canonical_url }}{{ pagename }}.html" />

{% block footer %}
{{ super() }}
<script script type="text/javascript">
var collapsedSections = ['Guides', 'Image and Video', 'Tabular', 'Text', 'Integrations', 'API Reference', 'Contributing a Task'];
</script>

{% endblock %}
2 changes: 2 additions & 0 deletions docs/source/_templates/theme_variables.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
'blog': 'https://www.pytorchlightning.ai/blog',
'resources': 'https://lightning-flash.readthedocs.io/',
'support': 'https://lightning-flash.readthedocs.io/',
'community': 'https://pytorch-lightning.slack.com',
'forums': 'https://pytorch-lightning.slack.com',
}
-%}
19 changes: 0 additions & 19 deletions docs/source/common/image_backbones.rst

This file was deleted.

9 changes: 0 additions & 9 deletions docs/source/common/object_detection_backbones.rst

This file was deleted.

53 changes: 28 additions & 25 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Lightning Flash

.. toctree::
:maxdepth: 1
:caption: Get started:
:caption: Getting started

quickstart
installation
Expand All @@ -18,58 +18,61 @@ Lightning Flash

.. toctree::
:maxdepth: 1
:caption: Tasks
:caption: Guides

general/training
general/finetuning
general/predictions
general/jit

.. toctree::
:maxdepth: 1
:caption: Image and Video

reference/task
reference/image_classification
reference/image_classification_multi_label
reference/image_embedder
reference/summarization
reference/text_classification
reference/text_classification_multi_label
reference/tabular_classification
reference/translation
reference/object_detection
reference/video_classification
reference/semantic_segmentation
reference/style_transfer
reference/video_classification

.. toctree::
:maxdepth: 1
:caption: Flash API
:caption: Tabular

general/data
general/callback
general/registry
general/serve
reference/tabular_classification

.. toctree::
:maxdepth: 1
:caption: Text

reference/text_classification
reference/text_classification_multi_label
reference/summarization
reference/translation

.. toctree::
:maxdepth: 1
:caption: Common Use Cases
:caption: Integrations

general/training
general/finetuning
general/predictions
general/jit
integrations/fiftyone

.. toctree::
:maxdepth: 1
:caption: API Reference

general/data
general/callback
general/registry
general/serve
code/core
code/data
code/image
code/tabular
code/text
code/video

.. toctree::
:maxdepth: 1
:caption: Integrations

integrations/fiftyone

.. toctree::
:maxdepth: 1
:caption: Contributing a Task
Expand Down
1 change: 0 additions & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ When you have enough data, you're likely better off training from scratch instea
A few Built-in Tasks
====================

- :doc:`General Task <reference/task>`
- :doc:`ImageClassification <reference/image_classification>`
- :doc:`ImageEmbedder <reference/image_embedder>`
- :doc:`TextClassification <reference/text_classification>`
Expand Down
4 changes: 2 additions & 2 deletions docs/source/reference/flash_to_production.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Flash makes it simple to deploy models in production.
Server Side
^^^^^^^^^^^

.. literalinclude:: ../../../flash_examples/serve/segmentic_segmentation/inference_server.py
.. literalinclude:: ../../../flash_examples/serve/semantic_segmentation/inference_server.py
:language: python
:lines: 14-


Client Side
^^^^^^^^^^^

.. literalinclude:: ../../../flash_examples/serve/segmentic_segmentation/client.py
.. literalinclude:: ../../../flash_examples/serve/semantic_segmentation/client.py
:language: python
:lines: 14-

Expand Down
77 changes: 29 additions & 48 deletions docs/source/reference/image_classification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,22 @@ Image Classification
####################

********
The task
The Task
********
The task of identifying what is in an image is called image classification. Typically, Image Classification is used to identify images containing a single object. The task predicts which ‘class’ the image most likely belongs to with a degree of certainty. A class is a label that describes what is in an image, such as ‘car’, ‘house’, ‘cat’ etc. For example, we can train the image classifier task on images of ants and it will learn to predict the probability that an image contains an ant.

------

*********
Inference
*********

The :class:`~flash.image.ImageClassifier` is already pre-trained on `ImageNet <http://www.image-net.org/>`_, a dataset of over 14 million images.


Use the :class:`~flash.image.ImageClassifier` pretrained model for inference on any string sequence using :func:`~flash.image.ImageClassifier.predict`:

.. literalinclude:: ../../../flash_examples/predict/image_classification.py
:language: python
:lines: 14-

For more advanced inference options, see :ref:`predictions`.
The task of identifying what is in an image is called image classification.
Typically, Image Classification is used to identify images containing a single object.
The task predicts which ‘class’ the image most likely belongs to with a degree of certainty.
A class is a label that describes what is in an image, such as ‘car’, ‘house’, ‘cat’ etc.

------

**********
Finetuning
**********
*******
Example
*******

Lets say you wanted to develope a model that could determine whether an image contains **ants** or **bees**, using the hymenoptera dataset.
Once we download the data using :func:`~flash.core.data.download_data`, all we need is the train data and validation data folders to create the :class:`~flash.image.ImageClassificationData`.

.. note:: The dataset contains ``train`` and ``validation`` folders, and then each folder contains a **bees** folder, with pictures of bees, and an **ants** folder with images of, you guessed it, ants.
Let's look at the task of predicting whether images contain Ants or Bees using the hymenoptera dataset.
The dataset contains ``train`` and ``validation`` folders, and then each folder contains a **bees** folder, with pictures of bees, and an **ants** folder with images of, you guessed it, ants.

.. code-block::
Expand All @@ -60,36 +45,32 @@ Once we download the data using :func:`~flash.core.data.download_data`, all we n
├── 10870992_eebeeb3a12.jpg
...
Once we've downloaded the data using :func:`~flash.core.data.download_data`, we create the :class:`~flash.image.classification.data.ImageClassificationData`.
We select a pre-trained backbone to use for our :class:`~flash.image.classification.model.ImageClassifier` and fine-tune on the hymenoptera data.
We then use the trained :class:`~flash.image.classification.model.ImageClassifier` for inference.
Finally, we save the model.
Here's the full example:

Now all we need is to train our task!

.. literalinclude:: ../../../flash_examples/finetuning/image_classification.py
.. literalinclude:: ../../../flash_examples/image_classification.py
:language: python
:lines: 14-

------

*********************
Changing the backbone
*********************
By default, we use a `ResNet-18 <https://arxiv.org/abs/1512.03385>`_ for image classification. You can change the model run by the task by passing in a different backbone.

.. testsetup::

from flash.core.data.utils import download_data
from flash.image import ImageClassificationData, ImageClassifier

download_data("https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip", "data/")
*******
Serving
*******

.. testcode::
The :class:`~flash.image.classification.model.ImageClassifier` is servable.
This means you can call ``.serve`` to serve your :class:`~flash.Task`.
Here's an example:

# 1. organize the data
data = ImageClassificationData.from_folders(
train_folder="data/hymenoptera_data/train/",
val_folder="data/hymenoptera_data/val/",
)
.. literalinclude:: ../../../flash_examples/serve/image_classification/inference_server.py
:language: python
:lines: 14-

# 2. build the task
task = ImageClassifier(num_classes=2, backbone="resnet34")
You can now perform inference from your client like this:

.. include:: ../common/image_backbones.rst
.. literalinclude:: ../../../flash_examples/serve/image_classification/client.py
:language: python
:lines: 14-
61 changes: 24 additions & 37 deletions docs/source/reference/image_classification_multi_label.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,23 @@ Multi-label Image Classification
################################

********
The task
The Task
********
Multi-label classification is the task of assigning a number of labels from a fixed set to each data point, which can be in any modality. In this example, we will look at the task of trying to predict the movie genres from an image of the movie poster.

------

********
The data
********
The data we will use in this example is a subset of the awesome movie poster genre prediction data set from the paper "Movie Genre Classification based on Poster Images with Deep Neural Networks" by Wei-Ta Chu and Hung-Jui Guo, resized to 128 by 128.
Take a look at their paper (and please consider citing their paper if you use the data) here: `www.cs.ccu.edu.tw/~wtchu/projects/MoviePoster/ <https://www.cs.ccu.edu.tw/~wtchu/projects/MoviePoster/>`_.

------

*********
Inference
*********

The :class:`~flash.image.ImageClassifier` is already pre-trained on `ImageNet <http://www.image-net.org/>`_, a dataset of over 14 million images.

We can use the :class:`~flash.image.ImageClassifier` model (pretrained on our data) for inference on any string sequence using :func:`~flash.image.ImageClassifier.predict`.
We can also add a simple visualisation by extending :class:`~flash.core.data.base_viz.BaseVisualization`, like this:

.. literalinclude:: ../../../flash_examples/predict/image_classification_multi_label.py
:language: python
:lines: 14-

For more advanced inference options, see :ref:`predictions`.
Multi-label classification is the task of assigning a number of labels from a fixed set to each data point, which can be in any modality (images in this case).
Multi-label image classification is supported by the :class:`~flash.image.classification.model.ImageClassifier` via the ``multi-label`` argument.

------

**********
Finetuning
**********
*******
Example
*******

Now let's look at how we can finetune a model on the movie poster data.
Once we download the data using :func:`~flash.core.data.download_data`, all we need is the train data and validation data folders to create the :class:`~flash.image.ImageClassificationData`.

.. note:: The dataset contains ``train`` and ``validation`` folders, and then each folder contains images and a ``metadata.csv`` which stores the labels.
Let's look at the task of trying to predict the movie genres from an image of the movie poster.
The data we will use is a subset of the awesome movie poster genre prediction data set from the paper "Movie Genre Classification based on Poster Images with Deep Neural Networks" by Wei-Ta Chu and Hung-Jui Guo, resized to 128 by 128.
Take a look at their paper (and please consider citing their paper if you use the data) here: `www.cs.ccu.edu.tw/~wtchu/projects/MoviePoster/ <https://www.cs.ccu.edu.tw/~wtchu/projects/MoviePoster/>`_.
The data set contains ``train`` and ``validation`` folders, and then each folder contains images and a ``metadata.csv`` which stores the labels.
Here's an overview:

.. code-block::
Expand All @@ -60,13 +38,22 @@ Once we download the data using :func:`~flash.core.data.download_data`, all we n
├── tt0326965.jpg
...
Once we've downloaded the data using :func:`~flash.core.data.download_data`, we need to create the :class:`~flash.image.classification.data.ImageClassificationData`.
We first create a function (``load_data``) to extract the list of images and associated labels which can then be passed to :meth:`~flash.image.classification.data.ImageClassificationData.from_files`.
We select a pre-trained backbone to use for our :class:`~flash.image.classification.model.ImageClassifier` and fine-tune on the posters data.
We then use the trained :class:`~flash.image.classification.model.ImageClassifier` for inference.
Finally, we save the model.
Here's the full example:

The ``metadata.csv`` files in each folder contain our labels, so we need to create a function (``load_data``) to extract the list of images and associated labels:

.. literalinclude:: ../../../flash_examples/finetuning/image_classification_multi_label.py
.. literalinclude:: ../../../flash_examples/image_classification_multi_label.py
:language: python
:lines: 14-

------

For more backbone options, see :ref:`image_classification`.
*******
Serving
*******

The :class:`~flash.image.classification.model.ImageClassifier` is servable.
For more information, see :ref:`image_classification`.
Loading

0 comments on commit 5d98cde

Please sign in to comment.