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.
- Returns
img – augmented image.
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
class
tensorpack.dataflow.imgaug.
ColorSpace
(mode, keepdims=True)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Convert into another color space.
-
class
tensorpack.dataflow.imgaug.
Grayscale
(keepdims=True, rgb=False, keepshape=False)[source]¶ Bases:
tensorpack.dataflow.imgaug.convert.ColorSpace
Convert RGB or BGR image to grayscale.
-
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
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
class
tensorpack.dataflow.imgaug.
CenterCrop
(crop_shape)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.ImageAugmentor
Crop the image at the center
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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]¶
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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
It’s NOT RECOMMENDED to use coordinates because the IAA library does not handle coordinates accurately.
Only uint8 images are supported by the IAA library.
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)), ])
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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))
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
class
tensorpack.dataflow.imgaug.
Hue
(range=0, 180, rgb=True)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Randomly change color hue.
-
class
tensorpack.dataflow.imgaug.
Brightness
(delta, clip=True)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Adjust brightness by adding a random number.
-
class
tensorpack.dataflow.imgaug.
BrightnessScale
(range, clip=True)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Adjust brightness by scaling by a random factor.
-
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.
-
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
whereadjusted_stddev = max(stddev, 1.0/sqrt(num_pixels * channels))
This augmentor always returns float32 images.
-
class
tensorpack.dataflow.imgaug.
GaussianBlur
(size_range=0, 3, sigma_range=0, 0, symmetric=True, max_size=None)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Gaussian blur the image with random window size
-
__init__
(size_range=0, 3, sigma_range=0, 0, symmetric=True, max_size=None)[source]¶ - Parameters
size_range (tuple[int]) – Gaussian window size would be 2 * size + 1, where size is randomly sampled from this [low, high) range.
sigma_range (tuple[float]) – min,max of the sigma value. 0 means opencv’s default.
symmetric (bool) – whether to use the same size & sigma for x and y.
max_size (int) – deprecated
-
-
class
tensorpack.dataflow.imgaug.
Gamma
(range=- 0.5, 0.5)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Randomly adjust gamma
-
class
tensorpack.dataflow.imgaug.
Clip
(min=0, max=255)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Clip the pixel values
-
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.
-
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.
-
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.
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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.
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
class
tensorpack.dataflow.imgaug.
Transpose
(prob=0.5)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.ImageAugmentor
Random transpose the image
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
class
tensorpack.dataflow.imgaug.
JpegNoise
(quality_range=40, 100)[source]¶ Bases:
tensorpack.dataflow.imgaug.base.PhotometricAugmentor
Random JPEG noise.
-
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.
-
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.
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
Transform
-
-
class
tensorpack.dataflow.imgaug.
BackgroundFiller
[source]¶ Bases:
object
Base class for all BackgroundFiller
-
class
tensorpack.dataflow.imgaug.
ConstantBackgroundFiller
(value)[source]¶ Bases:
tensorpack.dataflow.imgaug.paste.BackgroundFiller
Fill the background by a constant
-
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 returnedTransform
object should perform deterministic transforms through itsapply_*()
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.
- Returns
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.
-
class
tensorpack.dataflow.imgaug.
CropTransform
(y0, x0, h, w)[source]¶ Bases:
tensorpack.dataflow.imgaug.transform.Transform
Crop a subimage from an image.
-
class
tensorpack.dataflow.imgaug.
FlipTransform
(h, w, horiz=True)[source]¶ Bases:
tensorpack.dataflow.imgaug.transform.Transform
Flip the image.
-
class
tensorpack.dataflow.imgaug.
TransformList
(tfms)[source]¶ Bases:
tensorpack.dataflow.imgaug.transform.BaseTransform
Apply a list of transforms sequentially.