topostats.unet_masking#

Segment grains using a U-Net model.

Attributes#

Functions#

dice_loss(→ tensorflow.Tensor)

DICE loss function.

iou_loss(→ tensorflow.Tensor)

Intersection over Union loss function.

mean_iou(y_true, y_pred)

Mean Intersection Over Union metric, ignoring the background class.

predict_unet(→ numpy.typing.NDArray[numpy.bool_])

Predict cats segmentation from a flattened image.

make_bounding_box_square(→ tuple[int, int, int, int])

Make a bounding box square.

pad_bounding_box(→ tuple[int, int, int, int])

Pad a bounding box.

pad_crop(→ numpy.NDArray)

Pad a crop.

make_crop_square(→ numpy.NDArray)

Make a crop square.

Module Contents#

topostats.unet_masking.LOGGER#
topostats.unet_masking.dice_loss(y_true: numpy.typing.NDArray[numpy.float32], y_pred: numpy.typing.NDArray[numpy.float32], smooth: float = 1e-05) tensorflow.Tensor[source]#

DICE loss function.

Expects y_true and y_pred to be of shape (batch_size, height, width, 1).

Parameters:
  • y_true (npt.NDArray[np.float32]) – True values.

  • y_pred (npt.NDArray[np.float32]) – Predicted values.

  • smooth (float) – Smoothing factor to prevent division by zero.

Returns:

The DICE loss.

Return type:

tf.Tensor

topostats.unet_masking.iou_loss(y_true: numpy.typing.NDArray[numpy.float32], y_pred: numpy.typing.NDArray[numpy.float32], smooth: float = 1e-05) tensorflow.Tensor[source]#

Intersection over Union loss function.

Expects y_true and y_pred to be of shape (batch_size, height, width, 1).

Parameters:
  • y_true (npt.NDArray[np.float32]) – True values.

  • y_pred (npt.NDArray[np.float32]) – Predicted values.

  • smooth (float) – Smoothing factor to prevent division by zero.

Returns:

The IoU loss.

Return type:

tf.Tensor

topostats.unet_masking.mean_iou(y_true: numpy.typing.NDArray[numpy.float32], y_pred: numpy.typing.NDArray[numpy.float32])[source]#

Mean Intersection Over Union metric, ignoring the background class.

Parameters:
  • y_true (npt.NDArray[np.float32]) – True values.

  • y_pred (npt.NDArray[np.float32]) – Predicted values.

Returns:

The mean IoU.

Return type:

tf.Tensor

topostats.unet_masking.predict_unet(image: numpy.typing.NDArray[numpy.float32], model: keras.Model, confidence: float, model_input_shape: tuple[int | None, int, int, int], upper_norm_bound: float, lower_norm_bound: float) numpy.typing.NDArray[numpy.bool_][source]#

Predict cats segmentation from a flattened image.

Parameters:
  • image (npt.NDArray[np.float32]) – The image to predict the mask for.

  • model (keras.Model) – The U-Net model.

  • confidence (float) – The confidence threshold for the mask.

  • model_input_shape (tuple[int | None, int, int, int]) – The shape of the model input, including the batch and channel dimensions.

  • upper_norm_bound (float) – The upper bound for normalising the image.

  • lower_norm_bound (float) – The lower bound for normalising the image.

Returns:

The predicted mask.

Return type:

npt.NDArray[np.bool_]

topostats.unet_masking.make_bounding_box_square(crop_min_row: int, crop_min_col: int, crop_max_row: int, crop_max_col: int, image_shape: tuple[int, int]) tuple[int, int, int, int][source]#

Make a bounding box square.

Parameters:
  • crop_min_row (int) – The minimum row index of the crop.

  • crop_min_col (int) – The minimum column index of the crop.

  • crop_max_row (int) – The maximum EXCLUSIVE row index of the crop.

  • crop_max_col (int) – The maximum EXCLUSIVE column index of the crop.

  • image_shape (tuple[int, int]) – The shape of the image.

Returns:

The new crop indices.

Return type:

tuple[int, int, int, int]

Notes

The crop indices are inclusive on the minimum side and exclusive on the maximum side. So an object like this: 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0 With no padding, would have a bounding box of (1, 1, 4, 4) and not (1, 1, 3, 3). Hence the maximum index for the bbox is allowed to exceed the maximum index of the image by 1.

topostats.unet_masking.pad_bounding_box(crop_min_row: int, crop_min_col: int, crop_max_row: int, crop_max_col: int, image_shape: tuple[int, int], padding: int) tuple[int, int, int, int][source]#

Pad a bounding box.

Parameters:
  • crop_min_row (int) – The minimum row index of the crop.

  • crop_min_col (int) – The minimum column index of the crop.

  • crop_max_row (int) – The maximum row index of the crop.

  • crop_max_col (int) – The maximum column index of the crop.

  • image_shape (tuple[int, int]) – The shape of the image.

  • padding (int) – The padding to apply to the bounding box.

Returns:

The new crop indices.

Return type:

tuple[int, int, int, int]

topostats.unet_masking.pad_crop(crop: numpy.typing.NDArray, bbox: tuple[int, int, int, int], image_shape: tuple[int, int], padding: int) numpy.NDArray[source]#

Pad a crop.

Parameters:
  • crop (npt.NDArray) – The crop to pad.

  • bbox (tuple[int, int, int, int]) – The bounding box of the crop.

  • image_shape (tuple[int, int]) – The shape of the image.

  • padding (int) – The padding to apply to the crop.

Returns:

The padded crop.

Return type:

np.NDArray

topostats.unet_masking.make_crop_square(crop: numpy.typing.NDArray, bbox: tuple[int, int, int, int], image_shape: tuple[int, int]) numpy.NDArray[source]#

Make a crop square.

Parameters:
  • crop (npt.NDArray) – The crop to make square.

  • bbox (tuple[int, int, int, int]) – The bounding box of the crop.

  • image_shape (tuple[int, int]) – The shape of the image.

Returns:

The square crop.

Return type:

np.NDArray