用imgaug对图片进行数据增强
imgaug–introduction
imgaug是一个用于机器学习实验中图像增强的python库,支持python2.7和3.4以上的版本。 它支持多种增强技术,允许轻松组合这些技术,具有简单但功能强大的随机界面,可以在这些界面上增加图像和关键点/界标,并在后台进程中提供增强功能以提高性能。
安装方法
在自己已经激活的环境中,这里我选择pytorch的环境,输入
pip install imgaug
最终结果如下
安装成功。
或者想安装最新版本的imgaug,可直接在github上下载源码进行安装,方法如下:
pip install git+https://github.com/aleju/imgaug
函数介绍
函数功能介绍:https://www.pianshen.com/article/624834945/
参数的具体含义说明:
https://blog.csdn.net/u012897374/article/details/80142744
测试程序
整体流程为:定义变换序列(Sequential)→读入图片(imread)→执行变换(augment_images)→保存图片(imwrite)
#!usr/bin/python
# -*- coding: utf-8 -*-
import cv2
from imgaug import augmenters as iaa
import os
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# 定义一组变换方法.
seq = iaa.Sequential([
# 每个图片选择选择这其中的0到5种方法做变换,随机生成
#将Augmenter中的部分变换应用在图片处理上,而不是应用所有的Augmenter。例如:可以定义20种变换,但每次只选择其中的5个。但是不支持固定选择某一个Augmenter
#参数n: 从总的Augmenters中选择多少个。可以是一个int, tuple, list或者随机;random_order:是否每次顺序不一样。
iaa.SomeOf((0, 5),
[
iaa.Fliplr(0.5), # 对50%的图片进行水平镜像翻转
iaa.Flipud(0.5), # 对50%的图片进行垂直镜像翻转
# Convert some images into their superpixel representation,
# sample between 20 and 200 superpixels per image, but do
# not replace all superpixels with their average, only
# some of them (p_replace).
#对batch中的一部分图片应用一部分Augmenters,剩下的图片应用另外的Augmenters。
sometimes(
iaa.Superpixels(
p_replace=(0, 1.0),
n_segments=(20, 200)
)
),
# Blur each image with varying strength using
# gaussian blur (sigma between 0 and 3.0),
# average/uniform blur (kernel size between 2x2 and 7x7)
# median blur (kernel size between 3x3 and 11x11).
#每次从一系列Augmenters中选择一个来变换。
iaa.OneOf([
#高斯扰动
iaa.GaussianBlur((0, 3.0)),
#从最邻近像素中取均值来扰动。
iaa.AverageBlur(k=(2, 7)),
#通过最近邻中位数来扰动。
iaa.MedianBlur(k=(3, 11)),
]),
# Sharpen each image, overlay the result with the original
# image using an alpha between 0 (no sharpening) and 1
# (full sharpening effect).
#锐化
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
# Same as sharpen, but for an embossing effect.
#浮雕效果
iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
# Add gaussian noise to some images.
# In 50% of these cases, the noise is randomly sampled per
# channel and pixel.
# In the other 50% of all cases it is sampled once per
# pixel (i.e. brightness change).
#添加高斯噪声。
iaa.AdditiveGaussianNoise(
loc=0, scale=(0.0, 0.05*255)
),
# Invert each image's chanell with 5% probability.
# This sets each pixel value v to 255-v.
iaa.Invert(0.05, per_channel=True), # invert color channels
# Add a value of -10 to 10 to each pixel.
iaa.Add((-10, 10), per_channel=0.5),
# Add random values between -40 and 40 to images, with each value being sampled per pixel:
#按像素加。
iaa.AddElementwise((-40, 40)),
# Change brightness of images (50-150% of original value).
iaa.Multiply((0.5, 1.5)),
# Multiply each pixel with a random value between 0.5 and 1.5.
#按像素值乘。
iaa.MultiplyElementwise((0.5, 1.5)),
# Improve or worsen the contrast of images.
#改变图像的对比度
iaa.ContrastNormalization((0.5, 2.0)),
],
# do all of the above augmentations in random order 定义随机数
random_order=True
)
],random_order=True) #apply augmenters in random order
# 图片文件相关路径
path = 'yolo3_keras_fire/img/'
# 保存路径可以是不同的路径,但是需要先把保存地方的文件夹建好
savedpath = 'yolo3_keras_fire/img/'
imglist=[]
filelist = os.listdir(path)
# 遍历要增强的文件夹,把所有的图片保存在imglist中
for item in filelist:
img = cv2.imread(path + item)
#print('item is ',item)
#print('img is ',img)
#images = load_batch(batch_idx)
imglist.append(img)
#print('imglist is ' ,imglist)
print('all the picture have been appent to imglist')
#对文件夹中的图片进行增强操作,循环100次
for count in range(100):
images_aug = seq.augment_images(imglist)
for index in range(len(images_aug)):
filename = str(count) + str(index) +'.jpg'
#保存图片
cv2.imwrite(savedpath + filename,images_aug[index])
print('image of count%s index%s has been writen'%(count,index))
结果呈现
这里我先用三个图片的数据集进行了测试,结果如下:
可以看到每一次循环,三张图片都做一次增强(random选0到5种变换随机生成,最终数据集扩展到了992张。
部分数据结果选取如下:
最后,我用程序去测试了一下我们大创项目需要去做数据增强的数据集,先用了一张图片进行测试:
原图:
增强后的部分图片:
reference
[1] 深度学习之数据增强库imgaug使用方法 https://www.pianshen.com/article/624834945/
[2]深度学习之批量图片数据增强https://blog.csdn.net/zong596568821xp/article/details/83111124
imagaug图像分割增强 https://blog.csdn.net/limiyudianzi/article/details/86498416
数据增强对bounding boxes同时操作
https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html