Shortcuts

InstanceSegmentationData

class flash.image.instance_segmentation.data.InstanceSegmentationData(train_input=None, val_input=None, test_input=None, predict_input=None, data_fetcher=None, transform=<class 'flash.core.data.io.input_transform.InputTransform'>, transform_kwargs=None, val_split=None, batch_size=None, num_workers=0, sampler=None, pin_memory=True, persistent_workers=False)[source]
classmethod from_coco(train_folder=None, train_ann_file=None, val_folder=None, val_ann_file=None, test_folder=None, test_ann_file=None, predict_folder=None, input_cls=<class 'flash.core.integrations.icevision.data.IceVisionInput'>, transform=<class 'flash.core.integrations.icevision.transforms.IceVisionInputTransform'>, transform_kwargs=None, **data_module_kwargs)[source]

Creates a InstanceSegmentationData object from the given data folders and annotation files in the COCO JSON format.

For help understanding and using the COCO format, take a look at this tutorial: Create COCO annotations from scratch.

To learn how to customize the transforms applied for each stage, read our customizing transforms guide.

Parameters
  • train_folder (Optional[str]) – The folder containing images to use when training.

  • train_ann_file (Optional[str]) – The COCO format annotation file to use when training.

  • val_folder (Optional[str]) – The folder containing images to use when validating.

  • val_ann_file (Optional[str]) – The COCO format annotation file to use when validating.

  • test_folder (Optional[str]) – The folder containing images to use when testing.

  • test_ann_file (Optional[str]) – The COCO format annotation file to use when testing.

  • predict_folder (Optional[str]) – The folder containing images to use when predicting.

  • input_cls (Type[Input]) – The Input type to use for loading the data.

  • transform (TypeVar(INPUT_TRANSFORM_TYPE, Type[flash.core.data.io.input_transform.InputTransform], Callable, Tuple[Union[LightningEnum, str], Dict[str, Any]], Union[LightningEnum, str], None)) – The InputTransform type to use.

  • transform_kwargs (Optional[Dict]) – Dict of keyword arguments to be provided when instantiating the transforms.

  • data_module_kwargs (Any) – Additional keyword arguments to provide to the DataModule constructor.

Returns

The constructed InstanceSegmentationData.

Examples

The folder train_folder has the following contents:

train_folder
├── image_1.png
├── image_2.png
├── image_3.png
...

The file train_annotations.json contains the following:

{
    "annotations": [
        {"area": 50, "bbox": [10, 20, 5, 10], "category_id": 1, "id": 1, "image_id": 1, "iscrowd": 0,
        "segmentation": [[10, 20, 15, 20, 15, 30, 10, 30]]},
        {"area": 100, "bbox": [20, 30, 10, 10], "category_id": 2, "id": 2, "image_id": 2, "iscrowd": 0,
        "segmentation": [[20, 30, 30, 30, 30, 40, 20, 40]]},
        {"area": 125, "bbox": [10, 20, 5, 25], "category_id": 1, "id": 3, "image_id": 3, "iscrowd": 0,
        "segmentation": [[10, 20, 15, 20, 15, 45, 10, 45]]}
    ], "categories": [
        {"id": 1, "name": "cat", "supercategory": "annimal"},
        {"id": 2, "name": "dog", "supercategory": "annimal"}
    ], "images": [
        {"file_name": "image_1.png", "height": 64, "width": 64, "id": 1},
        {"file_name": "image_2.png", "height": 64, "width": 64, "id": 2},
        {"file_name": "image_3.png", "height": 64, "width": 64, "id": 3}
    ]
}
>>> from flash import Trainer
>>> from flash.image import InstanceSegmentation, InstanceSegmentationData
>>> datamodule = InstanceSegmentationData.from_coco(
...     train_folder="train_folder",
...     train_ann_file="train_annotations.json",
...     predict_folder="predict_folder",
...     transform_kwargs=dict(image_size=(128, 128)),
...     batch_size=2,
... )
>>> datamodule.num_classes
3
>>> datamodule.labels
['background', 'cat', 'dog']
>>> model = InstanceSegmentation(num_classes=datamodule.num_classes)
>>> trainer = Trainer(fast_dev_run=True)
>>> trainer.fit(model, datamodule=datamodule)  
Training...
>>> trainer.predict(model, datamodule=datamodule)  
Predicting...
classmethod from_files(predict_files=None, predict_transform=<class 'flash.core.integrations.icevision.transforms.IceVisionInputTransform'>, input_cls=<class 'flash.core.integrations.icevision.data.IceVisionInput'>, transform_kwargs=None, **data_module_kwargs)[source]

Creates a DataModule object from the given a list of files.

This is supported only for the predicting stage.

Parameters
  • predict_files (Optional[List[str]]) – The list of files containing the predict data.

  • predict_transform (TypeVar(INPUT_TRANSFORM_TYPE, Type[flash.core.data.io.input_transform.InputTransform], Callable, Tuple[Union[LightningEnum, str], Dict[str, Any]], Union[LightningEnum, str], None)) – The dictionary of transforms to use during predicting which maps.

  • input_cls (Type[Input]) – The Input used to create the dataset.

  • transform_kwargs (Optional[Dict]) – Keyword arguments provided to the transform on instantiation.

  • data_module_kwargs (Any) – The keywords arguments for creating the datamodule.

Return type

DataModule

Returns

The constructed data module.

classmethod from_folders(predict_folder=None, predict_transform=<class 'flash.core.integrations.icevision.transforms.IceVisionInputTransform'>, input_cls=<class 'flash.core.integrations.icevision.data.IceVisionInput'>, transform_kwargs=None, **data_module_kwargs)[source]

Creates a DataModule object from the given folders.

This is supported only for the predicting stage.

Parameters
  • predict_folder (Optional[str]) – The folder containing the predict data.

  • predict_transform (TypeVar(INPUT_TRANSFORM_TYPE, Type[flash.core.data.io.input_transform.InputTransform], Callable, Tuple[Union[LightningEnum, str], Dict[str, Any]], Union[LightningEnum, str], None)) – The dictionary of transforms to use during predicting which maps.

  • input_cls (Type[Input]) – The Input used to create the dataset.

  • transform_kwargs (Optional[Dict]) – Keyword arguments provided to the transform on instantiation.

  • data_module_kwargs (Any) – The keywords arguments for creating the datamodule.

Return type

DataModule

Returns

The constructed data module.

classmethod from_voc(labels, train_folder=None, train_target_folder=None, train_ann_folder=None, val_folder=None, val_target_folder=None, val_ann_folder=None, test_folder=None, test_target_folder=None, test_ann_folder=None, predict_folder=None, input_cls=<class 'flash.core.integrations.icevision.data.IceVisionInput'>, transform=<class 'flash.core.integrations.icevision.transforms.IceVisionInputTransform'>, transform_kwargs=None, **data_module_kwargs)[source]

Creates a InstanceSegmentationData object from the given data folders, mask folders, and annotation files in the PASCAL VOC (Visual Object Challenge) XML format.

Note

All three arguments *_folder, *_target_folder, and *_ann_folder are needed to load data for a particular stage.

To learn how to customize the transforms applied for each stage, read our customizing transforms guide.

Parameters
  • labels (List[str]) – A list of class labels. Note that the list should not include a label for the background class which will be added automatically as class zero (additional labels will be sorted).

  • train_folder (Optional[str]) – The folder containing images to use when training.

  • train_target_folder (Optional[str]) – The folder containing mask images to use when training.

  • train_ann_folder (Optional[str]) – The folder containing VOC format annotation files to use when training.

  • val_folder (Optional[str]) – The folder containing images to use when validating.

  • val_target_folder (Optional[str]) – The folder containing mask images to use when validating.

  • val_ann_folder (Optional[str]) – The folder containing VOC format annotation files to use when validating.

  • test_folder (Optional[str]) – The folder containing images to use when testing.

  • test_target_folder (Optional[str]) – The folder containing mask images to use when testing.

  • test_ann_folder (Optional[str]) – The folder containing VOC format annotation files to use when testing.

  • predict_folder (Optional[str]) – The folder containing images to use when predicting.

  • input_cls (Type[Input]) – The Input type to use for loading the data.

  • transform (TypeVar(INPUT_TRANSFORM_TYPE, Type[flash.core.data.io.input_transform.InputTransform], Callable, Tuple[Union[LightningEnum, str], Dict[str, Any]], Union[LightningEnum, str], None)) – The InputTransform type to use.

  • transform_kwargs (Optional[Dict]) – Dict of keyword arguments to be provided when instantiating the transforms.

  • data_module_kwargs (Any) – Additional keyword arguments to provide to the DataModule constructor.

Returns

The constructed InstanceSegmentationData.

Examples

The folder train_folder has the following contents:

train_folder
├── image_1.png
├── image_2.png
├── image_3.png
...

The folder train_masks has the following contents:

train_masks
├── image_1.png
├── image_2.png
├── image_3.png
...

The folder train_annotations has the following contents:

train_annotations
├── image_1.xml
├── image_2.xml
├── image_3.xml
...

The file image_1.xml contains the following:

<annotation>
    <filename>image_0.png</filename>
    <path>image_0.png</path>
    <source><database>example</database></source>
    <size><width>64</width><height>64</height><depth>3</depth></size>
    <object>
        <name>cat</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <occluded>0</occluded>
        <bndbox><xmin>10</xmin><xmax>15</xmax><ymin>20</ymin><ymax>30</ymax></bndbox>
    </object>
</annotation>
>>> from flash import Trainer
>>> from flash.image import InstanceSegmentation, InstanceSegmentationData
>>> datamodule = InstanceSegmentationData.from_voc(
...     ["cat", "dog"],
...     train_folder="train_folder",
...     train_target_folder="train_masks",
...     train_ann_folder="train_annotations",
...     predict_folder="predict_folder",
...     transform_kwargs=dict(image_size=(128, 128)),
...     batch_size=2,
... )
>>> datamodule.num_classes
3
>>> datamodule.labels
['background', 'cat', 'dog']
>>> model = InstanceSegmentation(num_classes=datamodule.num_classes)
>>> trainer = Trainer(fast_dev_run=True)
>>> trainer.fit(model, datamodule=datamodule)  
Training...
>>> trainer.predict(model, datamodule=datamodule)  
Predicting...
input_transform_cls

alias of flash.core.integrations.icevision.transforms.IceVisionInputTransform

Read the Docs v: stable
Versions
latest
stable
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0post1
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.