图像在 Keras 中未标准化
Images not standardizing in Keras
我正在关注 Python 中关于深度学习的 2017 年教程,我正在尝试标准化 well-known MNIST 数据集中的一些图像。作者对下面的这段代码有以下看法(决定全部包括在内,因此任何可能想要提供帮助的人都可以简单地 copy/paste 并重现该问题)。
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 class 或那个 class 的流函数中,所以我现在必须做这有点不同。问题是:我找不到如何。任何帮助将不胜感激
当前输出:
预期输出:
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 中关于深度学习的 2017 年教程,我正在尝试标准化 well-known MNIST 数据集中的一些图像。作者对下面的这段代码有以下看法(决定全部包括在内,因此任何可能想要提供帮助的人都可以简单地 copy/paste 并重现该问题)。
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 class 或那个 class 的流函数中,所以我现在必须做这有点不同。问题是:我找不到如何。任何帮助将不胜感激
当前输出:
预期输出:
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
将标准化图像可视化为非标准化图像。