biapy.data.generators.single_base_data_generatorο
Single image data generator for BiaPy.
This module provides the SingleBaseDataGenerator class, which supports flexible data loading, augmentation, and normalization for single image data in deep learning workflows. It includes a wide range of augmentation options for both 2D and 3D data, and is designed to work with BiaPyDataset objects and normalization modules.
- class biapy.data.generators.single_base_data_generator.SingleBaseDataGenerator(ndim: int, X: BiaPyDataset, norm_module: Dict, seed: int = 0, da: bool = True, da_prob: float = 0.5, rotation90: bool = False, rand_rot: bool = False, rnd_rot_range=(-180, 180), shear: bool = False, shear_range=(-20, 20), zoom: bool = False, zoom_range=(0.8, 1.2), zoom_in_z: bool = False, shift: bool = False, shift_range=(0.1, 0.2), affine_mode: Literal['constant', 'edge', 'symmetric', 'reflect', 'wrap'] = 'constant', vflip: bool = False, hflip: bool = False, elastic: bool = False, e_alpha=(240, 250), e_sigma: int = 25, e_mode: Literal['constant', 'edge', 'symmetric', 'reflect', 'wrap'] = 'constant', g_blur: bool = False, g_sigma: Tuple[float, float] = (1.0, 2.0), median_blur: bool = False, mb_kernel: Tuple[int, int] = (3, 7), motion_blur: bool = False, motb_k_range: Tuple[int, int] = (3, 8), gamma_contrast: bool = False, gc_gamma: Tuple[float, float] = (1.25, 1.75), dropout: bool = False, drop_range: Tuple[float, float] = (0, 0.2), val: bool = False, resize_shape: Tuple[int, ...] = (256, 256, 1), convert_to_rgb: bool = False, preprocess_f=None, preprocess_cfg=None)[source]ο
Bases:
DatasetCustom BaseDataGenerator to transform single image data.
- Parameters:
ndim (int) β Dimensions of the data (
2for 2D and3for 3D).X (BiaPyDataset) β X data.
norm_module (Dict) β Normalization module that defines the normalization steps to apply.
seed (int, optional) β Seed for random functions.
da (bool, optional) β To activate the data augmentation.
da_prob (float, optional) β Probability of doing each transformation.
rotation90 (bool, optional) β To make square (90, 180,270) degree rotations.
rand_rot (bool, optional) β To make random degree range rotations.
rnd_rot_range (tuple of float, optional) β Range of random rotations. E. g.
(-180, 180).shear (bool, optional) β To make shear transformations.
shear_range (tuple of int, optional) β Degree range to make shear. E. g.
(-20, 20).zoom (bool, optional) β To make zoom on images.
zoom_range (tuple of floats, optional) β Zoom range to apply. E. g.
(0.8, 1.2).zoom_in_z (bool, optional) β Whether to apply or not zoom in Z axis.
shift (float, optional) β To make shifts.
shift_range (tuple of float, optional) β Range to make a shift. E. g.
(0.1, 0.2).affine_mode (str, optional) β Method to use when filling in newly created pixels. Same meaning as in skimage (and numpy.pad()). E.g.
constant,reflectetc.vflip (bool, optional) β To activate vertical flips.
hflip (bool, optional) β To activate horizontal flips.
elastic (bool, optional) β To make elastic deformations.
e_alpha (tuple of ints, optional) β Strength of the distortion field. E. g.
(240, 250).e_sigma (int, optional) β Standard deviation of the gaussian kernel used to smooth the distortion fields.
e_mode (str, optional) β Parameter that defines the handling of newly created pixels with the elastic transformation.
g_blur (bool, optional) β To insert gaussian blur on the images.
g_sigma (tuple of floats, optional) β Standard deviation of the gaussian kernel. E. g.
(1.0, 2.0).median_blur (bool, optional) β To blur an image by computing median values over neighbourhoods.
mb_kernel (tuple of ints, optional) β Median blur kernel size. E. g.
(3, 7).motion_blur (bool, optional) β Blur images in a way that fakes camera or object movements.
motb_k_range (int, optional) β Kernel size to use in motion blur.
gamma_contrast (bool, optional) β To insert gamma constrast changes on images.
gc_gamma (tuple of floats, optional) β Exponent for the contrast adjustment. Higher values darken the image. E. g.
(1.25, 1.75).dropout (bool, optional) β To set a certain fraction of pixels in images to zero.
drop_range (tuple of floats, optional) β Range to take a probability
pto drop pixels. E.g.(0, 0.2)will take apfolowing0<=p<=0.2and then dropppercent of all pixels in the image (i.e. convert them to black pixels).val (bool, optional) β Advise the generator that the images will be to validate the model to not make random crops (as the val. data must be the same on each epoch).
resize_shape (tuple of ints, optional) β If defined the input samples will be scaled into that shape.
convert_to_rgb (bool, optional) β In case RGB images are expected, e.g. if
crop_shapechannel is 3, those images that are grayscale are converted into RGB.preprocess_f (function, optional) β The preprocessing function, is necessary in case you want to apply any preprocessing.
preprocess_cfg (dict, optional) β Configuration parameters for preprocessing, is necessary in case you want to apply any preprocessing.
- abstract save_aug_samples(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], orig_images: Dict, i: int, pos: int, out_dir: str, draw_grid: bool)[source]ο
Save transformed samples in order to check the generator.
- Parameters:
img (3D/4D Numpy array) β Image to use as sample. E.g.
(y, x, channels)for2Dand(z, y, x, channels)for3D.orig_images (dict) β Dict where the original image and mask are saved in βo_xβ and βo_yβ, respectively.
i (int) β Number of the sample within the transformed ones.
pos (int) β Number of the sample within the dataset.
out_dir (str) β Directory to save the images.
draw_grid (bool) β Whether to draw a grid or not.
- load_sample(idx: int, first_load: bool = False) Tuple[ndarray[tuple[int, ...], dtype[_ScalarType_co]], int][source]ο
Load one data sample given its corresponding index.
- Parameters:
idx (int) β Sample index counter.
first_load (bool, optional) β Whether its the first time a sample is loaded to prevent normalizing it.
- Returns:
img (3D/4D Numpy array) β X element. E.g.
(y, x, channels)in2Dand(z, y, x, channels)in3D.img_class (int) β Class of the image.
- getitem(index: int) Tuple[Tensor, int][source]ο
Generate one sample of data.
- Parameters:
index (int) β Index counter.
- Returns:
item β X and Y (if avail) elements. X is
(z, y, x, channels)if3Dor(y, x, channels)if2D. Y is an integer.- Return type:
tuple of 3D/4D Numpy arrays
- apply_transform(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Transform the input image with one of the selected choices based on a probability.
- Parameters:
image (3D/4D Numpy array) β Image to transform. E.g.
(y, x, channels)in2Dor(z, y, x, channels)in3D.- Returns:
image β Transformed image. E.g.
(y, x, channels)in2Dor(z, y, x, channels)in3D.- Return type:
3D/4D Numpy array
- draw_grid(im: ndarray[tuple[int, ...], dtype[_ScalarType_co]], grid_width: int | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]ο
Draw grid of the specified size on an image.
- Parameters:
im (3D/4D Numpy array) β Image to be modified. E.g.
(y, x, channels)in2Dor(z, y, x, channels)in3D.grid_width (int, optional) β Gridβs width.
- get_transformed_samples(num_examples: int, random_images: bool = True, save_to_dir: bool = True, out_dir: str = 'aug', train: bool = False, draw_grid: bool = True)[source]ο
Apply selected transformations to a defined number of images from the dataset.
- Parameters:
num_examples (int) β Number of examples to generate.
random_images (bool, optional) β Randomly select images from the dataset. If
Falsethe examples will be generated from the start of the dataset.save_to_dir (bool, optional) β Save the images generated. The purpose of this variable is to check the images generated by data augmentation.
out_dir (str, optional) β Name of the folder where the examples will be stored.
train (bool, optional) β To avoid drawing a grid on the generated images. This should be set when the samples will be used for training.
draw_grid (bool, optional) β Draw a grid in the generated samples. Useful to see some types of deformations.
- Returns:
sample_x β Batch of data. E.g.
(num_examples, y, x, channels)in2Dor(num_examples, z, y, x, channels)in3D.- Return type:
4D/5D Numpy array