keras中有自带的数据增强方式,直接调用非常方便。
ps: keras 的Image data preprocessing 官方文档:https://keras.io/api/preprocessing/image/
介绍非常详细的中文博客:https://blog.csdn.net/mieleizhi0522/article/details/82191331
https://blog.csdn.net/zmlovelx/article/details/93916043
代码如下,非常简洁明了:
from keras.preprocessing.image import ImageDataGenerator
path = './image' # 类别子文件夹的上一级
dst_path = './image1' #save path
#增强方式
datagen = ImageDataGenerator(rotation_range = 5, width_shift_range=0.02,horizontal_flip=True,)
gen = datagen.flow_from_directory( path,target_size = (200, 200),batch_size = 1,
classes=['2'], #可选对那几个文件夹进行数据增强,如classes=['1','2']
save_to_dir = dst_path,# 生成后的图像保存路
save_prefix = 'arg',save_format = 'jpg')
total_num=12 #增强的总数目
for i in range(total_num):
gen.next()
效果如下: