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

Commit

Permalink
Rename Serializer to Output and move to flash.core.data.io.output (#…
Browse files Browse the repository at this point in the history
…927)

Co-authored-by: Ananya Harsh Jha <[email protected]>
  • Loading branch information
ethanwharris and ananyahjha93 authored Nov 5, 2021
1 parent 6ce7d68 commit d1be93c
Show file tree
Hide file tree
Showing 59 changed files with 549 additions and 420 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Changed

- Changed classes named `*Serializer` and properties / variables named `serializer` to be `*Output` and `output` respectively ([#927](https://github.com/PyTorchLightning/lightning-flash/pull/927))

### Deprecated

- Deprecated `flash.core.data.process.Serializer` in favour of `flash.core.data.io.output.Output` ([#927](https://github.com/PyTorchLightning/lightning-flash/pull/927))

- Deprecated `Task.serializer` in favour of `Task.output` ([#927](https://github.com/PyTorchLightning/lightning-flash/pull/927))

### Fixed


## [0.5.2] - 2021-11-05

### Added
Expand Down
4 changes: 2 additions & 2 deletions docs/source/api/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ _________________________
:template: classtemplate.rst

~flash.core.classification.Classes
~flash.core.classification.ClassificationSerializer
~flash.core.classification.ClassificationOutput
~flash.core.classification.ClassificationTask
~flash.core.classification.FiftyOneLabels
~flash.core.classification.Labels
~flash.core.classification.Logits
~flash.core.classification.PredsClassificationSerializer
~flash.core.classification.PredsClassificationOutput
~flash.core.classification.Probabilities

flash.core.finetuning
Expand Down
13 changes: 11 additions & 2 deletions docs/source/api/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ flash.core.data
:local:
:backlinks: top

flash.core.data.io.output
_________________________

.. autosummary::
:toctree: generated/
:nosignatures:
:template: classtemplate.rst

~flash.core.data.io.output.Output
~flash.core.data.io.output.OutputMapping

flash.core.data.auto_dataset
____________________________

Expand Down Expand Up @@ -114,8 +125,6 @@ _______________________
~flash.core.data.process.Deserializer
~flash.core.data.process.Postprocess
~flash.core.data.process.Preprocess
~flash.core.data.process.SerializerMapping
~flash.core.data.process.Serializer

flash.core.data.properties
__________________________
Expand Down
2 changes: 1 addition & 1 deletion docs/source/api/flash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ flash
~flash.core.data.callback.FlashCallback
~flash.core.data.process.Preprocess
~flash.core.data.process.Postprocess
~flash.core.data.process.Serializer
~flash.core.data.io.output.Output
~flash.core.model.Task
~flash.core.trainer.Trainer
6 changes: 3 additions & 3 deletions docs/source/api/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ________________
detection.data.FiftyOneParser
detection.data.ObjectDetectionFiftyOneDataSource
detection.data.ObjectDetectionPreprocess
detection.serialization.FiftyOneDetectionLabels
detection.output.FiftyOneDetectionLabels

Keypoint Detection
__________________
Expand Down Expand Up @@ -102,8 +102,8 @@ ____________
segmentation.data.SemanticSegmentationFiftyOneDataSource
segmentation.data.SemanticSegmentationDeserializer
segmentation.model.SemanticSegmentationPostprocess
segmentation.serialization.FiftyOneSegmentationLabels
segmentation.serialization.SegmentationLabels
segmentation.output.FiftyOneSegmentationLabels
segmentation.output.SegmentationLabels

.. autosummary::
:toctree: generated/
Expand Down
4 changes: 2 additions & 2 deletions docs/source/common/finetuning_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Once you've finetuned, use the model to predict:

.. testcode:: finetune

# Serialize predictions as labels, automatically inferred from the training data in part 2.
model.serializer = Labels()
# Output predictions as labels, automatically inferred from the training data in part 2.
model.output = Labels()

predictions = model.predict(
[
Expand Down
14 changes: 7 additions & 7 deletions docs/source/general/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Here are common terms you need to be familiar with:
* - :class:`~flash.core.data.data_module.DataModule`
- The :class:`~flash.core.data.data_module.DataModule` contains the datasets, transforms and dataloaders.
* - :class:`~flash.core.data.data_pipeline.DataPipeline`
- The :class:`~flash.core.data.data_pipeline.DataPipeline` is Flash internal object to manage :class:`~flash.core.data.Deserializer`, :class:`~flash.core.data.data_source.DataSource`, :class:`~flash.core.data.process.Preprocess`, :class:`~flash.core.data.process.Postprocess`, and :class:`~flash.core.data.process.Serializer` objects.
- The :class:`~flash.core.data.data_pipeline.DataPipeline` is Flash internal object to manage :class:`~flash.core.data.Deserializer`, :class:`~flash.core.data.data_source.DataSource`, :class:`~flash.core.data.process.Preprocess`, :class:`~flash.core.data.process.Postprocess`, and :class:`~flash.core.data.io.output.Output` objects.
* - :class:`~flash.core.data.data_source.DataSource`
- The :class:`~flash.core.data.data_source.DataSource` provides :meth:`~flash.core.data.data_source.DataSource.load_data` and :meth:`~flash.core.data.data_source.DataSource.load_sample` hooks for creating data sets from metadata (such as folder names).
* - :class:`~flash.core.data.process.Preprocess`
Expand All @@ -37,8 +37,8 @@ Here are common terms you need to be familiar with:
* - :class:`~flash.core.data.process.Postprocess`
- The :class:`~flash.core.data.process.Postprocess` provides a simple hook-based API to encapsulate your post-processing logic.
The :class:`~flash.core.data.process.Postprocess` hooks cover from model outputs to predictions export.
* - :class:`~flash.core.data.process.Serializer`
- The :class:`~flash.core.data.process.Serializer` provides a single :meth:`~flash.core.data.process.Serializer.serialize` method that is used to convert model outputs (after the :class:`~flash.core.data.process.Postprocess`) to the desired output format during prediction.
* - :class:`~flash.core.data.io.output.Output`
- The :class:`~flash.core.data.io.output.Output` provides a single :meth:`~flash.core.data.io.output.Output.serialize` method that is used to convert model outputs (after the :class:`~flash.core.data.process.Postprocess`) to the desired output format during prediction.


*******************************************
Expand All @@ -59,7 +59,7 @@ Usually, extra processing logic should be added to bridge the gap between traini

The :class:`~flash.core.data.data_source.DataSource` class can be used to generate data sets from multiple sources (e.g. folders, numpy, etc.), that can then all be transformed in the same way.
The :class:`~flash.core.data.process.Preprocess` and :class:`~flash.core.data.process.Postprocess` classes can be used to manage the preprocessing and postprocessing transforms.
The :class:`~flash.core.data.process.Serializer` class provides the logic for converting :class:`~flash.core.data.process.Postprocess` outputs to the desired predict format (e.g. classes, labels, probabilities, etc.).
The :class:`~flash.core.data.io.output.Output` class provides the logic for converting :class:`~flash.core.data.process.Postprocess` outputs to the desired predict format (e.g. classes, labels, probabilities, etc.).

By providing a series of hooks that can be overridden with custom data processing logic (or just targeted with transforms),
Flash gives the user much more granular control over their data processing flow.
Expand Down Expand Up @@ -383,18 +383,18 @@ Example::
predictions = lightning_module(data)


Postprocess and Serializer
Postprocess and Output
__________________________


Once the predictions have been generated by the Flash :class:`~flash.core.model.Task`, the Flash
:class:`~flash.core.data.data_pipeline.DataPipeline` will execute the :class:`~flash.core.data.process.Postprocess` hooks and the
:class:`~flash.core.data.process.Serializer` behind the scenes.
:class:`~flash.core.data.io.output.Output` behind the scenes.

First, the :meth:`~flash.core.data.process.Postprocess.per_batch_transform` hooks will be applied on the batch predictions.
Then, the :meth:`~flash.core.data.process.Postprocess.uncollate` will split the batch into individual predictions.
Next, the :meth:`~flash.core.data.process.Postprocess.per_sample_transform` will be applied on each prediction.
Finally, the :meth:`~flash.core.data.process.Serializer.serialize` method will be called to serialize the predictions.
Finally, the :meth:`~flash.core.data.io.output.Output.serialize` method will be called to serialize the predictions.

.. note:: The transform can be applied either on device or ``CPU``.

Expand Down
8 changes: 4 additions & 4 deletions docs/source/general/predictions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Predict on a csv file
Serializing predictions
=======================

To change how predictions are serialized you can attach a :class:`~flash.core.data.process.Serializer` to your
:class:`~flash.core.model.Task`. For example, you can choose to serialize outputs as probabilities (for more options see the API
To change the output format of predictions you can attach an :class:`~flash.core.data.io.output.Output` to your
:class:`~flash.core.model.Task`. For example, you can choose to output probabilities (for more options see the API
reference below).


Expand All @@ -77,8 +77,8 @@ reference below).
"https://flash-weights.s3.amazonaws.com/0.5.2/image_classification_model.pt"
)
# 3. Attach the Serializer
model.serializer = Probabilities()
# 3. Attach the Output
model.output = Probabilities()
# 4. Predict whether the image contains an ant or a bee
predictions = model.predict("data/hymenoptera_data/val/bees/65038344_52a45d090d.jpg")
Expand Down
4 changes: 2 additions & 2 deletions docs/source/integrations/fiftyone.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ semantic segmentation tasks. Doing so is as easy as updating your model to use
one of the following serializers:

* :class:`FiftyOneLabels(return_filepath=True)<flash.core.classification.FiftyOneLabels>`
* :class:`FiftyOneSegmentationLabels(return_filepath=True)<flash.image.segmentation.serialization.FiftyOneSegmentationLabels>`
* :class:`FiftyOneDetectionLabels(return_filepath=True)<flash.image.detection.serialization.FiftyOneDetectionLabels>`
* :class:`FiftyOneSegmentationLabels(return_filepath=True)<flash.image.segmentation.output.FiftyOneSegmentationLabels>`
* :class:`FiftyOneDetectionLabels(return_filepath=True)<flash.image.detection.output.FiftyOneDetectionLabels>`

The :func:`~flash.core.integrations.fiftyone.visualize` function then lets you visualize
your predictions in the
Expand Down
10 changes: 5 additions & 5 deletions docs/source/template/optional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ Here's how we create our transforms in the :class:`~flash.image.classification.d
:language: python
:pyobject: ImageClassificationPreprocess.default_transforms

Add output serializers to your Task
======================================
Add outputs to your Task
========================

We recommend that you do most of the heavy lifting in the :class:`~flash.core.data.process.Postprocess`.
Specifically, it should include any formatting and transforms that should always be applied to the predictions.
If you want to support different use cases that require different prediction formats, you should add some :class:`~flash.core.data.process.Serializer` implementations in a ``serialization.py`` file.
If you want to support different use cases that require different prediction formats, you should add some :class:`~flash.core.data.io.output.Output` implementations in an ``output.py`` file.

Some good examples are in `flash/core/classification.py <https://github.com/PyTorchLightning/lightning-flash/blob/master/flash/core/classification.py>`_.
Here's the :class:`~flash.core.classification.Classes` :class:`~flash.core.data.process.Serializer`:
Here's the :class:`~flash.core.classification.Classes` :class:`~flash.core.data.io.output.Output`:

.. literalinclude:: ../../../flash/core/classification.py
:language: python
:pyobject: Classes

Alternatively, here's the :class:`~flash.core.classification.Logits` :class:`~flash.core.data.process.Serializer`:
Alternatively, here's the :class:`~flash.core.classification.Logits` :class:`~flash.core.data.io.output.Output`:

.. literalinclude:: ../../../flash/core/classification.py
:language: python
Expand Down
4 changes: 3 additions & 1 deletion flash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from flash.core.data.data_source import DataSource
from flash.core.data.datasets import FlashDataset, FlashIterableDataset
from flash.core.data.input_transform import InputTransform
from flash.core.data.io.output import Output
from flash.core.data.process import Postprocess, Preprocess, Serializer
from flash.core.model import Task # noqa: E402
from flash.core.trainer import Trainer # noqa: E402
Expand All @@ -44,9 +45,10 @@
"FlashCallback",
"FlashDataset",
"FlashIterableDataset",
"Preprocess",
"InputTransform",
"Output",
"Postprocess",
"Preprocess",
"Serializer",
"Task",
"Trainer",
Expand Down
8 changes: 4 additions & 4 deletions flash/audio/speech_recognition/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from flash.core.model import Task
from flash.core.registry import FlashRegistry
from flash.core.utilities.imports import _AUDIO_AVAILABLE
from flash.core.utilities.types import LR_SCHEDULER_TYPE, OPTIMIZER_TYPE, SERIALIZER_TYPE
from flash.core.utilities.types import LR_SCHEDULER_TYPE, OPTIMIZER_TYPE, OUTPUT_TYPE

if _AUDIO_AVAILABLE:
from transformers import Wav2Vec2Processor
Expand All @@ -41,7 +41,7 @@ class SpeechRecognition(Task):
learning_rate: Learning rate to use for training, defaults to ``1e-5``.
optimizer: Optimizer to use for training.
lr_scheduler: The LR scheduler to use during training.
serializer: The :class:`~flash.core.data.process.Serializer` to use when serializing prediction outputs.
output: The :class:`~flash.core.data.io.output.Output` to use when serializing prediction outputs.
"""

backbones: FlashRegistry = SPEECH_RECOGNITION_BACKBONES
Expand All @@ -54,7 +54,7 @@ def __init__(
optimizer: OPTIMIZER_TYPE = "Adam",
lr_scheduler: LR_SCHEDULER_TYPE = None,
learning_rate: float = 1e-5,
serializer: SERIALIZER_TYPE = None,
output: OUTPUT_TYPE = None,
):
os.environ["TOKENIZERS_PARALLELISM"] = "TRUE"
# disable HF thousand warnings
Expand All @@ -68,7 +68,7 @@ def __init__(
optimizer=optimizer,
lr_scheduler=lr_scheduler,
learning_rate=learning_rate,
serializer=serializer,
output=output,
)

self.save_hyperparameters()
Expand Down
Loading

0 comments on commit d1be93c

Please sign in to comment.