# tensorpack.dataflow.imgaug package¶

This package contains Tensorpack’s augmentors. Read the tutorial first for its design and general usage.

Note that other image augmentation libraries can be wrapped into Tensorpack’s interface as well. For example, imgaug.IAAugmentor and imgaug.Albumentations wrap two popular image augmentation libraries.

class tensorpack.dataflow.imgaug.ImageAugmentor[source]

Bases: object

Base class for an augmentor

ImageAugmentor should take images of type uint8 in range [0, 255], or floating point images in range [0, 1] or [0, 255].

rng

a numpy RandomState

augment(img)[source]

Create a transform, and apply it to augment the input image.

This can save you one line of code, when you only care the augmentation of “one image”. It will not return the Transform object to you so you won’t be able to apply the same transformation on other data associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. img – augmented image.
augment_coords(coords, param)[source]
augment_return_params(d)[source]
augment_with_params(d, param)[source]
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
reset_state()[source]

Reset rng and other state of the augmentor.

Similar to DataFlow.reset_state(), the caller of Augmentor is responsible for calling this method (once or more times) in the process that uses the augmentor before using it.

If you use a built-in augmentation dataflow (AugmentImageComponent, etc), this method will be called in the dataflow’s own reset_state method.

If you use Python≥3.7 on Unix, this method will be automatically called after fork, and you do not need to bother calling it.

class tensorpack.dataflow.imgaug.AugmentorList(augmentors)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Augment an image by a list of augmentors

__init__(augmentors)[source]
Parameters: augmentors (list) – list of ImageAugmentor instance to be applied.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
reset_state()[source]

Will reset state of each augmentor

class tensorpack.dataflow.imgaug.PhotometricAugmentor[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

A base class for ImageAugmentor which only affects pixels.

Subclass should implement _get_params(img) and _impl(img, params).

get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.ColorSpace(mode, keepdims=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Convert into another color space.

__init__(mode, keepdims=True)[source]
Parameters: mode – OpenCV color space conversion code (e.g., cv2.COLOR_BGR2HSV) keepdims (bool) – keep the dimension of image unchanged if OpenCV changes it.
class tensorpack.dataflow.imgaug.Grayscale(keepdims=True, rgb=False)[source]

Bases: tensorpack.dataflow.imgaug.convert.ColorSpace

Convert image to grayscale.

__init__(keepdims=True, rgb=False)[source]
Parameters: keepdims (bool) – return image of shape [H, W, 1] instead of [H, W] rgb (bool) – interpret input as RGB instead of the default BGR
class tensorpack.dataflow.imgaug.ToUint8[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Clip and convert image to uint8. Useful to reduce communication overhead.

class tensorpack.dataflow.imgaug.ToFloat32[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Convert image to float32, may increase quality of the augmentor.

class tensorpack.dataflow.imgaug.RandomCrop(crop_shape)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Randomly crop the image into a smaller one

__init__(crop_shape)[source]
Parameters: crop_shape – (h, w), int or a tuple of int
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.CenterCrop(crop_shape)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Crop the image at the center

__init__(crop_shape)[source]
Parameters: crop_shape – (h, w) tuple or a int
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.RandomCropRandomShape(wmin, hmin, wmax=None, hmax=None, max_aspect_ratio=None)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random crop with a random shape

__init__(wmin, hmin, wmax=None, hmax=None, max_aspect_ratio=None)[source]

Randomly crop a box of shape (h, w), sampled from [min, max] (both inclusive). If max is None, will use the input image shape.

Parameters: hmin, wmax, hmax (wmin,) – range to sample shape. max_aspect_ratio (float) – this argument has no effect and is deprecated.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.GoogleNetRandomCropAndResize(crop_area_fraction=(0.08, 1.0), aspect_ratio_range=(0.75, 1.333), target_shape=224, interp=cv2.INTER_LINEAR)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

The random crop and resize augmentation proposed in Sec. 6 of “Going Deeper with Convolutions” by Google. This implementation follows the details in fb.resnet.torch.

It attempts to crop a random rectangle with 8%~100% area of the original image, and keep the aspect ratio between 3/4 to 4/3. Then it resize this crop to the target shape. If such crop cannot be found in 10 iterations, it will do a ResizeShortestEdge + CenterCrop.

__init__(crop_area_fraction=(0.08, 1.0), aspect_ratio_range=(0.75, 1.333), target_shape=224, interp=cv2.INTER_LINEAR)[source]
Parameters: crop_area_fraction (tuple(float)) – Defaults to crop 8%-100% area. aspect_ratio_range (tuple(float)) – Defaults to make aspect ratio in 3/4-4/3. target_shape (int) – Defaults to 224, the standard ImageNet image shape.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.RandomCutout(h_range, w_range, fill=0.0)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

The cutout augmentation, as described in https://arxiv.org/abs/1708.04552

__init__(h_range, w_range, fill=0.0)[source]
Parameters: h_range (int or tuple) – the height of rectangle to cut. If a tuple, will randomly sample from this range [low, high) w_range (int or tuple) – similar to above fill (float) – the fill value
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.IAAugmentor(augmentor)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Wrap an augmentor form the IAA library: https://github.com/aleju/imgaug. Both images and coordinates are supported.

Note

1. It’s NOT RECOMMENDED to use coordinates because the IAA library does not handle coordinates accurately.

2. Only uint8 images are supported by the IAA library.

3. The IAA library can only produces images of the same shape.

Example:

from imgaug import augmenters as iaa  # this is the aleju/imgaug library
from tensorpack import imgaug  # this is not the aleju/imgaug library
# or from dataflow import imgaug  # if you're using the standalone version of dataflow
myaug = imgaug.IAAugmentor(
iaa.Sequential([
iaa.Sharpen(alpha=(0, 1), lightness=(0.75, 1.5)),
iaa.Fliplr(0.5),
iaa.Crop(px=(0, 100)),
])

__init__(augmentor)[source]
Parameters: augmentor (iaa.Augmenter) –
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Albumentations(augmentor)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Wrap an augmentor form the albumentations library: https://github.com/albu/albumentations. Coordinate augmentation is not supported by the library.

Example:

from tensorpack import imgaug
# or from dataflow import imgaug  # if you're using the standalone version of dataflow
import albumentations as AB
myaug = imgaug.Albumentations(AB.RandomRotate90(p=1))

__init__(augmentor)[source]
Parameters: augmentor (albumentations.BasicTransform) –
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Shift(horiz_frac=0, vert_frac=0, border=cv2.BORDER_REPLICATE, border_value=0)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random horizontal and vertical shifts

__init__(horiz_frac=0, vert_frac=0, border=cv2.BORDER_REPLICATE, border_value=0)[source]
Parameters: horiz_frac (float) – max abs fraction for horizontal shift vert_frac (float) – max abs fraction for horizontal shift border – cv2 border method border_value – cv2 border value for border=cv2.BORDER_CONSTANT
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Rotation(max_deg, center_range=(0, 1), interp=cv2.INTER_LINEAR, step_deg=None, border_value=0)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random rotate the image w.r.t a random center

__init__(max_deg, center_range=(0, 1), interp=cv2.INTER_LINEAR, step_deg=None, border_value=0)[source]
Parameters: max_deg (float) – max abs value of the rotation angle (in degree). center_range (tuple) – (min, max) range of the random rotation center. interp – cv2 interpolation method border – cv2 border method step_deg (float) – if not None, the stepping of the rotation angle. The rotation angle will be a multiple of step_deg. This option requires max_deg==180 and step_deg has to be a divisor of 180) border_value – cv2 border value for border=cv2.BORDER_CONSTANT
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.RotationAndCropValid(max_deg, interp=cv2.INTER_LINEAR, step_deg=None)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random rotate and then crop the largest possible rectangle. Note that this will produce images of different shapes.

__init__(max_deg, interp=cv2.INTER_LINEAR, step_deg=None)[source]
Parameters: interp, step_deg (max_deg,) – same as Rotation
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
static largest_rotated_rect(w, h, angle)[source]

Get largest rectangle after rotation. http://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders

class tensorpack.dataflow.imgaug.Affine(scale=None, translate_frac=None, rotate_max_deg=0.0, shear=0.0, interp=cv2.INTER_LINEAR, border_value=0)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random affine transform of the image w.r.t to the image center. Transformations involve:

• Translation (“move” image on the x-/y-axis)

• Rotation

• Scaling (“zoom” in/out)

• Shear (move one side of the image, turning a square into a trapezoid)

__init__(scale=None, translate_frac=None, rotate_max_deg=0.0, shear=0.0, interp=cv2.INTER_LINEAR, border_value=0)[source]
Parameters: scale (tuple of 2 floats) – scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b. Will keep original scale by default. translate_frac (tuple of 2 floats) – tuple of max abs fraction for horizontal and vertical translation. For example translate_frac=(a, b), then horizontal shift is randomly sampled in the range 0 < dx < img_width * a and vertical shift is randomly sampled in the range 0 < dy < img_height * b. Will not translate by default. shear (float) – max abs shear value in degrees between 0 to 180 interp – cv2 interpolation method border – cv2 border method border_value – cv2 border value for border=cv2.BORDER_CONSTANT
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Hue(range=(0, 180), rgb=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Randomly change color hue.

__init__(range=(0, 180), rgb=True)[source]
Parameters: range (list or tuple) – range from which the applied hue offset is selected (maximum range can be [-90,90] for both uint8 and float32) rgb (bool) – whether input is RGB or BGR.
class tensorpack.dataflow.imgaug.Brightness(delta, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

__init__(delta, clip=True)[source]
Parameters: delta (float) – Randomly add a value within [-delta,delta] clip (bool) – clip results to [0,255] even when data type is not uint8.
class tensorpack.dataflow.imgaug.BrightnessScale(range, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Adjust brightness by scaling by a random factor.

__init__(range, clip=True)[source]
Parameters: range (tuple) – Randomly scale the image by a factor in (range[0], range[1]) clip (bool) – clip results to [0,255] even when data type is not uint8.
class tensorpack.dataflow.imgaug.Contrast(factor_range, rgb=None, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Apply x = (x - mean) * contrast_factor + mean to each channel.

__init__(factor_range, rgb=None, clip=True)[source]
Parameters: factor_range (list or tuple) – an interval to randomly sample the contrast_factor. rgb (bool or None) – if None, use the mean per-channel. clip (bool) – clip to [0, 255] even when data type is not uint8.
class tensorpack.dataflow.imgaug.MeanVarianceNormalize(all_channel=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Linearly scales the image to have zero mean and unit norm. x = (x - mean) / adjusted_stddev where adjusted_stddev = max(stddev, 1.0/sqrt(num_pixels * channels))

This augmentor always returns float32 images.

__init__(all_channel=True)[source]
Parameters: all_channel (bool) – if True, normalize all channels together. else separately.
class tensorpack.dataflow.imgaug.GaussianBlur(max_size=3)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Gaussian blur the image with random window size

__init__(max_size=3)[source]
Parameters: max_size (int) – max possible Gaussian window size would be 2 * max_size + 1
class tensorpack.dataflow.imgaug.Gamma(range=(-0.5, 0.5))[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

__init__(range=(-0.5, 0.5))[source]
Parameters: range (list or tuple) – gamma range
class tensorpack.dataflow.imgaug.Clip(min=0, max=255)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Clip the pixel values

__init__(min=0, max=255)[source]
Parameters: max (min,) – the clip range
class tensorpack.dataflow.imgaug.Saturation(alpha=0.4, rgb=True, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Randomly adjust saturation. Follows the implementation in fb.resnet.torch.

__init__(alpha=0.4, rgb=True, clip=True)[source]
Parameters: alpha (float) – maximum saturation change. rgb (bool) – whether input is RGB or BGR. clip (bool) – clip results to [0,255] even when data type is not uint8.
class tensorpack.dataflow.imgaug.Lighting(std, eigval, eigvec, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Lighting noise, as in the paper ImageNet Classification with Deep Convolutional Neural Networks. The implementation follows fb.resnet.torch.

__init__(std, eigval, eigvec, clip=True)[source]
Parameters: std (float) – maximum standard deviation eigval – a vector of (3,). The eigenvalues of 3 channels. eigvec – a 3x3 matrix. Each column is one eigen vector. clip (bool) – clip results to [0,255] even when data type is not uint8.
class tensorpack.dataflow.imgaug.MinMaxNormalize(min=0, max=255, all_channel=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Linearly scales the image to the range [min, max].

This augmentor always returns float32 images.

__init__(min=0, max=255, all_channel=True)[source]
Parameters: max (float) – The new maximum value min (float) – The new minimum value all_channel (bool) – if True, normalize all channels together. else separately.
class tensorpack.dataflow.imgaug.RandomChooseAug(aug_lists)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Randomly choose one from a list of augmentors

__init__(aug_lists)[source]
Parameters: aug_lists (list) – list of augmentors, or list of (augmentor, probability) tuples
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
reset_state()[source]

Reset rng and other state of the augmentor.

Similar to DataFlow.reset_state(), the caller of Augmentor is responsible for calling this method (once or more times) in the process that uses the augmentor before using it.

If you use a built-in augmentation dataflow (AugmentImageComponent, etc), this method will be called in the dataflow’s own reset_state method.

If you use Python≥3.7 on Unix, this method will be automatically called after fork, and you do not need to bother calling it.

class tensorpack.dataflow.imgaug.MapImage(func, coord_func=None)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Map the image array by simple functions.

__init__(func, coord_func=None)[source]
Parameters: func – a function which takes an image array and return an augmented one coord_func – optional. A function which takes coordinates and return augmented ones. Coordinates should be Nx2 array of (x, y)s.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Identity[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

A no-op augmentor

get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.RandomApplyAug(aug, prob)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Randomly apply the augmentor with a probability. Otherwise do nothing

__init__(aug, prob)[source]
Parameters: aug (ImageAugmentor) – an augmentor. prob (float) – the probability to apply the augmentor.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
reset_state()[source]

Reset rng and other state of the augmentor.

Similar to DataFlow.reset_state(), the caller of Augmentor is responsible for calling this method (once or more times) in the process that uses the augmentor before using it.

If you use a built-in augmentation dataflow (AugmentImageComponent, etc), this method will be called in the dataflow’s own reset_state method.

If you use Python≥3.7 on Unix, this method will be automatically called after fork, and you do not need to bother calling it.

class tensorpack.dataflow.imgaug.RandomOrderAug(aug_lists)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Apply the augmentors with randomized order.

__init__(aug_lists)[source]
Parameters: aug_lists (list) – list of augmentors. The augmentors are assumed to not change the shape of images.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
reset_state()[source]

Reset rng and other state of the augmentor.

Similar to DataFlow.reset_state(), the caller of Augmentor is responsible for calling this method (once or more times) in the process that uses the augmentor before using it.

If you use a built-in augmentation dataflow (AugmentImageComponent, etc), this method will be called in the dataflow’s own reset_state method.

If you use Python≥3.7 on Unix, this method will be automatically called after fork, and you do not need to bother calling it.

class tensorpack.dataflow.imgaug.Flip(horiz=False, vert=False, prob=0.5)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random flip the image either horizontally or vertically.

__init__(horiz=False, vert=False, prob=0.5)[source]
Parameters: horiz (bool) – use horizontal flip. vert (bool) – use vertical flip. prob (float) – probability of flip.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Resize(shape, interp=cv2.INTER_LINEAR)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Resize image to a target size

__init__(shape, interp=cv2.INTER_LINEAR)[source]
Parameters: shape – (h, w) tuple or a int interp – cv2 interpolation method
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.RandomResize(xrange, yrange=None, minimum=(0, 0), aspect_ratio_thres=0.15, interp=cv2.INTER_LINEAR)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Randomly rescale width and height of the image.

__init__(xrange, yrange=None, minimum=(0, 0), aspect_ratio_thres=0.15, interp=cv2.INTER_LINEAR)[source]
Parameters: xrange (tuple) – a (min, max) tuple. If is floating point, the tuple defines the range of scaling ratio of new width, e.g. (0.9, 1.2). If is integer, the tuple defines the range of new width in pixels, e.g. (200, 350). yrange (tuple) – similar to xrange, but for height. Should be None when aspect_ratio_thres==0. minimum (tuple) – (xmin, ymin) in pixels. To avoid scaling down too much. aspect_ratio_thres (float) – discard samples which change aspect ratio larger than this threshold. Set to 0 to keep aspect ratio. interp – cv2 interpolation method
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.ResizeShortestEdge(size, interp=cv2.INTER_LINEAR)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Resize the shortest edge to a certain number while keeping the aspect ratio.

__init__(size, interp=cv2.INTER_LINEAR)[source]
Parameters: size (int) – the size to resize the shortest edge to.
get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Transpose(prob=0.5)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Random transpose the image

__init__(prob=0.5)[source]
Parameters: prob (float) – probability of transpose.
get_transform(_)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.JpegNoise(quality_range=(40, 100))[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Random JPEG noise.

__init__(quality_range=(40, 100))[source]
Parameters: quality_range (tuple) – range to sample JPEG quality
class tensorpack.dataflow.imgaug.GaussianNoise(sigma=1, clip=True)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Add random Gaussian noise N(0, sigma^2) of the same shape to img.

__init__(sigma=1, clip=True)[source]
Parameters: sigma (float) – stddev of the Gaussian distribution. clip (bool) – clip the result to [0,255] in the end.
class tensorpack.dataflow.imgaug.SaltPepperNoise(white_prob=0.05, black_prob=0.05)[source]

Bases: tensorpack.dataflow.imgaug.base.PhotometricAugmentor

Salt and pepper noise. Randomly set some elements in image to 0 or 255, regardless of its channels.

__init__(white_prob=0.05, black_prob=0.05)[source]
Parameters: white_prob (float), black_prob (float) – probabilities setting an element to 255 or 0.
class tensorpack.dataflow.imgaug.CenterPaste(background_shape, background_filler=None)[source]

Bases: tensorpack.dataflow.imgaug.base.ImageAugmentor

Paste the image onto the center of a background canvas.

__init__(background_shape, background_filler=None)[source]
Parameters: background_shape (tuple) – shape of the background canvas. background_filler (BackgroundFiller) – How to fill the background. Defaults to zero-filler.
get_transform(_)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.BackgroundFiller[source]

Bases: object

Base class for all BackgroundFiller

fill(background_shape, img)[source]

Return a proper background image of background_shape, given img.

Parameters: background_shape (tuple) – a shape (h, w) img – an image a background image
class tensorpack.dataflow.imgaug.ConstantBackgroundFiller(value)[source]

Bases: tensorpack.dataflow.imgaug.paste.BackgroundFiller

Fill the background by a constant

__init__(value)[source]
Parameters: value (float) – the value to fill the background.
class tensorpack.dataflow.imgaug.RandomPaste(background_shape, background_filler=None)[source]

Bases: tensorpack.dataflow.imgaug.paste.CenterPaste

Randomly paste the image onto a background canvas.

get_transform(img)[source]

Instantiate a Transform object to be used given the input image. Subclasses should implement this method.

The ImageAugmentor often has random policies which generate deterministic transform. Any of those random policies should happen inside this method and instantiate an actual deterministic transform to be performed. The returned Transform object should perform deterministic transforms through its apply_*() method.

In this way, the returned Transform object can be used to transform not only the input image, but other images or coordinates associated with the image.

Parameters: img (ndarray) – see notes of this class on the requirements. Transform
class tensorpack.dataflow.imgaug.Transform[source]

Bases: tensorpack.dataflow.imgaug.transform.BaseTransform

A deterministic image transformation, used to implement the (probably random) augmentors.

This class is also the place to provide a default implementation to any apply_xxx() method. The current default is to raise NotImplementedError in any such methods.

All subclasses should implement apply_image. The image should be of type uint8 in range [0, 255], or floating point images in range [0, 1] or [0, 255]

Some subclasses may implement apply_coords, when applicable. It should take and return a numpy array of Nx2, where each row is the (x, y) coordinate.

The implementation of each method may choose to modify its input data in-place for efficient transformation.

class tensorpack.dataflow.imgaug.ResizeTransform(h, w, new_h, new_w, interp)[source]

Bases: tensorpack.dataflow.imgaug.transform.Transform

Resize the image.

__init__(h, w, new_h, new_w, interp)[source]
Parameters: w (h,) – new_w (new_h,) – interp (int) – cv2 interpolation method
apply_coords(coords)[source]
apply_image(img)[source]
class tensorpack.dataflow.imgaug.CropTransform(y0, x0, h, w)[source]

Bases: tensorpack.dataflow.imgaug.transform.Transform

Crop a subimage from an image.

apply_coords(coords)[source]
apply_image(img)[source]
class tensorpack.dataflow.imgaug.FlipTransform(h, w, horiz=True)[source]

Bases: tensorpack.dataflow.imgaug.transform.Transform

Flip the image.

__init__(h, w, horiz=True)[source]
Parameters: w (h,) – horiz (bool) – whether to flip horizontally or vertically.
apply_coords(coords)[source]
apply_image(img)[source]
class tensorpack.dataflow.imgaug.TransformList(tfms)[source]

Bases: tensorpack.dataflow.imgaug.transform.BaseTransform

Apply a list of transforms sequentially.

__init__(tfms)[source]
Parameters: tfms (list[Transform]) –
class tensorpack.dataflow.imgaug.TransformFactory(name=None, **kwargs)[source]

Bases: tensorpack.dataflow.imgaug.transform.Transform

Create a Transform from user-provided functions.

__init__(name=None, **kwargs)[source]
Parameters: name (str, optional) – the name of this transform **kwargs – mapping from ‘apply_xxx’ to implementation of such functions.