Keras之ImageDataGenerator(图像生成)函数的应用详解

本文深入探讨Keras的ImageDataGenerator,通过代码示例解释如何进行特征标准化、ZCA白化、随机旋转、随机移动、随机翻转等图像增强技术,提升深度学习模型在计算机视觉任务中的表现。
摘要由CSDN通过智能技术生成

不去讲那些晦涩难懂的东西,直接上代码

第一、图像原图

# Plot images
from keras.datasets import mnist
from matplotlib import pyplot
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# create a grid of 3x3 images
for i in range(0, 9):
	pyplot.subplot(330 + 1 + i)
	pyplot.imshow(X_train[i], cmap=pyplot.get_cmap('gray'))
# show the plot
pyplot.show()

其中cmap=pyplot.get_cmap(‘gray’) 可以由cmap='gray’代替,两者等价,下面的函数中也可以替换
gray的值表示的图像的颜色,可选的颜色由以下几种
颜色图谱
运行此示例将提供以下图像,我们可以使用该图像与下面示例中的图像准备和增强进行比较。
结果如图

第二、特征标准化 Feature Standardization

还可以对整个数据集的像素值进行标准化。这称为特性标准化,反映了通常对表格数据集中的每一列执行的标准化类型。
可以通过在ImageDataGenerator类上设置featurewise center和featurewise std规范化参数来执行特性标准化。实际上,在默认情况下,这些参数被设置为True,并且创建一个不带参数的ImageDataGenerator实例将具有相同的效果。

# Standardize images across the dataset, mean=0, stdev=1
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][width][height][channels]
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1))
X_test = X_test.reshape((X_test.shape[0], 28, 28, 1))
# convert from int to float
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# define data preparation
datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)
# fit parameters from data
datagen.fit(X_train)
# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9):
	# create a grid of 3x3 images
	for i in range(0, 9):
		pyplot.subplot(330 + 1 + i)
		pyplot.imshow(X_batch[i].reshape(28, 28), cmap=pyplot.get_cmap('gray'))
	# show the plot
	pyplot.show()
	break

运行这个例子,你可以看到效果是不同的,似乎变暗和变亮不同的数字。(网上的说法,但是我运行出来的图像好像没啥区别,下面是我运行出来的图像和别人运行出来的图像)
网上的
我运行的

第三、ZCA Whitening ZCA 白化

图像的白化变换是一种线性代数运算,可以减少图像像素矩阵中的冗余度。
减少图像中的冗余意在更好地向学习算法突出图像中的结构和特征。
通常使用主成分分析(PCA)技术进行图像白化。最近,一种称为ZCA的替代方法(详见本技术报告附录A)显示了更好的转换图像效果,它保持了所有原始的尺寸,与PCA不同的是,转换后的图像仍然和原来一样。
您可以通过将ZCA美白参数设置为True来执行ZCA美白转换。

# ZCA wh
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值