title: ValueError: Shape mismatch: The shape of labels (received (15,)) should equal the shape of logits except for the last dimension (received (5, 3))
body: <p>I am getting this error when trying to fit a model:</p>
  
  <blockquote>
    <p>ValueError: Shape mismatch: The shape of labels (received (15,))
    should equal the shape of logits except for the last dimension
    (received (5, 3)).</p>
  </blockquote>
  
  <p>The code that's producing the error:</p>
  
  <pre><code>history = model.fit_generator(
    train_generator,
    epochs=10,
    validation_data=validation_generator)
  </code></pre>
  
  <p>This is the train_generator, validation generator is similar:</p>
  
  <pre><code>train_datagen = ImageDataGenerator(rescale=1./255)
  train_generator = train_datagen.flow_from_directory(
          train_dir,
          target_size=(IMG_WIDTH, IMG_HEIGHT),
          batch_size=5)
  </code></pre>
  
  <p>I try to get the shapes:</p>
  
  <pre><code>for data_batch, labels_batch in train_generator:
      print('data batch shape:', data_batch.shape)
      print('labels batch shape:', labels_batch.shape)
      break
  </code></pre>
  
  <blockquote>
    <p>data batch shape: (5, 192, 192, 3) labels batch shape: (5, 3)</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Here is the model, in case it is helpful:</p>
  
  <pre><code>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'])
  </code></pre>
  
  <p>Thanks!</p>
  
  <p><strong>Edit - Complete code</strong>:</p>
  
  <p>The directory contains two folders - train and validation and each of them has three subfolders with the images of the corresponding classes.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Thanks!</p>