ValueError: Shapes (None, 1) and (None, 3) are incompatible

I’ve been having trouble with this for a while now. I think it may be my use of the “train_datagen.flow” function inside the “def train_val_generators” function. It doesn’t seem to have a way to set its class_mode to categorical.

You will find the error messages at the bottom of this topic. Any help would be greatly appreciated. Thanks!

Parsing CSV File

def parse_data_from_input(filename):
  with open(filename) as file:
      n_cols = len(file.readline().split(","))
  
      images = np.loadtxt(filename,
                          delimiter = ',',
                          skiprows = 1,
                          usecols=np.arange(1, n_cols),
                          dtype = int)
      
      images = np.reshape(images, (int(images.size/784), 28, 28))
      
      labels = np.loadtxt(filename,
                          delimiter = ',',
                          skiprows = 1,
                          usecols = (0),
                          dtype = int)
    
      return images, labels

Creating Generators

def train_val_generators(training_images, training_labels, validation_images, validation_labels):
  training_images = np.expand_dims(training_images, 3)
  validation_images = np.expand_dims(validation_images, 3)

  train_datagen = ImageDataGenerator(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')

  train_generator = train_datagen.flow(x=training_images,
                                       y=training_labels,
                                       batch_size=32) 
  
  validation_datagen = ImageDataGenerator(rescale=1./255)

  validation_generator = validation_datagen.flow(x=validation_images,
                                                 y=validation_labels,
                                                 batch_size=32)

  return train_generator, validation_generator

Creating Model

def create_model():

  ### START CODE HERE       

  model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(3, activation='softmax')
  ])

  model.compile(optimizer = tf.keras.optimizers.RMSprop(learning_rate = 0.001),
                loss = "categorical_crossentropy",
                metrics=['accuracy'])
 
  return model

Training

model = create_model()
history = model.fit(train_generator,
                    epochs=15,
                    validation_data=validation_generator)

Error output

/usr/local/lib/python3.10/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1051, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1109, in compute_loss
        return self.compiled_loss(
    File "/usr/local/lib/python3.10/dist-packages/keras/engine/compile_utils.py", line 265, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 142, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 268, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 1984, in categorical_crossentropy
        return backend.categorical_crossentropy(
    File "/usr/local/lib/python3.10/dist-packages/keras/backend.py", line 5559, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 1) and (None, 3) are incompatible

You have filed this under DLS Course 4, but I do not recognize the code you show from DLS Course 4. Are you sure you filed this correctly?

If the labels are still in categorical form (as opposed to one hot), you have two choices:

Convert them yourself or you could also use the “sparse” version of categorical_crossentropy loss which does the conversion internally.

This is from the TF1 Course 2 (Convolutional Neural Networks in TensorFlow). Please move this to the correct category.

As Paul said, ‘use the “sparse” version of categorical_crossentropy loss’. Also, you are passing too many Arguments to ImageDataGenerator. I think that is not necessary. Moreover, your parse_data_from_input code seems incorrect to me.

Please move this to the correct category and wait for the official mentors of this course to respond to you.

Best,
Saif.