Newton
Newton

Reputation: 65

Wrong number of classes in Keras

Keras is finding a wrong number of classes in train and test set folders. I have 3 classes, but it keeps saying that there are 4. Can anyone help me, please?

Here the code:

cnn = Sequential()

cnn.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))


cnn.add(Conv2D(32, (3, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))


cnn.add(Conv2D(64, (3, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))

cnn.add(Conv2D(128, (3, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))

#Full connection
cnn.add(Dense(units = 64, activation = 'relu'))
cnn.add(Dense(units = 64, activation = 'relu'))
cnn.add(Dense(units = 3, activation = 'softmax'))

# Compiling the CNN
cnn.compile(optimizer = OPTIMIZER, loss = 'categorical_crossentropy', metrics = ['accuracy'])


     #Fitting
    from keras.preprocessing.image import ImageDataGenerator

    train_datagen = ImageDataGenerator(rescale = 1./255,
                                       shear_range = 0.2,
                                       zoom_range = 0.2,
                                       horizontal_flip = True)

    test_datagen = ImageDataGenerator(rescale = 1./255)

    training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                     target_size = tgt_size,
                                                     batch_size = batch_size,
                                                     class_mode = 'categorical')

    test_set = test_datagen.flow_from_directory('dataset/test_set',
                                                target_size = tgt_size,
                                                batch_size = batch_size,
                                                class_mode = 'categorical')

And the error:

Found 12000 images belonging to 4 classes.
Found 3000 images belonging to 4 classes.

Epoch 1/10
---------------------------------------------------------------------------
ValueError: Error when checking target: expected dense_15 to have 4 dimensions, but got array with shape (3, 4)

EDIT:

It only happens with Jupyter Notebook in Google Cloud. When I use Spyder locally, it finds the correct number of classes.

Upvotes: 4

Views: 3097

Answers (2)

Misan
Misan

Reputation: 187

For people still having issues with this on google colab. As stated above by bennyOoO, " Jupyter creates hidden checkpoint folders for backup purposes. That's why there is always one class (as in folder) extra when using flow_from_directory."

import os
import shutil

os.listdir("/content/some_data/or_some_train_data") #First find where the ".ipynb_checkpoints" is located.

shutil.rmtree("directory_where_.ipynb_checkpoints_is_located/.ipynb_checkpoints") #be careful with shutil.rmtree() because it deletes every tree in that path. In other words, do not make mistakes.

Upvotes: 4

bennyOoO
bennyOoO

Reputation: 358

As you might have found out by yourself by now, Jupyter creates hidden checkpoint folders for backup purposes. That's why there is always one class (as in folder) extra when using flow_from_directory. The simplest solution would be to just delete that hidden folder.

Upvotes: 7

Related Questions