LION.utils.image_transforms

Image transforms in plain torch.

gaussian_blur and rotate give the same results as torchvision.transforms.functional’s (checked in pytests/test_utils.py), which LION used before, without the torchvision dependency. Both work on any number of leading dimensions, on any device, and are differentiable.

random_crop, random_flip_rotate and random_erasing are GaussianDenoiserSolver’s augmentations (crop and erasing: kornia’s RandomCrop and RandomErasing before, with the same defaults), on batches (B, C, H, W). They draw from torch’s random number generator on the CPU, so LION.utils.rng.seeded makes them the same every time.

Functions

gaussian_blur(x, kernel_size, sigma[, dims])

Gaussian blur of x along dims, with reflected borders: a tensor of any shape, e.g. images (B, C, H, W) or sinograms (B, C, angles, detectors).

gaussian_kernel1d(kernel_size, sigma[, ...])

Normalised 1D Gaussian kernel of odd size kernel_size and standard deviation sigma (in pixels), sampled at the pixel centres.

random_crop(x, size)

A size (h, w) crop of each image of the batch x (B, C, H, W), at a random place in each image.

random_erasing(x[, p, scale, ratio, value])

Each image of the batch x (B, C, H, W), with probability p, with a random rectangle set to value: its area a fraction of the image's drawn uniformly from scale, its aspect ratio (height / width) drawn log-uniformly from ratio (up to 10 draws for one that fits, as in torchvision's RandomErasing).

random_flip_rotate(x)

Each image of the batch x (B, C, H, W) flipped and rotated by a random one of the 8 symmetries of the square (rotations by multiples of 90 degrees, with or without a flip), as denoisers are trained (e.g. KAIR's augment_img).

rotate(x, angle[, interpolation])

Images x (..., H, W) rotated counter-clockwise by angle degrees about their centre, with the shape and dtype of x: points that come from outside the image are 0.


LION.utils.image_transforms.gaussian_blur(x, kernel_size, sigma, dims=(-2, -1))[source]

Gaussian blur of x along dims, with reflected borders: a tensor of any shape, e.g. images (B, C, H, W) or sinograms (B, C, angles, detectors). Returns the blurred tensor, with the shape and dtype of x.

kernel_size: the odd kernel size, one for all dims or one each; sigma: the standard deviation in pixels, likewise; dims: the dimensions to blur along (the last two, an image’s, by default; (-1,) blurs along the last only).

LION.utils.image_transforms.gaussian_kernel1d(kernel_size, sigma, dtype=None, device=None)[source]

Normalised 1D Gaussian kernel of odd size kernel_size and standard deviation sigma (in pixels), sampled at the pixel centres.

LION.utils.image_transforms.random_crop(x, size)[source]

A size (h, w) crop of each image of the batch x (B, C, H, W), at a random place in each image.

LION.utils.image_transforms.random_erasing(x, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0.0)[source]

Each image of the batch x (B, C, H, W), with probability p, with a random rectangle set to value: its area a fraction of the image’s drawn uniformly from scale, its aspect ratio (height / width) drawn log-uniformly from ratio (up to 10 draws for one that fits, as in torchvision’s RandomErasing). Returns a new tensor.

LION.utils.image_transforms.random_flip_rotate(x)[source]

Each image of the batch x (B, C, H, W) flipped and rotated by a random one of the 8 symmetries of the square (rotations by multiples of 90 degrees, with or without a flip), as denoisers are trained (e.g. KAIR’s augment_img). Images that aren’t square get one of the 4 symmetries that keep their shape.

LION.utils.image_transforms.rotate(x, angle, interpolation='nearest')[source]

Images x (…, H, W) rotated counter-clockwise by angle degrees about their centre, with the shape and dtype of x: points that come from outside the image are 0. interpolation: “nearest” or “bilinear”.