Sampling & Data augmentation

Note

This module is a work in progress and only implement sampling methods. We want to generalize some of the dataloaders code.

Sampling and data augmentation functions.

Data augmentation not implemented yet…

class biom3d.utils.data_augmentation.SmartPatch(local_crop_shape, global_crop_shape, min_overlap, global_crop_scale=1.0, global_crop_min_shape_scale=1.0)[source]

Randomly crop and resize the images to a certain crop_shape.

This class provide two functionalities: - global_crop_resize: method performs a random crop and resize. - local_crop_resize: performs a second random crop that overlaps with the global one, with a minimum overlap ratio defined by min_overlap.

Variables:
  • local_crop_shape (numpy.ndarray) – Shape of local crop

  • global_crop_shape (numpy.ndarray) – Minimal crop size

  • global_crop_scale (numpy.ndarray | float) – Value between 0 and 1. Factor multiplying (img_shape - global_crop_min_shape) and added to the global_crop_min_shape. A value of 1 means that the maximum shape of the global crop will be the image shape. A value of 0 means that the maximum value will be the global_crop_min_shape.

  • global_crop_shape – shape of local crop

  • global_crop_min_shape_scale (numpy.ndarray) – Factor multiplying the minimal global_crop_shape, 1.0 is a good default

  • alpha (float) – 1 - min_overlap; used internally to determine maximum allowed center displacement.

  • global_crop_center (ndarra | None) – The center coordinates of the global crop, once computed.

__init__(local_crop_shape, global_crop_shape, min_overlap, global_crop_scale=1.0, global_crop_min_shape_scale=1.0)[source]

Initialize a SmartPatch object.

Parameters:
  • local_crop_shape (list or tuple of 3 ints) – Shape of the local crop.

  • global_crop_shape (list or tuple of 3 ints) – Minimal shape for the global crop.

  • min_overlap (float) – Value between 0 and 1. Minimum required overlap between local and global crops.

  • global_crop_scale (float or list/tuple of 3 floats, default=1.0) – Scaling factor(s) applied to (image_shape - global_crop_min_shape). Controls how large the global crop can be beyond its minimum shape. - 1.0 means the crop can reach the full image size. - 0.0 means the crop stays at minimal shape.

  • global_crop_min_shape_scale (float or list/tuple of 3 floats, default=1.0) – Scaling factor(s) applied to global_crop_shape to define the minimum global crop shape.

global_crop_resize(img: ndarray, msk: ndarray | None = None) ndarray | tuple[ndarray, ndarray][source]

Perform a random global crop and resize on the input image (and optional mask).

The crop shape is randomly selected between a minimum shape (scaled by global_crop_min_shape_scale) and a maximum shape controlled by global_crop_scale and the image size.

The crop is then extracted and resized to the fixed global_crop_shape.

Parameters:
  • img (numpy.ndarray) – Input image tensor with shape (C, H, W, D).

  • msk (numpy.ndarray, optional) – Optional mask tensor with the same spatial dimensions as img.

Returns:

  • crop_img (numpy.ndarray) – Cropped and resized image.

  • crop_msk (numpy.ndarray, optional) – Cropped and resized mask, if msk is provided.

local_crop_pad(img: ndarray, msk: ndarray | None = None) ndarray | tuple[ndarray, ndarray][source]

Perform a local crop centered near the global crop center with padding if needed.

This method requires global_crop_resize to have been called before, so that self.global_crop_center is defined.

The local crop overlaps with the global crop by at least the configured minimum overlap.

Parameters:
  • crop_img (numpy.ndarray) – Input image tensor with shape (C, H, W, D).

  • crop_msk (numpy.ndarray, optional) – Optional mask tensor with the same spatial dimensions as img.

Raises:

AssertionError – If global_crop_resize has not been called before.

Returns:

  • crop_img (numpy.ndarray) – Cropped and resized image.

  • crop_msk (numpy.ndarray, optional) – Cropped and resized mask, if msk is provided.

local_crop_resize(img: ndarray, msk: ndarray | None = None) ndarray | tuple[ndarray, ndarray][source]

Perform a local crop with random size and resize, overlapping the global crop.

This method requires global_crop_resize to have been called before, so that self.global_crop_center is defined.

The crop size is randomly selected within self.local_crop_scale fraction of the image size, and positioned to ensure a minimum overlap with the global crop.

Parameters:
  • img (numpy.ndarray) – Input image tensor with shape (C, H, W, D).

  • msk (numpy.ndarray, optional) – Optional mask tensor with the same spatial dimensions as img.

Raises:

AssertionError – If global_crop_resize has not been called before.

Returns:

  • crop_img (numpy.ndarray) – Input image tensor with shape (C, H, W, D).

  • crop_msk (numpy.ndarray, optional) – Optional mask tensor with the same spatial dimensions as img.

biom3d.utils.data_augmentation.centered_pad(img: ndarray, final_size: Iterable[int], msk: ndarray | None = None) ndarray | tuple[ndarray, ndarray][source]

Centered pad an img and msk to fit the final_size.

Parameters:
  • img (numpy.ndarray) – The image to pad.

  • final_size (iterable of int) – The size of the image after the pad.

  • msk (numpy.ndarray, optional) – The mask to pad.

Returns:

  • img (numpy.ndarray) – Padded image.

  • msk (numpy.ndarray, optional) – Padded mask.