简体   繁体   English

Keras 中的 ImageDataGenerator 形状问题

[英]ImageDataGenerator shape issues in Keras

datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=15,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images
        # (std, mean, and principal components if ZCA whitening is applied).
        # datagen.fit(x_train)


        print(x_train.shape)

        def data_generator(generator, x, y1, y2, batch_size):
            genX = generator.flow(x, seed=7, batch_size=batch_size)
            genY1 = generator.flow(y1, seed=7, batch_size=batch_size)
            genY2 = generator.flow(y2, seed=7, batch_size=batch_size)
            while(True):
                Xi = genX.next()
                Yi1 = genY1.next()
                Yi2 = genY2.next()
                yield Xi, [Yi1, Yi2]

And this is how I'm calling model.fit_generator这就是我如何调用 model.fit_generator

model.fit_generator(data_generator(datagen, x_train, y_train, y_aux_train, params['batch_size']),
                            epochs=params['epochs'], steps_per_epoch=150,
                            validation_data=data_generator(datagen, x_test, y_test, y_aux_test, params['batch_size']), 
                            validation_steps=100, callbacks=[reduce_lr, tensorboard],verbose=2)

This is the error I get -这是我得到的错误 -

ValueError: ('Input data in NumpyArrayIterator should have rank 4. You passed an array with shape', (5630, 4)) ValueError: (' NumpyArrayIterator的输入数据应为 4 级。您传递了一个具有形状的数组', (5630, 4))

What are your x , y1 and y2 ?你的xy1y2多少? The input data to ImageDataGenerator must have 4 dimensions (batch, channel, height, width). ImageDataGenerator 的输入数据必须有 4 个维度(批次、通道、高度、宽度)。 Your data is something completely different, that is why you are getting the error.您的数据完全不同,这就是您收到错误的原因。

UPDATE:更新:

According to the docs :根据 文档

flow(x, y=None, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None)

x: Input data. x:输入数据。 Numpy array of rank 4 or a tuple.排名 4 或元组的 Numpy 数组。 If tuple, the first element should contain the images and the second element another numpy array or a list of numpy arrays that gets passed to the output without any modifications.如果是元组,则第一个元素应包含图像,第二个元素应包含另一个 numpy 数组或 numpy 数组列表,这些数组无需任何修改即可传递给输出。 Can be used to feed the model miscellaneous data along with the images.可用于随图像一起提供模型杂项数据。

In genY1 = generator.flow(y1, seed=7, batch_size=batch_size) you pass your labels (that are of shape (4,) as I can see) as features, and ImageDataGenerator expects them to have 4 dimensions.genY1 = generator.flow(y1, seed=7, batch_size=batch_size)您将标签(如我所见,形状为 (4,))作为特征传递,ImageDataGenerator 期望它们具有 4 个维度。

You should not pass your labels like this.你不应该像这样传递你的标签。 Try instead something like this:尝试这样的事情:

datagen.flow((x_train, [y_train, y_aux_train]), batch_size=params['batch_size'])

Or:或者:

datagen.flow(x=x_train, y=[y_train, y_aux_train], batch_size=params['batch_size'])

I don't know the shape of the input you are giving... But from the error, I can tell you the following... Yes, your input value for parameter x must be of rank 4... I'll explain to you with an example.我不知道你给出的输入的形状......但是从错误中,我可以告诉你以下......是的,你的参数 x 的输入值必须是 4 级......我会解释给你举个例子。 MNIST dataset when downloaded from Keras.datasets they come as NumPy ndarray of shape 60000(for test), 28,28 if you pass this to ImageDataGenerator as an input for the parameter, 'x' you get the same error.当从 Keras.datasets 下载 MNIST 数据集时,它们作为形状为 60000(用于测试)、28,28 的 NumPy ndarray,如果您将其作为参数的输入传递给 ImageDataGenerator,“x”您会得到相同的错误。 You can simply overcome that by resizing your array.您可以通过调整数组大小来简单地克服这个问题。

<your image ndarray>.reshape(<your image ndarray>.shape[0],28,28,1)

NOTE: The last value in the above line ie 1 for grayscale images and 3 for RGB images respectively which is the number of color channels in your image.注意:上一行中的最后一个值,即灰度图像为 1,RGB 图像为 3,这是图像中颜色通道的数量。

Therefore I suggest you resize your ndarray such that it has four dimensions.因此,我建议您调整 ndarray 的大小,使其具有四个维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM