首页 > 解决方案 > 使用 Python 进行数据增强

问题描述

我目前正在从事一个与 CNN 相关的项目,我是该特定领域的新手。我有一组图像,其中包含 500 张关于织物缺陷的图像。如何将图像数量增加到 2000 个?我可以在此使用的任何库?

标签: conv-neural-networkdata-augmentation

解决方案


图像增强的首选库是imgaug

该文档是自我解释的,但这里是一个示例:


import numpy as np
from imgaug import augmenters as iaa
from PIL import Image

# load image and convert to matrix
image = np.array(Image.open("<path to image>"))

# convert image to matrix
# image must passed into a list because you can also put a list of multiple images into the augmenter, but for this demonstration we will only take one.
image = [image]

# all these augmentation techniques will applied with a certain probability
augmenter = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontal flips
    iaa.Crop(percent=(0, 0.1)), # random crops

    iaa.Sometimes(
        0.5,
        iaa.GaussianBlur(sigma=(0, 0.5))
    ),

    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),

], random_order=True) # apply augmenters in random order

augmented_image = augmenter(images=image)

augmented_image现在是一个列表,其中包含一个原始图像的增强图像。既然您说要从 500 个图像中创建 2000 个,您可以执行以下操作:您将每个图像增强 4 次,即像这样:


total_images = []
for image_path in image_paths:
    image = Image.load(image_path)

    # create a list with for times the same image
    images = [image for i in range(4)]
    
    # pass it into the augmenter and get 4 different augmentations
    augmented_images = augmenter(images=images)
    
    # add all images to a list or save it otherwise
    total_images += augmented_images

推荐阅读