stand_mapping.utils package

Submodules

stand_mapping.utils.fishnet module

stand_mapping.utils.fishnet.make_buffered_fishnet(xmin, ymin, xmax, ymax, crs, spacing=1000, overlap=50)[source]

Makes GeoDataFrame with a fishnet grid with optional overlapping edges.

Converts an existing lidar tiling scheme into one that has overlapping tiles and which is aligned with a grid based on the spacing parameter.

Parameters:
  • ymin, xmax, ymax (xmin,) – Values indicating the extent of the existing lidar data.
  • crs (Coordinate Reference System) – Must be readable by GeoPandas to create a GeoDataFrame.
  • spacing (int) – Length and width of tiles in new tiling scheme prior to buffering
  • overlap (int) – Amount of overlap between neighboring tiles.

stand_mapping.utils.scoring module

Utility functions for scoring instance segmentations against ground truth images.

masks_iou function is adapted from https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/utils.py

Mask R-CNN The MIT License (MIT) Copyright (c) 2017 Matterport, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

boundary_f1_score is adapted from https://github.com/fperazzi/davis/blob/master/python/lib/davis/measures/f_boundary.py

Copyright (c) 2016, Federico Perazzi
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • The names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Federico Perazzi ‘AS IS’ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Federico Perazzi BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

stand_mapping.utils.scoring.boundary_f1_score(gt_masks, pred_masks, nodata=None, bound_thresh=2)[source]

Compute mean,recall and decay from per-frame evaluation. Calculates precision/recall for boundaries between foreground_mask and gt_mask using morphological operators to speed it up.

Parameters:
  • gt_masks (array, shape (height, width)) – ground truth masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • pred_masks (array, shape (height, width)) – predicted masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • bound_thresh (int) – distance threshold from a boundary that will be accepted as a detection, in pixels
  • nodata (array, shape (height, width)) – optional nodata mask; pixels where nodata is True will be excluded from scoring (set to 0)
Returns:

iou, dilated_iou, f1, precision, recall – IOU, IOU with dilated boundaries, F1, Precision, and Recall scores comparing ground truth and predicted masks

Return type:

numeric

stand_mapping.utils.scoring.mask_f1_score(gt_masks, pred_masks, nodata=None, num_classes=None, iou_thresh=0.5)[source]

Calculates Precision, Recall, and F1 Score given an array with IoU scores for ground truth vs. predicted instance masks.

IoU scores greater than iou_thresh are considered true positive detections.

Parameters:
  • gt_masks (array, shape (height, width)) – ground truth masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • pred_masks (array, shape (height, width)) – predicted masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • nodata (array, shape (height, width)) – optional nodata mask; pixels where nodata is True will be excluded from scoring (set to 0)
  • num_classes (int, optional) – number of classes that occur across gt_masks and pred_masks. If not specified, only the unique classes observed in each layer will be used.
  • iou_thresh (numeric, default = 0.5) – value for intersection over union between two masks for which an instance is considered detected or not detected
Returns:

iou, f1, precision, recall – average IOU, F1, Precision, and Recall scores comparing ground truth and predicted masks

Return type:

numeric

stand_mapping.utils.scoring.masks_iou(gt_masks, pred_masks, nodata=None, num_classes=None)[source]

Computes the Intersection over Union (IoU) for each combination of masks in a ground-truth image and predicted segmentation/labels image.

Parameters:
  • gt_masks (array, shape (height, width)) – ground truth masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • pred_masks (array, shape (height, width)) – predicted masks represented as a single channel array with each distinct object in the image indicate with a different non-zero integer
  • nodata (array, shape (height, width)) – optional nodata mask; pixels where nodata is True will be excluded from scoring (set to 0)
  • num_classes (int, optional) – number of classes that occur across gt_masks and pred_masks. If not specified, only the unique classes observed in each layer will be used.
Returns:

iou – array containing iou scores for each ground truth mask and each predicted mask.

Return type:

array, shape (gt_instances, pred_instances)

stand_mapping.utils.scoring.rasterize_boundary(gdf, out_shape, transform, dist=False)[source]

Rasterizes lines and boundaries of polygons in a GeoDataFrame such that the presence of a boundary or line is indicated with a 1 (present) or 0 (absent). Optionally, can return the Euclidean distance from the nearest boundary instead of the binary mask.

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame whose lines and boundaries will be rasterized
  • out_shape (2-tuple or list-like) – (height, width) of desired output raster
  • transform (Affine) – rasterio-style (not GDAL-style) affine transformation matrix which translates pixel coordinates to geographic coordinates
  • dist (bool) – if False (default), returns a binary mask for boundaries in the GeoDataFrame, otherwise returns Euclidean distance to the nearest boundary.
Returns:

ras – array with binary boundary presence/absence or distance to boundary

Return type:

arr

stand_mapping.utils.scoring.rasterize_polygons(gdf, out_shape, transform)[source]

Rasterizes a GeoDataFrame such that each distinct geometry is rendered with a distinct integer value in the output raster.

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame to be rasterized
  • out_shape (2-tuple or list-like) – (height, width) of desired output raster
  • transform (Affine) – rasterio-style (not GDAL-style) affine transformation matrix which translates pixel coordinates to geographic coordinates
Returns:

ras – array with each geometry labeled with a distinct integer

Return type:

arr

stand_mapping.utils.scoring.single_to_multichannel_mask(mask_img, num_classes=None)[source]

From a single array with instances indicated by distinct non-zero integer masks, returns a stack of arrays with each object shown in a single array.

Parameters:
  • mask_img (array, shape (width, height)) – array with each instance to be detected indicated by unique non-zero integer mask.
  • num_classes (int) – number of classes to include as channels in masks
Returns:

masks – boolean arrays with mask of each instance indicated as True.

Return type:

array, shape (width, height, instances)

Module contents