tlc.data_types.segmentation¶
Defines representations of instance segmentation data in 3LC.
Module Contents¶
Classes¶
Class |
Description |
|---|---|
A dictionary representation of a COCO RLE. |
|
Container for mask-based instance segmentation. |
|
Container for polygon-based instance segmentation. |
Data¶
Data |
Description |
|---|---|
API¶
- class CocoRle¶
Bases:
typing.TypedDictA dictionary representation of a COCO RLE.
Initialize self. See help(type(self)) for accurate signature.
- class SegmentationMasks¶
Bases:
tlc.data_types._geometry_base.GeometryBaseContainer for mask-based instance segmentation.
Axis order — important. Masks are stored as a
(H, W, N)uint8 array (height, width, instance index). This differs from the PyTorch / Mask R-CNN convention of(N, H, W). Usemask_format="nhw"on the constructor to supply PyTorch-style input — it gets transposed to(H, W, N)once during__post_init__. Read access (seg.masks) always returns the native(H, W, N)layout.Per-instance metadata (labels, confidences, etc.) is stored in
per_instance_extras.Objects can be created in two ways:
Direct construction: Pass all data at once.
Build incrementally: Use
create_empty()andadd_instance().
Example:
import numpy as np # Direct construction (native (H, W, N) layout) masks = np.zeros((480, 640, 2), dtype=np.uint8) masks[10:50, 10:50, 0] = 1 masks[100:200, 100:200, 1] = 1 seg = SegmentationMasks( image_width=640, image_height=480, masks=masks, labels=[1, 2], ) # Direct construction from PyTorch-style (N, H, W) input nhw = np.zeros((2, 480, 640), dtype=np.uint8) seg = SegmentationMasks( image_width=640, image_height=480, masks=nhw, mask_format="nhw", labels=[1, 2], ) # Build incrementally — single (H, W) masks, no axis ambiguity seg = SegmentationMasks.create_empty(image_width=640, image_height=480) mask = np.zeros((480, 640), dtype=np.uint8) mask[10:50, 10:50] = 1 seg.add_instance(mask=mask, label=1) # Serialization — masks are RLE-encoded into the row, decoded back on read row = seg.to_row() seg = SegmentationMasks.from_row(row)
- add_instance(
- *,
- mask: ndarray,
- label: int | None = None,
- confidence: float | None = None,
- per_instance_extras: Mapping[str, Any] | None = None,
Add a single mask instance.
- Parameters:
mask – A 2D (H, W) binary mask for this instance.
label – Optional integer class label for this instance.
confidence – Optional confidence score for this instance.
per_instance_extras – Optional dictionary of per-instance attributes. The first call establishes which keys are expected; subsequent calls must provide the same keys.
- Raises:
ValueError – If the mask shape does not match (image_height, image_width).
- classmethod create_empty( ) SegmentationMasks¶
Create an empty SegmentationMasks object to build up incrementally.
- Parameters:
image_width – Width of the image in pixels (required for correct empty mask shape).
image_height – Height of the image in pixels (required for correct empty mask shape).
- Returns:
An empty SegmentationMasks object ready for adding instances.
- mask_format: InitVar['hwn' | 'nhw'] = hwn¶
Layout of the input
masksarray. Storage is always"hwn"((H, W, N)).Use
"nhw"for PyTorch / Mask R-CNN style(N, H, W)inputs. Only matters for 3D inputs; a 2D(H, W)mask is unambiguously a single instance.
- masks: ndarray = field(...)¶
(H, W, N)uint8 array of binary masks. Native storage layout.For PyTorch-style
(N, H, W)input passmask_format="nhw"to the constructor; masks are transposed to the native layout once during__post_init__.
- classmethod schema(
- classes: str | Sequence[str] | Sequence[dict[str, str]] | Sequence[MapElement] | dict[float, str] | dict[int, str] | dict[float, MapElement] | dict[int, MapElement] | None = None,
- *,
- include_per_instance_confidence: bool = False,
- include_is_crowd: bool = False,
- display_name: str = '',
- description: str = '',
- writable: bool = True,
- default_visible: bool = True,
- display_importance: float = 0,
- per_instance_schemas: dict[str, Schema] | None = None,
Build the column
Schemadescribing mask-based instance segmentation.- Parameters:
classes – A class map for per-instance labels, or
Noneto skip labels.include_per_instance_confidence – Whether to add a per-instance confidence field.
include_is_crowd – Whether to add an
is_crowdfield.display_name – Column display name in the Dashboard.
description – Column description.
writable – Whether the column is editable in the Dashboard.
default_visible – Whether the column is visible by default.
display_importance – Ordering weight for column display.
per_instance_schemas – Custom per-instance schemas (advanced).
- Returns:
A column schema describing the serialized form of this class.
- SegmentationMasksFormat = None¶
- class SegmentationPolygons¶
Bases:
tlc.data_types._geometry_base.GeometryBaseContainer for polygon-based instance segmentation.
Each instance has one polygon (list of alternating x,y coords). Per-instance metadata (labels, confidences, etc.) is stored in
per_instance_extras.Objects can be created in two ways:
Direct construction: Pass all data at once.
Build incrementally: Use
create_empty()andadd_instance().
Example:
import numpy as np # Direct construction seg = SegmentationPolygons( image_width=640, image_height=480, polygons=[[10.0, 20.0, 50.0, 20.0, 30.0, 60.0]], labels=[1], ) # Build incrementally seg = SegmentationPolygons.create_empty(image_width=640, image_height=480) seg.add_instance( polygon=[10.0, 20.0, 50.0, 20.0, 30.0, 60.0], label=1, ) # Serialization — convert to/from 3LC Table row format # Default ``from_row`` returns absolute pixel coords; call ``.to_relative()`` for [0, 1] output. # When reading via ``table[i][col]``, ``Schema.from_row`` honors the schema's ``relative=`` # setting automatically and applies ``.to_relative()`` on your behalf when set. row = seg.to_row() seg = SegmentationPolygons.from_row(row).to_relative()
- add_instance(
- *,
- polygon: list[float] | ndarray,
- label: int | None = None,
- confidence: float | None = None,
- per_instance_extras: Mapping[str, Any] | None = None,
Add a single polygon instance.
- Parameters:
polygon – Flat list of alternating x,y coordinates. Must have even length >= 6 (at least 3 points).
label – Optional integer class label for this instance.
confidence – Optional confidence score for this instance.
per_instance_extras – Optional dictionary of per-instance attributes. The first call establishes which keys are expected; subsequent calls must provide the same keys.
- Raises:
ValueError – If the polygon has fewer than 6 coordinates or odd length.
- classmethod create_empty( ) SegmentationPolygons¶
Create an empty SegmentationPolygons object to build up incrementally.
- Parameters:
image_width – Width of the image in pixels.
image_height – Height of the image in pixels.
- Returns:
An empty SegmentationPolygons object ready for adding instances.
- polygons: list[list[float]] = field(...)¶
Per-instance polygons,
list[list[float]](alternating x, y coords) after coercion. The constructor also accepts an ndarray of shape(num_instances, 2*num_points);__post_init__converts it to list-of-lists. Use the list form (or pre-convert with[list(map(float, p)) for p in arr]) to satisfy strict type checking.
- relative: bool = False¶
Whether
polygonsare in relative[0, 1]coords (True) or absolute pixel coords (False, default). Naming matches the schema’srelative=kwarg and the value type’spolygons_are_relativeflag.from_rowreturns absolute pixel coords by default; the schema’spolygons_are_relativeflag (set viaSchema(..., relative=True)) is honored bySchema.from_rowand causes a finalto_relative()to be applied. On write,to_rowreads this field to know whether to scale up before RLE-encoding.
- classmethod schema(
- classes: str | Sequence[str] | Sequence[dict[str, str]] | Sequence[MapElement] | dict[float, str] | dict[int, str] | dict[float, MapElement] | dict[int, MapElement] | None = None,
- *,
- relative: bool = False,
- include_per_instance_confidence: bool = False,
- include_is_crowd: bool = False,
- display_name: str = '',
- description: str = '',
- writable: bool = True,
- default_visible: bool = True,
- display_importance: float = 0,
- per_instance_schemas: dict[str, Schema] | None = None,
Build the column
Schemadescribing polygon-based instance segmentation.- Parameters:
classes – A class map for per-instance labels, or
Noneto skip labels.relative – Stores the column’s preferred coordinate convention on the value type (
polygons_are_relative). When reading through the schema (e.g.table[i][col]),tlc.Schema.from_row()honors this flag and appliesSegmentationPolygons.to_relativeautomatically. Direct calls toSegmentationPolygons.from_rowskip the schema and always return absolute coords; call.to_relative()on the result if you need normalized output.include_per_instance_confidence – Whether to add a per-instance confidence field.
include_is_crowd – Whether to add an
is_crowdfield.display_name – Column display name in the Dashboard.
description – Column description.
writable – Whether the column is editable in the Dashboard.
default_visible – Whether the column is visible by default.
display_importance – Ordering weight for column display.
per_instance_schemas – Custom per-instance schemas (advanced).
- Returns:
A column schema describing the serialized form of this class.
- to_absolute() SegmentationPolygons¶
Return a copy with polygons in absolute pixel coordinates.
Idempotent: returns
selfunchanged when already in pixel coords.- Raises:
ValueError – If
image_widthorimage_heightis not positive.
- to_relative() SegmentationPolygons¶
Return a copy with polygons in relative
[0, 1]coordinates.Idempotent: returns
selfunchanged when already relative.- Raises:
ValueError – If
image_widthorimage_heightis not positive.