SemanticSegmentationLabels#

class SemanticSegmentationLabels[source]#

Bases: Labels

Representation of Semantic Segmentation labels.

__init__(extent: Box, num_classes: int, dtype: dtype)[source]#

Constructor.

Parameters
  • extent (Box) – The extent of the region to which the labels belong, in global coordinates.

  • num_classes (int) – Number of classes.

  • dtype (dtype) –

Methods

__init__(extent, num_classes, dtype)

Constructor.

add_predictions(windows, predictions[, crop_sz])

Populate predictions.

add_window(window, values)

Set labels for the given window.

filter_by_aoi(aoi_polygons, null_class_id, ...)

Keep only the values that lie inside the AOI.

from_predictions(windows, predictions, ...)

Instantiate from windows and their corresponding predictions.

get_class_mask(window, class_id[, threshold])

Get a binary mask representing all pixels of a class.

get_label_arr(window[, null_class_id])

Get labels as a 2D array of class IDs.

get_score_arr(window[, null_class_id])

Get (C, H, W) array of pixel scores.

get_windows(**kwargs)

Generate sliding windows over the local extent.

make_empty(extent, num_classes[, smooth])

Instantiate an empty instance.

mask_fill(window, mask, fill_value)

Given a window and a binary mask, set all the pixels in the window for which the mask is ON to the fill_value.

save(uri)

Save to file.

abstract __add__(other: SemanticSegmentationLabels) SemanticSegmentationLabels[source]#

Merge self with other labels.

Parameters

other (SemanticSegmentationLabels) –

Return type

SemanticSegmentationLabels

abstract __getitem__(window: Box) ndarray[source]#

Get labels for the given window.

Parameters

window (Box) –

Return type

ndarray

__init__(extent: Box, num_classes: int, dtype: dtype)[source]#

Constructor.

Parameters
  • extent (Box) – The extent of the region to which the labels belong, in global coordinates.

  • num_classes (int) – Number of classes.

  • dtype (dtype) –

__setitem__(window: Box, values: ndarray) None[source]#

Set labels for the given window.

Parameters
Return type

None

add_predictions(windows: Iterable[Box], predictions: Iterable[Any], crop_sz: Optional[int] = None) None[source]#

Populate predictions.

Parameters
  • windows (Iterable[Box]) – Boxes in pixel coords, specifying chips in the raster.

  • predictions (Iterable[Any]) – The model predictions for each chip specified by the windows.

  • crop_sz (Optional[int]) – Number of rows/columns of pixels from the edge of prediction windows to discard. This is useful because predictions near edges tend to be lower quality and can result in very visible artifacts near the edges of chips. This should only be used if the given windows represent a sliding-window grid over the scene extent with overlap between adjacent windows. Defaults to None.

Return type

None

abstract add_window(window: Box, values: ndarray) List[Box][source]#

Set labels for the given window.

Parameters
Return type

List[Box]

filter_by_aoi(aoi_polygons: List[Polygon], null_class_id: int, **kwargs) SemanticSegmentationLabels[source]#

Keep only the values that lie inside the AOI.

This is an inplace operation.

Parameters
Return type

SemanticSegmentationLabels

classmethod from_predictions(windows: Iterable[Box], predictions: Iterable[Any], extent: Box, num_classes: int, smooth: bool = False, crop_sz: Optional[int] = None) SemanticSegmentationLabels[source]#

Instantiate from windows and their corresponding predictions.

Parameters
  • windows (Iterable[Box]) – Boxes in pixel coords, specifying chips in the raster.

  • predictions (Iterable[Any]) – The model predictions for each chip specified by the windows.

  • extent (Box) – The extent of the region to which the labels belong, in global coordinates.

  • num_classes (int) – Number of classes.

  • smooth (bool, optional) – If True, creates a SemanticSegmentationSmoothLabels object. If False, creates a SemanticSegmentationDiscreteLabels object. Defaults to False.

  • crop_sz (Optional[int]) – Number of rows/columns of pixels from the edge of prediction windows to discard. This is useful because predictions near edges tend to be lower quality and can result in very visible artifacts near the edges of chips. This should only be used if the given windows represent a sliding-window grid over the scene extent with overlap between adjacent windows. Defaults to None.

Returns

If smooth=True, returns a SemanticSegmentationSmoothLabels. Otherwise, a SemanticSegmentationDiscreteLabels.

Return type

SemanticSegmentationLabels

get_class_mask(window: Box, class_id: int, threshold: Optional[float] = None) ndarray[source]#

Get a binary mask representing all pixels of a class.

Parameters
Return type

ndarray

abstract get_label_arr(window: Box, null_class_id: int = -1) ndarray[source]#

Get labels as a 2D array of class IDs.

Note: The returned array is not guaranteed to be the same size as the input window.

Parameters
  • window (Box) –

  • null_class_id (int) –

Return type

ndarray

abstract get_score_arr(window: Box, null_class_id: int = -1) ndarray[source]#

Get (C, H, W) array of pixel scores.

Parameters
  • window (Box) –

  • null_class_id (int) –

Return type

ndarray

get_windows(**kwargs) List[Box][source]#

Generate sliding windows over the local extent.

The keyword args are passed to Box.get_windows() and can therefore be used to control the specifications of the windows.

If the keyword args do not contain size, a list of length 1, containing the full extent is returned.

Parameters

**kwargs – Extra args for Box.get_windows().

Return type

List[Box]

classmethod make_empty(extent: Box, num_classes: int, smooth: bool = False) SemanticSegmentationLabels[source]#

Instantiate an empty instance.

Parameters
  • extent (Box) – The extent of the region to which the labels belong, in global coordinates.

  • num_classes (int) – Number of classes.

  • smooth (bool, optional) – If True, creates a SemanticSegmentationSmoothLabels object. If False, creates a SemanticSegmentationDiscreteLabels object. Defaults to False.

Returns

If smooth=True, returns a SemanticSegmentationSmoothLabels. Otherwise, a SemanticSegmentationDiscreteLabels.

Raises

ValueError – if num_classes and extent are not specified, but smooth=True.

Return type

SemanticSegmentationLabels

abstract mask_fill(window: Box, mask: ndarray, fill_value: Any) None[source]#

Given a window and a binary mask, set all the pixels in the window for which the mask is ON to the fill_value.

Parameters
Return type

None

abstract save(uri: str) None#

Save to file.

Parameters

uri (str) –

Return type

None