为什么验证精度恒定为20%?

时间:2019-05-06 08:58:29

标签: keras conv-neural-network

我正在尝试使用Keras实现5类动物分类器。我正在从头开始构建CNN,但很奇怪的是,对于所有时期,验证准确性均保持在0.20不变。知道为什么会这样吗?数据集文件夹包含训练,测试和验证文件夹。每个文件夹包含5个文件夹,分别对应5个类。我在做什么错了?

我尝试了多个优化器,但问题仍然存在。我在下面包含了代码示例。

import warnings
warnings.filterwarnings("ignore")

#First convolution layer
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))

#Second convolution layer
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))

#Flatten the outputs of the convolution layer into a 1D contigious array
model.add(Flatten())

#Add a fully connected layer containing 256 neurons
model.add(Dense(256, activation='relu',kernel_initializer='he_normal'))
model.add(BatchNormalization())

#Add another fully connected layer containing 256 neurons
model.add(Dense(256, activation='relu',kernel_initializer='he_normal'))
model.add(BatchNormalization())

#Add the ouput layer containing 5 neurons, because we have 5 categories
model.add(Dense(5, activation='softmax',kernel_initializer='glorot_uniform'))

optim=RMSprop(lr=1e-6)
model.compile(loss='categorical_crossentropy',optimizer=optim,metrics=['accuracy'])
model.summary()

#We will use the below code snippet for rescaling the images to 0-1 for all the train and test images
train_datagen = ImageDataGenerator(rescale=1./255)

#We won't augment the test data. We will just use ImageDataGenerator to rescale the images.
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(train_data_dir,
                                                    classes=['frog', 'giraffe', 'horse', 'tiger','dog'],
                                                    target_size=(img_width, img_height),
                                                    batch_size=batch_size,
                                                    class_mode='categorical',
                                                    shuffle=False)

validation_generator = test_datagen.flow_from_directory(validation_data_dir,
                                                        classes=['frog', 'giraffe', 'horse', 'tiger','dog'],
                                                        target_size=(img_width, img_height),
                                                        batch_size=batch_size,
                                                        class_mode='categorical',
                                                        shuffle=False)
hist=History()

model.fit_generator(train_generator,
                    steps_per_epoch=nb_train_samples // batch_size,
                    epochs=epochs,
                    validation_data=validation_generator,
                    validation_steps=nb_validation_samples // batch_size,
                    callbacks=[hist])

model.save('models/basic_cnn_from_scratch_model.h5') #Save the model weights #Load using: model = load_model('cnn_from_scratch_weights.h5') from keras.models import load_model
print("Time taken to train the baseline model from scratch: ",datetime.now()-global_start)

3 个答案:

答案 0 :(得分:1)

看起来您的输出始终是同一动物,因此您的准确度为20%。我强烈建议您检查测试输出以查看它们是否相同。

您还说过您正在构建一个CNN,但是在发布的代码片段中,我仅看到密集的层,密集的体系结构很难完成此任务,并且它很小。图片的大小是多少?

希望有帮助!

答案 1 :(得分:1)

检查以下内容以获取数据:

  1. 很好地整理训练数据(到处都可以看到shuffle=False
  2. 正确规范化所有数据(我看你在做rescale=1./255,也许还可以)
  3. 正确的火车/ val拆分(您似乎也在这样做)

模型建议:

  1. 使用多个Conv2D层,然后使用最后一个Dense。这是最适合图像分类问题的方法。您还可以查看经过尝试和测试的流行架构。例如AlexNet
  2. 可以将优化器更改为Adam并尝试使用不同的学习率
  3. 查看您的训练和验证损失图,看它们是否符合预期

此外,我想您已按照评论中的说明纠正了第二Conv2D层的形状。

答案 2 :(得分:0)

这些模型现在可以正常工作了。我已删除shuffle = False属性。纠正了第二卷积层的输入形状。将优化器更改为adam。我已经达到了将近94%的验证准确性。但是,我尚未在看不见的数据上测试过该模型。模型中有些过拟合。我将不得不使用一些积极的辍学来减少他们。谢谢!