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) andy(labels) should have the same
length. Found: x.shape = (60, 60, 1), y.shape = (12464,)
This might help for further inspections :

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)
python tensorflow image-processing keras
add a comment |
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) andy(labels) should have the same
length. Found: x.shape = (60, 60, 1), y.shape = (12464,)
This might help for further inspections :

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)
python tensorflow image-processing keras
add a comment |
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) andy(labels) should have the same
length. Found: x.shape = (60, 60, 1), y.shape = (12464,)
This might help for further inspections :

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)
python tensorflow image-processing keras
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) andy(labels) should have the same
length. Found: x.shape = (60, 60, 1), y.shape = (12464,)
This might help for further inspections :

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)
python tensorflow image-processing keras
python tensorflow image-processing keras
edited Jan 2 at 11:45
today
11.1k22038
11.1k22038
asked Jan 2 at 11:22
Mahdi RafatjahMahdi Rafatjah
555317
555317
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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'])
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
add a comment |
You need to change the shape of your labels
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))
This does not work and produces almost identical error "ValueError:x(images tensor) andy(labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"
– Mahdi Rafatjah
Jan 2 at 15:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005424%2fkeras-imagedatagenerator-problem-with-data-and-label-shape%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'])
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
add a comment |
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'])
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
add a comment |
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'])
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'])
answered Jan 2 at 11:42
todaytoday
11.1k22038
11.1k22038
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
add a comment |
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
I expected the following error for this situation "AttributeError: 'list' object has no attribute 'shape'" That is why I never think of that. So much thanks by the way.
– Mahdi Rafatjah
Jan 2 at 15:24
add a comment |
You need to change the shape of your labels
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))
This does not work and produces almost identical error "ValueError:x(images tensor) andy(labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"
– Mahdi Rafatjah
Jan 2 at 15:26
add a comment |
You need to change the shape of your labels
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))
This does not work and produces almost identical error "ValueError:x(images tensor) andy(labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"
– Mahdi Rafatjah
Jan 2 at 15:26
add a comment |
You need to change the shape of your labels
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))
You need to change the shape of your labels
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))
answered Jan 2 at 11:40
Shubham PanchalShubham Panchal
534210
534210
This does not work and produces almost identical error "ValueError:x(images tensor) andy(labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"
– Mahdi Rafatjah
Jan 2 at 15:26
add a comment |
This does not work and produces almost identical error "ValueError:x(images tensor) andy(labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"
– Mahdi Rafatjah
Jan 2 at 15:26
This does not work and produces almost identical error "ValueError:
x (images tensor) and y (labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"– Mahdi Rafatjah
Jan 2 at 15:26
This does not work and produces almost identical error "ValueError:
x (images tensor) and y (labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464, 1)"– Mahdi Rafatjah
Jan 2 at 15:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005424%2fkeras-imagedatagenerator-problem-with-data-and-label-shape%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown