torchelie.transforms¶
-
class
torchelie.transforms.
ResizeNoCrop
(size, mode=2)¶ Resize a PIL image so that its longer border is of size size
- Parameters
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.
- Parameters
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- Returns
params (i, j, h, w) to be passed to
crop
.- Return type
tuple
-
class
torchelie.transforms.
AdaptPad
(sz, padding_mode='constant', fill=0)¶ Pad an input image so that it reaches size size
- Parameters
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
- Parameters
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
- Parameters
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.
- Parameters
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
-
berserk_mode
() → torchelie.transforms.randaugment.RandAugment¶ Load even more transforms
-
forward
(img: PIL.Image.Image) → PIL.Image.Image¶ img (PIL Image or Tensor): Image to be transformed.
- Returns
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.
- Parameters
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.
- Parameters
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.
- Parameters
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
(min_ratio: int = 1, max_ratio: int = 3, p: float = 0.5, interpolation: torchvision.transforms.InterpolationMode = torchvision.transforms.InterpolationMode.BILINEAR)¶ Randomly subsample images.
- Parameters
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, **jpeg_args)¶ Add some random jpeg compression artifacts
- Parameters
p (float) – probability of applying the filter
min_compression (float) – minimum quality (1: maximum quality)
torchelie.transforms.differentiable¶
-
class
torchelie.transforms.differentiable.
AllAtOnceColor
(B: int, init: Optional[torch.Tensor] = None)¶ Similar to AllAtOnceGeometric, performs multiple color transforms at once.
- Parameters
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
- Parameters
x (torch.Tensor) – input
- Returns
transformed x
-
brightness
(alpha: float, prob: float = 1.0) → torchelie.transforms.differentiable.AllAtOnceColor¶ Change brightness by a factor alpha
- Parameters
alpha (float) – scale factor
-
contrast
(alpha: float, prob: float = 1.0) → torchelie.transforms.differentiable.AllAtOnceColor¶ Scale contrast by factor alpha
- Parameters
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.
- Parameters
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]
- Parameters
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].
- Parameters
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
- Parameters
batch (4D images tensor) – the tensor to crop
size ((int, int)) – size of the resulting image as (height, width)
- Returns
The cropped image
-
torchelie.transforms.differentiable.
crop
(img, warped=True, sub_img_factor=2)¶ Randomly crop a sub_img_factor smaller part of img.
- Parameters
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)
-
torchelie.transforms.differentiable.
gblur
(input)¶ Gaussian blur with kernel size 3
- Parameters
input (3D or 4D image(s) tensor) – input image
- Returns
the blurred tensor
-
torchelie.transforms.differentiable.
mblur
(input)¶ Mean (or average) blur with kernel size 3
- Parameters
input (3D or 4D image(s) tensor) – input image
- Returns
the blurred tensor
-
torchelie.transforms.differentiable.
roll
(img, x_roll, y_roll)¶ Wrap an image
- Parameters
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
- Returns
The rolled tensor