SemanticSegmentationSmoothLabels#

class SemanticSegmentationSmoothLabels[source]#

Bases: SemanticSegmentationLabels

Membership-scores for each pixel for each class.

Maintains a num_classes x H x W array where value_{ijk} represents the probability (or some other measure) of pixel_{jk} belonging to class i. A discrete label array can be obtained from this by argmax’ing along the class dimension.

__init__(extent: ~rastervision.core.box.Box, num_classes: int, dtype: ~typing.Any = <class 'numpy.float16'>, dtype_hits: ~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 scores array. Defaults to np.float16.

  • dtype_hits (Any) – dtype of the hits 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_scores)

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 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 score to 1 and all others to zero.

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

Save labels as rasters and/or vectors.

__add__(other: SemanticSegmentationSmoothLabels) SemanticSegmentationSmoothLabels[source]#

Merge self with other by adding pixel scores and hits.

Parameters

other (SemanticSegmentationSmoothLabels) –

Return type

SemanticSegmentationSmoothLabels

__getitem__(window: Box) ndarray[source]#

Get labels for the given window.

Parameters

window (Box) –

Return type

ndarray

__init__(extent: ~rastervision.core.box.Box, num_classes: int, dtype: ~typing.Any = <class 'numpy.float16'>, dtype_hits: ~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 scores array. Defaults to np.float16.

  • dtype_hits (Any) – dtype of the hits array. Defaults to np.uint8.

__setitem__(window: Box, values: ndarray) None#

Set labels for the given window.

Parameters
Return type

None

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_scores: ndarray) None[source]#

Set labels for the given window.

Parameters
  • window (Box) –

  • pixel_class_scores (ndarray) –

Return type

None

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

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, crop_sz: Optional[int] = None) 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.

  • 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

SemanticSegmentationSmoothLabels

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

Get a binary mask representing all pixels of a class.

Parameters
Return type

ndarray

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.

Parameters

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

Return type

List[Box]

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

Instantiate an empty instance.

Parameters
  • extent (Box) –

  • num_classes (int) –

Return type

SemanticSegmentationSmoothLabels

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

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

Parameters
Return type

None

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

Save labels as rasters 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(s).

  • class_config (ClassConfig) – The ClassConfig.

  • bbox (Optional[Box]) – User-specified crop of the extent. Must be provided if the corresponding RasterSource has bbox != extent.

  • 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.

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

  • smooth_output (bool, optional) – If True, saves labels as a raster of class scores (one band for each class). Defaults to True.

  • smooth_as_uint8 (bool, optional) – If True, stores smooth class scores as np.uint8 (0-255) values rather than as np.float32 discrete labels, to help save memory/disk space. Defaults to False.

  • 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(s). Defaults to None.

Return type

None