图像增强,在图像样本数量较少的时候,我们可以通过对样本图像进行平移、旋转、翻转、裁剪、缩放、噪声扰动、亮度以及对比度等操作来进行图像增强,从而增加样本数量。
TensorFlow中的图像增强可以参考:https://mp.csdn.net/postedit/84556489
在此主要说一下Keras中的图像增强,在Keras中我们可以通过ImageDataGenerator函数来实现图像增强。
ImageDataGenerator函数的使用介绍:
featurewise_center = False,输入特征的均值为0
samplewise_center = False,每一个样本均值为0
featurewise_std_normalization = False,每一个特征维度除以该维度的标准差
samplewise_std_normalization = False,每一个特征(所有维度)整体除以相应的标准差
zca_whitening = False,ZCA白化
zca_epsilon = 1e-6,白化值
rotation_range = 0,随机选择角度
width_shift_range = 0.,随机水平偏移量
height_shift_range = 0.,随机垂直偏移量
brightness_range = None,
shear_range = 0.,剪切范围
zoom_range = 0.,随机缩放幅度
channel_shift_range = 0.,随机通道偏移幅度
fill_mode = 'nearest',根据给定的模式填充输入边界之外的点可选值{“constant”, “nearest”, “reflect” or “wrap”}
cval = 0.,浮点数或整数,用于边界外点的值
horizontal_flip = False,随机水平翻转
vertical_flip = False,随机垂直翻转
rescale = None,
preprocessing_function = None,
data_format = None,
validation_split = 0.0,保留用于验证的图像分数
dtype = None
单张图像数据增强:
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from matplotlib import pyplot as plt
img=image.load_img('0.jpg')
imgGen=ImageDataGenerator(rotation_range = 90,width_shift_range = 0.2,height_shift_range = 0.2,zoom_range = 0.3)
x=image.img_to_array(img)
x=np.expand_dims(x,axis=0)
gen=imgGen.flow(x,batch_size=1)
for i in range(10):#显示10种增强后的形状
x_batch = next(gen)
plt.figure()
plt.imshow(x_batch[0]/256)
plt.savefig(str(i+3) + '.jpg')
plt.show()
数据集数据增强:
#主要是对像MNIST、CIFAR10等数据集中的数据进行增强,然后通过plt绘制出来
imgGen = ImageDataGenerator()
imgGen.fit(X_train)
# 创建目录,并保存图像
try:
os.mkdir('image')
except:
print('The fold is exist!')
for X_batch, y_batch in imgGen.flow(X_train, y_train, batch_size=9, save_to_dir='image', save_prefix='oct',
save_format='png'):
for i in range(0, 9):#选取9章图像
plt.subplot(331 + i)
plt.imshow(X_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
plt.show()
break