因此,我试图显示已经增强的图像。但是出现了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)。任何帮助,将不胜感激

最佳答案

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)

关于python - 使用ImageDataGenerator后图像数据的形状无效(64、125、125、3),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64305483/

10-12 23:45