biapy.data.generators.augmentorsο
Augmentation utilities for image and mask data in deep learning workflows.
This module provides a variety of data augmentation functions for images and masks, including cutout, cutblur, cutmix, cutnoise, misalignment, cropping, flipping, rotation, zoom, gamma/contrast adjustment, blurring, dropout, elastic deformation, shear, shift, and more. These augmentations are designed to improve model robustness and generalization for both 2D and 3D data formats.
- biapy.data.generators.augmentors.cutout(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]], z_size: int, nb_iterations: Tuple[int, int] = (1, 3), size: Tuple[float, float] = (0.2, 0.4), cval: int = 0, res_relation: Tuple[float, ...] = (1.0, 1.0), apply_to_mask: bool = False) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]ο
Apply augmentation using Cutout technique.
Cutout data augmentation presented in Improved Regularization of Convolutional Neural Networks with Cutout.
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (Numpy array) β Mask to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.z_size (int) β Size of z dimension. Used for 3D images as the z axis has been merged with the channels. Set to -1 to when do not want to be applied.
nb_iterations (tuple of ints, optional) β Number of areas to fill the image with. E.g.
(1, 3).size (tuple of floats, optional) β Range to choose the size of the areas to create.
cval (int, optional) β Value to fill the area with.
res_relation (tuple of floats, optional) β Relation between axis resolution in
(x,y,z). E.g.(1,1,0.27)for anisotropic data of 8umx8umx30um resolution.apply_to_mask (boolean, optional) β To apply cutout to the mask.
- Returns:
out (Numpy array) β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (Numpy array) β Transformed mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
Examples
Calling this function with
nb_iterations=(1,3),size=(0.05,0.3),apply_to_mask=Falsemay result in:
Input imageο
Corresponding maskο
Augmented imageο
Augmented maskο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.cutblur(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], size: Tuple[float, float] = (0.2, 0.4), down_ratio_range: Tuple[int, int] = (2, 8), only_inside: bool = True) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Apply CutBlur data augmentation.
CutBlur data augmentation introduced in Rethinking Data Augmentation for Image Super-resolution: A Comprehensive Analysis and a New Strategy and adapted from https://github.com/clovaai/cutblur .
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.size (float, optional) β Size of the region to transform.
down_ratio_range (tuple of ints, optional) β Downsampling ratio range to be applied. E.g.
(2, 8).only_inside (bool, optional) β If
Trueonly the region inside will be modified (cut LR into HR image). IfFalsethe50%of the times the region inside will be modified (cut LR into HR image) and the other50%the inverse will be done (cut HR into LR image). See Figure 1 of the official paper.
- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with
size=(0.2,0.4),down_ratio_range=(2,8),only_inside=Truemay result in:
Input imageο
Augmented imageο
Input imageο
Augmented imageο
The grid and the red square are painted for visualization purposes.
- biapy.data.generators.augmentors.cutmix(im1: ndarray[tuple[int, ...], dtype[_ScalarType_co]], im2: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask1: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask2: ndarray[tuple[int, ...], dtype[_ScalarType_co]], heat1: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None, heat2: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None, size: Tuple[float, float] = (0.2, 0.4)) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None][source]ο
Apply Cutmix data augmentation.
Cutmix augmentation introduced in CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features. With this augmentation a region of the image sample is filled with a given second image. This implementation is used for semantic segmentation so the masks of the images are also needed. It assumes that the images are of the same shape.
- Parameters:
im1 (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.im2 (Numpy array) β Image to paste into the region of
im1. E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D.mask1 (Numpy array) β Mask to transform (belongs to
im1). E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D.mask2 (Numpy array) β Mask to paste into the region of
mask1. E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D.heat1 (Numpy array or None) β Heatmap to transform (belongs to
im1). E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D. IfNone, no heatmap is used.heat2 (Numpy array or None) β Heatmap to paste into the region of
heat1. E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D. IfNone, no heatmap is used.size (tuple of floats, optional) β Range to choose the size of the areas to transform. E.g.
(0.2, 0.4).
- Returns:
out (Numpy array) β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.m_out (Numpy array) β Transformed mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.h_out (Numpy array or None) β Transformed heatmap. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
Examples
Calling this function with
size=(0.2,0.4)may result in:
Input imageο
Corresponding maskο
Augmented imageο
Augmented maskο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.cutnoise(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], scale: Tuple[float, float] = (0.1, 0.2), nb_iterations: Tuple[int, int] = (1, 3), size: Tuple[float, float] = (0.2, 0.4)) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Apply Cutnoise data augmentation.
Cutnoise data augmentation. Randomly add noise to a cuboid region in the image to force the model to learn denoising when making predictions.
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.scale (tuple of floats, optional) β Scale of the random noise. E.g.
(0.1, 0.2).nb_iterations (tuple of ints, optional) β Number of areas with noise to create. E.g.
(1, 3).size (boolean, optional) β Range to choose the size of the areas to transform. E.g.
(0.2, 0.4).
- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with
scale=(0.1,0.2),nb_iterations=(1,3)andsize=(0.2,0.4)may result in:
Input imageο
Augmented imageο
Input imageο
Augmented imageο
The grid and the red squares are painted for visualization purposes.
- biapy.data.generators.augmentors.misalignment(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]], displacement: int = 16, rotate_ratio: float = 0.0) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]ο
Apply mis-alignment data augmentation.
Mis-alignment data augmentation of image stacks. This augmentation is applied to both images and masks.
Implementation based on PyTorch Connectomicsβ misalign.py.
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (Numpy array) β Mask to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.displacement (int, optional) β Maximum pixel displacement in
xy-plane.rotate_ratio (float, optional) β Ratio of rotation-based mis-alignment.
- Returns:
out (Numpy array) β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.m_out (Numpy array) β Transformed mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
Examples
Calling this function with
displacement=16androtate_ratio=0.5may result in:
Input imageο
Corresponding maskο
Augmented imageο
Augmented maskο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.brightness(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], brightness_factor: Tuple[float, float] = (0, 0)) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Randomly adjust brightness between a range.
- Parameters:
image (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.brightness_factor (tuple of 2 floats) β Range of brightnessβ intensity. E.g.
(0.1, 0.3).
- Returns:
image β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with
brightness_factor=(0.1,0.3),mode='mix',invert=Falseandinvert_p=0may result in:
Input imageο
Augmented imageο
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.contrast(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], contrast_factor: Tuple[float, float] = (0, 0)) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Contrast augmentation.
- Parameters:
image (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.contrast_factor (tuple of 2 floats) β Range of contrastβs intensity. E.g.
(0.1, 0.3).
- Returns:
image β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with
contrast_factor=(0.1,0.3),mode='mix',invert=Falseandinvert_p=0may result in:
Input imageο
Augmented imageο
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.missing_sections(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], iterations: Tuple[int, int] = (30, 40), channel_prob: float = 0.5) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Augment the image by creating a black line in a random position.
Implementation based on PyTorch Connectomicsβ missing_parts.py.
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.iterations (tuple of 2 ints, optional) β Iterations to dilate the missing line with. E.g.
(30, 40).channel_prob (float, optional) β Probability of applying a missing section to each channel individually.
- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with
iterations=(30,40)may result in:
Input imageο
Augmented imageο
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.shuffle_channels(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Augment the image by shuffling its channels.
- Parameters:
img (3D/4D Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
3D/4D Numpy array
Examples
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.grayscale(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Augment the image by converting it into grayscale.
- Parameters:
img (3D/4D Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
3D/4D Numpy array
Examples
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.GridMask(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], z_size: int, ratio: float = 0.6, d_range: Tuple[float, ...] = (30.0, 60.0), rotate: int = 1, invert: bool = False) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Apply GridMask data augmentation presented in GridMask Data Augmentation.
GridMask is a data augmentation technique that randomly masks out grid-like regions in the image, which helps the model to learn more robust features by forcing it to focus on different parts of the image Code adapted from https://github.com/dvlab-research/GridMask/blob/master/imagenet_grid/utils/grid.py.
- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.z_size (int) β Size of z dimension. Used for 3D images as the z axis has been merged with the channels. Set to -1 to when do not want to be applied.
ratio (tuple of floats, optional) β Range to choose the size of the areas to create.
d_range (tuple of floats, optional) β Range to choose the
dvalue in the original paper.rotate (float, optional) β Rotation of the mask in GridMask. Needs to be between
[0,1]where 1 is 360 degrees.invert (bool, optional) β Whether to invert the mask.
- Returns:
out β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
Examples
Calling this function with the default settings may result in:
Input imageο
Augmented imageο
The grid is painted for visualization purposes.
- biapy.data.generators.augmentors.random_crop_pair(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]], random_crop_size: Tuple[int, ...], val: bool = False, draw_prob_map_points: bool = False, img_prob: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, weight_map: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, scale: Tuple[int, ...] = (1, 1)) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], int, int, int, int][source]ο
Apply random crop for an image and its mask.
No crop is done in those dimensions that
random_crop_sizeis greater than the input image shape in those dimensions. For instance, if an input image is400x150andrandom_crop_sizeis224x224the resulting image will be224x150.- Parameters:
image (Numpy 3D array) β Image. E.g.
(y, x, channels).mask (Numpy 3D array) β Image mask. E.g.
(y, x, channels).random_crop_size (2 int tuple) β Size of the crop. E.g.
(height, width).val (bool, optional) β If the image provided is going to be used in the validation data. This forces to crop from the origin, e. g.
(0, 0)point.draw_prob_map_points (bool, optional) β To return the pixel chosen to be the center of the crop.
img_prob (Numpy 3D array, optional) β Probability of each pixel to be chosen as the center of the crop. E. .g.
(y, x, channels).weight_map (bool, optional) β Weight map of the given image. E.g.
(y, x, channels).scale (tuple of 2 ints, optional) β Scale factor the second image given. E.g.
(2,2).
- Returns:
img (2D Numpy array) β Crop of the given image. E.g.
(y, x, channels).weight_map (2D Numpy array, optional) β Crop of the given imageβs weigth map. E.g.
(y, x, channels).ox (int, optional) β X coordinate in the complete image of the chose central pixel to make the crop.
oy (int, optional) β Y coordinate in the complete image of the chose central pixel to make the crop.
x (int, optional) β X coordinate in the complete image where the crop starts.
y (int, optional) β Y coordinate in the complete image where the crop starts.
- biapy.data.generators.augmentors.random_3D_crop_pair(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]], random_crop_size: Tuple[int, ...], val: bool = False, img_prob: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, weight_map: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, draw_prob_map_points: bool = False, scale: Tuple[int, ...] = (1, 1, 1)) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]], int, int, int, int, int, int][source]ο
Extract a random 3D patch from the given image and mask.
No crop is done in those dimensions that
random_crop_sizeis greater than the input image shape in those dimensions. For instance, if an input image is10x400x150andrandom_crop_sizeis10x224x224the resulting image will be10x224x150.- Parameters:
image (4D Numpy array) β Data to extract the patch from. E.g.
(z, y, x, channels).mask (4D Numpy array) β Data mask to extract the patch from. E.g.
(z, y, x, channels).random_crop_size (3D int tuple) β Shape of the patches to create. E.g.
(z, y, x).val (bool, optional) β If the image provided is going to be used in the validation data. This forces to crop from the origin, e.g.
(0, 0)point.img_prob (Numpy 4D array, optional) β Probability of each pixel to be chosen as the center of the crop. E. g.
(z, y, x, channels).weight_map (bool, optional) β Weight map of the given image. E.g.
(z, y, x, channels).draw_prob_map_points (bool, optional) β To return the voxel chosen to be the center of the crop.
scale (tuple of 3 ints, optional) β Scale factor the second image given. E.g.
(2,4,4).
- Returns:
img (4D Numpy array) β Crop of the given image. E.g.
(z, y, x, channels).weight_map (4D Numpy array, optional) β Crop of the given imageβs weigth map. E.g.
(z, y, x, channels).oz (int, optional) β Z coordinate in the complete image of the chose central pixel to make the crop.
oy (int, optional) β Y coordinate in the complete image of the chose central pixel to make the crop.
ox (int, optional) β X coordinate in the complete image of the chose central pixel to make the crop.
z (int, optional) β Z coordinate in the complete image where the crop starts.
y (int, optional) β Y coordinate in the complete image where the crop starts.
x (int, optional) β X coordinate in the complete image where the crop starts.
- biapy.data.generators.augmentors.random_crop_single(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], random_crop_size: Tuple[int, ...], val: bool = False, draw_prob_map_points: bool = False, weight_map: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], int, int, int, int][source]ο
Random crop for a single image.
No crop is done in those dimensions that
random_crop_sizeis greater than the input image shape in those dimensions. For instance, if an input image is400x150andrandom_crop_sizeis224x224the resulting image will be224x150.- Parameters:
image (Numpy 3D array) β Image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.random_crop_size (2 int tuple) β Size of the crop. E.g.
(y, x).val (bool, optional) β If the image provided is going to be used in the validation data. This forces to crop from the origin, e. g.
(0, 0)point.draw_prob_map_points (bool, optional) β To return the pixel chosen to be the center of the crop.
weight_map (bool, optional) β Weight map of the given image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- Returns:
img (2D Numpy array) β Crop of the given image. E.g.
(y, x).weight_map (2D Numpy array, optional) β Crop of the given imageβs weigth map. E.g.
(y, x).oy (int, optional) β Y coordinate in the complete image of the chose central pixel to make the crop.
ox (int, optional) β X coordinate in the complete image of the chose central pixel to make the crop.
y (int, optional) β Y coordinate in the complete image where the crop starts.
y (int, optional) β X coordinate in the complete image where the crop starts.
- biapy.data.generators.augmentors.random_3D_crop_single(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], random_crop_size: Tuple[int, ...], val: bool = False, draw_prob_map_points: bool = False, weight_map: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], int, int, int, int, int, int][source]ο
Random crop for a single image.
No crop is done in those dimensions that
random_crop_sizeis greater than the input image shape in those dimensions. For instance, if an input image is50x400x150andrandom_crop_sizeis30x224x224the resulting image will be30x224x150.- Parameters:
image (Numpy 3D array) β Image. E.g.
(z, y, x, channels).random_crop_size (2 int tuple) β Size of the crop. E.g.
(z, y, x).val (bool, optional) β If the image provided is going to be used in the validation data. This forces to crop from the origin, e. g.
(0, 0)point.draw_prob_map_points (bool, optional) β To return the pixel chosen to be the center of the crop.
weight_map (bool, optional) β Weight map of the given image. E.g.
(z, y, x, channels).
- Returns:
img (2D Numpy array) β Crop of the given image. E.g.
(z, y, x).weight_map (2D Numpy array, optional) β Crop of the given imageβs weigth map. E.g.
(z, y, x).ox (int, optional) β Z coordinate in the complete image of the chose central pixel to make the crop.
oy (int, optional) β Y coordinate in the complete image of the chose central pixel to make the crop.
ox (int, optional) β X coordinate in the complete image of the chose central pixel to make the crop.
z (int, optional) β Z coordinate in the complete image where the crop starts.
y (int, optional) β Y coordinate in the complete image where the crop starts.
x (int, optional) β X coordinate in the complete image where the crop starts.
- biapy.data.generators.augmentors.center_crop_single(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], crop_shape: Tuple[int, ...]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Extract the central patch from a single image.
- Parameters:
img (3D/4D array) β Image. E.g.
(y, x, channels)or(z, y, x, channels).crop_shape (2/3 int tuple) β Size of the crop. E.g.
(y, x)or(z, y, x).
- Returns:
img β Center crop of the given image. E.g.
(y, x, channels)or(z, y, x, channels).- Return type:
3D/4D Numpy array
- biapy.data.generators.augmentors.resize_img(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], shape: Tuple[int, ...]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Resize input image to given shape.
- Parameters:
img (3D/4D Numpy array) β Data to extract the patch from. E.g.
(y, x, channels)for2Dor(z, y, x, channels)for3D.shape (2D/3D int tuple) β Shape to resize the image to. E.g.
(y, x)for2D(z, y, x)for3D.
- Returns:
img β Resized image. E.g.
(y, x, channels)for2Dor(z, y, x, channels)for3D.- Return type:
3D/4D Numpy array
- biapy.data.generators.augmentors.rotation(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, angles: Tuple[int, int] | List[int] = [], mode: str = 'reflect', mask_type: str = 'as_mask') ndarray[tuple[int, ...], dtype[_ScalarType_co]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None, ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None][source]ο
Apply a rotation to input
imageandmask(if provided).- Parameters:
img (3D/4D Numpy array) β Image to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.angles (List of ints, optional) β List of angles to choose the rotation to be made. E.g. [90,180,360].
mode (str, optional) β How to fill up the new values created. Options:
constant,reflect,wrap,symmetric.mask_type (str, optional) β How to treat the mask during interpolation. Either as βas_maskβ (order 0) or βas_imageβ (order 1).
- Returns:
img (3D/4D Numpy array) β Rotated image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Rotated mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Rotated heatmap. Returned if
maskis provided. E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D.
- biapy.data.generators.augmentors.zoom(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], zoom_range: Tuple[float, ...], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, zoom_in_z: bool = False, mode: str = 'reflect', mask_type: str = 'as_mask') ndarray[tuple[int, ...], dtype[_ScalarType_co]] | Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None, ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None][source]ο
Apply zoom to input
imageandmask(if provided).- Parameters:
img (3D/4D Numpy array) β Image to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.zoom_range (tuple of floats) β Defines minimum and maximum factors to scale the images. E.g. (0.8, 1.2).
mask (3D/4D Numpy array, optional) β Mask to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to rotate. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.zoom_in_z (bool, optional) β Whether to apply or not zoom in Z axis.
mode (str, optional) β How to fill up the new values created. Options:
constant,reflect,wrap,symmetric.mask_type (str, optional) β How to treat the mask during interpolation. Either as βas_maskβ (order 0) or βas_imageβ (order 1).
- Returns:
img (3D/4D Numpy array) β Zoomed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Zoomed mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Zoomed heatmap. Returned if
maskis provided. E.g.(y, x, channels)for2Dor(y, x, z, channels)for3D.
- biapy.data.generators.augmentors.gamma_contrast(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], gamma: Tuple[float, float] = (0, 1)) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Apply gamma contrast to input
image.- Parameters:
img (Numpy array) β Image to transform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.gamma (tuple of 2 floats, optional) β Range of gamma intensity. E.g.
(0.8, 1.3).
- Returns:
img β Transformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
Numpy array
- biapy.data.generators.augmentors.shear(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], shear: tuple, mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, cval: float = 0, mask_type: str = 'as_mask', mode: str = 'constant')[source]ο
Apply a shear transformation to an image (and optional mask/heatmap).
- Parameters:
image (3D/4D Numpy array) β Image to shear. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to shear. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to shear. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.shear (tuple) β Shear range (min, max) in degrees for both x and y directions.
cval (float) β Value used for points outside the boundaries.
mask_type (str) β How to treat the mask during interpolation. Either as βas_maskβ (order 0) or βas_imageβ (order 1).
mode (str) β Points outside boundaries are filled according to this mode.
- Returns:
img (3D/4D Numpy array) β Sheared image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Sheared mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Sheared heatmap. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- biapy.data.generators.augmentors.shift(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, shift_range: tuple | None = None, cval: float = 0, mask_type: str = 'as_mask', mode: str = 'constant')[source]ο
Shift an image (and optional mask/heatmap) by a random amount within a range.
- Parameters:
image (3D/4D Numpy array) β Image to shift. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to shift. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to shift. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.shift_range (Optional[tuple]) β Range (min, max) for random shift in both x and y directions.
cval (float) β Value used for points outside the boundaries.
mask_type (str) β How to treat the mask during interpolation. Either as βas_maskβ (order 0) or βas_imageβ (order 1).
mode (str) β Points outside boundaries are filled according to this mode.
- Returns:
img (3D/4D Numpy array) β Shifted image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Shifted mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Shifted heatmap. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- biapy.data.generators.augmentors.flip_horizontal(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None)[source]ο
Flip an image (and optional mask/heatmap) horizontally (left-right).
- Parameters:
image (3D/4D Numpy array) β Image to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- Returns:
img (3D/4D Numpy array) β Flipped image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array or None) β Flipped mask if provided, else None.
heat (3D/4D Numpy array or None) β Flipped heatmap if provided, else None.
- biapy.data.generators.augmentors.flip_vertical(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None)[source]ο
Flip an image (and optional mask/heatmap) vertically (up-down).
- Parameters:
image (3D/4D Numpy array) β Image to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- Returns:
img (3D/4D Numpy array) β Flipped image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Flipped mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Flipped heatmap. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.
- biapy.data.generators.augmentors.gaussian_blur(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], sigma: float | tuple = (0.5, 1.5))[source]ο
Apply Gaussian blur to an image.
- Parameters:
image (Numpy array) β Image to Blur. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.sigma (float or tuple) β Standard deviation for Gaussian kernel. If tuple, a random value is chosen from the range.
- Returns:
img β Blurred image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
NDArray
- biapy.data.generators.augmentors.median_blur(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], k_range: tuple | None = None)[source]ο
Apply median blur to an image.
- Parameters:
image (3D/4D Numpy array) β Image to Blur. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.k_range (Optional[tuple]) β Range (min, max) for random kernel size (must be odd).
- Returns:
img β Blurred image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
NDArray
- biapy.data.generators.augmentors.motion_blur(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], k_range: tuple | None = None)[source]ο
Apply motion blur to an image.
- Parameters:
image (3D/4D Numpy array) β Image to flip. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.k_range (Optional[tuple]) β Range (min, max) for random kernel size (must be odd).
- Returns:
img β Blurred image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
NDArray
- biapy.data.generators.augmentors.dropout(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], drop_range: tuple = (0.1, 0.2), random_state: RandomState | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Randomly set a fraction of pixels in the image to zero (dropout).
- Parameters:
image (3D/4D Numpy array) β Image to apply dropout. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.drop_range (tuple) β Range for dropout probability. A value is randomly chosen from this range.
random_state (Optional[np.random.RandomState]) β Random state for reproducibility.
- Returns:
img β Image after dropout. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.- Return type:
3D/4D Numpy array
- biapy.data.generators.augmentors.elastic(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, heat: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None = None, alpha: float | tuple = 14, sigma: float = 4, mask_type: str = 'as_mask', cval: float = 0, mode: str = 'constant', random_seed=None) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None, ndarray[tuple[int, ...], dtype[_ScalarType_co]] | None][source]ο
Apply elastic deformation to an image (and optional mask/heatmap).
- Parameters:
image (3D/4D Numpy array) β Image to deform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Mask to deform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Heatmap (float mask) to deform. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.alpha (float, optional) β Scaling factor for deformation intensity.
sigma (float, optional) β Standard deviation for Gaussian filter.
cval (float, optional) β Value used for points outside the boundaries.
mode (str, optional) β Points outside boundaries are filled according to this mode.
mask_type (str, optional) β How to treat the mask during interpolation. Either as βas_maskβ (order 0) or βas_imageβ (order 1).
random_seed (int, optional) β Random seed for reproducibility.
- Returns:
img (3D/4D Numpy array) β Deformed image. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.mask (3D/4D Numpy array, optional) β Deformed mask. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.heat (3D/4D Numpy array, optional) β Deformed heatmap. E.g.
(y, x, channels)for2Dor(y, x, z, channels)for3D.