ImageDataGenerator 不喜欢我的 fashionMNIST 数据集。它需要哪个输入?

ImageDataGenerator doesn't like my fashionMNIST dataset. Which input does it need?

我有一组名为 train_images 和 train_labels 来自 tensorflow 基本图像分类指南:

https://www.tensorflow.org/tutorials/keras/classification

我加载数据集:

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

这两个列表的形状分别是: (60000, 28, 28) (60000,)

在那之后我想使用 ImageDataGenerator 水平翻转一些图像但是当我用我的火车列表拟合模型时,它 returns 我的一个错误说 x 应该是一个等级为 4[=15 的数组=]

我已经试过了

train_images = (np.expand_dims(train_images,0))

所以形状变成 (1,60000,28,28) (我必须这样做才能让模型检查单个图像) 但它不适用于模型

这是代码的其余部分:

aug = ImageDataGenerator(rotation_range=20, horizontal_flip=True)

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
    ])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
    )

BS=32
EPOCHS=10
H = model.fit_generator(
    aug.flow(train_images, train_labels, batch_size=BS),
    validation_data=(test_images, test_labels),
    steps_per_epoch=len(train_images) // BS,
    epochs=EPOCHS)

这是生成的错误:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-65-e49da92bcb89> in <module>()
      5 #train_images.shape
      6 H = model.fit_generator(
----> 7         aug.flow(train_images, train_labels, batch_size=BS),
      8         validation_data=(test_images, test_labels),
      9         steps_per_epoch=len(train_images) // BS,

1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)

    115             raise ValueError('Input data in `NumpyArrayIterator` '
    116                              'should have rank 4. You passed an array '
--> 117                              'with shape', self.x.shape)
    118         channels_axis = 3 if data_format == 'channels_last' else 1
    119         if self.x.shape[channels_axis] not in {1, 3, 4}:

ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (60000, 28, 28))

实际上train_images是(图像的N°,宽度,高度)它在等待的第4轴是什么? 如何执行此操作?

您应该将图像转换为 4D 张量。现在你有了 NHW 格式(批量尺寸、高度、宽度)。该错误表明您应该具有 NHWC 格式 - 批次、高度、宽度、通道。所以你需要做

train_images = (np.expand_dims(train_images, axis=3))

这将添加一个通道维度(大小为 1),生成的形状将为 (60000,28,28,1),它应该可以解决您的问题。

Channel 应该是 4D 张量的最后一个维度。因此,请尝试使用 train_images = (np.expand_dims(train_images, -1)) 而不是 train_images = (np.expand_dims(train_images,0))。希望对你有帮助。