简体   繁体   English

ValueError:形状不匹配:标签的形状(收到的(15,))应该等于逻辑的形状,除了最后一个维度(收到的(5,3))

[英]ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (5, 3))

I am getting this error when trying to fit a model:尝试拟合 model 时出现此错误:

ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (5, 3)). ValueError:形状不匹配:标签的形状(接收到的(15,))应该等于逻辑的形状,除了最后一个维度(接收到的(5,3))。

The code that's producing the error:产生错误的代码:

history = model.fit_generator(
  train_generator,
  epochs=10,
  validation_data=validation_generator)

This is the train_generator, validation generator is similar:这就是train_generator,验证生成器类似:

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

I try to get the shapes:我尝试获取形状:

for data_batch, labels_batch in train_generator:
    print('data batch shape:', data_batch.shape)
    print('labels batch shape:', labels_batch.shape)
    break

data batch shape: (5, 192, 192, 3) labels batch shape: (5, 3)数据批次形状:(5, 192, 192, 3) 标签批次形状:(5, 3)

When I change the batch size, the shape of labels in the error changes accordingly (batch size of 3 gives an error with label shape (9,) for example, I have 3 classes).当我更改批次大小时,错误中标签的形状会相应更改(批次大小为 3 会产生形状为 label (9,) 的错误,例如,我有 3 个类)。 But my concern is that it is being done by the train_generator, is there anything I can do to fix this?但我担心的是它是由 train_generator 完成的,我能做些什么来解决这个问题吗? Moreover, when I print the shapes from the train_generator, it seems right.此外,当我从 train_generator 打印形状时,它似乎是正确的。

Here is the model, in case it is helpful:这是 model,以备不时之需:

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
for i in range(2):
  model.add(layers.Conv2D(128, (3, 3), activation='relu'))
  model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))


model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

Thanks!谢谢!

Edit - Complete code :编辑 - 完整代码

The directory contains two folders - train and validation and each of them has three subfolders with the images of the corresponding classes.该目录包含两个文件夹 - train 和 validation,每个文件夹都有三个子文件夹,其中包含相应类别的图像。

try:
 %tensorflow_version 2.x # enable TF 2.x in Colab
except Exception:
  pass

from tensorflow.keras import datasets, layers, models

IMG_WIDTH = 192
IMG_HEIGHT = 192
train_dir = 'train'
validation_dir = 'validation'

from google.colab import drive
drive.mount('/content/drive')

import os
os.chdir("drive/My Drive/colab")

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_directory(
        validation_dir,
        target_size=(IMG_WIDTH, IMG_HEIGHT),
        batch_size=5)

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
for i in range(2):
  model.add(layers.Conv2D(128, (3, 3), activation='relu'))
  model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit_generator(
      train_generator,
      epochs=10,
      validation_data=validation_generator)

Thanks!谢谢!

The difference between sparse_categorical_crossentropy and categorical_crossentropy is whether your targets are one-hot encoded. sparse_categorical_crossentropycategorical_crossentropy之间的区别在于您的目标是否是单热编码的。

The shape of label batch is (5,3) , which means it has been one-hot encoded. label 批次的形状是(5,3) ,这意味着它已经过一次热编码。 So you should use categorical_crossentropy loss function.所以你应该使用categorical_crossentropy loss function。

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

I think it is related to your loss function just try to use "categorical_crossentropy" instead of "sparse..."我认为这与您的损失有关 function 只是尝试使用“categorical_crossentropy”而不是“sparse ...”

I encountered this error while I was working on the Fashion Most dataset.我在处理 Fashion Most 数据集时遇到了这个错误。 I figured I was not including the Flatten layer in my model. Once I added the layer, the problem was sorted.我想我的 model 中没有包括 Flatten 层。添加该层后,问题就解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:形状不匹配:标签的形状(收到的 (1,))应该等于 logits 的形状,除了最后一个维度(收到的 (10, 30)) - ValueError: Shape mismatch: The shape of labels (received (1,)) should equal the shape of logits except for the last dimension (received (10, 30)) ValueError:形状不匹配:标签的形状(收到的 (200,))应该等于 logits 的形状,除了最后一个维度(收到的 (100, 2)) - ValueError: Shape mismatch: The shape of labels (received (200,)) should equal the shape of logits except for the last dimension (received (100, 2)) Tensorflow fit ValueError: Shape mismatch: the shape of labels (received (16640,)) 应该等于 logits 的形状,除了最后一个维度 - Tensorflow fit ValueError: Shape mismatch: The shape of labels (received (16640,)) should equal the shape of logits except for the last dimension TensorFlow 2.0 SparseCategoricalCrossentropy valueError: Shape mismatch: 标签的形状应该等于 logits 的形状,除了最后一个 - TensorFlow 2.0 SparseCategoricalCrossentropy valueError: Shape mismatch: The shape of labels should equal the shape of logits except for the last Logit的形状和标签不匹配 - Shape of logits and labels mismatch logit和标签之间的形状不匹配 - Shape mismatch between logits and labels ValueError: `logits` 和 `labels` 必须具有相同的形状,收到 ((None, 16) vs (None, 1)) - ValueError: `logits` and `labels` must have the same shape, received ((None, 16) vs (None, 1)) ValueError:`logits` 和 `labels` 必须具有相同的形状 - ValueError: `logits` and `labels` must have the same shape 形状错误:标签的形状与对数的形状不兼容 - Shape mismacth: shape of labels is incompatible with shape of logits 排名不匹配:标签排名(收到2)应该等于logits排名减去1(收到2) - Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM