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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit * Add instance segmentation and keypoint detection tasks * Updates * Updates * Updates * Add docs * Update API reference * Fix some tests * Small fix * Drop failing JIT test * Updates * Updates * Fix a test * Initial credits support * Credit -> provider * Update available backbones * Add adapter * Fix a test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updates * Fixes * Refactor * Refactor * Refactor * minor changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * 0.5.0dev * pl * imports * Update adapter.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adapter.py * Updates * Add transforms to and from icevision records * Fix tests * Try fix * Update CHANGELOG.md * Fix tests * Fix a test * Try fix * Try fix * Add some docs * Add API reference * Small updates * pep fix * Fixes Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka <[email protected]> Co-authored-by: Ananya Harsh Jha <[email protected]>
- Loading branch information
1 parent
d094fee
commit d9dc2f0
Showing
54 changed files
with
2,482 additions
and
731 deletions.
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
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,31 @@ | ||
|
||
.. _instance_segmentation: | ||
|
||
##################### | ||
Instance Segmentation | ||
##################### | ||
|
||
******** | ||
The Task | ||
******** | ||
|
||
Instance segmentation is the task of segmenting objects images and determining their associated classes. | ||
|
||
The :class:`~flash.image.instance_segmentation.model.InstanceSegmentation` and :class:`~flash.image.instance_segmentation.data.InstanceSegmentationData` classes internally rely on `IceVision <https://airctic.com/>`_. | ||
|
||
------ | ||
|
||
******* | ||
Example | ||
******* | ||
|
||
Let's look at instance segmentation with `The Oxford-IIIT Pet Dataset <https://www.robots.ox.ac.uk/~vgg/data/pets/>`_ from `IceData <https://github.com/airctic/icedata>`_. | ||
Once we've downloaded the data, we can create the :class:`~flash.image.instance_segmentation.data.InstanceSegmentationData`. | ||
We select a ``mask_rcnn`` with a ``resnet18_fpn`` backbone to use for our :class:`~flash.image.instance_segmentation.model.InstanceSegmentation` and fine-tune on the pets data. | ||
We then use the trained :class:`~flash.image.instance_segmentation.model.InstanceSegmentation` for inference. | ||
Finally, we save the model. | ||
Here's the full example: | ||
|
||
.. literalinclude:: ../../../flash_examples/instance_segmentation.py | ||
:language: python | ||
:lines: 14- |
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,31 @@ | ||
|
||
.. _keypoint_detection: | ||
|
||
################## | ||
Keypoint Detection | ||
################## | ||
|
||
******** | ||
The Task | ||
******** | ||
|
||
Keypoint detection is the task of identifying keypoints in images and their associated classes. | ||
|
||
The :class:`~flash.image.keypoint_detection.model.KeypointDetector` and :class:`~flash.image.keypoint_detection.data.KeypointDetectionData` classes internally rely on `IceVision <https://airctic.com/>`_. | ||
|
||
------ | ||
|
||
******* | ||
Example | ||
******* | ||
|
||
Let's look at keypoint detection with `BIWI Sample Keypoints (center of face) <https://www.kaggle.com/kmader/biwi-kinect-head-pose-database>`_ from `IceData <https://github.com/airctic/icedata>`_. | ||
Once we've downloaded the data, we can create the :class:`~flash.image.keypoint_detection.data.KeypointDetectionData`. | ||
We select a ``keypoint_rcnn`` with a ``resnet18_fpn`` backbone to use for our :class:`~flash.image.keypoint_detection.model.KeypointDetector` and fine-tune on the BIWI data. | ||
We then use the trained :class:`~flash.image.keypoint_detection.model.KeypointDetector` for inference. | ||
Finally, we save the model. | ||
Here's the full example: | ||
|
||
.. literalinclude:: ../../../flash_examples/keypoint_detection.py | ||
:language: python | ||
:lines: 14- |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = "0.4.1dev" | ||
__version__ = "0.5.0dev" | ||
__author__ = "PyTorchLightning et al." | ||
__author_email__ = "[email protected]" | ||
__license__ = "Apache-2.0" | ||
|
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,162 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from abc import abstractmethod | ||
from typing import Any, Callable, Optional | ||
|
||
from torch import nn | ||
from torch.utils.data import DataLoader, Sampler | ||
|
||
import flash | ||
from flash.core.data.auto_dataset import BaseAutoDataset | ||
from flash.core.model import DatasetProcessor, ModuleWrapperBase, Task | ||
|
||
|
||
class Adapter(DatasetProcessor, ModuleWrapperBase, nn.Module): | ||
"""The ``Adapter`` is a lightweight interface that can be used to encapsulate the logic from a particular | ||
provider within a :class:`~flash.core.model.Task`.""" | ||
|
||
@classmethod | ||
@abstractmethod | ||
def from_task(cls, task: "flash.Task", **kwargs) -> "Adapter": | ||
"""Instantiate the adapter from the given :class:`~flash.core.model.Task`. | ||
This includes resolution / creation of backbones / heads and any other provider specific options. | ||
""" | ||
|
||
def forward(self, x: Any) -> Any: | ||
pass | ||
|
||
def training_step(self, batch: Any, batch_idx: int) -> Any: | ||
pass | ||
|
||
def validation_step(self, batch: Any, batch_idx: int) -> None: | ||
pass | ||
|
||
def test_step(self, batch: Any, batch_idx: int) -> None: | ||
pass | ||
|
||
def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> Any: | ||
pass | ||
|
||
def training_epoch_end(self, outputs) -> None: | ||
pass | ||
|
||
def validation_epoch_end(self, outputs) -> None: | ||
pass | ||
|
||
def test_epoch_end(self, outputs) -> None: | ||
pass | ||
|
||
|
||
class AdapterTask(Task): | ||
"""The ``AdapterTask`` is a :class:`~flash.core.model.Task` which wraps an :class:`~flash.core.adapter.Adapter` | ||
and forwards all of the hooks. | ||
Args: | ||
adapter: The :class:`~flash.core.adapter.Adapter` to wrap. | ||
kwargs: Keyword arguments to be passed to the base :class:`~flash.core.model.Task`. | ||
""" | ||
|
||
def __init__(self, adapter: Adapter, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
self.adapter = adapter | ||
|
||
@property | ||
def backbone(self) -> nn.Module: | ||
return self.adapter.backbone | ||
|
||
def forward(self, x: Any) -> Any: | ||
return self.adapter.forward(x) | ||
|
||
def training_step(self, batch: Any, batch_idx: int) -> Any: | ||
return self.adapter.training_step(batch, batch_idx) | ||
|
||
def validation_step(self, batch: Any, batch_idx: int) -> None: | ||
return self.adapter.validation_step(batch, batch_idx) | ||
|
||
def test_step(self, batch: Any, batch_idx: int) -> None: | ||
return self.adapter.test_step(batch, batch_idx) | ||
|
||
def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> Any: | ||
return self.adapter.predict_step(batch, batch_idx, dataloader_idx=dataloader_idx) | ||
|
||
def training_epoch_end(self, outputs) -> None: | ||
return self.adapter.training_epoch_end(outputs) | ||
|
||
def validation_epoch_end(self, outputs) -> None: | ||
return self.adapter.validation_epoch_end(outputs) | ||
|
||
def test_epoch_end(self, outputs) -> None: | ||
return self.adapter.test_epoch_end(outputs) | ||
|
||
def process_train_dataset( | ||
self, | ||
dataset: BaseAutoDataset, | ||
batch_size: int, | ||
num_workers: int, | ||
pin_memory: bool, | ||
collate_fn: Callable, | ||
shuffle: bool = False, | ||
drop_last: bool = True, | ||
sampler: Optional[Sampler] = None, | ||
) -> DataLoader: | ||
return self.adapter.process_train_dataset( | ||
dataset, batch_size, num_workers, pin_memory, collate_fn, shuffle, drop_last, sampler | ||
) | ||
|
||
def process_val_dataset( | ||
self, | ||
dataset: BaseAutoDataset, | ||
batch_size: int, | ||
num_workers: int, | ||
pin_memory: bool, | ||
collate_fn: Callable, | ||
shuffle: bool = False, | ||
drop_last: bool = False, | ||
sampler: Optional[Sampler] = None, | ||
) -> DataLoader: | ||
return self.adapter.process_val_dataset( | ||
dataset, batch_size, num_workers, pin_memory, collate_fn, shuffle, drop_last, sampler | ||
) | ||
|
||
def process_test_dataset( | ||
self, | ||
dataset: BaseAutoDataset, | ||
batch_size: int, | ||
num_workers: int, | ||
pin_memory: bool, | ||
collate_fn: Callable, | ||
shuffle: bool = False, | ||
drop_last: bool = False, | ||
sampler: Optional[Sampler] = None, | ||
) -> DataLoader: | ||
return self.adapter.process_test_dataset( | ||
dataset, batch_size, num_workers, pin_memory, collate_fn, shuffle, drop_last, sampler | ||
) | ||
|
||
def process_predict_dataset( | ||
self, | ||
dataset: BaseAutoDataset, | ||
batch_size: int = 1, | ||
num_workers: int = 0, | ||
pin_memory: bool = False, | ||
collate_fn: Callable = lambda x: x, | ||
shuffle: bool = False, | ||
drop_last: bool = True, | ||
sampler: Optional[Sampler] = None, | ||
) -> DataLoader: | ||
return self.adapter.process_predict_dataset( | ||
dataset, batch_size, num_workers, pin_memory, collate_fn, shuffle, drop_last, sampler | ||
) |
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
Empty file.
Oops, something went wrong.