簡體   English   中英

Keras ImageDataGenerator:數據和標簽形狀出現問題

[英]Keras ImageDataGenerator: problem with data and label shape

我想用Keras你可以看到中產生更多的圖片在這里 ,使用此代碼(幾乎相同源>隨機輪作 ):

# Random Rotations
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from keras import backend as K
datagen = ImageDataGenerator(rotation_range=90)
# fit parameters from data
datagen.fit(cats["images"])

print(np.asarray(cats["label"]).shape)  #output=(12464,)
print(np.asarray(cats["images"]).shape) #output=(12464, 60, 60, 1)

# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(cats["images"], cats["label"], batch_size=9):
    # create a grid of 3x3 images
    for i in range(0, 9):
        pyplot.subplot(330 + 1 + i)
        pyplot.imshow(X_batch[i].reshape(28, 28), cmap=pyplot.get_cmap('gray'))
    # show the plot
    pyplot.show()
    break

但是我收到以下錯誤:

ValueError: x (圖像張量)和y (標簽)的長度應相同。 找到:x.shape =(60,60,1),y.shape =(12464,)

這可能有助於進一步檢查: 在此處輸入圖片說明

我想該庫應該出問題了,好像我將圖像的形狀更改為60x60而不是60x60x1一樣,我將得到:

ValueError: .fit()輸入應該具有等級4。得到形狀為( .fit()數組

cats['images']cats['labels']很可能是Python列表。 首先使用np.array將它們轉換為數組,然后將它們傳遞給flow方法:

cats['images'] = np.array(cats['images'])
cats['labels'] = np.array(cats['labels'])

您需要更改標簽的形狀

labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM