Keras中的类数错误

Keras中的类数错误,keras,conv-neural-network,Keras,Conv Neural Network,Keras在train和test set文件夹中发现了错误数量的类。我有3节课,但它一直说有4节。有人能帮我吗 代码如下: 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),

Keras在train和test set文件夹中发现了错误数量的类。我有3节课,但它一直说有4节。有人能帮我吗

代码如下:

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')
错误是:

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)
编辑:


这只发生在谷歌云中的Jupyter笔记本上。当我在本地使用Spyder时,它会找到正确数量的类。

正如您现在可能已经发现的那样,Jupyter会创建隐藏的检查点文件夹以进行备份。这就是为什么在使用来自目录的flow_时,总是有一个额外的类(如在文件夹中)。
最简单的解决方案是删除隐藏的文件夹。

对于在google colab上仍有问题的用户。正如bennyOoO所述, “Jupyter为备份目的创建隐藏的检查点文件夹。这就是为什么在使用\u目录中的flow\u时,总是有一个额外的类(如文件夹中的类)。”


你试过在第一个密集层之前展平吗?每个文件夹都是一个类,那么你在训练集和测试集文件夹中有多少个文件夹?@MatiasValdenegro我有4个文件夹。然后keras会看到4个类,为什么你认为你有3个类?就是这样,当我使用4个类时,它说我有5个,当我有3个时,它会找到4个。对我来说,这是一个pycache文件夹,里面有一个init.py。但同样的原因。
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.