带有流量的keras imagedatagenerator,获得valueerror:`x`(图像张量)和'y(标签)应具有相同的长度
我正在研究Coursera上的CNN课程,当我尝试解决任务笔记本时,这是我抛出的错误。
valueerror:`x`(图像张量)和'y(标签)应具有相同的长度。找到:X.Shape =(27455、28、28、1),y.Shape =(7172、28、28、1)
,
但它们的长度不是相同的吗? (那是尺寸。) 以下是我的代码块,它正在创建问题:
# GRADED FUNCTION: train_val_generators
def train_val_generators(training_images, training_labels, validation_images, validation_labels):
"""
Creates the training and validation data generators
Args:
training_images (array): parsed images from the train CSV file
training_labels (array): parsed labels from the train CSV file
validation_images (array): parsed images from the test CSV file
validation_labels (array): parsed labels from the test CSV file
Returns:
train_generator, validation_generator - tuple containing the generators
"""
### START CODE HERE
# In this section, you will have to add another dimension to the data
# So, for example, if your array is (10000, 28, 28)
# You will need to make it (10000, 28, 28, 1)
# Hint: np.expand_dims
training_images = np.expand_dims(training_images, axis=3)
validation_images = np.expand_dims(validation_images, axis=3)
print(training_images.shape)
print(validation_images.shape)
# Instantiate the ImageDataGenerator class
# Don't forget to normalize pixel values
# and set arguments to augment the images (if desired)
train_datagen = ImageDataGenerator(
# Your Code Here
rescale = 1./255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest'
)
# Pass in the appropriate arguments to the flow method
train_generator = train_datagen.flow(x=training_images,
y=validation_images,
batch_size=32)
# Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)
# Remember that validation data should not be augmented
validation_datagen = ImageDataGenerator(
rescale = 1./255
)
# Pass in the appropriate arguments to the flow method
validation_generator = validation_datagen.flow(x=training_images,
y=validation_images,
batch_size=32)
### END CODE HERE
return train_generator, validation_generator
运行此单元格后,它可以正常工作,并在我的图像中添加了额外的维度。以下代码单元提出了上述问题。
# Test your generators
train_generator, validation_generator = train_val_generators(training_images, training_labels, validation_images, validation_labels)
print(f"Images of training generator have shape: {train_generator.x.shape}")
print(f"Labels of training generator have shape: {train_generator.y.shape}")
print(f"Images of validation generator have shape: {validation_generator.x.shape}")
print(f"Labels of validation generator have shape: {validation_generator.y.shape}")
这是我的整个错误Messasge:
(27455, 28, 28, 1)
(7172, 28, 28, 1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-c93bf4854fbc> in <module>()
1 # Test your generators
----> 2 train_generator, validation_generator = train_val_generators(training_images, training_labels, validation_images, validation_labels)
3
4 print(f"Images of training generator have shape: {train_generator.x.shape}")
5 print(f"Labels of training generator have shape: {train_generator.y.shape}")
3 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
87 'should have the same length. '
88 'Found: x.shape = %s, y.shape = %s' %
---> 89 (np.asarray(x).shape, np.asarray(y).shape))
90 if sample_weight is not None and len(x) != len(sample_weight):
91 raise ValueError('`x` (images tensor) and `sample_weight` '
ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (27455, 28, 28, 1), y.shape = (7172, 28, 28, 1)
我尝试在Stackoverflow中搜索许多类似的问题,这些问题谈到了更改其尺寸,但是我的尺寸是正确的,我认为,因为更改它绝对没有任何作用。对此有任何见识吗?请帮忙。谢谢! :_)
I'm working out the CNN course on Coursera, and as I try to solve the assignment notebook, this is the error thrown at me.
ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (27455, 28, 28, 1), y.shape = (7172, 28, 28, 1)
But aren't they of the same length? (the dimension that is.)
The following is my code block that is creating the issue:
# GRADED FUNCTION: train_val_generators
def train_val_generators(training_images, training_labels, validation_images, validation_labels):
"""
Creates the training and validation data generators
Args:
training_images (array): parsed images from the train CSV file
training_labels (array): parsed labels from the train CSV file
validation_images (array): parsed images from the test CSV file
validation_labels (array): parsed labels from the test CSV file
Returns:
train_generator, validation_generator - tuple containing the generators
"""
### START CODE HERE
# In this section, you will have to add another dimension to the data
# So, for example, if your array is (10000, 28, 28)
# You will need to make it (10000, 28, 28, 1)
# Hint: np.expand_dims
training_images = np.expand_dims(training_images, axis=3)
validation_images = np.expand_dims(validation_images, axis=3)
print(training_images.shape)
print(validation_images.shape)
# Instantiate the ImageDataGenerator class
# Don't forget to normalize pixel values
# and set arguments to augment the images (if desired)
train_datagen = ImageDataGenerator(
# Your Code Here
rescale = 1./255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest'
)
# Pass in the appropriate arguments to the flow method
train_generator = train_datagen.flow(x=training_images,
y=validation_images,
batch_size=32)
# Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)
# Remember that validation data should not be augmented
validation_datagen = ImageDataGenerator(
rescale = 1./255
)
# Pass in the appropriate arguments to the flow method
validation_generator = validation_datagen.flow(x=training_images,
y=validation_images,
batch_size=32)
### END CODE HERE
return train_generator, validation_generator
After running this cell, it's working fine and adding an extra dimension to my images. The following code cell raises the above-mentioned issue.
# Test your generators
train_generator, validation_generator = train_val_generators(training_images, training_labels, validation_images, validation_labels)
print(f"Images of training generator have shape: {train_generator.x.shape}")
print(f"Labels of training generator have shape: {train_generator.y.shape}")
print(f"Images of validation generator have shape: {validation_generator.x.shape}")
print(f"Labels of validation generator have shape: {validation_generator.y.shape}")
This is my entire error messasge:
(27455, 28, 28, 1)
(7172, 28, 28, 1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-c93bf4854fbc> in <module>()
1 # Test your generators
----> 2 train_generator, validation_generator = train_val_generators(training_images, training_labels, validation_images, validation_labels)
3
4 print(f"Images of training generator have shape: {train_generator.x.shape}")
5 print(f"Labels of training generator have shape: {train_generator.y.shape}")
3 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
87 'should have the same length. '
88 'Found: x.shape = %s, y.shape = %s' %
---> 89 (np.asarray(x).shape, np.asarray(y).shape))
90 if sample_weight is not None and len(x) != len(sample_weight):
91 raise ValueError('`x` (images tensor) and `sample_weight` '
ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (27455, 28, 28, 1), y.shape = (7172, 28, 28, 1)
I tried searching many similar problems in StackOverflow, which talked about changing its dimensions, but my dimensions are correct I think because changing it did absolutely nothing. Any insights on this? Please help. Thanks! :_)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Train_datagen.Flow
和validation_datagen.flow
中,您犯了两个小错误。对于y
参数,您可以通过validation_images
,但是您需要通过triending_labels
和vility_labels
。我纠正上述错误,并使用随机图像和简单的CNN模型编写完整的代码,并适合它。
输出:
In the
train_datagen.flow
andvalidation_datagen.flow
, you make two small mistakes. For they
parameter, you passvalidation_images
, but you need to passtraining_labels
andvalidation_labels
.I correct the above mistakes and write full code with random images and a simple CNN model and fit it.
Output: