17、Python 图像数据增强工具:imgaug 和 albumentations

一、imgaug 安装和卸载

  • 安装: pip install imgaug and pip install imagecorruptions and pip install image_augment
  • 卸载:pip uninstall imgaug
  • 使用:from imgaug import augmenters as iaa
  • TODO:iaa 基本不更新了,考虑后续换成这一个,https://github.com/albumentations-team/albumentations,安装 pip install -U albumentations
    在这里插入图片描述

二、augmenters.meta

  • 主要包含的类有:
    • Augmenter (base class for all augmenters)、Sequential、SomeOf、OneOf、Sometimes
    • WithChannels、ChannelShuffle、Lambda、Identity
    • AssertLambda、AssertShape
  • Augmenter 类的主要方法有:
    • augment_batches, augment_batch, augment_image, augment_images
    • augment_bounding_boxes、augment_keypoints、augment_heatmaps、augment_segmentation_maps、augment_polygons、augment_line_strings
    • augment、pool、__call__
  • __call__ 中调用了 augment 方法,因此所有的基类都可以都可以当做函数一样来调用

1、iaa.Sequential()

iaa.Sequential(children=None,
               random_order=False)

# 参数解释: List augmenter containing child augmenters to apply to inputs
children: 
    想要应用在图像上的 Augmenter 或者 Augmenter 列表
random_order: 
	是否对每个 batch 的图片应用不同顺序的 Augmenter list;当设置为 True 时,不同 batch 之间图片的处理顺序都会不一样,
	但是同一个 batch 内顺序相同

>>> seq = iaa.Sequential([
>>>     iaa.Fliplr(0.5),
>>>     iaa.Flipud(0.5)
>>> ], random_order=True)
>>> imgs_aug = seq.augment_images(images=imgs)
>>> imgs_aug = seq(images=imgs)  # 简化使用版本,__call__ 中调用了 augment 方法,所有的基类都可以都可以当做函数一样来调用

2、iaa.SomeOf()

iaa.SomeOf(n=None,
		   children=None,
           random_order=False)

# 参数解释:List augmenter that applies only some of its children to inputs
n: 从总的 Augmenter list 中选择多少个,可以是一个 int, tupleint, 每个 batch 随机选择 n 个 Augmenter 来处理图片
    tuple,每个 batch 随机从(a, b)中选择 n 个 Augmenter 来处理图片,a 最小为 0,b 最大为 None(代表所有)
children: 
    想要应用在图像上的 Augmenter 或者 Augmenter 列表
random_order: 
	是否对每个 batch 的图片应用不同顺序的 Augmenter list;当设置为 True 时,不同 batch 之间图片的处理顺序都会不一样,
	但是同一个 batch 内顺序相同

3、iaa.OneOf()

iaa.OneOf(children=None)

# 参数解释:Augmenter that always executes exactly one of its children
children: 
    想要应用在图像上的 Augmenter 或者 Augmenter 列表

4、iaa.Sometimes()

iaa.Sometimes(p=0.5,
              then_list=None,
              else_list=None)

# 参数解释:Apply child augmenter(s) with a probability of p
p: 一个 batch 中多大比例的图片会使用 then_list 中的增强器进行增强
then_list: 增强器列表,一个 batch 中 p 概率的图片会使用
else_list: 增强器列表,一个 batch 中 1-p 概率的图片会使用

>>> import imgaug.augmenters as iaa
>>> aug = iaa.Sometimes(0.5, iaa.GaussianBlur(0.3))  # Apply GaussianBlur to 50% of all input images.

# Apply GaussianBlur to 50% of all input images. Apply Fliplr to the other 50% of all input images.
>>> aug = iaa.Sometimes(0.5, iaa.GaussianBlur(0.3), iaa.Fliplr(1.0))

5、iaa.WithChannels()

iaa.WithChannels(channels=None,
                 children=None)

# 参数解释:Apply child augmenters to specific channels
channels: 
    int 或者 int list,哪些 channel 要被用来变换
children: 
    要执行的变换

>>> import imgaug.augmenters as iaa
>>> aug = iaa.WithChannels([0], iaa.Add(10))  # For RGB Image, It will make images appear more red

6、iaa.ChannelShuffle()

iaa.ChannelShuffle(p=1.0, 
				   channels=None)

# 参数解释:
p:Probability of shuffling channels in any given image
channels: 
    None(all channels may be shuffled) or list of int(only the channels with indices in that list may be shuffled)
    
>>> import imgaug.augmenters as iaa
>>> aug = iaa.ChannelShuffle(0.35)  # Shuffle all channels of 35% of all images
>>> aug = iaa.ChannelShuffle(0.35, channels=[0, 1]) # Shuffle only channels 0 and 1 of 35% of all images. 

7、iaa.Lambda()

# 自定义数据增强器
iaa.Lambda(func_images=None, func_heatmaps=None,
           func_segmentation_maps=None, func_keypoints=None,
           func_bounding_boxes="keypoints", func_polygons="keypoints",
           func_line_strings="keypoints")

>>> import imgaug.augmenters as iaa
>>>
>>> def func_images(images, random_state, parents, hooks):
>>>     """Replace every second row in input images with black pixels"""
>>>     images[:, ::2, :, :] = 0
>>>     return images
>>>
>>> aug = iaa.Lambda(
>>>     func_images=func_images
>>> )

8、iaa.Identity()

aug_identity = iaa.Identity()  # Create an augmenter that does not change inputs

三、augmenters.arithmetic

  • 主要包含的类有:
    • Add、AddElementwise、Multiply、MultiplyElementwise、JpegCompression、Invert、Solarize(Invert 的特例)
    • AdditiveGaussianNoise、AdditiveLaplaceNoise、AdditivePoissonNoise、ImpulseNoise、SaltAndPepper、CoarseSaltAndPepper、Salt、CoarseSalt、Pepper、CoarsePepper
    • Cutout、Dropout、CoarseDropout、Dropout2d、TotalDropout、ReplaceElementwise

1、iaa.Add()/iaa.Multiply()

iaa.Add(value=(-20, 20), 
        per_channel=False)

# 参数解释:Add a value to all pixels in an image
value:每张图片每个像素加上相同的数值,可以是 int, int tuple, list
per_channel:每个通道是否使用相同的 value


# This augmenter can be used to make images lighter or darker
iaa.Multiply(mul=(0.8, 1.2), per_channel=False)  

# 参数解释:Multiply all pixels in an image with a random value sampled once per image
mul:每张图片每个像素乘上的数值,可以是 int, int tuple, list
per_channel:每个通道是否使用相同的 mul

>>> aug = iaa.Add((-10, 10), per_channel=0.5)

2、iaa.AddElementwise()/iaa.MultiplyElementwise()

iaa.AddElementwise(value=(-20, 20), 
                   per_channel=False)

# 参数解释:Add to the pixels of images values that are pixelwise randomly sampled
value:每张图片每个像素加上的数值(可不同),可以是 int, int tuple, list
per_channel:每个通道是否使用相同的 value


# This augmenter can be used to make images lighter or darker
iaa.MultiplyElementwise(mul=(0.8, 1.2), per_channel=False)  

# 参数解释:Multiply image pixels with values that are pixelwise randomly sampled
mul:每张图片每个像素乘上的数值(可不同),可以是 int, int tuple, list
per_channel:每个通道是否使用相同的 mul

>>> aug = iaa.AddElementwise((-10, 10), per_channel=True)

3、iaa.JpegCompression()

iaa.JpegCompression(compression=(0, 100))

# 参数解释:Degrade the quality of images by JPEG-compressing them
compression:压缩等级,范围为 0~100,越大表示压缩的越厉害,可以是 int, int tuple, list

>>> aug = iaa.JpegCompression(compression=(70, 99))

4、iaa.Invert()

iaa.Invert(p=1, 
           per_channel=False, 
           min_value=None, 
           max_value=None,
           threshold=None, 
           invert_above_threshold=0.5)

# 参数解释:Invert all values in images,sets a pixel from value v to 255-v
p:所有图片(img batch)反色的比例,float
per_channel:每个通道是否使用相同的 p
min_value: Minimum of the value range of input images
max_value: Maximum of the value range of input images
threshold: A threshold to use in order to invert only numbers above or below the threshold
invert_above_threshold:
	1、If True, only values>=threshold will be inverted,Otherwise, only values<threshold will be inverted.
    2、If a number, then expected to be in the interval [0.0, 1.0] and denoting an imagewise probability

>>> aug = iaa.Invert(0.1, per_channel=0.5)

5、iaa.AdditiveGaussianNoise()

iaa.AdditiveGaussianNoise(loc=0, 
                          scale=(0, 15), 
                          per_channel=False)

# 参数解释:Add noise sampled from gaussian distributions elementwise to images
loc/scale:均值和标准差,可以是 int/float, int/float tuple, list
per_channel:每个通道是否使用相同的值

>>> aug = iaa.AdditiveGaussianNoise(scale=0.1*255, per_channel=True)

四、augmenters.imgcorruptlike/augmenters.color/augmenters.blur/augmenters.whether

  • augmenters.imgcorruptlike 主要包含的类有:
    • GaussianNoise、ShotNoise、ImpulseNoise(脉冲)、SpeckleNoise(斑点)
    • GaussianBlur、MotionBlur、DefocusBlur(虚焦)、ZoomBlur、GlassBlur
    • Fog(雾)、Frost(霜)、Snow、Spatter(溅、洒)
    • Contrast、Brightness、Saturate
    • ElasticTransform、JpegCompression、Pixelate
    • Note:Expected the provided image to have a width and height of at least 32 pixels, as that is the lower limit that the wrapped imagecorruptions functions use
  • augmenters.color 主要包含的类有:
    • WithColorspace、ChangeColorspace、Grayscale、WithBrightnessChannels、MultiplyAndAddToBrightness、MultiplyBrightness、AddToBrightness
    • WithHueAndSaturation、MultiplyHueAndSaturation、MultiplyHue、MultiplySaturation、RemoveSaturation、
    • AddToHueAndSaturation、AddToHue、AddToSaturation、ChangeColorTemperature、KMeansColorQuantization、UniformColorQuantization、Posterize
  • augmenters.blur 主要包含的类有:
    • MotionBlur、GaussianBlur、AverageBlur、MedianBlur、BilateralBlur、MeanShiftBlur
  • augmenters.whether 主要包含的类有:
    • FastSnowyLandscape、CloudLayer、Clouds、Fog、SnowflakesLayer、Snowflakes、RainLayer、Rain

1、iaa.imgcorruptlike.GaussianNoise()

iaa.imgcorruptlike.GaussianNoise(severity=(1, 5))

# 参数解释:
severity : strength of the corruption
    int, 可选,默认为 1~5

# eg:
iaa.imgcorruptlike.GaussianNoise(severity=2)
iaa.imgcorruptlike.ShotNoise(severity=2)
iaa.imgcorruptlike.ImpulseNoise(severity=2)
iaa.imgcorruptlike.SpeckleNoise(severity=2)

2、iaa.imgcorruptlike.GaussianBlur()

iaa.imgcorruptlike.GaussianBlur(severity=(1, 5))

# 参数解释:
severity : strength of the corruption
    int, 可选,默认为 1~5

# eg:
iaa.imgcorruptlike.MotionBlur(severity=2)
iaa.imgcorruptlike.DefocusBlur(severity=2)
iaa.imgcorruptlike.ZoomBlur(severity=2)
iaa.imgcorruptlike.GlassBlur(severity=2)

3、iaa.imgcorruptlike.Fog()

iaa.imgcorruptlike.Fog(severity=(1, 5))

# 参数解释:
severity : strength of the corruption
    int, 可选,默认为 1~5

# eg:
iaa.imgcorruptlike.Fog(severity=2)
iaa.imgcorruptlike.Frost(severity=2)
iaa.imgcorruptlike.Snow(severity=2)
iaa.imgcorruptlike.Spatter(severity=2)

4、iaa.imgcorruptlike.Contrast()

iaa.imgcorruptlike.Contrast(severity=(1, 5))

# 参数解释:
severity : strength of the corruption
    int, 可选,默认为 1~5

# eg:
iaa.imgcorruptlike.Contrast(severity=2)
iaa.imgcorruptlike.Brightness(severity=2)
iaa.imgcorruptlike.Saturate(severity=2)

5、iaa.imgcorruptlike.ElasticTransform()

iaa.imgcorruptlike.ElasticTransform(severity=(1, 5))

# 参数解释:
severity : strength of the corruption
    int, 可选,默认为 1~5

# eg:
iaa.imgcorruptlike.ElasticTransform(severity=2)
iaa.imgcorruptlike.JpegCompression(severity=2)
iaa.imgcorruptlike.Pixelate(severity=2)

6、iaa.WithColorspace()

iaa.WithColorspace(to_colorspace,
                   from_colorspace='RGB',
                   children=None)

# 参数解释:
to_colorspace:
    要变换的颜色空间,可为 RGB, BGR, HSV, YUV, GRAY, CIE, YCrCb,HLS, Lab, Luv
from_colorspace: 
    原来的颜色空间, 默认 RGB
children: 
    要执行的变换

# 先将图片从 RGB 变换到 HSV,然后将 v 值增加 10,然后再变换回 RGB
aug = iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                         children=iaa.WithChannels([2], iaa.Add(10)))

7、iaa.ChangeColorspace()

iaa.ChangeColorspace(to_colorspace,
                     from_colorspace='RGB',
                     alpha=1.0)

# 参数解释:Augmenter to change the colorspace of images
to_colorspace:
    要变换的颜色空间,可为 RGB, BGR, HSV, GRAY, CIE, YCrCb,HLS, Lab, Luv
from_colorspace: 
    原来的颜色空间, 默认 RGB
alpha: 
    The alpha value of the new colorspace when overlayed over the old one

8、iaa.Grayscale()

iaa.Grayscale(alpha=1, 
              from_colorspace=CSPACE_RGB)

# 参数解释:Augmenter to convert images to their grayscale versions
from_colorspace: 
    原来的颜色空间, 默认 RGB
alpha: 
    The alpha value of the grayscale image when overlayed over the old image

五、augmenters.geometric

  • 主要包含的类有:
    • Affine(scale、translate、rotate、shear)、PerspectiveTransform、ElasticTransformation(PiecewiseAffine)
    • Rot90、WithPolarWarping、Jigsaw

1、iaa.Affine()

iaa.Affine(scale=1.0, 
           translate_percent=None, 
           translate_px=None, 
           rotate=0.0, 
           shear=0.0,
           order=1, 
           cval=0, 
           mode="constant",
           fit_output=False, 
           backend="auto")        # backend="cv2"

# 参数解释:Augmenter to apply affine transformations to images
scale: 图像缩放因子,默认不缩放,可以是 float, float tuple, list, dict
    1、如果是 float, 则所有图片按照这种比例缩放
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同
    4、如果是 dict{“x”:(1 or 2 or 3), “y”:(1 or 2 or 3)},每个 x 或 y 的值都可以是上面 1,2,3 中的任意一个,此时 x-axis 和 y-axis 的缩放比例不一样
    5、eg: aug = iaa.Affine(scale=2.0)
    
translate_percent: 平移比例,默认不平移,可以是 float, float tuple, list, dict
	1、具体含义与 scale 相同,用正负来表示平移方向(正表示往右平移,负表示往左平移)
	2、平移后空出来的像素默认使用 0 来填充(可以通过 cval 和 mode 来设置)
	3、eg:aug = iaa.Affine(translate_percent=0.1)
    
translate_px: 按照像素来进行平移,可以是 int, int tuple, list, dict
    1、具体含义与 translate_percent 相同(除了参数类型换成 int)
    2、eg:aug = iaa.Affine(translate_px={"x": (-16, 16), "y": (-4, 4)})
    
rotate: 旋转角度,可以为 int, int tuple, list
    1、具体含义与 translate_percent 相同(除了不支持 dict2、旋转角度在 -360~360 度之间,用正负来表示旋转方向(正表示顺时针旋转,负表示往逆时针旋转)
    3、旋转是围绕者图像中心点来做的
    4、eg:aug = iaa.Affine(rotate=(-6, 6))
    
shear: 错切的程度,可以为 int, int tuple, list, dict
    1、具体含义与 translate_percent 相同(除了参数类型换成 int)
    2、错切角度在 -360~360 度之间,用正负来表示错切方向
    3、eg: aug = iaa.Affine(shear={"y": (-45, 45)})
    
order: 插值顺序,默认为双线性插值,可以为 int, list, imgaug.ALL
    1、插值方式有:最邻近插值(0),双线性插值(默认 1),双二次插值(不推荐 2),双三次插值(3),Bi-quartic(特别慢 4),Bi-quintic(特别慢 5)
    2、如果是 int,则所有图片运用相同的插值方式
    3、如果是 list,则每张图片会从列表中随机选取一种插值方式
    4、如果是 imgaug.ALL,则每次从所有的插值方法中随机选取一种插值方式,backend=skimage 可取 [0, 1, 3, 4, 5], backend=cv2 可取 [0, 1, 3]
    5、eg:aug = iaa.Affine(scale=2.0, order=[0, 1])
    
cval: 指定填充的常量值,只有在 mode=constant 的时候才会生效,可以是 int, float, tuple, list, imgaug.ALL
    1、如果是 float/int, 则所有图片按照此值进行填充
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取三个值(3 channel)进行填充 
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行填充
    4、如果是 imgaug.ALL, 则会从 [0,255] 之间随机选取一个值进行填充
    5、eg:aug = iaa.Affine(translate_px=16, cval=(0, 255))
    
mode: 采用何种方式填充经过变换后空白的像素点,可以是 string, string list, imgaug.ALL
    1、string 有:constant(采用一个常量填充)、edge(采用边缘的像素值填充)、symmetric、reflect、wrap
    2、如果是 string, 则所有图片按照 string 代表的方式进行填充
    3、如果是 string list, 则每张图片会从列表中随机选取一种方式进行填充 
    4、如果是 imgaug.ALL, 则会从所有可能的方式中随机选取一种方式进行填充 
    5、eg:aug = iaa.Affine(translate_px=16, mode=["constant", "edge"])
    
fit_output: 是否保留全图,默认为 False
	1、Whether to modify the affine transformation so that the whole output image is always contained in the image plane (True) or accept parts of the image being outside the image plane (False)
	2、This can be thought of as first applying the affine transformation and then applying a second transformation to "zoom in" on the new image so that it fits the image plane

backend: auto or skimage or cv2

2、iaa.PerspectiveTransform()

iaa.PerspectiveTransform(scale=(0.0, 0.06), 
                         cval=0, 
                         mode="constant",
                         keep_size=True, 
                         fit_output=False, 
                         polygon_recoverer="auto")     

# 参数解释:Apply random four point perspective transformations to images
scale: 图像缩放因子,默认不缩放,可以是 float, float tuple, list, dict
    1、如果是 float, 则所有图片按照这种比例缩放
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同
    4、如果是 dict{“x”:(1 or 2 or 3), “y”:(1 or 2 or 3)},每个 x 或 y 的值都可以是上面 1,2,3 中的任意一个,此时 x-axis 和 y-axis 的缩放比例不一样
       
cval: 指定填充的常量值,只有在 mode=constant 的时候才会生效,可以是 int, float, tuple, list, imgaug.ALL
    1、如果是 float/int, 则所有图片按照此值进行填充
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取三个值(3 channel)进行填充 
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行填充
    4、如果是 imgaug.ALL, 则会从 [0,255] 之间随机选取一个值进行填充
    
mode: 采用何种方式填充经过变换后空白的像素点,可以是 string, string list, imgaug.ALL
    1、string 有:constant(采用一个常量填充)、edge(采用边缘的像素值填充)、symmetric、reflect、wrap
    2、如果是 string, 则所有图片按照 string 代表的方式进行填充
    3、如果是 string list, 则每张图片会从列表中随机选取一种方式进行填充 
    4、如果是 imgaug.ALL, 则会从所有可能的方式中随机选取一种方式进行填充 
    5、eg:aug = iaa.Affine(translate_px=16, mode=["constant", "edge"])

keep_size: 是否 resize 回原图大小,默认为 True

fit_output: 是否保留全图,默认为 False
	1、Whether to modify the affine transformation so that the whole output image is always contained in the image plane (True) or accept parts of the image being outside the image plane (False)
	2、This can be thought of as first applying the affine transformation and then applying a second transformation to "zoom in" on the new image so that it fits the image plane

>>> eg:aug = iaa.PerspectiveTransform(scale=(0.01, 0.15), keep_size=False)

3、iaa.ElasticTransformation()

iaa.ElasticTransformation(alpha=(0.0, 40.0), 
                          sigma=(4.0, 8.0),   # 10:1 seems to be good for alpha and sigma
                          order=3, 
                          cval=0,
                          mode="constant",
                          polygon_recoverer="auto")     

# 参数解释:Transform images by moving pixels locally around using displacement fields
alpha: 控制位移的强度,较高的值表示像素相对于畸变场方向移动的多
    1、如果是 float, 则所有图片按照这种比例来移动
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行缩放, 此时 x-axis 和 y-axis 的缩放比例相同

sigma: 控制位移的平滑度,较高的值会导致图片更平滑,较低的值会导致一些像素点和周围的非常不同,从而导致噪点和像素化的图像
    1、如果是 float, 则所有图片按照这种比例进行平滑操作
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取一个值来进行平滑操作
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行平滑操作

order: 插值顺序,默认为双线性插值,可以为 int, list, imgaug.ALL
    1、插值方式有:最邻近插值(0),双线性插值(默认 1),双二次插值(不推荐 2),双三次插值(3),Bi-quartic(特别慢 4),Bi-quintic(特别慢 5)
    2、如果是 int,则所有图片运用相同的插值方式
    3、如果是 list,则每张图片会从列表中随机选取一种插值方式
    4、如果是 imgaug.ALL,则每次从所有的插值方法中随机选取一种插值方式,backend=skimage 可取 [0, 1, 3, 4, 5], backend=cv2 可取 [0, 1, 3]
    5、eg:aug = iaa.Affine(scale=2.0, order=[0, 1])
    
cval: 指定填充的常量值,只有在 mode=constant 的时候才会生效,可以是 int, float, tuple, list, imgaug.ALL
    1、如果是 float/int, 则所有图片按照此值进行填充
    2、如果是 float tuple(a, b), 则每张图片会从均匀分布中随机选取三个值(3 channel)进行填充 
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行填充
    4、如果是 imgaug.ALL, 则会从 [0,255] 之间随机选取一个值进行填充
    
mode: 采用何种方式填充经过变换后空白的像素点,可以是 string, string list, imgaug.ALL
    1、string 有:constant(采用一个常量填充)、edge(采用边缘的像素值填充)、symmetric、reflect、wrap
    2、如果是 string, 则所有图片按照 string 代表的方式进行填充
    3、如果是 string list, 则每张图片会从列表中随机选取一种方式进行填充 
    4、如果是 imgaug.ALL, 则会从所有可能的方式中随机选取一种方式进行填充 
    5、eg:aug = iaa.Affine(translate_px=16, mode=["constant", "edge"])

六、augmenters.size/augmenters.flip

  • augmenters.size 主要包含的类有:
    • Resize、CropAndPad(Crop、Pad)、PadToFixedSize(CenterPadToFixedSize)、CropToFixedSize(CenterCropToFixedSize)
    • CropToMultiplesOf、CenterCropToMultiplesOf、PadToMultiplesOf、CenterPadToMultiplesOf、CropToPowersOf、CenterCropToPowersOf
    • PadToPowersOf、CenterPadToPowersOf、CropToAspectRatio、CenterCropToAspectRatio、PadToAspectRatio、CenterPadToAspectRatio
    • CropToSquare、CenterCropToSquare、PadToSquare、CenterPadToSquare、KeepSizeByResize
  • augmenters.flip 主要包含的类有:
    • Fliplr、Flipud

1、iaa.Resize()

iaa.Resize(size, 
           interpolation="cubic")       -

# 参数解释:Augmenter that resizes images to specified heights and widths
scale: 图像缩放因子,默认不缩放,可以是 int/float, int/float tuple, int/float list, dict
    1、如果是 int, 则此值会作为所有图片新的宽和高
    2、如果是 float, 则所有图片按照这种比例(W*v,H*v)进行缩放
    3、如果是 int/float tuple(a, b), 则每张图片会从均匀分布中随机选取一个值(int)或比例(float)来进行缩放
    4、如果是 int/float list[a, b], 则每张图片会从列表中随机选取一个值(int)或比例(float)来进行缩放
    5、如果是 dict{“height/width/shorter-side/longer-side”:(1 or 2 or 3 or 4)},
       每个参数的值都可以是上面 1,2,3 中的任意一个, 且均有 keep-aspect-ratio 参数

interpolation: 图像插值方式,默认为 cubic,可以为 string, list, imgaug.ALL
    1、插值方式有:nearest、linear、area、cubic
    2、如果是 string,则所有图片运用相同的插值方式
    3、如果是 list,则每张图片会从列表中随机选取一种插值方式
    4、如果是 imgaug.ALL,则每次从所有的插值方法中随机选取一种插值方式
	

# eg:    
aug = iaa.Resize(32, interpolation=["linear", "cubic"])
aug = iaa.Resize((0.5, 0.75))
aug = iaa.Resize([16, 32, 64])
aug = iaa.Resize({"height": 32})  # width will not change
aug = iaa.Resize({"height": 32, "width": 48})
aug = iaa.Resize({"height": 32, "width": "keep-aspect-ratio"})
aug = iaa.Resize({"shorter-side": 224, "longer-side": "keep-aspect-ratio"})  # min_side resize 
aug = iaa.Resize({"height": (0.5, 0.75), "width": [16, 32, 64]})

2、iaa.CropAndPad()

iaa.CropAndPad(px=None, 
               percent=None, 
               pad_mode="constant", 
               pad_cval=0,
               keep_size=True, 
               sample_independently=True) 

# 参数解释:Crop/pad images by pixel amounts or fractions of image sizes 
px: 按照像素来进行 crop/pad,可以是 int, int tuple,注意与 percent 不能同时存在
	1、若为负数则进行 crop 操作,若为正数则进行 pad 操作
    2、如果是 int, 则此值会作用于所有图片进行 crop/pad
    3、如果是 int tuple(a, b), 则每张图片会从均匀分布中随机选取一个值进行 crop/pad,
       如果是一个 4 个元素的 tuple, 那么 4 个元素分别代表 top, right, bottom, left(顺时针)
       每个元素可以是 int 或者 int tuple

percent: 按照比例来进行 crop/pad,可以是 float, float tuple
		 1、具体含义与 px 相同, 注意与 px 不能同时存在
    
pad_mode: 采用何种方式填充经过变换后空白的像素点,可以是 string, string list, imgaug.ALL
    1、填充方式有: constant, edge, linear_ramp, maximum, median, minimum, reflect, symmetric, wrap
    2、如果是 string, 则所有图片按照 string 代表的方式进行填充
    3、如果是 string list, 则每张图片会从列表中随机选取一种方式进行填充 
    4、如果是 imgaug.ALL, 则会从所有可能的方式中随机选取一种方式进行填充 

pad_cval: 指定填充的常量值,只有在 mode=constant 的时候才会生效,可以是 int, float, tuple, list, imgaug.ALL
    1、如果是 float/int, 则所有图片按照此值进行填充
    2、如果是 float/int tuple(a, b), 则每张图片会从均匀分布中随机选取三个值(3 channel)进行填充 
    3、如果是 list[a1, a2, a3...], 则每张图片会从列表中随机选取一个值来进行填充
    4、如果是 imgaug.ALL, 则会从 [0,255] 之间随机选取一个值进行填充
    5、eg:aug = iaa.Affine(translate_px=16, cval=(0, 255)) 

keep_size: 
	1、经过 crop 后,图像大小会改变,默认在 crop 或者 pad 后再缩放到原来的大小
	2、若不想缩放回原来的大小,可以设置为 False
	
sample_independently:
	1、默认 True,四个方位的值独立采样;如果设置为 False,则每次选出来的值会同时作用在四个方位

# eg:
aug = iaa.CropAndPad(px=(-10, 0))
aug = iaa.CropAndPad(px=(0, 10))
aug = iaa.CropAndPad(percent=(0, 0.1))
aug = iaa.CropAndPad(px=(0, 10), pad_mode=ia.ALL, pad_cval=(0, 255))
aug = iaa.CropAndPad(px=(0, 10), sample_independently=False)  # sampled value is used for all sides 
aug = iaa.CropAndPad(px=(0, 10), keep_size=False)
aug = iaa.CropAndPad(px=((0, 10), (0, 5), (0, 10), (0, 5)))  # top, right, bottom, left, 顺时针
aug = iaa.CropAndPad(px=(-10, 10))  # either crop (negative sample) or pad (positive sample)

3、iaa.CropToFixedSize()/iaa.PadToFixedSize()

iaa.CropToFixedSize(width, 
                    height, 
                    position="uniform") 

# 参数解释:如果图片的宽或高已经小于指定的宽和高,则此边将不进行 crop(Note:图像可能有小于此尺寸的)
width/height: int or None
	1、如果是 int, 则所有图片按照此值进行 crop
	2、如果是 None, 则此边将不进行 crop

position:
	1、支持的类型有:uniform, normal, center, left-top, left-center, left-bottom, center-top, 
	              center-center, center-bottom, right-top, right-center, right-bottom
	2、如果是 string, 则所有图片按照 string 代表的方式进行 crop
	

# eg: 等价于 iaa.CenterCropToFixedSize(width=100, height=100)
aug = iaa.CropToFixedSize(width=100, height=100, position="center") 



iaa.PadToFixedSize(width, 
                   height, 
                   pad_mode="constant", 
                   pad_cval=0,
                   position="uniform") 

# 参数解释:如果图片的宽或高已经大于指定的宽和高,则此边将不进行 pad(Note:图像可能有大于此尺寸的)
width/height: int or None
	1、如果是 int, 则所有图片按照此值进行 pad
	2、如果是 None, 则此边将不进行 pad

position:
	1、支持的类型有:uniform, normal, center, left-top, left-center, left-bottom, center-top, 
	              center-center, center-bottom, right-top, right-center, right-bottom
	2、如果是 string, 则所有图片按照 string 代表的方式进行 pad
	

# eg:
aug = iaa.PadToFixedSize(width=100, height=100, pad_mode=ia.ALL)

4、iaa.CropToAspectRatio()/iaa.PadToAspectRatio()

iaa.CropToAspectRatio(aspect_ratio, 
                      position="uniform") 

# 参数解释:Crop images until their width/height matches an aspect ratio
aspect_ratio: 宽高比 (width/height),若为 1.0,则等同于 iaa.CropToSquare()

position:
	1、支持的类型有:uniform, normal, center, left-top, left-center, left-bottom, center-top, 
	              center-center, center-bottom, right-top, right-center, right-bottom
	2、如果是 string, 则所有图片按照 string 代表的方式进行 crop
	

# eg:
aug = iaa.CropToAspectRatio(2.0)



iaa.PadToAspectRatio(aspect_ratio, 
                     pad_mode="constant", 
                     pad_cval=0,
                     position="uniform") 

# 参数解释:Pad images until their width/height matches an aspect ratio
aspect_ratio: 宽高比 (width/height),若为 1.0,则等同于 iaa.PadToSquare()

position:
	1、支持的类型有:uniform, normal, center, left-top, left-center, left-bottom, center-top, 
	              center-center, center-bottom, right-top, right-center, right-bottom
	2、如果是 string, 则所有图片按照 string 代表的方式进行 pad
	

# eg:
aug = iaa.PadToAspectRatio(2.0)

5、iaa.Fliplr()/iaa.Flipud()

iaa.Fliplr(p=1) # 左右翻转,p 为 翻转的概率
iaa.Flipud(p=1) # 上下翻转,p 为 翻转的概率

七、augmenters.convolutional、augmenters.pooling、augmenters.edges

  • augmenters.convolutional 主要包含的类有:
    • Convolve、Sharpen、Emboss、EdgeDetect、DirectedEdgeDetect
  • augmenters.pooling 主要包含的类有:
    • MaxPooling、AveragePooling、MinPooling、MedianPooling
  • augmenters.edges 主要包含的类有:
    • Canny

1、iaa.Convolve()

iaa.Convolve(matrix=None) 

# 参数解释:Apply a convolution to input images
matrix: 卷积核,H×W ndarray
	

# eg:
>>> import imgaug.augmenters as iaa
>>> matrix = np.array([[0, -1, 0],
>>>                    [-1, 4, -1],
>>>                    [0, -1, 0]])
>>> aug = iaa.Convolve(matrix=matrix)

在这里插入图片描述

2、iaa.MaxPooling()

iaa.MaxPooling(kernel_size=(1, 5), keep_size=True) 

# 参数解释:Apply max pooling to images
kernel_size:
    1、如果是 int, 则此值会作为所有图片卷积核
    2、如果是 int tuple(a, b), 则每张图片会从均匀分布中随机选取一个值作为所有图片卷积核
    3、如果是 int list[a, b], 则每张图片会从列表中随机选取一个值作为所有图片卷积核

keep_size:默认为 True,池化后是否缩放回原来的尺寸
	

# eg:
aug = iaa.MaxPooling(2, keep_size=False)
aug = iaa.MaxPooling((1, 7))
aug = iaa.MaxPooling([2, 8])
aug = iaa.MaxPooling(((1, 7), (1, 7)))

八、augmenters.pillike/augmenters.blend/augmenters.contrast/augmenters.segmentation

  • augmenters.pillike 主要包含的类有:
    • Solarize、Posterize、Equalize、Autocontrast
    • EnhanceColor、EnhanceContrast、EnhanceBrightness、EnhanceSharpness
    • FilterBlur、FilterSmooth、FilterSmoothMore、FilterEdgeEnhance、FilterEdgeEnhanceMore、FilterFindEdges
    • FilterContour、FilterEmboss、FilterSharpen、FilterDetail、Affine
  • augmenters.blend 主要包含的类有:
    • BlendAlpha、BlendAlphaMask、BlendAlphaElementwise、BlendAlphaSimplexNoise、BlendAlphaFrequencyNoise
    • BlendAlphaSomeColors、BlendAlphaHorizontalLinearGradient、BlendAlphaVerticalLinearGradient、BlendAlphaSegMapClassIds
    • BlendAlphaBoundingBoxes、BlendAlphaRegularGrid、BlendAlphaCheckerboard
  • augmenters.contrast 主要包含的类有:
    • GammaContrast、SigmoidContrast、LogContrast、LinearContrast
    • AllChannelsHistogramEqualization、HistogramEqualization、AllChannelsCLAHE、CLAHE
  • augmenters.segmentation 主要包含的类有:
    • Superpixels、Voronoi、UniformVoronoi、RegularGridVoronoi、RelativeRegularGridVoronoi

九、参考资料

0、https://github.com/albumentations-team/albumentations
1、https://github.com/aleju/imgaug
2、https://github.com/aleju/imgaug-doc
3、https://imgaug.readthedocs.io/_/downloads/en/latest/pdf/
4、https://imgaug.readthedocs.io/en/latest/
5、https://gitee.com/alavaien/imgaug
6、imgaug学习笔记
7、深度学习之数据增强库imgaug使用方法
8、深度学习数据增广技术一览

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值