topostats.unet_masking#
Segment grains using a U-Net model.
Attributes#
Functions#
|
DICE loss function. |
|
Intersection over Union loss function. |
|
Mean Intersection Over Union metric, ignoring the background class. |
|
Predict cats segmentation from a flattened image. |
|
Make a bounding box square. |
|
Pad a bounding box. |
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 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.
- Returns:
The new crop indices.
- Return type:
tuple[int, int, int, int]
- 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]