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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With