Image Transforms¶
-
normalize_01(img)[source]¶ Normalize img to [0, 1].
- Parameters
img – Tensor with pixel values in [0, 255].
- Returns
Tensor withl pixel values normalized to [0, 1].
-
normalize_11(img)[source]¶ Normalize img to [-1, 1].
- Parameters
img – Tensor with pixel values in [0, 255].
- Returns
Tensor with pixel values normalized to [-1, 1].
-
denormalize_11(img)[source]¶ Denormalize img from [-1, 1] to [0, 255].
- Parameters
img – Tensor with pixel values in [-1, 1].
- Returns
Tensor with pixel values in [0, 255].
-
crop_naive(img_tensor, num_crops, patch_dims, random_seed=None)[source]¶ - Crops a number of patches with specified dimensions from input tensor containing elements of tensorflow.image.Cropping is considered naive because there is no check to verify the diversity of cropped patches. This means you will sometimes end up with uniform and “uninteresting” batches of cropped patches (e.g. only blue patches cropped from the background sky of an image).See simple_sr.utils.image.image_transforms.crop_divers for a different approach.Input tensor is not mutated.
- Parameters
img_tensor –
Tensor containing elements of tensorflow.image.May be of rank 3 or rank 4num_crops –
Number of crops to obtain per element.patch_dims –
List or tuple with Dimensions (including channels) of cropped patches.e.g. to crop 64x64 Patches from an RGB image patch_dims should be (64, 64, 3).random_seed –
random seed to use for locating and cropping patches, if None cropping will be random.
Warning
Only set the seed for debugging since every cropped patch will be the same.
- Returns
Tensor containing patches cropped from input tensor.
-
crop_divers(img, num_crops, patch_dims, min_variation_patch, min_variation_batch, max_trys_patch=100, max_trys_batch=20, random_seed=None)[source]¶ - Crops a number of patches with specified dimensions from input tensor containing elements of tensorflow.image, while trying to maintain a minimum diversity inside cropped patches and across batches of patches.
Warning
This is experimental, performance (in regards to training time) is likely to suffer. Some experimentation is needed to find a good balance of efficiency and diversity in patches.
- Parameters
img – A tensorflow.image tensor containing the image to crop patches from.
num_crops – Number of patches to crop.
patch_dims –
Dimensions of patches to crop.Channels need to be specified as well, e.g. for 64x64 patches from RGB image => (64, 64, 3).min_variation_patch – Minimum variation between pixel values of cropped patch for patch to be accepted into batch.
min_variation_batch – Minimum variation between pixel values across batch of cropped patches for batch to be accepted.
max_trys_patch –
Number of trys to crop patches above min_variation_patch threshold.After number of trys is exhausted every patch will be accepted.max_trys_batch –
Number of trys to assemble batch of cropped patches above min_variation_batch threshold.After number of trys is exhausted every batch will be accepted.random_seed –
Random seed for debuggin purposes, every cropped patch will be the same for a single image.
- Returns
A tensor containing the specified number of cropped patches
-
rotate90(img_tensor, rotations=None)[source]¶ - Perform a number of 90 degree rotations on a tensorflow.image tensor.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
rotations –
Number of rotations to perform.If None number of rotations will be randomly sampled from [1, 3].
- Returns
Tensor containing rotated versions of input tensor.
-
adjust_hue(img_tensor, delta_range=None)[source]¶ - Adjust hue value of a tensor containing elements of tensorflow.image.The adjusting factor is randomly sampled from the interval specified by delta_range.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
delta_range –
List or tuple specifying lower and upper bounds for sampling interval of adjustment delta.If no range is supplied the interval defaults to [-0.07, 0.07].
- Returns
Tensor containing hue adjusted versions of input tensor.
-
adjust_saturation(img_tensor, factor_range=None)[source]¶ - Adjust saturation value of a tensor containing elements of tensorflow.image.The adjusting factor is randomly sampled from the interval specified by factor_range.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
factor_range –
List or tuple specifying lower and upper bounds for sampling interval of adjustment factor.If no range is supplied the interval defaults to [0.4, 2.0].
- Returns
Tensor containing saturation adjusted versions of input tensor.
-
adjust_brightness(img_tensor, delta_range=None)[source]¶ - Adjust brightness value of a tensor containing elements of tensorflow.image.The adjusting factor is randomly sampled from the interval specified by delta_range.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
delta_range –
List or tuple specifying lower and upper bounds for sampling interval of adjustment delta.If no range is supplied the interval defaults to [0.05, 0.2].
- Returns
Tensor containing brightness adjusted versions of input tensor.
-
adjust_contrast(img_tensor, factor_range=None)[source]¶ - Adjust contrast value of a tensor containing elements of tensorflow.image.The adjusting factor is randomly sampled from the interval specified by factor_range.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
factor_range –
List or tuple specifying lower and upper bounds for sampling interval of adjustment factor.If no range is supplied the interval defaults to [0.5, 2.5].
- Returns
Tensor containing contrast adjusted versions of input tensor.
-
adjust_gamma(img_tensor, factor_range=None)[source]¶ - Adjust gamma value of a tensor containing elements of tensorflow.image.The adjusting factor is randomly sampled from the interval specified by factor_range.Tensor may be of rank 3 or rank 4.Input tensor is not mutated.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
factor_range –
List or tuple specifying lower and upper bounds for sampling interval of adjustment factor.If no range is supplied the interval defaults to [0.5, 1.5].
- Returns
Tensor containing gamma adjusted versions of input tensor.
-
adjust_jpg_quality(img, quality_range=None)[source]¶ - Adds jpg noise to a tensorflow.image tensor. Tensor needs to be of rank 3.The noise level that will be applied to the input tensor is randomly sampled in the interval specified by quality_range.Input tensor is not mutated.
- Parameters
img – A tensorflow.image.
quality_range –
List or tuple for lower and upper bounds for randomly sampling jpg degradation level.Lower values mean more degradation/less jpg quality.If no quality range is supplied interval defaults to [10, 50]
- Returns
A tensorflow.image with jpg degradation
-
flip_along_x(img_tensor)[source]¶ - Flip elements of a tensor along its x-axis. Tensor may be of rank 3 or rank 4.Does not mutate the input tensor.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
- Returns
Tensor containing flipped versions of input tensor.
-
flip_along_y(img_tensor)[source]¶ - Flip elements of a tensor along its y-axis. Tensor may be of rank 3 or rank 4.Does not mutate the input tensor.
- Parameters
img_tensor – Tensor containing elements of tensorflow.image.
- Returns
Tensor containing flipped versions of input tensor.
-
resize(img_tensor, resize_dims, resize_filter='bilinear', antialias=True)[source]¶ - Method to resize tensorflow.image tensors to supplied dimensions.Does not mutate the input image.
- Parameters
img_tensor – tensorflow.image tensor to be resized. May be of rank 3 or rank 4.
resize_dims – A tuple of (height, width) to specify the dimensions the input image should be resized to.
resize_filter – A tensorflow.image.ResizeMethod to use for resizing the input image.
antialias – Whether to use antialiasing during resize operation.
- Returns
A tensor containing resized versions of input tensor.
-
augment_img(img, augmentations=None, return_as_tf_dataset=False)[source]¶ Convenience method to create multiple augmented versions of an image.
- Parameters
img – tensorflow.image tensor to create augmented versions of.
augmentations – A list of augmentation functions.
return_as_tf_dataset – Whether to return the augmented images as a tf.dataset or a python list.
- Returns
A collection of tensorflow.image tensors containing the augmented images either inside a list or a tf.dataset depending return_as_tf_dataset parameter.