SemanticSegmentationDiscreteLabels#

class SemanticSegmentationDiscreteLabels[source]#

Bases: SemanticSegmentationLabels

Vote-counts for each pixel belonging to each class.

Maintains a num_classes x H x W array where value_{ijk} represents how many times pixel_{jk} has been classified as class i. A label array can be obtained from this by argmax’ing along the class dimension. Can also be turned into a score converting counts to probabilities.

__init__(extent: ~rastervision.core.box.Box, num_classes: int, dtype: ~typing.Any = <class 'numpy.uint8'>)[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 (Any) – dtype of the counts array. Defaults to np.uint8.

Methods

__init__(extent, num_classes[, dtype])

Constructor.

add_predictions(windows, predictions[, crop_sz])

Populate predictions.

add_window(window, pixel_class_ids)

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_label_arr(window[, null_class_id])

Get labels as array of class IDs.

get_score_arr(window)

Get array of pixel scores.

get_windows(**kwargs)

Generate sliding windows over the local extent.

make_empty(extent, num_classes)

Instantiate an empty instance.

mask_fill(window, mask, fill_value)

Set fill_value'th class ID's count to 1 and all others to zero.

save(uri, crs_transformer, class_config[, ...])

Save labels as a raster and/or vectors.

__init__(extent: ~rastervision.core.box.Box, num_classes: int, dtype: ~typing.Any = <class 'numpy.uint8'>)[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 (Any) – dtype of the counts array. Defaults to np.uint8.

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

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

add_window(window: Box, pixel_class_ids: ndarray) None[source]#

Set labels for the given window.

Parameters
Return type

None

filter_by_aoi(aoi_polygons: list, null_class_id: int, **kwargs) SemanticSegmentationLabels#

Keep only the values that lie inside the AOI.

Parameters
  • aoi_polygons (list) –

  • null_class_id (int) –

Return type

SemanticSegmentationLabels

classmethod from_predictions(windows: Iterable[Box], predictions: Iterable[Any], extent: Box, num_classes: int, crop_sz: Optional[int] = None) Union[SemanticSegmentationDiscreteLabels, SemanticSegmentationSmoothLabels][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.

  • 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

Union[SemanticSegmentationDiscreteLabels, SemanticSegmentationSmoothLabels]: If smooth=True, returns a

SemanticSegmentationSmoothLabels. Otherwise, a SemanticSegmentationDiscreteLabels.

Return type

Union[SemanticSegmentationDiscreteLabels, SemanticSegmentationSmoothLabels]

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

Get labels as array of class IDs.

Returns null_class_id for pixels for which there is no data.

Parameters
  • window (Box) –

  • null_class_id (int) –

Return type

ndarray

get_score_arr(window: Box) ndarray[source]#

Get array of pixel scores.

Parameters

window (Box) –

Return type

ndarray

get_windows(**kwargs) List[Box]#

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.

Return type

List[Box]

classmethod make_empty(extent: Box, num_classes: int) SemanticSegmentationDiscreteLabels[source]#

Instantiate an empty instance.

Parameters
  • extent (Box) –

  • num_classes (int) –

Return type

SemanticSegmentationDiscreteLabels

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

Set fill_value’th class ID’s count to 1 and all others to zero.

Parameters
Return type

None

save(uri: str, crs_transformer: CRSTransformer, class_config: ClassConfig, tmp_dir: Optional[str] = None, save_as_rgb: bool = False, raster_output: bool = True, rasterio_block_size: int = 512, vector_outputs: Optional[Sequence[VectorOutputConfig]] = None, profile_overrides: Optional[dict] = None) None[source]#

Save labels as a raster and/or vectors.

If URI is remote, all files will be first written locally and then uploaded to the URI.

Parameters
  • uri (str) – URI of directory in which to save all output files.

  • crs_transformer (CRSTransformer) – CRSTransformer to configure CRS and affine transform of the output GeoTiff.

  • class_config (ClassConfig) – The ClassConfig.

  • tmp_dir (Optional[str], optional) – Temporary directory to use. If None, will be auto-generated. Defaults to None.

  • save_as_rgb (bool, optional) – If True, Saves labels as an RGB image, using the class-color mapping in the class_config. Defaults to False.

  • raster_output (bool, optional) – If True, saves labels as a raster of class IDs (one band). Defaults to True.

  • rasterio_block_size (int, optional) – Value to set blockxsize and blockysize to. Defaults to 512.

  • vector_outputs (Optional[Sequence[VectorOutputConfig]], optional) – List of VectorOutputConfig’s containing vectorization configuration information. Only classes for which a VectorOutputConfig is specified will be saved as vectors. If None, no vector outputs will be produced. Defaults to None.

  • profile_overrides (Optional[dict], optional) – This can be used to arbitrarily override properties in the profile used to create the output GeoTiff. Defaults to None.

Return type

None