Keras模型评估准确率卡在50%,使用ImageDataGenerator

我正在尝试使用model.evaluate来找出我保存的Keras模型的准确率。

我使用以下方式加载了我的模型:

model = keras.models.load_model("../input/modelpred/2_convPerSection_4_sections")

我有一个CSV文件,包含两列,一列是图像的文件名,另一列是标签。以下是一个示例:

id,label
95d04f434d05c1565abdd1cbf250499920ae8ecf.tif,0
169d0a4a1dbd477f9c1a00cd090eff28ac9ef2c1.tif,0
51cb2710ab9a05569bbdedd838293c37748772db.tif,1
4bbb675f8fde60e7f23b3354ee8df223d952c83c.tif,1
667a242a7a02095f25e0833d83062e8d14a897cd.tif,0

我将这个CSV文件加载到pandas数据框中,并将其输入到ImageDataGenerator中:

df = pd.read_csv("../input/cancercsv/df_test.csv", dtype=object)
test_path = "../input/histopathologic-cancer-detection/train"
test_data_generator = ImageDataGenerator(rescale=1./255).flow_from_dataframe(dataframe = df,                                                                                  directory=test_path,                                                                                  x_col = "id",                                                                                  y_col = "label",                                                                                  target_size=(96,96),                                                                                  batch_size=16,                                                                                  shuffle=False)

现在我尝试使用以下代码评估我的模型:

val = model.evaluate(test_data_generator, verbose = 1)
print(val)

然而,准确率始终停留在50%,但我的模型在训练时的验证准确率达到了90%。

以下是返回的结果:

163/625 [======>.......................] - ETA: 21s - loss: 1.1644 - accuracy: 0.5000

我通过使用matplotlib和scikit-learn创建ROC曲线,确保了我的模型正常工作并且生成器正确地输入数据,这产生了90%的AUC,所以我不确定问题出在哪里:

predictions = model.predict_generator(test_data_generator, steps=len(test_data_generator), verbose = 1)
false_positive_rate, true_positive_rate, threshold = roc_curve(test_data_generator.classes, np.round(predictions))
area_under_curve = auc(false_positive_rate, true_positive_rate)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(false_positive_rate, true_positive_rate, label='AUC = {:.3f}'.format(area_under_curve))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()

类似的提问指出问题出在ImageDataGenerator中的shuffle参数设置为True,但我的始终设置为False。另一个类似的问题通过重新训练并使用sigmoid激活函数而不是softmax来解决,但我最后的层使用了sigmoid,所以这不可能是问题所在。

这是我第一次使用Keras。我做错了什么?


回答:

问题出在flow函数中的class_mode参数。默认是categorical

将其设置为binary解决了问题。修正后的代码如下:

test_data_generator = ImageDataGenerator(rescale=1./255).flow_from_dataframe(dataframe = df,                                                                                  directory=test_path,                                                                                  x_col = "id",                                                                                  y_col = "label",                                                                                 class_mode = 'binary',                                                                                  target_size=(96,96),                                                                                  batch_size=16,                                                                                  shuffle=False)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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