Semseg patch fast¶
This dataloader is the default dataloader, it is inspired from nnUnet but simplified.
Dataset primitives for 3D segmentation dataset. Solution: patch approach with the whole dataset into memory.
- class biom3d.datasets.semseg_patch_fast.LabelToLong(label_name: str)[source]¶
Transform to convert label data to long (integer) type.
- Variables:
label_name (str) – Name of the label to be transformed.
- class biom3d.datasets.semseg_patch_fast.SemSeg3DPatchFast(*args: Any, **kwargs: Any)[source]¶
Dataset class for semantic segmentation with 3D patches. Supports data augmentation and efficient loading.
- Variables:
img_path (str) – Path to collection containing the image files.
msk_path (str) – Path to collection containing the mask files.
fg_path (str | None) – Path to collection containing the foreground files.
batch_size (int) – Size of a batch.
patch_size (numpy.ndarray) – Size of a patch.
aug_patch_size (numpy.ndarray | None) – Size of augmented patch size, may be bigger than patch size.
nbof_steps (int) – Number of steps (batches) per epoch.
load_data (bool) – If True, load the entire dataset into memory.
handler (DataHandler) – DataHandler used to load data.
train (bool) – If True, use the dataset for training; otherwise, use it for validation.
fnames (list[str]) – List of image paths relative to img_path.
use_aug (bool) – Whether to use data augmentation
fg_rate (float) – Foreground rate, used to force foreground inclusion in patches.
crop_scale (float) – Scale factor for crop size during augmentation.
use_softmax (bool) – If True, use softmax activation.
batch_idx (int) – Current batch index.
- __init__(img_path: str, msk_path: str, batch_size: int, patch_size: ndarray, nbof_steps: int, folds_csv: str | None = None, fold: int = 0, val_split: float = 0.25, train: bool = True, use_aug: bool = True, aug_patch_size: ndarray | None = None, fg_path: str | None = None, fg_rate: float = 0.33, crop_scale: float = 1.0, load_data: bool = False, use_softmax: bool = True)[source]¶
Dataset class for semantic segmentation with 3D patches. Supports data augmentation and efficient loading.
- Parameters:
img_path (str) – Path to collection containing the image files.
msk_path (str) – Path to collection containing the mask files.
batch_size (int) – Batch size for dataset sampling.
patch_size (numpy.ndarray) – Size of the patches to be used.
nbof_steps (int) – Number of steps (batches) per epoch.
folds_csv (str, optional) – CSV file containing fold information for dataset splitting.
fold (int, default=0) – The current fold number for training/validation splitting.
val_split (float, default=0.25) – Proportion of data to be used for validation.
train (bool, default=True) – If True, use the dataset for training; otherwise, use it for validation.
use_aug (bool, default=True) – If True, apply data augmentation.
aug_patch_size (numpy.ndarray, optional) – Patch size to use for augmented patches.
fg_path (str, optional) – Path to collection containing foreground information.
fg_rate (float, default=0.33) – Foreground rate, used to force foreground inclusion in patches. If > 0, force the use of foreground, needs to run some pre-computations (note: better use the foreground scheduler)
crop_scale (float, default=1.0) – Scale factor for crop size during augmentation. If > 1, then use random_crop_resize instead of random_crop_pad
load_data (bool, default=False) – If True, load the entire dataset into memory.
use_softmax (bool, default=True) – If True, use softmax activation.
- Raises:
AssertionError – If fold_csv is None and not valid path for datas, or empty collections If crop_scale < 1
- biom3d.datasets.semseg_patch_fast.centered_crop(img: ndarray, msk: ndarray, center: Iterable[int], crop_shape: Iterable[int], margin: Iterable[float] = array([0., 0., 0.])) tuple[ndarray, ndarray][source]¶
Do a crop, forcing the location voxel to be located in the center of the crop.
- Parameters:
img (numpy.ndarray) – Image data.
msk (ndaarray) – Mask data.
center (iterable of int) – Center voxel location for cropping.
crop_shape (iterable of int) – Shape of the crop.
margin (iterable of float, default np.zeros(3)) – Margin around the center location.
- Raises:
AssertionError – If center is out of the image
- Returns:
crop_img (numpy.ndarray) – Cropped image data.
crop_msk (numpy.ndarray) – Cropped mask data.
- biom3d.datasets.semseg_patch_fast.centered_pad(img: ndarray, final_size: Iterable[int], msk: ndarray | None = None) ndarray | tuple[ndarray, ndarray][source]¶
Do a centered pad an img and msk to fit the final_size.
- Parameters:
img (numpy.ndarray) – Image data.
final_size (iterable of int) – Final size of the cropped image and mask.
msk (numpy.ndarray, optional) – Mask data.
- Returns:
pad_img (numpy.ndarray) – Cropped image data, focused on the foreground region.
pad_msk (numpy.ndarray, optional) – Cropped mask data, corresponding to the cropped image region.
- biom3d.datasets.semseg_patch_fast.foreground_crop(img: ndarray, msk: ndarray, final_size: Iterable[int], fg_margin: Iterable[float], fg: dict[int, ndarray] | None = None, use_softmax: bool = True) tuple[ndarray, ndarray][source]¶
Do a foreground crop.
- Parameters:
img (numpy.ndarray) – Image data.
msk (numpy.ndarray) – Mask data.
final_size (iterable of int) – Final size of the cropped image and mask.
fg_margin (iterable of float) – Margin around the foreground location.
fg (dict of int to numpy.ndarray, optional) – Foreground information.
use_softmax (bool, default=True) – If True, assumes softmax activation.
- Returns:
img (numpy.ndarray) – Cropped image data, focused on the foreground region.
msk (numpy.ndarray) – Cropped mask data, corresponding to the cropped image region.
- biom3d.datasets.semseg_patch_fast.located_crop(img: ndarray, msk: ndarray, location: Iterable[int], crop_shape: Iterable[int], margin: Iterable[float] = array([0., 0., 0.])) tuple[ndarray, ndarray][source]¶
Do a crop, forcing the location voxel to be located in the crop.
- Parameters:
img (numpy.ndarray) – Image data.
msk (numpy.ndarray) – Mask data.
location (iterable of int) – Specific voxel location to include in the crop.
crop_shape (iterable of int) – Shape of the crop.
margin (iterable of float) – Margin around the location.
- Returns:
crop_img (numpy.ndarray) – Cropped image data.
crop_msk (numpy.ndarray) – Cropped mask data.
- biom3d.datasets.semseg_patch_fast.random_crop(img: ndarray, msk: ndarray, crop_shape: Iterable[int], force_in: bool = True) tuple[ndarray, ndarray][source]¶
Randomly crop a portion of size prop of the original image size.
- Parameters:
img (numpy.ndarray) – Image data.
msk (numpy.ndarray) – Mask data.
crop_shape – Shape of the crop.
force_in (bool, optional) – If True, ensures the crop is fully within the image boundaries.
- Raises:
AssertionError – If image shape (minus C) is not the same shape as crop_shape
- Returns:
crop_img (numpy.ndarray) – Cropped image data.
crop_msk (numpy.ndarray) – Cropped mask data.
- biom3d.datasets.semseg_patch_fast.random_crop_pad(img: ndarray, msk: ndarray, final_size: Iterable[int], fg_rate: float = 0.33, fg_margin: Iterable[float] = array([0., 0., 0.]), fg: dict[int, ndarray] | None = None, use_softmax: bool = True) tuple[ndarray, ndarray][source]¶
Do a random crop and pad if needed.
- Parameters:
img (numpy.ndarray) – Image data.
msk (numpy.ndarray) – Mask data.
final_size – Final size of the image and mask after cropping and padding.
fg_rate (float, default=0.33) – Probability of focusing the crop on the foreground.
fg_margin (iterable of float, default=np.zeros(3)) – Margin around the foreground location.
fg (dict of int to numpy.ndarray, optional) – Foreground information.
use_softmax (bool, default=True) – If True, assumes softmax activation.
- Returns:
img (numpy.ndarray) – Cropped and padded image data.
msk (numpy.ndarray) – Cropped and padded mask data.
- biom3d.datasets.semseg_patch_fast.random_crop_resize(img: ndarray, msk: ndarray, crop_scale: float, final_size: Iterable[int], fg_rate: int = 0.33, fg_margin: Iterable[float] = array([0., 0., 0.])) tuple[ndarray, ndarray][source]¶
Do a random crop and resize if needed.
- Parameters:
img (numpy.ndarray) – Image data.
msk (numpy.ndarray) – Mask data.
crop_scale (float, >=1) – Scale factor for the crop size.
final_size (iterable of int) – Final size of the image and mask after cropping and resizing.
fg_rate (float, default=0.33) – Probability of focusing the crop on the foreground.
fg_margin (iterable of float, default=np.zeros(3)) – Margin around the foreground location.
- Raises:
ValueError – If crop_scale < 1.
- Returns:
img (numpy.ndarray) – Cropped and resized image data.
msk (numpy.ndarray) – Cropped and resized mask data.