My Project
Functions | Variables
transformations Namespace Reference

Functions

def int_image (float_image)
 Image type cast ##. More...
 
def float_image (int_image)
 
def rand_floats (size, mag, maxval, minval=None)
 Parameters utils ##. More...
 
def invScale_rand_floats (size, mag, maxval, minval)
 
def zero_stack (tensor, zero_pos)
 
def float_parameter (level, maxval)
 
def flipLR (x)
 Tranformations ##. More...
 
def flipUD (x)
 
def rotate (x, angle)
 
def translate (x, translation)
 
def shear (x, shear)
 
def contrast (x, contrast_factor)
 
def color (x, color_factor)
 
def brightness (x, brightness_factor)
 
def sharpeness (x, sharpness_factor)
 
def posterize (x, bits)
 
def solarize (x, thresholds)
 
def blend (x, y, alpha)
 
def auto_contrast (x)
 
def equalize (x)
 

Variables

dictionary TF_no_mag = {'Identity', 'FlipUD', 'FlipLR', 'Random', 'RandBlend'}
 
dictionary TF_no_grad = {'Solarize', 'Posterize', '=Solarize', '=Posterize'}
 
dictionary TF_ignore_mag = TF_no_mag | TF_no_grad
 
int PARAMETER_MAX = 1
 
float PARAMETER_MIN = 0.1
 
dictionary TF_dict
 

Detailed Description

PyTorch implementation of some PIL image transformations.

    Those implementation are thinked to take advantages of batched computation of PyTorch on GPU.

    Based on Kornia library.
    See: https://github.com/kornia/kornia

    And PIL.
    See: 
https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py
https://github.com/python-pillow/Pillow/blob/9c78c3f97291bd681bc8637922d6a2fa9415916c/src/PIL/Image.py#L2818

    Inspired from AutoAugment.
    See: https://github.com/tensorflow/models/blob/fc2056bce6ab17eabdc139061fef8f4f2ee763ec/research/autoaugment/augmentation_transforms.py

Function Documentation

◆ auto_contrast()

def transformations.auto_contrast (   x)
NOT TESTED - EXTRA SLOW

◆ blend()

def transformations.blend (   x,
  y,
  alpha 
)
Creates a new images by interpolating between two input images, using a constant alpha.
    
    x and y should have the same size.
    alpha should have the same batch size as the images.

    Apply batch wise :
        out = image1 * (1.0 - alpha) + image2 * alpha

Args:
    x (Tensor): Batch of images.
    y (Tensor): Batch of images.
    alpha (Tensor):  The interpolation alpha factor for each images.
Returns:
    (Tensor): Batch of solarized images.

◆ brightness()

def transformations.brightness (   x,
  brightness_factor 
)
Adjust brightness of images.

Args:
    x (Tensor): Batch of images.
    brightness_factor (Tensor): Brightness factor for each images. 
    0.0 gives a black image. A factor of 1.0 gives the original image.

Returns:
    (Tensor): Batch of adjusted images.

◆ color()

def transformations.color (   x,
  color_factor 
)
Adjust color of images.

Args:
    x (Tensor): Batch of images.
    color_factor (Tensor): Color factor for each images. 
    0.0 gives a black and white image. A factor of 1.0 gives the original image.

Returns:
    (Tensor): Batch of adjusted images.

◆ contrast()

def transformations.contrast (   x,
  contrast_factor 
)
Adjust contast of images.

Args:
    x (FloatTensor): Batch of images.
    contrast_factor (FloatTensor): Contrast adjust factor per element in the batch. 
    0 generates a compleatly black image, 1 does not modify the input image while any other non-negative number modify the brightness by this factor.

Returns:
    (Tensor): Batch of adjusted images.

◆ equalize()

def transformations.equalize (   x)
NOT WORKING

◆ flipLR()

def transformations.flipLR (   x)

Tranformations ##.

Flip horizontaly/Left-Right images.
    
    Args:
        x (Tensor): Batch of images.

    Returns: 
        (Tensor): Batch of fliped images.

◆ flipUD()

def transformations.flipUD (   x)
Flip vertically/Up-Down images.
    
    Args:
        x (Tensor): Batch of images.

    Returns: 
        (Tensor): Batch of fliped images.

◆ float_image()

def transformations.float_image (   int_image)
Convert a int Tensor/Image to an float Tensor/Image.

    Args:
        int_image (ByteTensor): Image tensor.

    Returns:
        (FloatTensor) Converted tensor.

◆ float_parameter()

def transformations.float_parameter (   level,
  maxval 
)
Scale level between 0 and maxval.

    Args:
        level (float): Level of the operation that will be between [PARAMETER_MIN, PARAMETER_MAX].
        maxval: Maximum value that the operation can have. This will be scaled to level/PARAMETER_MAX.
    Returns:
        A float that results from scaling `maxval` according to `level`.

◆ int_image()

def transformations.int_image (   float_image)

Image type cast ##.

Convert a float Tensor/Image to an int Tensor/Image.

Be warry that this transformation isn't bijective, each conversion will result in small loss of information.
Granularity: 1/256 = 0.0039.

This will also result in the loss of the gradient associated to input as gradient cannot be tracked on int Tensor.

Args:
    float_image (FloatTensor): Image tensor.

Returns:
    (ByteTensor) Converted tensor.

◆ invScale_rand_floats()

def transformations.invScale_rand_floats (   size,
  mag,
  maxval,
  minval 
)
Generate a batch of random values.

    Similar to rand_floats() except that the mag is used in an inversed scale.

    Mag:[0,PARAMETER_MAX] => [PARAMETER_MAX, 0]

    Args:
        size (int): Number of value to generate.
        mag (float): Level of the operation that will be between [PARAMETER_MIN, PARAMETER_MAX].
        maxval (float): Maximum value that can be generated. This will be scaled to mag/PARAMETER_MAX.
        minval (float): Minimum value that can be generated. (default: -maxval)

    Returns:
        (Tensor) Generated batch of float values between [minval, maxval].

◆ posterize()

def transformations.posterize (   x,
  bits 
)
Reduce the number of bits for each color channel.

    Be warry that the cast to integers block the gradient propagation.
Args:
    x (Tensor): Batch of images.
    bits (Tensor): The number of bits to keep for each channel (1-8).

Returns:
    (Tensor): Batch of posterized images.

◆ rand_floats()

def transformations.rand_floats (   size,
  mag,
  maxval,
  minval = None 
)

Parameters utils ##.

Generate a batch of random values.

    Args:
        size (int): Number of value to generate.
        mag (float): Level of the operation that will be between [PARAMETER_MIN, PARAMETER_MAX].
        maxval (float): Maximum value that can be generated. This will be scaled to mag/PARAMETER_MAX.
        minval (float): Minimum value that can be generated. (default: -maxval)

    Returns:
        (Tensor) Generated batch of float values between [minval, maxval].

◆ rotate()

def transformations.rotate (   x,
  angle 
)
Rotate images.

    Args:
        x (Tensor): Batch of images.
        angle (Tensor): Angles (degrees) of rotation for each images.

    Returns:
        (Tensor): Batch of rotated images.

◆ sharpeness()

def transformations.sharpeness (   x,
  sharpness_factor 
)
Adjust sharpness of images.

Args:
    x (Tensor): Batch of images.
    sharpness_factor (Tensor): Sharpness factor for each images. 
    0.0 gives a black image. A factor of 1.0 gives the original image.

Returns:
    (Tensor): Batch of adjusted images.

◆ shear()

def transformations.shear (   x,
  shear 
)
Shear images.

Args:
    x (Tensor): Batch of images.
    shear (Tensor): Angle of shear for each images.

Returns:
    (Tensor): Batch of skewed images.

◆ solarize()

def transformations.solarize (   x,
  thresholds 
)
Invert all pixel values above a threshold.

    Be warry that the use of the inequality (x>tresholds) block the gradient propagation.
Args:
    x (Tensor): Batch of images.
    thresholds (Tensor):  All pixels above this level are inverted

Returns:
    (Tensor): Batch of solarized images.

◆ translate()

def transformations.translate (   x,
  translation 
)
Translate images.

    Args:
        x (Tensor): Batch of images.
        translation (Tensor): Distance (pixels) of translation for each images.

    Returns:
        (Tensor): Batch of translated images.

◆ zero_stack()

def transformations.zero_stack (   tensor,
  zero_pos 
)
Add a row of zeros to a Tensor.

    This function is intended to be used with single row Tensor, thus returning a 2 dimension Tensor.

    Args:
        tensor (Tensor): Tensor to be stacked with zeros.
        zero_pos (int): Wheter the zeros should be added before or after the Tensor. Either 0 or 1.

    Returns:
        Stacked Tensor.