参考博客1:https://blog.csdn.net/qianbin3200896/article/details/88656524?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
参考博客2:https://blog.csdn.net/xuhe93/article/details/105826868
博客1的方法很简单:pip install Augmentor 在本地cmd终端下输入,第二点呢 这是默认安装在你pycharm中的默认下环境中,我的默认是3.7py,所以我得切换到3.7下的环境执行。
#导入数据增强工具
import Augmentor
#确定原始图像存储路径以及掩码文件存储路径
p = Augmentor.Pipeline("test1")
p.ground_truth("test2")
#图像旋转: 按照概率0.8执行,最大左旋角度10,最大右旋角度10
p.rotate(probability=0.8, max_left_rotation=10, max_right_rotation=10)
#图像左右互换: 按照概率0.5执行
p.flip_left_right(probability=0.5)
#图像放大缩小: 按照概率0.8执行,面积为原始图0.85倍
p.zoom_random(probability=0.3, percentage_area=0.85)
#最终扩充的数据样本数
p.sample(20)
执行后,最终在test1文件目录下会生成一个output文件夹,该文件夹中即为增强过后的图片,如下图所示:
标签和转换后的图有编号,可以对应上。主要是旋转,对称上下左右翻转,缩放。
在看看第二种方法:代码源博客不全,参考我下面的代码把:
'''''
#2 数据增强代码测试
import glob
from keras.preprocessing.image import ImageDataGenerator,load_img,img_to_array,array_to_img
import os
import Augmentor
import cv2
class myAugmentation(object):
"""
一个用于图像增强的类:
首先:分别读取训练的图片和标签,然后将图片和标签合并用于下一个阶段使用
然后:使用Keras的预处理来增强图像
最后:将增强后的图片分解开,分为训练图片和训练标签
"""
def __init__(self, train_path="D:/shujuzengqiang/1", label_path="D:/shujuzengqiang/2", merge_path="D:/shujuzengqiang/merge", aug_merge_path="D:/shujuzengqiang/aug_merge",
aug_train_path="D:/shujuzengqiang/aug_train", aug_label_path="D:/shujuzengqiang/aug_label"):
"""
使用glob从路径中得到所有的“.img_type”文件,初始化类:__init__()
"""
self.train_imgs = glob.glob(train_path + "/*" )
self.label_imgs = glob.glob(label_path + "/*" )
self.train_path = train_path
self.label_path = label_path
self.merge_path = merge_path
self.aug_merge_path = aug_merge_path
self.aug_train_path = aug_train_path
self.aug_label_path = aug_label_path
self.slices = len(self.train_imgs)
self.datagen = ImageDataGenerator(
rotation_range=180,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.05,
zoom_range=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
if not os.path.exists(self.merge_path):
os.mkdir(self.merge_path)
if not os.path.exists(self.aug_merge_path):
os.mkdir(self.aug_merge_path)
if not os.path.exists(self.aug_label_path):
os.mkdir(self.aug_label_path)
if not os.path.exists(self.aug_train_path):
os.mkdir(self.aug_train_path)
def Augmentation(self):
"""
Start augmentation.....
"""
trains = self.train_imgs
labels = self.label_imgs
path_train = self.train_path
path_label = self.label_path
path_merge = self.merge_path
path_aug_merge = self.aug_merge_path
if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
print("trains can't match labels")
return 0
for i in range(len(trains)):
img_t = load_img(trains[i])
img_l = load_img(labels[i])
x_t = img_to_array(img_t)
x_l = img_to_array(img_l)
x_t[:, :, 2] = x_l[:, :, 0]
img_tmp = array_to_img(x_t)
img_tmp.save(path_merge + "/" + str(i) + ".png")
img = x_t
img = img.reshape((1,) + img.shape)
savedir = path_aug_merge + "/" + str(i)
if not os.path.lexists(savedir):
os.mkdir(savedir)
self.doAugmentate(img, savedir, str(i))
def doAugmentate(self, img, save_to_dir, save_prefix, batch_size=1, save_format='png', imgnum=10):
# 增强一张图片的方法
"""
augmentate one image
"""
datagen = self.datagen
i = 0
for batch in datagen.flow(img,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=save_prefix,
save_format=save_format):
i += 1
if i >= imgnum:
break
def splitMerge(self):
# 将合在一起的图片分开
"""
split merged image apart
"""
path_merge = self.aug_merge_path
path_train = self.aug_train_path
path_label = self.aug_label_path
for i in range(self.slices):
path = path_merge + "/" + str(i)
train_imgs = glob.glob(path + "/*.png")
savedir = path_train + "/" + str(i)
if not os.path.lexists(savedir):
os.mkdir(savedir)
savedir = path_label + "/" + str(i)
if not os.path.lexists(savedir):
os.mkdir(savedir)
for imgname in train_imgs:
midname = imgname[imgname.rindex("\\") + 1:]
img = cv2.imread(imgname)
img_train = img[:, :, 2] # cv2 read image rgb->bgr
img_label = img[:, :, 0]
cv2.imwrite(path_train + "/" + str(i) + "/" + midname, img_train)
cv2.imwrite(path_label + "/" + str(i) + "/" + midname, img_label)
if __name__ == "__main__":
aug = myAugmentation()
aug.Augmentation()
aug.splitMerge()
'''''
里面只需要改路径就行,看看每个文件夹代表什么。每个图片都生成自己单独的文件夹,里面是这个图片的一系列变化,
主要的效果就是各种角度的旋转,平移,
def doAugmentate(self, img, save_to_dir, save_prefix, batch_size=1, save_format=‘tif’, imgnum=10):
修改10 可以变换生成的图像数目。
当然数据增强是针对数据集较小的时候,很多标注的数据集一般都几百张,数据增强可以有效提高数据量,可以扩充训练数据集。但也并非万能的,有时过度信任数据增强会带来负面效果,还会增加网络训练时间。需酌情使用。
阅读参考3:https://blog.csdn.net/JNingWei/article/details/79219838?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight