【问题标题】:Invalid shape (64, 125, 125, 3) for image data after using ImageDataGenerator使用 ImageDataGenerator 后图像数据的形状(64、125、125、3)无效
【发布时间】:2020-10-11 15:01:26
【问题描述】:

所以我正在尝试显示已经增强的图像。但得到Invalid shape (64, 125, 125, 3) for image data 错误。这是我的代码:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255,
                                    zoom_range=0.1,
                                    rotation_range=25,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1,
                                    shear_range=0.1, horizontal_flip=True,
                                    fill_mode='nearest')

val_datagen = ImageDataGenerator(rescale=1./255)

# build image augmentation generators
train_generator = train_datagen.flow(train_data, train_labels_enc, batch_size=64, shuffle=True)
val_generator = val_datagen.flow(val_data, val_labels_enc, batch_size=64, shuffle=False)


from matplotlib import pyplot
for i in range(9):
    # define subplot
    pyplot.subplot(330 + 1 + i)
    # generate batch of images
    batch = train_generator.next()
    # convert to unsigned integers for viewing
    image = batch[0].astype('uint8')
    # plot raw pixel data
    pyplot.imshow(image)

pyplot.show()

错误指向pyplot.imshow(image)。任何帮助将不胜感激

【问题讨论】:

  • 试试image[0,:,:,:],它将显示64张图片中的第一张图片。您不能一次显示 64 张图像

标签: python tensorflow opencv matplotlib keras


【解决方案1】:

batch 对象是 (images, labels) 的元组,因此如果选择 batch[0],则选择所有图像。

试试:

for i in range(9):
    pyplot.subplot(330 + 1 + i)
    images, labels = train_generator.next()
    image = (images[0]*255).astype('uint8')
    pyplot.imshow(image)

【讨论】:

  • 你和sai的回答都让错误消失了,但情节只显示黑色图像
  • 那是因为您将像素除以 255。请参阅我的编辑
猜你喜欢
  • 2022-08-23
  • 2021-11-08
  • 2021-09-15
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多