ImageDataGenerator不喜欢我的fashionMNIST数据集。需要什么输入?

时间:2019-10-25 10:19:02

标签: python tensorflow keras image-preprocessing

我从tensorflow基本图像分类指南中获得了一个名为train_images和train_labels的集合:

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水平翻转一些图像,但是当我将模型与训练列表拟合时,它返回一条错误消息,提示x应该是4级数组。

我已经尝试做

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轴是什么? 该如何执行?

2 个答案:

答案 0 :(得分:0)

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

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

这将添加一个通道尺寸(大小为1),结果形状为(60000,28,28,1),它应该可以解决您的问题。

答案 1 :(得分:0)

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