class torchelie.transforms.ResizeNoCrop(size, mode=2)

Resize a PIL image so that its longer border is of size size


size (int) – max size of the image

class torchelie.transforms.ResizedCrop(size, scale=1, ratio=1, interpolation=torchvision.transforms.InterpolationMode.BILINEAR)

Crop the given PIL Image to size. A crop of size of the original size is made. This crop is finally resized to given size.

  • size – expected output size of each edge

  • scale – zoom factor

  • interpolation – Default: PIL.Image.BILINEAR

static get_params(img, scale, ratio=1)

Get parameters for crop. :param img: Image to be cropped. :type img: PIL Image :param scale: range of size of the origin size cropped :type scale: float


params (i, j, h, w) to be passed to crop.

Return type


class torchelie.transforms.AdaptPad(sz, padding_mode='constant', fill=0)

Pad an input image so that it reaches size size

  • sz ((int, int)) – target size

  • padding_mode (str) – one of the modes of torchvision.transforms.pad

class torchelie.transforms.MultiBranch(transforms)

Transform an image with multiple transforms


transforms (list of transforms) – the parallel set of transforms

class torchelie.transforms.Canny(thresh_low: int = 100, thresh_high: int = 200)

Run Canny edge detector over an image. Requires OpenCV to be installed

  • thresh_low (int) – lower threshold (default: 100)

  • thresh_high (int) – upper threshold (default: 200)

class torchelie.transforms.RandAugment(n_transforms: int, magnitude: float, interpolation: torchvision.transforms.InterpolationMode = torchvision.transforms.InterpolationMode.BILINEAR, fill: Optional[List[float]] = None)

RandAugment policy from RandAugment: Practical automated data augmentation with a reduced search space.

  • n_transforms (int) – how many transforms to apply

  • magnitude (float) – magnitude of the transforms. 10 is base rate, can be set to more.

  • interpolation – interpolation to use for suitable transforms

  • fill – fill value to use for suitable transforms


Load even more transforms

forward(img: PIL.Image.Image)PIL.Image.Image

img (PIL Image or Tensor): Image to be transformed.


RandAugmented image.

Return type

PIL Image or Tensor

class torchelie.transforms.Posterize(min_bits: int = 4, max_bits: int = 8)

Apply a Posterize filter with a random number of bits.

  • min_bits (int) – minimum color encoding bits

  • max_bits (int) – maximum color encoding bits

class torchelie.transforms.Solarize(max_thresh: int = 128)

Apply a Solarize filter with a random threshold.


max_thresh (int) – upper bound for the random threshold.

class torchelie.transforms.Cutout(min_size: float, max_size: float)

Applies a random Cutout filter erasing at most :code:`max_size*100`% of the picture.


max_size (float) – the maximum ratio that can be erased. 0 means no erasure, 1 means up to the whole image can be erased.

class torchelie.transforms.Identity

Do nothing

class torchelie.transforms.Subsample(max_ratio: int = 3, p: float = 0.5, interpolation: torchvision.transforms.InterpolationMode = torchvision.transforms.InterpolationMode.BILINEAR)

Randomly subsample images.

  • p (float) – the transform is applied with probability p

  • max_ratio (int) – maximum subscaling factor

  • interpolation (InterpolationMode) – interpolation mode

class torchelie.transforms.JPEGArtifacts(min_compression: float = 0.5, p: float = 0.5)

Add some random jpeg compression artifacts

  • p (float) – probability of applying the filter

  • min_compression (float) – minimum quality (1: maximum quality)


class torchelie.transforms.differentiable.AllAtOnceColor(B: int, init: Optional[torch.Tensor] = None)

Similar to AllAtOnceGeometric, performs multiple color transforms at once.

  • B (int) – batch size

  • init (torch.Tensor) – an initial user supplied transformation matrix. If not provided, default to identity.

apply(x: torch.Tensor)torch.Tensor

Applies transforms on x


x (torch.Tensor) – input


transformed x

brightness(alpha: float, prob: float = 1.0)torchelie.transforms.differentiable.AllAtOnceColor

Change brightness by a factor alpha


alpha (float) – scale factor

contrast(alpha: float, prob: float = 1.0)torchelie.transforms.differentiable.AllAtOnceColor

Scale contrast by factor alpha


alpha (float) – scale factor

class torchelie.transforms.differentiable.AllAtOnceGeometric(B: int, init: Optional[torch.Tensor] = None)

Various geometric transforms packed up into an affine transformation matrix. Transformations can be stacked then applied on a 4D tensor to reduce artifact, memory usage, and compute. Fully differentiable.

>>> img = torch.randn(10, 3, 32, 32)
>>> transformed = AllAtOnceGeometric(10)            .translate(5, 5).scale(0.9).apply(img)

Note: the transformations get sampled at creation, so that each call to apply() runs the same transforms. Construct another AllAtOnceGeometric object for another set of transform. This allows to easily run the same transforms on paired datasets.

Note2: Each transform has a prob argument which specifies whether to use the transform or bypass it. This makes it easy to implement StyleGAN2-ADA.

  • B (int) – batch size

  • init (torch.Tensor) – an initial user supplied transformation matrix. If not provided, default to identity.

rotate(theta: float, prob: float = 1.0)torchelie.transforms.differentiable.AllAtOnceGeometric

Rotate the image by an angle randomly sampled between [-theta, theta]


theta (float) – an angle in degrees

scale(x: float, y: float, prob: float = 1.0)torchelie.transforms.differentiable.AllAtOnceGeometric

Randomly scale the image horizontally by a factor [1 - x; 1 + x] and vertically by a factor of [1 - y; 1 + y].

  • x (float) – horizontal factor

  • y (float) – vertical factor

translate(x: float, y: float, prob: float = 1.0)torchelie.transforms.differentiable.AllAtOnceGeometric

Randomly translate image horizontally with an offset sampled in [-x, x] and vertically [-y, y]. Note that the coordinate are not pixel coordinate but texel coordinate between [-1, 1]

torchelie.transforms.differentiable.center_crop(batch, size)

Crop the center of a 4D images tensor

  • batch (4D images tensor) – the tensor to crop

  • size ((int, int)) – size of the resulting image as (height, width)


The cropped image

torchelie.transforms.differentiable.crop(img, warped=True, sub_img_factor=2)

Randomly crop a sub_img_factor smaller part of img.

  • img (3D or 4D image(s) tensor) – input image(s)

  • warped (bool) – Whether the image should be considered warped (default: True)

  • sub_img_factor (float) – fraction of the image to take. For instance, 2 will crop a quarter of the image (half the width, half the height). (default: 2)


Gaussian blur with kernel size 3


input (3D or 4D image(s) tensor) – input image


the blurred tensor


Mean (or average) blur with kernel size 3


input (3D or 4D image(s) tensor) – input image


the blurred tensor

torchelie.transforms.differentiable.roll(img, x_roll, y_roll)

Wrap an image

  • img (3D or 4D image(s) tensor) – an image tensor

  • x_roll (int) – how many pixels to roll on the x axis

  • y_roll (int) – how many pixels to roll on the y axis


The rolled tensor