英文:
ImageDataGenerator regarding length of inputs
问题
当我将这两个列表传递给.flow()
函数时,出现了以下错误信息:
ValueError: `x`(图像张量)和 `y`(标签)应具有相同的长度。发现:x.shape = (32, 32, 3),y.shape = (2, 4)
但是,当我打印x.shape
和y.shape
时,它告诉我它们的长度相同。以下是我的代码:
dategen = ImageDataGenerator(
rescale = 1./255
)
labels = list(df["label"])
filepaths = list(df["photo"])
features = []
for filepath in filepaths:
img = Image.open(filepath)
img = img.resize((32,32))
features.append(np.array(img))
features = np.array(features)
labels = np.array(labels)
print(features.shape) #打印结果为 (2, 32, 32, 3)
print(labels.shape) #打印结果为 (2, 4)
train_gen = datagen.flow(
x = features,
y = labels,
batch_size = 1
)
我认为也许特征数组的第一个维度已经被修剪了,所以我尝试扩展特征数组的第一个维度:
features = np.expand_dims(features, axis = 0)
print(features.shape) #打印结果为 (1, 2, 32, 32, 3)
但然后我得到了这个错误:
ValueError: ... 发现:x.shape = (1, 2, 32, 32, 3),...
发生了什么?
英文:
I have a list of features containing 3D numpy arrays representing a RGB image as well as a list of labels which are 1D numpy arrays. I want to perform image augmentation using the ImageDataGenerator.flow(). When passing the two lists into .flow(), I get:
ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (32, 32, 3), y.shape = (2, 4)
However, when I print x.shape and y.shape, it tells me that they are the same length. Here is my code:
dategen = ImageDataGenerator(
rescale = 1./255
)
labels = list(df["label"])
filepaths = list(df["photo"])
features = []
for filepath in filepaths:
img = Image.open(filepath)
img = img.resize((32,32))
features.append(np.array(img))
features = np.array(features)
labels = np.array(labels)
print(features.shape) #Prints (2, 32, 32, 3)
print(labels.shape) #Prints (2, 4)
train_gen = datagen.flow(
x = features,
y = labels,
batch_size = 1
)
I thought that perhaps the first dimension of the features array has been trimmed so I tried expanding the first dimension of the features array
features = np.expand_dims(features, axis = 0)
print(features.shape) #Prints (1, 2, 32, 32, 3)
But then I get this error:
ValueError: ... Found: x.shape = (1, 2, 32, 32, 3), ...
What is going on?
答案1
得分: 0
我通过添加这行代码解决了它。形状仍然相同,但在运行这行代码后它不知何故就起作用了。
labels = np.reshape(labels, (features.shape[0], -1))
英文:
I solved it by adding this line of code. The shape is still the same but somehow it works after running this.
labels = np.reshape(labels, (features.shape[0], -1))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论