Object API#

Warning

The DALI TorchVision API is an experimental feature and is subject to change.

This page documents the operator classes currently implemented in the DALI TorchVision API. They are drop-in replacements for their counterparts in torchvision.transforms.v2. For a general introduction, see TorchVision API Overview. To add an operator that is not listed here, see Implementing custom operators. For the functional counterparts, see Functional API.

Every operator accepts the additional device argument ("cpu" or "gpu"). nvidia.dali.experimental.torchvision.Compose additionally accepts batch_size.

Available in the nvidia.dali.experimental.torchvision.v2 module.

Composing transforms#

class nvidia.dali.experimental.torchvision.Compose(
op_list,
batch_size=16,
num_threads=16,
**dali_pipeline_kwargs,
)#

Composes transforms together in a single pipeline

This class chains multiple DALI operations in a sequential manner, similar to torchvision.transforms.Compose. The Compose class implements a callable which runs the pipeline.

Parameters:
  • op_list (list) – List of DALI operators.

  • batch_size (int, optional, default = DEFAULT_BATCH_SIZE) – Batch size.

  • num_threads (int, optional, default = DEFAULT_NUM_THREADS) – Number of threads.

  • **dali_pipeline_kwargs – Additional keyword arguments for the DALI pipeline.

Geometry transforms#

class nvidia.dali.experimental.torchvision.Resize(
size,
interpolation=InterpolationMode.BILINEAR,
max_size=None,
antialias=True,
device='cpu',
)#

Resize the input image to the given size If the image is torch Tensor, it is expected to have […, H, W] shape, where … means a maximum of two leading dimensions

Parameters:
  • size (sequence or int) – Desired output size. If size is a sequence like (h, w), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size).

  • interpolation (InterpolationMode or int) – nvidia.dali.experimental.torchvision.InterpolationMode. Default is InterpolationMode.BILINEAR. If input is Tensor, only InterpolationMode.NEAREST, InterpolationMode.NEAREST_EXACT, InterpolationMode.BILINEAR and InterpolationMode.BICUBIC are supported.

  • max_size (int, optional) – The maximum allowed for the longer edge of the resized image. If the longer edge of the image is greater than max_size after being resized according to size, size will be overruled so that the longer edge is equal to max_size. As a result, the smaller edge may be shorter than size. This is only supported if size is an int.

  • antialias (bool, optional) – Whether to apply antialiasing. If True, antialiasing will be applied. If False, antialiasing will not be applied.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the resize. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.CenterCrop(size, device='cpu')#

Crop the input at the center.

If the input is a torch.Tensor it can have an arbitrary number of leading batch dimensions. For example, the image tensor can have […, C, H, W] shape.

If image size is smaller than output size along any edge, image is padded with 0 and then center cropped.

Parameters:
  • size (sequence or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the crop. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.RandomCrop(
size,
padding=None,
pad_if_needed=False,
fill=0,
padding_mode='constant',
device='cpu',
)#

Crop the input at a random location.

If the input is a torch.Tensor it can have an arbitrary number of leading batch dimensions. For example, the image tensor can have […, C, H, W] shape.

Parameters:
  • size (sequence or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • padding (int or sequence, optional, default = None) – Optional padding on each border of the image, applied before cropping. If a single int or a sequence of length 1 is provided this is used to pad all borders. If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively. If a sequence of length 4 is provided this is the padding for the left, top, right and bottom borders respectively.

  • pad_if_needed (bool, optional, default = False) – Pad the image if it is smaller than the desired size.

  • fill (number or tuple, optional, default = 0) – Pixel fill value used when the padding_mode is constant.

  • padding_mode (Literal["constant", "edge", "reflect", "symmetric"], optional, default="constant") – Type of padding. Should be: constant, edge, reflect or symmetric.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the crop. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.RandomResizedCrop(
size,
scale=(0.08, 1.0),
ratio=(0.75, 1.3333333333333333),
interpolation=InterpolationMode.BILINEAR,
antialias=True,
device='cpu',
)#

Crop a random portion of the input and resize it to a given size.

If the input is a torch.Tensor it can have an arbitrary number of leading batch dimensions. For example, the image tensor can have […, C, H, W] shape.

Parameters:
  • size (sequence or int) – Expected output size of the crop. If size is an int instead of sequence like (h, w), a square output size (size, size) is made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • scale (tuple of float, optional, default = (0.08, 1.0)) – Lower and upper bounds for the random crop area, relative to the input image area.

  • ratio (tuple of float, optional, default = (3 / 4, 4 / 3)) – Lower and upper bounds for the random crop aspect ratio, width / height.

  • interpolation (InterpolationMode or int, optional, default = InterpolationMode.BILINEAR) – Interpolation mode to use for resizing. Legacy PIL integer codes (0 = NEAREST, 1 = LANCZOS, 2 = BILINEAR, 3 = BICUBIC, 4 = BOX, 5 = HAMMING) are accepted for torchvision compatibility.

  • antialias (bool, optional, default = True) – Whether to apply antialiasing during resize.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the crop. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.Pad(padding, fill=0, padding_mode='constant', device='cpu')#

Pad the input on all sides with the given padding_mode type and fill value.

Parameters:
  • padding (int or sequence) – Padding on each border. If a single int is provided this is used to pad all borders. If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively. If a sequence of length 4 is provided this is the padding for the left, top, right and bottom borders respectively.

  • fill (number or tuple or dict, optional, default = 0) – Pixel fill value used when the padding_mode is constant.

  • padding_mode (Literal["constant", "edge", "reflect", "symmetric"], optional,) –

    Type of padding. Should be: constant, edge, reflect or symmetric. Default is “constant”.

    constant: pads with a constant value, this value is specified with fill edge: pads with the last value at the edge of the image. reflect: pads with reflection of image without repeating the last value on the edge.

    For example, padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode will result in [3, 2, 1, 2, 3, 4, 3, 2]

    symmetric: pads with reflection of image repeating the last value on the edge.

    For example, padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode will result in [2, 1, 1, 2, 3, 4, 4, 3]

  • device (Literal["cpu", "gpu"], optional, default = "cpu")

class nvidia.dali.experimental.torchvision.RandomHorizontalFlip(p=0.5, device='cpu')#

Randomly horizontally flips the given image randomly with a given probability.

Parameters:
  • p (float) – Probability of the image being flipped. Default value is 0.5

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the flip. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.RandomVerticalFlip(p=0.5, device='cpu')#

Randomly vertically flips the given image randomly with a given probability.

Parameters:
  • p (float) – Probability of the image being flipped. Default value is 0.5

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the flip. Can be "cpu" or "gpu".

Color and filtering transforms#

class nvidia.dali.experimental.torchvision.ColorJitter(
brightness=0.0,
contrast=0.0,
saturation=0.0,
hue=0.0,
device='cpu',
)#

Randomly change the brightness, contrast, saturation and hue of an image or video.

Parameters:
  • brightness (float or tuple of float (min, max)) – How much to jitter brightness. brightness_factor is chosen uniformly from [max(0, 1 - brightness), 1 + brightness] or the given [min, max]. Should be non negative numbers.

  • contrast (float or tuple of float (min, max)) – How much to jitter contrast. contrast_factor is chosen uniformly from [max(0, 1 - contrast), 1 + contrast] or the given [min, max]. Should be non-negative numbers.

  • saturation (float or tuple of float (min, max)) – How much to jitter saturation. saturation_factor is chosen uniformly from [max(0, 1 - saturation), 1 + saturation] or the given [min, max]. Should be non negative numbers.

  • hue (float or tuple of float (min, max)) – How much to jitter hue. hue_factor is chosen uniformly from [-hue, hue] or the given [min, max]. Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5. To jitter hue, the pixel values of the input image has to be non-negative for conversion to HSV space. DALI rotates hue linearly in YIQ, so results are close to but not identical to torchvision.transforms.v2.functional.adjust_hue, which rotates in HSV. The gap grows with the angle, reaching roughly 24 degrees at a 90 degree rotation.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the color jitter. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.Grayscale(num_output_channels=1, device='cpu')#

Convert images or videos to grayscale.

Parameters:

num_output_channels (int) – Number of channels desired for output image. Should be 1 or 3.

class nvidia.dali.experimental.torchvision.RandomGrayscale(p=0.1, device='cpu')#

Randomly convert image or videos to grayscale with a probability of p (default 0.1)

Parameters:

p (float) – probability that image or video should be converted to grayscale.

class nvidia.dali.experimental.torchvision.GaussianBlur(kernel_size, sigma=(0.1, 2.0), device='cpu', seed=-1)#

Blurs image with randomly chosen Gaussian blur kernel.

The convolution will be using reflection padding corresponding to the kernel size, to maintain the input shape.

If the input is a Tensor, it is expected to have […, C, H, W] shape, where … means an arbitrary number of leading dimensions.

Parameters:
  • kernel_size (int or sequence) – Size of the Gaussian kernel. Should be a positive integer or a sequence of two positive integers.

  • sigma (float or tuple of float) – Standard deviation to be used for creating kernel to perform blurring. If float, sigma is fixed. If it is tuple of float (min, max), sigma is chosen uniformly at random to lie in the given range.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the GaussianBlur. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.Normalize(mean, std, inplace=False, device='cpu')#

Normalize a tensor image with mean and standard deviation.

This transform does not support PIL Image. Given mean: (mean[1],…,mean[n]) and std: (std[1],..,std[n]) for n channels, this transform will normalize each channel of the input torch.*Tensor i.e., output[channel] = (input[channel] - mean[channel]) / std[channel]

Parameters:
  • mean (sequence) – Sequence of means for each channel.

  • std (sequence) – Sequence of standard deviations for each channel.

  • inplace (bool, optional) – Bool to make this operation in-place. Not supported.

Control flow#

class nvidia.dali.experimental.torchvision.RandomApply(op_list, p=0.5, device='cpu')#

Apply randomly a list of transformations with a given probability.

Parameters:
  • op_list (Sequence[Callable]) – List of transformations to apply.

  • p (float, optional, default = 0.5) – Probability of applying the transformations.

  • device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the operator. Can be "cpu" or "gpu".

Type conversion transforms#

class nvidia.dali.experimental.torchvision.PILToTensor(device='cpu')#

Signals PipelineHWC to return a uint8 CHW torch.Tensor instead of PIL.Image.

When placed in a Compose op list this operator does not modify the data inside the DALI pipeline. It acts as a marker that causes PipelineHWC (PIL input path) to return a CHW torch.Tensor with dtype uint8 and values in [0, 255].

Parameters:

device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the operator. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.ToPILImage(device='cpu')#

Signals PipelineCHW to return a PIL.Image instead of torch.Tensor.

When placed in a Compose op list this operator does not modify the data inside the DALI pipeline. It acts as a marker that causes PipelineCHW (torch.Tensor input path) to convert the CHW output tensor back to a PIL.Image.

Parameters:

device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the operator. Can be "cpu" or "gpu".

class nvidia.dali.experimental.torchvision.ToPureTensor(device='cpu')#

Signals the pipeline to return a torch.Tensor instead of the default output type.

When placed in a Compose op list this operator does not modify the data inside the DALI pipeline. It acts as a marker that causes PipelineWithLayout to return a plain torch.Tensor in CHW layout regardless of the pipeline’s default output type.

Parameters:

device (Literal["cpu", "gpu"], optional, default = "cpu") – Device to use for the operator. Can be "cpu" or "gpu".

Enumerations#

class nvidia.dali.experimental.torchvision.InterpolationMode(*values)#

Interpolation modes compatible with torchvision.transforms.InterpolationMode.