Keras in google colab shows ValueError after changing number of classes


I am a student working on a school project that needs me to identify agricultural pests from images. I am using Google colab to run the code. This is my code

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

image_size = (50, 50)
batch_size = 300

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/gdrive/My Drive/pest/train",
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/gdrive/My Drive/pest/validation",

data_augmentation = keras.Sequential(

def make_model(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)
    # Image augmentation block
    x = data_augmentation(inputs)

    # Entry block
    x = layers.Rescaling(1.0 / 255)(x)
    x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.Conv2D(64, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    previous_block_activation = x  # Set aside residual

    for size in [128, 256, 512, 728]:
        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

        # Project residual
        residual = layers.Conv2D(size, 1, strides=2, padding="same")(
        x = layers.add([x, residual])  # Add back residual
        previous_block_activation = x  # Set aside next residual

    x = layers.SeparableConv2D(1024, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.GlobalAveragePooling2D()(x)
    if num_classes == 2:
        activation = "sigmoid"
        units = 1
        activation = "softmax"
        units = num_classes

    x = layers.Dropout(0.5)(x)
    outputs = layers.Dense(units, activation=activation)(x)
    return keras.Model(inputs, outputs)

model = make_model(input_shape=image_size + (3,), num_classes=8)
keras.utils.plot_model(model, show_shapes=True)

epochs = 50

callbacks = [
    train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,

However, when I ran it gives me an error

Epoch 1/50
ValueError                                Traceback (most recent call last)
<ipython-input-6-49bbab22d55e> in <module>()
     10 )
---> 12     train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,
     13 )

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1010, in step_function  **
        outputs =, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 919, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 1932, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File "/usr/local/lib/python3.7/dist-packages/keras/", line 5247, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)

    ValueError: `logits` and `labels` must have the same shape, received ((None, 8) vs (None, 1)).

I got the code straight from (keras documentation). Before changing num_classes=2 to num_classes=8, the code did ran but only had 0.1 accuracy. My teacher said it is a formatting error but I followed a tutorial and it ran previously. The format is training dataset in /content/gdrive/My Drive/pest/train and validation dataset in /content/gdrive/My Drive/pest/validation.

Any solutions?


The problem is that you are using binary_crossentropy loss even when your problem is not binary.

You need to use categorical_crossentropy for non binary cases.

It will also depend on your labels’ shape: if they are not one-hot-encoded, you need to use sparse_categorical_crossentropy.

My advise is to use always soft max activation for the output with as much neurons as labels, since it works even for two labels:

activation = "softmax"
units = num_classes
# more code...


Answered By – Rodrigo Laguna

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published