我正在学习 2017 年关于 Python 深度学习的教程,并且正在尝试对著名的 MNIST 数据集中的一些图像进行标准化。作者对下面的这段代码有以下看法(决定将其全部包含在内,因此任何可能想要提供帮助的人都可以简单地复制/粘贴并重现该问题)。
It is also possible to standardize pixel values across the entire dataset. This is called feature standardization and mirrors the type of standardization often performed for each column in a tabular dataset. This is different to sample standardization described in the previous section as pixel values are standardized across all samples (all images in the dataset). In this case each image is considered a feature. You can perform feature standardization by setting the featurewise center and featurewise std normalization arguments on the ImageDataGenerator class.
这是代码:
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 类或该类的流函数中,因此我现在必须以不同的方式执行此操作。问题是:我找不到如何。任何帮助将不胜感激
最佳答案
Keras 中的
ImageDataGenerator
对代码示例中的所有图像执行标准化。您可以使用以下代码检查实际的 X_batch
值:
for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9, shuffle=False):
print(X_batch[0])
break
该问题与 imshow
的行为有关,默认情况下为 uses color normalization 。因此,imshow
将标准化图像可视化为非标准化图像。
关于python - Keras 中的图像未标准化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57664006/