albumentations.pytorch.transforms


Converts images/masks to PyTorch Tensors, inheriting from BasicTransform. For images: Converts `HWC` format to PyTorch `CHW` format

Members

ToTensor3Dclass

ToTensor3D(
    p: float = 1.0
)

Convert 3D volume data and masks to PyTorch tensors (D,H,W,C or D,H,W -> C,D,H,W). For 3D medical imaging pipelines; p=1.0 by default. This transform is designed for 3D medical imaging data. It converts numpy arrays to PyTorch tensors and ensures consistent channel positioning. For all inputs (volume data and masks): - Input: (D, H, W, C) or (D, H, W) - depth, height, width, [channels] - Output: (C, D, H, W) - channels first format for PyTorch For single-channel input, adds C=1 dimension Note: This transform always moves channels to first position as this is the standard PyTorch format. For masks that need to stay in DHWC format, use a different transform or handle the transposition after this transform.

Parameters

NameTypeDefaultDescription
pfloat1.0Probability of applying the transform. Default: 1.0

Examples

>>> transform = ToTensor3D(p=1.0)

Notes

This transform always moves channels to first position as this is the standard PyTorch format. For masks that need to stay in DHWC format, use a different transform or handle the transposition after this transform.

ToTensorV2class

ToTensorV2(
    transpose_mask: bool = False,
    p: float = 1.0
)

Converts images/masks to PyTorch Tensors, inheriting from BasicTransform. For images: Converts `HWC` format to PyTorch `CHW` format

Parameters

NameTypeDefaultDescription
transpose_maskboolFalseIf True, transposes 3D input mask dimensions from `[height, width, num_channels]` to `[num_channels, height, width]`.
pfloat1.0Probability of applying the transform. Default: 1.0.

Examples

>>> transform = ToTensorV2(transpose_mask=True)