Data Augmentation: 基于albumentations库的图像数据增强

Python Data Augmentation 基于albumentations库的图像数据增强

Albumentations 是一个用于图像增强的 Python 库。图像增强用于深度学习和计算机视觉任务,以提高训练模型的质量。图像增强的目的是从现有数据中创建新的训练样本。

​ 这是一个示例,说明如何应用来自 Albumentations 的一些增强功能从原始图像创建新图像:

***tf的Keras也提供了类似的封装方法:ImageDataGenerator类

ImageDataGenerator中文文档

为什么选择albumentations

  • Albumentations支持所有常见的计算机视觉任务,例如分类、语义分割、实例分割、对象检测和姿势估计。

  • 该库提供了一个简单的统一 API**来处理所有数据类型:图像(RBG 图像、灰度图像、多光谱图像)、分割蒙版、边界框和关键点。

  • 该库包含70 多种不同的增强功能,可从现有数据中生成新的训练样本。

  • albumentations很快(https://github.com/albumentations-team/albumentations#benchmarking-results)。我们对每个新版本进行基准测试,以确保增强提供最大速度。

  • 适用于流行的深度学习框架,例如 PyTorch 和 TensorFlow。顺便说一下,Albulentations 是PyTorch 生态系统的一部分。

  • 由专家撰写。作者在生产计算机视觉系统和参与竞争性机器学习方面都有经验。许多核心团队成员是 Kaggle Masters 和 Grandmasters。

  • 该库广泛用于工业、深度学习研究、机器学习竞赛和开源项目。

增强列表

像素级变换

像素级变换将仅更改输入图像,并且将保留任何其他目标,例如 masks, bounding boxes, and keypoints unchanged。像素级变换列表:

  • AdvancedBlur
  • Blur
  • CLAHE
  • ChannelDropout
  • ChannelShuffle
  • ColorJitter
  • Downscale
  • Emboss
  • Equalize
  • FDA
  • FancyPCA
  • FromFloat
  • GaussNoise
  • GaussianBlur
  • GlassBlur
  • HistogramMatching
  • HueSaturationValue
  • ISONoise
  • ImageCompression
  • InvertImg
  • MedianBlur
  • MotionBlur
  • MultiplicativeNoise
  • Normalize
  • PixelDistributionAdaptation
  • Posterize
  • RGBShift
  • RandomBrightnessContrast
  • RandomFog
  • RandomGamma
  • RandomRain
  • RandomShadow
  • RandomSnow
  • RandomSunFlare
  • RandomToneCurve
  • RingingOvershoot
  • Sharpen
  • Solarize
  • Superpixels
  • TemplateTransform
  • ToFloat
  • ToGray
  • ToSepia
  • UnsharpMask

空间级变换

空间级变换将同时更改输入图像以及其他目标,例如masks, bounding boxes, and keypoints。下表显示了每个转换支持哪些附加目标。

Transform Image Masks BBoxes Keypoints
Affine
CenterCrop
CoarseDropout
Crop
CropAndPad
CropNonEmptyMaskIfExists
ElasticTransform
Flip
GridDistortion
GridDropout
HorizontalFlip
Lambda
LongestMaxSize
MaskDropout
NoOp
OpticalDistortion
PadIfNeeded
Perspective
PiecewiseAffine
PixelDropout
RandomCrop
RandomCropNearBBox
RandomGridShuffle
RandomResizedCrop
RandomRotate90
RandomScale
RandomSizedBBoxSafeCrop
RandomSizedCrop
Resize
Rotate
SafeRotate
ShiftScaleRotate
SmallestMaxSize
Transpose
VerticalFlip

Python 代码实现

先声明一个augmentation的pipeline:想要如何augmentation,把操作加到compose的参数里就行了
RandomCrop(width=256, height=256),代表随机截取256*256的区域
HorizontalFlip(p=0.5),参数p代表有0.5的概率进行水平翻转,其余p均同理

# Declare an augmentation pipeline
transform = album.Compose([
    album.RandomCrop(width=256, height=256),
    album.VerticalFlip(p=0.5),
    album.HorizontalFlip(p=0.5),
    album.RandomBrightnessContrast(p=0.2),
])

图像读入,这里用到了cv2库(原图是BGRA的.png格式,需要用cvtcolor转为RGB通道)

image = cv2.cvtColor(cv2.imread('test.png'), cv2.COLOR_BGRA2RGB)

# Augment an image
transformed = transform(image=image)
transformed_image = transformed["image"]

定义一个显示多图的函数

def visualize(**images):
    """
    Plot images in one row
    """
    n_images = len(images)
    plt.figure(figsize=(20,8)) # (20,8)
    for idx, (name, image) in enumerate(images.items()):
        plt.subplot(1, n_images, idx + 1)
        plt.xticks([]); 
        plt.yticks([])
        # get title from the parameter names
        plt.title(name.replace('_',' ').title(), fontsize=20)
        plt.imshow(image)
    plt.show()

visualize原图和augmentation后的图像

visualize(
    origin_image = image,
    transformed_image = transformed_image
)

Data Augmentation: 基于albumentations库的图像数据增强_第1张图片

Reference

​ https://github.com/albumentations-team/albumentations

你可能感兴趣的:(python,计算机视觉,深度学习)