Shortcuts

Instance Segmentation

The Task

Instance segmentation is the task of segmenting objects images and determining their associated classes.

The InstanceSegmentation and InstanceSegmentationData classes internally rely on IceVision.


Example

Let’s look at instance segmentation with The Oxford-IIIT Pet Dataset from IceData. Once we’ve downloaded the data, we can create the InstanceSegmentationData. We select a mask_rcnn with a resnet18_fpn backbone to use for our InstanceSegmentation and fine-tune on the pets data. We then use the trained InstanceSegmentation for inference. Finally, we save the model. Here’s the full example:

from functools import partial

import flash
from flash.core.utilities.imports import example_requires
from flash.image import InstanceSegmentation, InstanceSegmentationData

example_requires("image")

import icedata  # noqa: E402

# 1. Create the DataModule
data_dir = icedata.pets.load_data()

datamodule = InstanceSegmentationData.from_folders(
    train_folder=data_dir,
    val_split=0.1,
    parser=partial(icedata.pets.parser, mask=True),
)

# 2. Build the task
model = InstanceSegmentation(
    head="mask_rcnn",
    backbone="resnet18_fpn",
    num_classes=datamodule.num_classes,
)

# 3. Create the trainer and finetune the model
trainer = flash.Trainer(max_epochs=1)
trainer.finetune(model, datamodule=datamodule, strategy="freeze")

# 4. Detect objects in a few images!
predictions = model.predict(
    [
        str(data_dir / "images/yorkshire_terrier_9.jpg"),
        str(data_dir / "images/yorkshire_terrier_12.jpg"),
        str(data_dir / "images/yorkshire_terrier_13.jpg"),
    ]
)
print(predictions)

# 5. Save the model!
trainer.save_checkpoint("instance_segmentation_model.pt")

Flash Zero

The instance segmentation task can be used directly from the command line with zero code using Flash Zero. You can run the above example with:

flash instance_segmentation

To view configuration options and options for running the instance segmentation task with your own data, use:

flash instance_segmentation --help
Read the Docs v: latest
Versions
latest
stable
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
pdf
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.