问题
I am getting this error when trying to fit a model:
ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (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_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)
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). But my concern is that it is being done by the train_generator, is there anything I can do to fix this? Moreover, when I print the shapes from the train_generator, it seems right.
Here is the model, in case it is helpful:
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.
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!
回答1:
The difference between sparse_categorical_crossentropy
and categorical_crossentropy
is whether your targets are one-hot encoded.
The shape of label batch is (5,3)
, which means it has been one-hot encoded. So you should use categorical_crossentropy
loss function.
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
回答2:
I think it is related to your loss function just try to use "categorical_crossentropy" instead of "sparse..."
来源:https://stackoverflow.com/questions/58398491/valueerror-shape-mismatch-the-shape-of-labels-received-15-should-equal-th