fovi.utils.fastaugs.functional

fovi.utils.fastaugs.functional.radians(x, /)

Convert angle x from degrees to radians.

fovi.utils.fastaugs.functional.set_trace(frame=None, header=None)[source]

Start debugging from frame.

If frame is not specified, debugging starts from caller’s frame.

fovi.utils.fastaugs.functional.mask_batch(b: Tensor, p=.5, rng=None)[source]
fovi.utils.fastaugs.functional.mask_batch(b: numpy.ndarray, p=0.5, rng=None)[source]
Noindex:

fovi.utils.fastaugs.functional.mask_tensor(x, p=0.5, neutral=0., rng=None)[source]

Mask elements of x with neutral with probability 1-p modified from https://github.com/fastai/fastai2/blob/master/fastai2/vision/augment.py

fovi.utils.fastaugs.functional.normalize(x: ndarray, mean, std)[source]
fovi.utils.fastaugs.functional.normalize(x: torch.Tensor, mean, std, inplace=False)[source]
Noindex:

fovi.utils.fastaugs.functional.to_device(x, device)[source]

Move tensor to device

fovi.utils.fastaugs.functional.to_numpy(x)[source]

Convert tensor to numpy array

fovi.utils.fastaugs.functional.to_float(x)[source]

Convert tensor to float

fovi.utils.fastaugs.functional.div_(x, val=255., dtype=torch.float32)[source]
fovi.utils.fastaugs.functional.to_channels_first(x: Tensor)[source]
fovi.utils.fastaugs.functional.to_channels_first(x: numpy.ndarray)[source]
Noindex:

fovi.utils.fastaugs.functional.to_channels_last(x: Tensor)[source]
fovi.utils.fastaugs.functional.to_channels_last(x: numpy.ndarray)[source]
Noindex:

fovi.utils.fastaugs.functional.random_grayscale(b: Tensor, idx, num_output_channels=3)[source]

Convert random set of images in batch b to grayscale using ITU-R 601-2 luma transform

fovi.utils.fastaugs.functional.random_grayscale(b: numpy.ndarray, idx, num_output_channels=3)[source]

Convert random set of images in batch b to grayscale using ITU-R 601-2 luma transform

fovi.utils.fastaugs.functional.random_grayscale(b, idx)[source]
fovi.utils.fastaugs.functional.to_grayscale(x: Tensor, num_output_channels=1)[source]

Convert to grayscale like PIL, using ITU-R 601-2 luma transform L = R * 299/1000 + G * 587/1000 + B * 114/1000

fovi.utils.fastaugs.functional.to_grayscale(x: numpy.ndarray, num_output_channels=1)[source]

Convert to grayscale like PIL, using ITU-R 601-2 luma transform L = R * 299/1000 + G * 587/1000 + B * 114/1000

fovi.utils.fastaugs.functional.to_grayscale(x: PIL.Image.Image, num_output_channels=1)[source]

Convert to grayscale like PIL, using ITU-R 601-2 luma transform L = R * 299/1000 + G * 587/1000 + B * 114/1000

fovi.utils.fastaugs.functional.dummy_red(bs=1, s=5)[source]

test generating batch of red squares

fovi.utils.fastaugs.functional.mat3(value)[source]

Identity matrix with given value

fovi.utils.fastaugs.functional.hue_mat(hue)[source]

Composes transformation matrix for hue

fovi.utils.fastaugs.functional.sat_mat(saturation)[source]

Composes transformation matrix for saturation

fovi.utils.fastaugs.functional.val_mat(value)[source]

Composes transformation matrix for value

fovi.utils.fastaugs.functional.hsv_mat(h, s, v)[source]

Composes transformation matrix for hue, sat, val transform over intermediate Yiq space

fovi.utils.fastaugs.functional.hsv_mat2(h, s, v, brightness, contrast)[source]

Composes transformation matrix for hue, sat, val transform over intermediate Yiq space

fovi.utils.fastaugs.functional.hsv_jitter_tensor(x, mat)[source]
fovi.utils.fastaugs.functional.hsv_jitter_array(x, mat)[source]
fovi.utils.fastaugs.functional.hsv_jitter_array2(x, mat)[source]
fovi.utils.fastaugs.functional.hsv_jitter(x, h, s, v)[source]

Performs hue, saturation, value transform on an RGB image with interediate Yiq transform

The RGB image can be a TensorImage with channels first (CxHxW; or a batch NxCxHxW), or an ArrayImage with channels last (HxWxC; or a batch NxHxWxC).

Tensors are channels first because PyTorch/Torchvision operations assume so (though it looks like PyTorch 1.5 has a channels_last feature).

Arrays are channels last so that this transformation can be used with the Albumentations library, which operates over numpy arrays with RGB channels last.

inputs:

x: RGB image [0-1] or batch of RGB images h: hue rotation angle in degrees (any angle, wraps at 360), or list of angles (one per image in batch) s: multiply saturation by this number, single value or list of values (one per image in batch) v: multiply value by this number, single value or list of values (one per image in batch)

out:

transformed RGB image (or batch of images images) clamped between 0,1 since Yiq values can be out of RGB range

fovi.utils.fastaugs.functional.hsv_jitter2(x: Tensor, h, s, v, b, c)[source]

Performs hue, saturation, value transform on an RGB image with interediate Yiq transform

The RGB image can be a TensorImage with channels first (CxHxW; or a batch NxCxHxW), or an ArrayImage with channels last (HxWxC; or a batch NxHxWxC).

Tensors are channels first because PyTorch/Torchvision operations assume so (though it looks like PyTorch 1.5 has a channels_last feature).

Arrays are channels last so that this transformation can be used with the Albumentations library, which operates over numpy arrays with RGB channels last.

inputs:

x: RGB image [0-1] or batch of RGB images h: hue rotation angle in degrees (any angle, wraps at 360), or list of angles (one per image in batch) s: multiply saturation by this number, single value or list of values (one per image in batch) v: multiply value by this number, single value or list of values (one per image in batch) b: brightness jitter operates in RGB space c: contrast jitter

out:

transformed RGB image (or batch of images images) clamped between 0,1 since Yiq values can be out of RGB range

fovi.utils.fastaugs.functional.hsv_jitter2(x: numpy.ndarray, h, s, v, b, c)[source]
fovi.utils.fastaugs.functional.hsv_jitter2(x, h, s, v, b, c)[source]
fovi.utils.fastaugs.functional.random_hsv_jitter2(x: Tensor, idx, h, s, v, b, c)[source]

randomly apply hsv jitter to each image in a batch

fovi.utils.fastaugs.functional.random_hsv_jitter2(x, h, s, v, b, c)[source]
fovi.utils.fastaugs.functional._get_scale_factor(x: Tensor, sf)[source]
fovi.utils.fastaugs.functional._get_scale_factor(x: numpy.ndarray, sf)[source]
Noindex:

fovi.utils.fastaugs.functional.adjust_brightness(x: Tensor, scale_factor, max_value=255.)[source]
fovi.utils.fastaugs.functional.adjust_brightness(x: numpy.ndarray, scale_factor, max_value=255.0)[source]
fovi.utils.fastaugs.functional.adjust_brightness(x: PIL.Image.Image, scale_factor, max_value=255)[source]
fovi.utils.fastaugs.functional.adjust_brightness_pil(x, brightness, max_value=255.)[source]

Adjust contrast like PIL ImageEnhance

fovi.utils.fastaugs.functional.sigmoid(x)[source]
fovi.utils.fastaugs.functional.logit(x, clamp=1e-7)[source]

Logit of x, clipped to avoid inf.

fovi.utils.fastaugs.functional.random_adjust_brightness(b: Tensor, scale_factor, idx, max_value)[source]

Adjust brightness, *= scale_factor

fovi.utils.fastaugs.functional.random_adjust_brightness(b: numpy.ndarray, scale_factor, idx, max_value)[source]

Adjust brightness, *= scale_factor

fovi.utils.fastaugs.functional.adjust_contrast(x: Tensor, scale_factor, max_value=1.)[source]
fovi.utils.fastaugs.functional.adjust_contrast(x: numpy.ndarray, scale_factor, max_value=1.0)[source]
fovi.utils.fastaugs.functional.adjust_contrast(x: PIL.Image.Image, scale_factor, max_value=255)[source]
fovi.utils.fastaugs.functional.adjust_contrast_logit(x: Tensor, scale_factor, max_value=255.)[source]
fovi.utils.fastaugs.functional.adjust_contrast_pil(x, contrast, max_value=255.)[source]

Adjust contrast like PIL ImageEnhance

fovi.utils.fastaugs.functional.adjust_rms_contrast(x, contrast, max_value=255.)[source]

Adjust the RMS contrast (std of the pixel intensities)

fovi.utils.fastaugs.functional.adjust_rms_contrast_logit(x, contrast, max_value=255.)[source]

Adjust the RMS contrast (std of the log pixel intensities)

fovi.utils.fastaugs.functional.random_adjust_contrast(b: Tensor, scale_factor, idx, max_value)[source]

Adjust brightness, *= scale_factor

fovi.utils.fastaugs.functional.random_adjust_contrast(b: numpy.ndarray, scale_factor, idx, max_value)[source]

Adjust brightness, *= scale_factor

fovi.utils.fastaugs.functional._prepare_mat(x, mat)[source]

prepare transformation matrix mat to apply to ImageBatch x using _grid_sample from https://github.com/fastai/fastai2/blob/master/fastai2/vision/augment.py

fovi.utils.fastaugs.functional._grid_sample(x, coords, mode='bilinear', padding_mode='reflection', align_corners=None)[source]

Resample pixels in coords from x by mode, with padding_mode in (‘reflection’,’border’,’zeros’). from https://github.com/fastai/fastai2/blob/master/fastai2/vision/augment.py

fovi.utils.fastaugs.functional.affine_transform(x: Tensor, mat, sz=None, align_corners=True, mode='bilinear', pad_mode='reflection')[source]

apply affine transformation matrix mat using _grid_sample

fovi.utils.fastaugs.functional.flip_mat(flip: Tensor)[source]

create transformation matrix for horizontal flip flip: flip values inserted into matrix; -1 (flip) or 1 (don’t flip)

fovi.utils.fastaugs.functional.flip_mat(flip: numpy.ndarray)[source]
fovi.utils.fastaugs.functional.flip_mat(flip: int)[source]
fovi.utils.fastaugs.functional.flip_mat(flip: float)[source]
fovi.utils.fastaugs.functional.flip_mat(flip)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas: Tensor)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas: numpy.ndarray)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas: float)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas: int)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas: list)[source]
fovi.utils.fastaugs.functional._prepare_thetas(thetas)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords: Tensor)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords: numpy.ndarray)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords: float)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords: int)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords: list)[source]
fovi.utils.fastaugs.functional._prepare_coords(coords)[source]
fovi.utils.fastaugs.functional._prepare_rot_pt(b, xs, ys)[source]

convert coordinates from pixel coordinates to image-centered [-1,1] coordinates

fovi.utils.fastaugs.functional._prepare_param(param: Tensor)[source]
fovi.utils.fastaugs.functional._prepare_param(param: numpy.ndarray)[source]
fovi.utils.fastaugs.functional._prepare_param(param: float)[source]
fovi.utils.fastaugs.functional._prepare_param(param: int)[source]
fovi.utils.fastaugs.functional._prepare_param(param: list)[source]
fovi.utils.fastaugs.functional._prepare_param(param)[source]
fovi.utils.fastaugs.functional.rotate_mat(degrees, x=0.5, y=0.5)[source]

create transformation matrix for rotation by theta degrees aground point x,y

fovi.utils.fastaugs.functional.rotate(b, thetas, xs=0.5, ys=0.5, pad_mode='zeros')[source]

rotate image(s) b by theta(s) around (x,y) points xs and ys.

Works with a single image, or multiple images, on the cpu or gpu. Images are assumed to be TensorImage [CxHxW] or TensorBatch [NxCxHxW].

For multiple images, each image will be rotated by the same theta if only one theta value is passed. To rotate images individually, pass one theta per image (as a list, np array, or tensor).

If a single image is passed with multiple thetas, that image will be rotated N times (N = number of thetas).

Center of rotation can also be set once for all images (set a single x,y value), or individually for each image by passing one x,y per image (as list, np array or tensor). Position is specified as pct of image (e.g., .5,.5 is the image center)

Parameters:
  • b (tensor) – TensorImage or TensorImageBatch

  • thetas (any) – angle of rotation in degrees

  • xs (any) – center of rotation x-coordinate [0,1; .5=horizontal center of image]

  • ys (any) – center of rotation y-coordinate [0,1; .5=vertical center of image]

fovi.utils.fastaugs.functional.zoom_mat(scale, col_pct=0.5, row_pct=0.5)[source]

create transformation matrix for zoom / scale scale: scale values col_pct: horizontal position [0,1] upon which to center zoom [.5 = horizontal center of image] row_pct: vertical position [0,1] upon which to center zoom [.5 = vertical center of image]

fovi.utils.fastaugs.functional.zoom(b, scales, xs=0.5, ys=0.5, pad_mode='zeros')[source]

zoom image(s) b by scale(s) around point(s) xs,`ys`.

Works with a single image, or multiple images, on the cpu or gpu. Images are assumed to be TensorImage [CxHxW] or TensorBatch [NxCxHxW].

For multiple images, each image will be zoomed by the same scale if only one scale value is passed. To scale images individually, pass one scale per image (as a list, np array, or tensor).

If a single image is passed with multiple scales, that image will be scaled N times (where N is the number of scales).

Center of zoom can also be set once for all images (set a single x,y value), or individually for each image by passing one x,y per image (as list, np array or tensor).

Parameters:
  • b (tensor) – TensorImage or TensorImageBatch

  • thetas (any) – angle of rotation in degrees

  • xs (any) – center of zoom x-coordinate [0,1; .5=horizontal center of image]

  • ys (any) – center of zoom y-coordinate [0,1; .5=vertical center of image]

fovi.utils.fastaugs.functional.translate_mat(tx, ty)[source]

create transformation matrix for translation tx: pct of image to translate in the horizontal direction ty: pct of image to translate in the vertical direction

fovi.utils.fastaugs.functional.rotate_object_mat(degrees, ctr_x, ctr_y, scale, dest_x, dest_y)[source]
fovi.utils.fastaugs.functional.rotate_object(b, degrees, ctr_x=0.5, ctr_y=0.5, scale=1.0, dest_x=None, dest_y=None, pad_mode='border')[source]

rotate image(s) b by theta(s) around (x,y) points xs and ys.

Works with a single image, or multiple images, on the cpu or gpu. Images are assumed to be TensorImage [CxHxW] or TensorBatch [NxCxHxW].

For multiple images, each image will be rotated by the same theta if only one theta value is passed. To rotate images individually, pass one theta per image (as a list, np array, or tensor).

If a single image is passed with multiple thetas, that image will be rotated N times (N = number of thetas).

Center of rotation can also be set once for all images (set a single x,y value), or individually for each image by passing one x,y per image (as list, np array or tensor). Position is specified as pct of image (e.g., .5,.5 is the image center)

Parameters:
  • b (tensor) – TensorImage or TensorImageBatch

  • thetas (any) – angle of rotation in degrees

  • xs (any) – center of rotation x-coordinate [0,1; .5=horizontal center of image]

  • ys (any) – center of rotation y-coordinate [0,1; .5=vertical center of image]

fovi.utils.fastaugs.functional.grid_sample(b: Tensor, grid: Tensor, align_corners=False)[source]
fovi.utils.fastaugs.functional.random_grid_sample(b: Tensor, idx, grid, align_corners=True)[source]
fovi.utils.fastaugs.functional.random_grid_sample(b, idx, grid, align_corners=True)[source]
Noindex:

fovi.utils.fastaugs.functional.apply_mask(b: Tensor, mask: Tensor)[source]
fovi.utils.fastaugs.functional.srgb_to_lrgb(image: Tensor, inplace=True) Tensor[source]

Converts a standard RGB image to linearized RGB.

References

https://en.wikipedia.org/wiki/SRGB http://www.w3.org/Graphics/Color/sRGB

Parameters:

image (torch.Tensor) – RGB Image to be converted to XYZ with shape \((*, 3, H, W)\).

Returns:

XYZ version of the image with shape \((*, 3, H, W)\).

Return type:

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_xyz(input)  # 2x3x4x5
fovi.utils.fastaugs.functional.rgb_to_xyz(image: Tensor) Tensor[source]

Converts a RGB image to XYZ.

Parameters:

image (torch.Tensor) – RGB Image to be converted to XYZ with shape \((*, 3, H, W)\).

Returns:

XYZ version of the image with shape \((*, 3, H, W)\).

Return type:

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_xyz(input)  # 2x3x4x5
fovi.utils.fastaugs.functional.xyz_to_lms(image: Tensor) Tensor[source]

Converts a XYZ image to CAT02 LMS.

Parameters:

image (torch.Tensor) – XYZ Image to be converted to LMS with shape \((*, 3, H, W)\).

Returns:

LMS version of the image with shape \((*, 3, H, W)\).

Return type:

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = xyz_to_lms(input)  # 2x3x4x5
fovi.utils.fastaugs.functional.srgb_to_lms(image: Tensor) Tensor[source]

Converts a standard RGB image -> linear RGB -> XYZ -> to CAT02 LMS.

LMS (long, medium, short), is a color space which represents the response of the three types of cones of the human eye, named for their responsivity (sensitivity) peaks at long, medium, and short wavelengths.

References

https://en.wikipedia.org/wiki/LMS_color_space

Parameters:

image (torch.Tensor) – sRGB Image to be converted to LMS with shape \((*, 3, H, W)\).

Returns:

LMS version of the image with shape \((*, 3, H, W)\).

Return type:

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = srgb_to_lms(input)  # 2x3x4x5
fovi.utils.fastaugs.functional.lms_to_lgrby(image: Tensor) Tensor[source]

Converts a standard LMS image to Luminance, R+,G+,B+,Y+ channels.

LMS (long, medium, short), is a color space which represents the response of the three types of cones of the human eye, named for their responsivity (sensitivity) peaks at long, medium, and short wavelengths.

References

https://en.wikipedia.org/wiki/LMS_color_space

Parameters:

image (torch.Tensor) – sRGB Image to be converted to LMS with shape \((*, 3, H, W)\).

Returns:

LMS version of the image with shape \((*, 3, H, W)\).

Return type:

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = srgb_to_lms(input)  # 2x3x4x5
fovi.utils.fastaugs.functional.crop(b, crop_height, crop_width, h_start, w_start)[source]

crop_height, crop_width in pixels, h_start and w_start in percentage

fovi.utils.fastaugs.functional.solarization(image, threshold)[source]
fovi.utils.fastaugs.functional.random_solarization(b: Tensor, idx, threshold)[source]

Solarize random set of images in batch b

fovi.utils.fastaugs.functional.random_solarization(b: numpy.ndarray, idx, threshold)[source]

Solarize random set of images in batch b

fovi.utils.fastaugs.functional.random_gaussian_blur2d(x, blur_indices, selected_kernels, kernels, padding_mode='symmetric')[source]
fovi.utils.fastaugs.functional.generate_batch_permutations(batch_size, N, rng=None)[source]

Generates a batch of permutations, each of size N, without using Python loops.

Parameters:
  • batch_size (int) – The number of permutations to generate.

  • N (int) – The size of each permutation.

Returns:

A (batch_size x N) tensor where each row is a random permutation of 0 to N-1.

Return type:

torch.Tensor

fovi.utils.fastaugs.functional.to_torch_image(b: ndarray, device='cpu', dtype=torch.float32, from_numpy=True)[source]

convert to torch image; assumes HxWxC

fovi.utils.fastaugs.functional.to_torch_image(b: torch.Tensor, device='cpu', dtype=torch.float32, **kwargs)[source]

convert to torch image; assumes HxWxC or BxHxWxC

fovi.utils.fastaugs.functional.to_torch_image(b, device='cpu', dtype=torch.float32, from_numpy=True)[source]