我正在学习2017年关于Python深度学习的教程,我试图从著名的MNIST数据集中标准化一些图像。作者对下面这段代码有如下评价(决定将其全部包括在内,所以任何可能想要帮助的人都可以简单地复制/粘贴并重现这个问题)。
也可以对整个数据集的像素值进行标准化。这被称为特征标准化,它反映了通常为表格数据集中的每一列进行的标准化类型。 数据集中每一列的标准化。这与上一节所述的样本标准化不同,因为像素值是在所有样本(数据集中的所有图像)中进行标准化。在这种情况下,每个图像都被视为一个特征。你可以通过设置featurewise center 和featurewise std normalization参数来执行特征标准化。
Here is the code:
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot as plt
from keras import backend as K
K.set_image_dim_ordering('th')
#Load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True) #This standardizes pixel values across the entire dataset.
# 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, shuffle=False):
#Create grid of 3x3 images
for i in range(0,9):
plt.subplot(330 + 1 + i)
plt.imshow(X_batch[i].reshape(28,28), cmap=plt.get_cmap('gray'))
plt.show()
break
我认为,自2017年以来,Keras中的某些东西发生了变化,特别是在ImageDataGenerator类中,或者在该类的流程函数中,所以我现在必须以某种不同的方式来做这件事。问题是:我找不到方法。如果有任何帮助,我将不胜感激