效果可视、操作变化范围可控、操作随机
源码网址:https://github.com/mdbloice/Augmentor
详情介绍:https://augmentor.readthedocs.io/en/master/code.html
Augmentor使用基于流水线的方法,其中按顺序添加操作以生成管道。然后,图像通过此管道传递,每个操作在图像通过时应用于图像。此外,根据每个操作的用户定义的概率值,Augmentor在通过管道时随机应用操作。
Augmentor允许创建扩充管道,该管道将随机应用的操作链接在一起,其中每个操作的参数也在用户指定的范围内随机选择。这意味着每次图像通过管道时,都会返回不同的图像。根据管道中的操作数量以及每个操作可用的值范围,可以以这种方式创建大量新图像数据。
附件1给出了所有的操作函数,有哪些操作可以做呢?
旋转、镜像、失真、放大、缩小、裁剪、直方图均衡化、倾斜、灰度图像、改变亮度、饱和度、对比度等等。
在命令窗口,输入命令pip install Augmentor,进行安装(windows和linux同样适用)。
#首先进入python环境,创建数据增强实例
import Augmentor#导入Augmentor模块
p = Augmentor.Pipeline("/home/user/augmentor_data_tests")#创建数据增强实例
#添加具体操作:
p.rotate90(probability=0.5)
p.rotate270(probability=0.5)
p.flip_left_right(probability=0.8)
p.flip_top_bottom(probability=0.3)
p.crop_random(probability=1, percentage_area=0.5)
p.resize(probability=1.0, width=120, height=120)
#添加数据增强类型:
p.sample(100)#随机选取待增强样本,生成100张样本
#p.process()#对待增强的每张样本进行增强
def rotate90(self, probability):
“”"
Rotate an image by 90 degrees.
The operation will rotate an image by 90 degrees, and will be performed with a probability of that specified by the **probability**
parameter.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
“”"
def rotate180(self, probability):
“”"
Rotate an image by 180 degrees.
The operation will rotate an image by 180 degrees, and will be performed with a probability of that specified by the **probability**
parameter.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
“”"
def rotate270(self, probability):
“”"
Rotate an image by 270 degrees.
The operation will rotate an image by 270 degrees, and will be performed with a probability of that specified by the **probability**
parameter.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
“”"
def rotate_random_90(self, probability):
“”"
Rotate an image by either 90, 180, or 270 degrees, selected randomly.
This function will rotate by either 90, 180, or 270 degrees. This is useful to avoid scenarios where images may be rotated back to their original positions (such as a :func:rotate90
and a :func:rotate270
being performed directly afterwards. The random rotation is chosen uniformly from 90, 180, or 270 degrees. The probability controls the chance of the operation being performed at all, and does not affect the rotation degree.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
“”"
def rotate(self, probability, max_left_rotation, max_right_rotation):
“”"
Rotate an image by an arbitrary amount.
The operation will rotate an image by an random amount, within a range specified. The parameters **max_left_rotation**
and **max_right_rotation**
allow you to control this range. If you wish to rotate the images by an exact number of degrees, set both **max_left_rotation**
and **max_right_rotation**
to the same value.
note: This function will rotate in place, and crop the largest possible rectangle from the rotated image.
In practice, angles larger than 25 degrees result in images that do not render correctly, therefore there is a limit of 25 degrees for this function.
If this function returns images that are not rendered correctly, then you must reduce the **max_left_rotation**
and **max_right_rotation**
arguments!
probability: A value between 0 and 1 representing the probability that the operation should be performed.
max_left_rotation: The maximum number of degrees the image can be rotated to the left.
max_right_rotation: The maximum number of degrees the image can be rotated to the right.
type probability: Float
type max_left_rotation: Integer
type max_right_rotation: Integer
“”"
def rotate_without_crop(self, probability, max_left_rotation, max_right_rotation, expand=False):
“”"
Rotate an image without automatically cropping.
The **expand**
parameter controls whether the image is enlarged to contain the new rotated images, or if the image size is maintained Defaults to false
so that images maintain their dimensions when using this function.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
max_left_rotation: The maximum number of degrees the image can be rotated to the left.
max_right_rotation: The maximum number of degrees the image can be rotated to the right.
type probability: Float
type max_left_rotation: Integer
type max_right_rotation: Integer
expand: Controls whether the image’s size should be increased to accommodate the rotation. Defaults to false
so that images maintain their original dimensions after rotation.
“”"
def flip_top_bottom(self, probability):
“”"
Flip (mirror) the image along its vertical axis, i.e. from top to bottom.
seealso:The : flip_left_right
function.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
type probability: Float
“”"
def flip_left_right(self, probability):
“”"
Flip (mirror) the image along its horizontal axis, i.e. from left to right.
seealso: The flip_top_bottom
function.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
type probability: Float
“”"
def flip_random(self, probability):
“”"
Flip (mirror) the image along either its horizontal or vertical axis.
This function mirrors the image along either the horizontal axis or the vertical access. The axis is selected randomly.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
type probability: Float
“”"
def random_distortion(self, probability, grid_width, grid_height, magnitude):
“”"
Performs a random, elastic distortion on an image.
This function performs a randomised, elastic distortion controlled by the parameters specified. The grid width and height controls how fine the distortions are. Smaller sizes will result in larger, more pronounced, and less granular distortions. Larger numbers will result in finer, more granular distortions. The magnitude of the distortions can be controlled using magnitude. This can be random or fixed.
Good values for parameters are between 2 and 10 for the grid width and height, with a magnitude of between 1 and 10. Using values outside of these approximate ranges may result in unpredictable behaviour.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
grid_width: The number of rectangles in the grid’s horizontal axis.
grid_height: The number of rectangles in the grid’s vertical axis.
magnitude: The magnitude of the distortions.
type probability: Float
type grid_width: Integer
type grid_height: Integer
type magnitude: Integer
“”"
def gaussian_distortion(self, probability, grid_width, grid_height, magnitude, corner, method, mex=0.5, mey=0.5, sdx=0.05, sdy=0.05):
“”"
Performs a random, elastic gaussian distortion on an image.
This function performs a randomised, elastic gaussian distortion controlled by the parameters specified. The grid width and height controls how fine the distortions are. Smaller sizes will result in larger, more pronounced, and less granular distortions. Larger numbers will result in finer, more granular distortions. The magnitude of the distortions can be controlled using magnitude. This can be random or fixed.
Good values for parameters are between 2 and 10 for the grid width and height, with a magnitude of between 1 and 10. Using values outside of these approximate ranges may result in unpredictable behaviour.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
grid_width: The number of rectangles in the grid’s horizontal axis.
grid_height: The number of rectangles in the grid’s vertical axis.
magnitude: The magnitude of the distortions.
corner: which corner of picture to distort. Possible values: “bell”(circular surface applied), “ul”(upper left), “ur”(upper right), “dl”(down left), “dr”(down right).
method: possible values: “in”(apply max magnitude to the chosen corner), “out”(inverse of method in).
mex: used to generate 3d surface for similar distortions. Surface is based on normal distribution.
mey: used to generate 3d surface for similar distortions. Surface is based on normal distribution.
sdx: used to generate 3d surface for similar distortions. Surface is based on normal distribution.
sdy: used to generate 3d surface for similar distortions. Surface is based on normal distribution.
type probability: Float
type grid_width: Integer
type grid_height: Integer
type magnitude: Integer
type corner: String
type method: String
type mex: Float
type mey: Float
type sdx: Float
type sdy: Float
For values : **mex**
, **mey**
, **sdx**
, and **sdy**
the surface is based on the normal distribution:
math:e^{- \Big( \frac{(x-\text{mex})^2}{\text{sdx}} + \frac{(y-\text{mey})^2}{\text{sdy}} \Big) }
“”"
def zoom(self, probability, min_factor, max_factor):
“”"
Zoom in to an image, while maintaining its size. The amount by which the image is zoomed is a randomly chosen value between**min_factor**
and**max_factor**
.
Typical values may be min_factor=1.1
and max_factor=1.5
.
To zoom by a constant amount, set **min_factor**
and **max_factor**
to the same value.
seealso: See :funczoom_random
for zooming into random areas of the image.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
min_factor: The minimum factor by which to zoom the image.
max_factor: The maximum factor by which to zoom the image.
type probability: Float
type min_factor: Float
type max_factor: Float
“”"
def zoom_random(self, probability, percentage_area, randomise_percentage_area=False):
“”"
Zooms into an image at a random location within the image.
You can randomise the zoom level by setting the**randomise_percentage_area**
argument to true.
seealso: See :func:zoom
for zooming into the centre of images.
probability: The probability that the function will execute when the image is passed through the pipeline.
percentage_area: The area, as a percentage of the current image’s area, to crop.
randomise_percentage_area: If True, will use **percentage_area**
as an upper bound and randomise the crop from between 0 and **percentage_area**
.
“”"
def scale(self, probability, scale_factor):
“”"
Scale (enlarge) an image, while maintaining its aspect ratio. This returns an image with larger dimensions than the original image.
Use :func:resize
to resize an image to absolute pixel values.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
scale_factor: The factor to scale by, which must be greater than 1.0.
type probability: Float
type scale_factor: Float
“”"
def resize(self, probability, width, height, resample_filter=“BICUBIC”):
“”"
Resize an image according to a set of dimensions specified by the user in pixels.
probability: A value between 0 and 1 representing the probability that the operation should be performed. For resizing, it is recommended that the probability be set to 1.
width: The new width that the image should be resized to.
height: The new height that the image should be resized to.
resample_filter: The resampling filter to use. Must be one of BICUBIC, BILINEAR, ANTIALIAS, or NEAREST.
type probability: Float
type width: Integer
type height: Integer
type resample_filter: String
“”"
def crop_by_size(self, probability, width, height, centre=True):
“”"
Crop an image according to a set of dimensions.
Crop each image according to **width**
and **height**
, by default at the centre of each image, otherwise at a random location within the image.
seealso: See :func:crop_random
to crop a random, non-centred area of the image.
If the crop area exceeds the size of the image, this function will crop the entire area of the image.
probability: The probability that the function will execute when the image is passed through the pipeline.
width: The width of the desired crop.
height: The height of the desired crop.
centre: If True, crops from the centre of the image, otherwise crops at a random location within the image, maintaining the dimensions specified.
type probability: Float
type width: Integer
type height: Integer
type centre: Boolean
“”"
def crop_centre(self, probability, percentage_area, randomise_percentage_area=False):
“”"
Crops the centre of an image as a percentage of the image’s area.
probability: The probability that the function will execute when the image is passed through the pipeline.
percentage_area: The area, as a percentage of the current image’s area, to crop.
randomise_percentage_area: If True, will use **percentage_area**
as an upper bound and randomise the crop from between 0 and **percentage_area**
.
type probability: Float
type percentage_area: Float
type randomise_percentage_area: Boolean
“”"
def crop_random(self, probability, percentage_area, randomise_percentage_area=False):
“”"
Crop a random area of an image, based on the percentage area to be returned.
This function crops a random area from an image, based on the area you specify using **percentage_area**
.
probability: The probability that the function will execute when the image is passed through the pipeline.
percentage_area: The area, as a percentage of the current image’s area, to crop.
randomise_percentage_area: If True, will use**percentage_area**
as an upper bound and randomise the crop from between 0 and **percentage_area**
.
type probability: Float
type percentage_area: Float
type randomise_percentage_area: Boolean
“”"
def shear(self, probability, max_shear_left, max_shear_right):
“”"
Shear the image by a specified number of degrees.
In practice, shear angles of more than 25 degrees can cause unpredictable behaviour. If you are observing images that are incorrectly rendered (e.g. they do not contain any information) then reduce the shear angles.
probability: The probability that the operation is performed.
max_shear_left: The max number of degrees to shear to the left. Cannot be larger than 25 degrees.
max_shear_right: The max number of degrees to shear to the right. Cannot be larger than 25 degrees.
“”"
def skew_left_right(self, probability, magnitude=1):
“”"
Skew an image by tilting it left or right by a random amount. The magnitude of this skew can be set to a maximum using the magnitude parameter. This can be either a scalar representing the
maximum tilt, or vector representing a range.
To see examples of the various skews, see :ref:perspectiveskewing
.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
magnitude: The maximum tilt, which must be value between 0.1 and 1.0, where 1 represents a tilt of 45 degrees.
type probability: Float
type magnitude: Float
“”"
def skew_top_bottom(self, probability, magnitude=1):
“”"
Skew an image by tilting it forwards or backwards by a random amount. The magnitude of this skew can be set to a maximum using the magnitude parameter. This can be either a scalar representing the maximum tilt, or vector representing a range.
To see examples of the various skews, see :ref:perspectiveskewing
.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
magnitude: The maximum tilt, which must be value between 0.1 and 1.0, where 1 represents a tilt of 45 degrees.
type probability: Float
type magnitude: Float
“”"
def skew_tilt(self, probability, magnitude=1):
“”"
Skew an image by tilting in a random direction, either forwards, backwards, left, or right, by a random amount. The magnitude of this skew can be set to a maximum using the magnitude parameter. This can be either a scalar representing the maximum tilt, or vector representing a range.
To see examples of the various skews, see :ref:perspectiveskewing
.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
magnitude: The maximum tilt, which must be value between 0.1 and 1.0, where 1 represents a tilt of 45 degrees.
type probability: Float
type magnitude: Float
“”"
def skew_corner(self, probability, magnitude=1):
“”"
Skew an image towards one corner, randomly by a random magnitude.
To see examples of the various skews, see :ref:perspectiveskewing
.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
magnitude: The maximum skew, which must be value between 0.1 and 1.0.
“”"
def skew(self, probability, magnitude=1):
“”"
Skew an image in a random direction, either left to right, top to bottom, or one of 8 corner directions.
To see examples of all the skew types, see :ref:perspectiveskewing
.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
magnitude: The maximum skew, which must be value between 0.1 and 1.0.
type probability: Float
type magnitude: Float
“”"
def histogram_equalisation(self, probability=1.0):
“”"
Apply histogram equalisation to the image.
probability: A value between 0 and 1 representing the probability that the operation should be performed. For histogram, equalisation it is recommended that the probability be set to 1.
type probability: Float
“”"
def greyscale(self, probability):
“”"
Convert images to greyscale. For this operation, setting the**probability**
to 1.0 is recommended.
seealso: The :func:black_and_white
function.
probability: A value between 0 and 1 representing the probability that the operation should be performed. For resizing, it is recommended that the probability be set to 1.
type probability: Float
“”"
def black_and_white(self, probability, threshold=128):
“”"
Convert images to black and white. In other words convert the image to use a 1-bit, binary palette. The threshold defaults to 128, but can be controlled using the **threshold**
parameter.
seealso: The :func:greyscale
function.
probability: A value between 0 and 1 representing the probability that the operation should be performed. For resizing, it is recommended that the probability be set to 1.
threshold: A value between 0 and 255 which controls the threshold point at which each pixel is converted to either black or white. Any values above this threshold are converted to white, and any values below this threshold are converted to black.
type probability: Float
type threshold: Integer
“”"
def invert(self, probability):
“”"
Invert an image. For this operation, setting the**probability**
to 1.0 is recommended.
warning:This function will cause errors if used on binary, 1-bit palette images (e.g. black and white).
probability: A value between 0 and 1 representing the probability that the operation should be performed. For resizing, it is recommended that the probability be set to 1.
“”"
def random_brightness(self,probability,min_factor,max_factor):
“”"
Random change brightness of an image.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image brightness. The value 0.0 gives a black image, value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
max_factor: A value should be bigger than min_factor that define the maximum adjustment of image brightness. The value 0.0 gives a black image, value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
“”"
def random_color(self,probability,min_factor,max_factor):
“”"
Random change saturation of an image.
probability: Controls the probability that the operation is performed when it is invoked in the pipeline.
min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image saturation. The value 0.0 gives a black and white image, value 1.0 gives the original image.
max_factor: A value should be bigger than min_factor that define the maximum adjustment of image saturation. The value 0.0 gives a black and white image, value 1.0 gives the original image.
“”"
def random_contrast(self,probability,min_factor,max_factor):
“”"
Random change image contrast.
probability: Controls the probability that the operation is performed when it is invoked in the pipeline.
min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image contrast. The value 0.0 gives s solid grey image, value 1.0 gives the original image.
max_factor: A value should be bigger than min_factor that define the maximum adjustment of image contrast. The value 0.0 gives s solid grey image, value 1.0 gives the original image.
“”"
def random_erasing(self, probability, rectangle_area):
“”"
Work in progress. This operation performs a Random Erasing operation, as described in https://arxiv.org/abs/1708.04896
_ by Zhong et al.
Its purpose is to make models robust to occlusion, by randomly replacing rectangular regions with random pixel values.
For greyscale images the random pixels values will also be greyscale, and for RGB images the random pixels values will be in RGB.
This operation is subject to change, the original work describes several ways of filling the random regions, including a random solid colour or greyscale value. Currently this operations uses the method which yielded the best results in the tests performed by Zhong et al.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
rectangle_area: The percentage area of the image to occlude with the random rectangle, between 0.1 and 1.
“”"
odels robust to occlusion, by randomly replacing rectangular regions with random pixel values.
For greyscale images the random pixels values will also be greyscale, and for RGB images the random pixels values will be in RGB.
This operation is subject to change, the original work describes several ways of filling the random regions, including a random solid colour or greyscale value. Currently this operations uses the method which yielded the best results in the tests performed by Zhong et al.
probability: A value between 0 and 1 representing the probability that the operation should be performed.
rectangle_area: The percentage area of the image to occlude with the random rectangle, between 0.1 and 1.
“”"