CNN模型未能学习

我正在构建一个CNN模型来对图像进行分类,但我猜测我的模型未能学习,因为准确率和损失函数的值一直保持不变。请看下面的代码:

构建图像训练、测试和验证数据集

import pandas as pdfrom keras_preprocessing.image import ImageDataGeneratorimport numpy as np#从三个.txt文件中创建三个数据集。trainingfile = pd.read_table('data/training.txt', delim_whitespace=True, names=('class', 'image'))testingfile = pd.read_table('data/testing.txt', delim_whitespace=True, names=('class', 'image'))validationfile = pd.read_table('data/validation.txt', delim_whitespace=True, names=('class', 'image'))#改变目标变量类型trainingfile = trainingfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])testingfile = testingfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])validationfile = validationfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])#数据增强datagen=ImageDataGenerator()train_datagen = ImageDataGenerator(     #我们应用少量旋转,因为通常照片是居中的    rotation_range=5,    zoom_range=0.1)#最终包含图像的数据集train=train_datagen.flow_from_dataframe(dataframe=trainingfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb',batch_size=32)test=datagen.flow_from_dataframe(dataframe=testingfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb',batch_size=32)#对验证数据集不进行数据增强。validation=datagen.flow_from_dataframe(dataframe=validationfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb', batch_size=32)

CNN模型

from keras.models import Sequentialfrom keras.layers import Dense, Conv2D, Flatten, Activation, Dropout, MaxPooling2D, BatchNormalizationfrom keras.constraints import maxnorm#创建模型model = Sequential()#第一卷积块model.add(Conv2D(32, kernel_size = (3, 3), activation='relu', input_shape=(256, 256,3)))model.add(MaxPooling2D(pool_size=(2,2)))model.add(BatchNormalization())#第二卷积块model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(BatchNormalization())#第三卷积块model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(BatchNormalization())#第四卷积块model.add(Conv2D(96, kernel_size=(3,3), activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(BatchNormalization())#第五卷积块model.add(Conv2D(32, kernel_size=(3,3), activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(BatchNormalization())#Dropoutmodel.add(Dropout(0.2))model.add(Flatten())model.add(Dense(128, activation='relu'))#model.add(Dropout(0.3))model.add(Dense(3, activation = 'softmax'))from keras import regularizers, optimizersfrom keras.optimizers import RMSpropfrom keras.callbacks import EarlyStopping# 编译模型model.compile(optimizer='adam',loss="categorical_crossentropy",metrics=["accuracy"])# 提前停止es = EarlyStopping(monitor='val_loss', mode='min', verbose=1,patience=10)

训练模型

h=model.fit_generator(generator=train,                      validation_data=validation,                      epochs=50,                      callbacks=[es])

结果

enter image description here

这是我第一次使用fit_generator,可能我没有正确使用它?


回答:

从结果图中可以看出,你只训练了一个epoch。这可能是因为提前停止的设置过于严格。尝试将patience设置为3来调整EarlyStopping回调。

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3)

编辑

过拟合示例:

enter image description here

查看这篇文章,了解更多关于如何处理过拟合的信息。

Related Posts

神经网络反向传播代码不工作

我需要编写一个简单的由1个输出节点、1个包含3个节点的…

值错误:y 包含先前未见过的标签:

我使用了 决策树分类器,我想将我的 输入 作为 字符串…

使用不平衡数据集进行特征选择时遇到的问题

我正在使用不平衡数据集(54:38:7%)进行特征选择…

广义随机森林/因果森林在Python上的应用

我在寻找Python上的广义随机森林/因果森林算法,但…

如何用PyTorch仅用标量损失来训练神经网络?

假设我们有一个神经网络,我们希望它能根据输入预测三个值…

什么是RNN中间隐藏状态的良好用途?

我已经以三种不同的方式使用了RNN/LSTM: 多对多…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注