ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1))

2.4k views Asked by At

I try to make a convolutional neural network in order to classify dogs and cat. I got error mentioned in the title.

From my search, some said that error belongs to different versions of tensorflow and keras libraries, others said that it is a syntax error. I will leave my code here, tell me where i made mistakes.

#IMPORTING LIBRARIES
import tensorflow as tf
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator



#IMAGE DATA PREPROCESSING

#preprocessing the training set
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\training_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')

#preprocessing the test set
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\test_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')


 
#BULDING THE CNN
#
#
#initialising the cnn
cnn = tf.keras.models.Sequential()


#convolution
cnn.add(tf.keras.Input(shape=(64, 64, 3)))
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))


#pooling
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#adding a SECOND CONVOLUTIONAL LAYER
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu'))
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#flattening
cnn.add(tf.keras.layers.Flatten())


#full connection
cnn.add(tf.keras.layers.Dense(units = 128 , activation = 'relu'))


#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 4 , activation = 'sigmoid'))




#TRAINING THE CNN
#
#
#compiling the cnn
cnn.compile(optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'] )


#training the cnn on the training_set & test_set
cnn.fit(x = training_set , validation_data = test_set , epochs = 25)



#MAKING A SINGLE PREDICTION
import numpy as np
test_image = image.load_img('dataset/single_predictioncat_or_dog_1.jpg' , test_size = (64 , 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image , axis = 0)
result = cnn.predic(test_image)
training_set.class_indices

and the error is:

ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1))
1

There are 1 answers

0
Rauf Bhat On

Change the last dense layer. Instead of 4 make it 1.

#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 1 , activation = 'sigmoid'))

This might be happening because your label is a single value for each input. Whereas, your final layer defined has 4 outputs. The loss function is not able compare two different shapes.

If you still want to add more than one unit in the final dense layer, make all your outputs a one hot vector and add as many units in the dense layer as there are labels in your data set.