Augmentation layers and models
Implementations of layers for augmenting images
Augmentation
Bases: Layer
Base augmentation class
Used to create Random Augmentation layers
Methods:
random_execute: returns boolean variable based on given probability. Determines whether the layer should activate on given batch augment_single_image: abstract method. Should be implemented by child classes. Distorts a single image random_augment_single_image: randomly augment an image with given probability call: run the layer on a batch of images
Source code in conftrainer/augmentation/layers.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
random_execute(probability)
staticmethod
Return boolean variable based on given probability
Generate random float from 0 to 1 and return whether it's smaller than given float
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probability |
probability to return True |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
bool
|
whether generated value is smaller than given prob |
Raises:
| Type | Description |
|---|---|
NotImplementedError: if the child class doesn't implement this method
|
|
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Abstract method for augmenting a single image.
Should be implemented by child classes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image |
image to distort |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.Tensor
|
distorted image |
Source code in conftrainer/augmentation/layers.py
random_augment_single_image(image)
Randomly augment a single image with given probabilty
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image |
image to augment |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.Tensor
|
|
Source code in conftrainer/augmentation/layers.py
call(inputs, training)
Randomly run the layer on given inputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs |
batch of input images |
required | |
training |
whether to run the layer in training or inference mode. The layer is inactive in inference mode and returns the inputs as is |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
tf.Tensor
|
distorted version of images with given probability, original ones otherwise |
Source code in conftrainer/augmentation/layers.py
RandomColorJitter
Bases: Augmentation
Randomly Jitter a batch of images by adjusting brightness, contrast, saturation and hue
Parameters:
prob: float probability of layer to activate brightness: float upper limit of a random brightness shift. Lower is always 0 contrast: float, list of float shift of contrast. If a list of float is given, min value of that list serves as the lower boundary of contrast shift and the max value as the upper boundary. If a single float is given, lower and upper boundaries are determined as 1-contrast and 1+contrast saturation: float, list of float shift of saturation. If a list of float is given, min value of that list serves as the lower boundary of saturation shift and the second value as the upper boundary. If a single float is given, lower and upper boundaries are determined as 1-saturation and 1+saturation hue: float upper limit of random hue shift. Lower is always 0
Methods:
call: run the layer on given batch of images
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Randomly adjust the brightness, contrast, saturation and hue of an image
Source code in conftrainer/augmentation/layers.py
RandomZoomCrop
Bases: Augmentation
Randomly zoom and crop an image
Parameters:
prob: float the probability of layer to activate min_area: float minimal ratio of area of the original image to leave after cropping shape: tuple of int input/output shape of the images
Methods:
call: run the layer on a given batch of images
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Generate a random crop window, crop the image and resize it to given shape
Source code in conftrainer/augmentation/layers.py
RandomSolarize
Bases: Augmentation
Randomly solarize a batch of images, inverting pixels with low values.
Expects the inputs to be in range [0,255]
Parameters:
prob: float the probability of layer to activate threshold: int upper bound of pixels to invert
Methods:
call: run the layer on a given batch of images
Notes:
When paired with RandomInvert, the pixels of high values will be inverted instead
Source code in conftrainer/augmentation/layers.py
RandomInvert
Bases: Augmentation
Randomly invert pixel values of a batch of images
Expects the inputs to be in range [0,255]
Parameters:
prob: float the probability of layer to activate
Methods:
call: run the layer on a given batch of images
Notes:
When paired with RandomSolarize, only the part of the pixels is inverted
Source code in conftrainer/augmentation/layers.py
RandomBlur
Bases: Augmentation
Randomly blur a batch of images by applying a 2d Gaussian filter with a random sigma. For more info about the filter and its arguments, see tensorflow_addons.image.gaussian_filter2d
Parameters:
factor: float the probability of layer to activate filter_shape: tuple of int shape of the blur filter to apply. If tuple with more than 2 items is passed, only the first 2 number will be used sigma_low: float, default: 0.1 lower boundary of random sigma parameter to use when blurring sigma_high: float, default: 2 upper boundary of random sigma parameter to use when blurring
Methods:
call: run the layer on a given batch of images
Source code in conftrainer/augmentation/layers.py
get_random_transformation()
Create gaussian kernels to augment the image
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Apply gaussian blurring to single image
Source code in conftrainer/augmentation/layers.py
get_kernel(sigma, filter_size)
staticmethod
Create a gaussian kernel with given sigma and shape
Source code in conftrainer/augmentation/layers.py
RandomGrayscale
Bases: Augmentation
Randomly turn a batch of images to Grayscale
Parameters:
prob: float the probability of layer to activate
Methods:
call: run the layer on a given batch of images
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Turn an image to grayscale
RandomSepia
Bases: Augmentation
Randomly turn images to sepia color scheme
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor |
probability of the layer to activate |
required |
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Turn a single image to sepia
Source code in conftrainer/augmentation/layers.py
RandomChannelShuffle
Bases: Augmentation
Randomly Shuffle the order of channels of RGB images
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor |
probability of layer to activate on each call |
required |
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Shuffle the order of channels of a single image
RandomChannelShift
Bases: Augmentation
Randomly shift one or multiple channel's of RGB images. Expects the pixel values to be in range [0, 255]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor |
probability of the layer to activate |
required | |
channels |
list of indices of channels to shift |
None
|
|
delta |
maximal magnitude of distortions |
0
|
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Shift pixel values of given channels of a single image by random values
Source code in conftrainer/augmentation/layers.py
RandomAdjustJpegQuality
Bases: Augmentation
Randomly compress the Jpeg quality of a batch of images.
The pixel values of images are expected to be in range [0, 255]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor |
probability of the layer to activate |
required | |
min_quality |
positive integer < 100, minimal quality of image ofter distortion |
25
|
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Randomly adjust jpeg quality of a single image
RandomPixelDropout
Bases: Augmentation
Randomly turn pixel values of an image to 0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor |
probability of the layer to activate |
required | |
dropout_probability |
probability of each pixel value to be turned to 0 |
0.001
|
Source code in conftrainer/augmentation/layers.py
augment_single_image(image)
Randomly dropout pixel values of a single image
Source code in conftrainer/augmentation/layers.py
RandomHorizontalFlip
Bases: Augmentation
Randomly flip images horizontally
Source code in conftrainer/augmentation/layers.py
RandomVerticalFlip
Bases: Augmentation
Randomly flip images vertically