ffcv.transforms module¶
- class ffcv.transforms.ToDevice(device, non_blocking=True)[source]¶
Move tensor to device.
- Parameters:
device (torch.device) – Device to move to.
non_blocking (bool) – Asynchronous if copying from CPU to GPU.
- class ffcv.transforms.ToTorchImage(channels_last=True, convert_back_int16=True)[source]¶
Change tensor to PyTorch format for images (B x C x H x W).
- Parameters:
channels_last (bool) – Use torch.channels_last.
convert_back_int16 (bool) – Convert to float16.
- class ffcv.transforms.NormalizeImage(mean: ndarray, std: ndarray, type: dtype)[source]¶
Fast implementation of normalization and type conversion for uint8 images to any floating point dtype.
Works on both GPU and CPU tensors.
- Parameters:
mean (np.ndarray) – The mean vector.
std (np.ndarray) – The standard deviation vector.
type (np.dtype) – The desired output type for the result as a numpy type. If the transform is applied on a GPU tensor it will be converted as the equivalent torch dtype.
- class ffcv.transforms.Convert(target_dtype)[source]¶
Convert to target data type.
- Parameters:
target_dtype (numpy.dtype or torch.dtype) – Target data type.
- class ffcv.transforms.Squeeze(*dims)[source]¶
Remove given dimensions of input of size 1. Operates on tensors.
- Parameters:
*dims (List[int]) – Dimensions to squeeze.
- class ffcv.transforms.View(target_dtype)[source]¶
View array using np.view or torch.view.
- Parameters:
target_dtype (numpy.dtype or torch.dtype) – Target data type.
- class ffcv.transforms.RandomResizedCrop(scale: Tuple[float, float], ratio: Tuple[float, float], size: int)[source]¶
Crop a random portion of image with random aspect ratio and resize it to a given size. Chances are you do not want to use this augmentation and instead want to include RRC as part of the decoder, by using the :cla:`~ffcv.fields.rgb_image.ResizedCropRGBImageDecoder` class.
- Parameters:
scale (Tuple[float, float]) – Lower and upper bounds for the ratio of random area of the crop.
ratio (Tuple[float, float]) – Lower and upper bounds for random aspect ratio of the crop.
size (int) – Side length of the output.
- class ffcv.transforms.RandomHorizontalFlip(flip_prob: float = 0.5)[source]¶
Flip the image horizontally with probability flip_prob. Operates on raw arrays (not tensors).
- Parameters:
flip_prob (float) – The probability with which to flip each image in the batch horizontally.
- class ffcv.transforms.RandomTranslate(padding: int, fill: Tuple[int, int, int] = (0, 0, 0))[source]¶
Translate each image randomly in vertical and horizontal directions up to specified number of pixels.
- Parameters:
padding (int) – Max number of pixels to translate in any direction.
fill (tuple) – An RGB color ((0, 0, 0) by default) to fill the area outside the shifted image.
- class ffcv.transforms.Cutout(crop_size: int, fill: Tuple[int, int, int] = (0, 0, 0))[source]¶
Cutout data augmentation (https://arxiv.org/abs/1708.04552).
- Parameters:
crop_size (int) – Size of the random square to cut out.
fill (Tuple[int, int, int], optional) – An RGB color ((0, 0, 0) by default) to fill the cutout square with. Useful for when a normalization layer follows cutout, in which case you can set the fill such that the square is zero post-normalization.
- class ffcv.transforms.ImageMixup(alpha: float, same_lambda: bool)[source]¶
Mixup for images. Operates on raw arrays (not tensors).
- Parameters:
alpha (float) – Mixup parameter alpha
same_lambda (bool) – Whether to use the same value of lambda across the whole batch, or an individually sampled lambda per image in the batch
- class ffcv.transforms.LabelMixup(alpha: float, same_lambda: bool)[source]¶
Mixup for labels. Should be initialized in exactly the same way as :cla:`ffcv.transforms.ImageMixup`.
- class ffcv.transforms.Poison(mask: ndarray, alpha: ndarray, indices, clamp=(0, 255))[source]¶
Poison specified images by adding a mask with given opacity. Operates on raw arrays (not tensors).
- Parameters:
mask (ndarray) – The mask to apply to each image.
alpha (float) – The opacity of the mask.
indices (Sequence[int]) – The indices of images that should have the mask applied.
clamp (Tuple[int, int]) – Clamps the final pixel values between these two values (default: (0, 255)).
- class ffcv.transforms.ReplaceLabel(indices, new_label: int)[source]¶
Replace label of specified images.
- Parameters:
indices (Sequence[int]) – The indices of images to relabel.
new_label (int) – The new label to assign.
- class ffcv.transforms.ModuleWrapper(module: torch.nn.Module)[source]¶
Transform using the given torch.nn.Module
- Parameters:
module (torch.nn.Module) – The module for transformation
- class ffcv.transforms.RandomBrightness(magnitude: float, p=0.5)[source]¶
Randomly adjust image brightness. Operates on raw arrays (not tensors).
- Parameters:
magnitude (float) – randomly choose brightness enhancement factor on [max(0, 1-magnitude), 1+magnitude]
p (float) – probability to apply brightness
- class ffcv.transforms.RandomContrast(magnitude, p=0.5)[source]¶
Randomly adjust image contrast. Operates on raw arrays (not tensors).
- Parameters:
magnitude (float) – randomly choose contrast enhancement factor on [max(0, 1-magnitude), 1+magnitude]
p (float) – probability to apply contrast
- class ffcv.transforms.RandomSaturation(magnitude, p=0.5)[source]¶
Randomly adjust image color balance. Operates on raw arrays (not tensors).
- Parameters:
magnitude (float) – randomly choose color balance enhancement factor on [max(0, 1-magnitude), 1+magnitude]
p (float) – probability to apply saturation