Convert to COCO format

MS-COCO is a popular JSON-based format for segmentation, object detection (bbox), and instance segmentation tasks. Popular frameworks that use this framework include Detectron2. To learn more about how to train your own Detectron2 model with Labelbox data, check out these guides for panoptic segmentation and object detection.

Labelbox currently supports converting data into both the COCO object and panoptic formats. Both formats support instance detection. The primary difference is that the object detection format encodes objects as polygons, allowing for overlapping instances. The panoptic dataset, however, encodes each class at the pixel level. This allows users to encode well defined objects as instances and amorphous background classes as individual pixels. When determining which format to use, consider whether or not you need to model amorphous background classes. If not, use the object detection format. The object format is also the original coco format and will have more open source tooling compatible with it.

Caveats:

  • name field must be present for the annotations
  • nested classifications are not supported
import labelbox as lb
from labelbox.data.serialization import COCOConverter
from labelbox.data.serialization.labelbox_v1.converter import LBV1Converter

client = lb.Client(API_KEY)
project = client.get_project('PROJECT_ID')
project_export = project.export_labels(download=True)
labels = LBV1Converter.deserialize(project_export)

mask_path = "./masks/"
image_path = './images/'

# Option 1: for converting to object detection format 
coco_labels = COCOConverter.serialize_instances(
    labels,
    image_root=image_path,
    ignore_existing_data=True
)

# Option 2: for converting to panpoptic format
coco_labels = COCOConverter.serialize_panoptic(
    labels,
    image_root=image_path,
    mask_root=mask_path,
    ignore_existing_data=True
)
import labelbox as lb
from labelbox.data.serialization import COCOConverter
from labelbox.data.serialization.labelbox_v1.converter import LBV1Converter

client = lb.Client(API_KEY)
model_run = client.get_model_run('MODEL_RUN_ID')
model_run_export = model_run.export_labels(download=True)
labels = LBV1Converter.deserialize(model_run_export)


mask_path = "./masks/"
image_path = './images/'

# Option 1: for converting to object detection format 
coco_labels = COCOConverter.serialize_instances(
    labels,
    image_root=image_path,
    ignore_existing_data=True
)

# Option 2: for converting to panpoptic format
coco_labels = COCOConverter.serialize_panoptic(
    labels,
    image_root=image_path,
    mask_root=mask_path,
    ignore_existing_data=True
)