Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras ImageDataGenerator: problem with data and label shape

I wanted to generate more images using Keras as you can see in here, using this code (almost the same as source>Random Rotations):

# 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

But I get the following error:

ValueError: x (images tensor) and y (labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464,)

This might help for further inspections : enter image description here

I imagine there should be something wrong with the library as if I change the shape of my image into 60x60 instead of 60x60x1 I'll get:

ValueError: Input to .fit() should have rank 4. Got array with shape: (12464, 60, 60)

like image 868
M at Avatar asked Dec 04 '22 19:12

M at


1 Answers

It is very likely that the cats['images'] and cats['labels'] are Python lists. First convert them to arrays using np.array and then pass them to flow method:

cats['images'] = np.array(cats['images'])
cats['labels'] = np.array(cats['labels'])
like image 185
today Avatar answered Dec 28 '22 22:12

today