简体   繁体   English

Keras model。使用 ImageDataGenerator 时,评估精度停留在 50%

[英]Keras model.evaluate accuracy stuck at 50 percent while using ImageDataGenerator

I am trying to find the accuracy of my saved Keras model using model.evaluate .我正在尝试使用model.evaluate找到我保存的 Keras model 的准确性。

I have loaded in my model using this:我已经使用这个加载了我的 model:

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

I have a CSV file with two columns, one for the filename of an image and one for the label.我有一个包含两列的 CSV 文件,一列用于图像的文件名,另一列用于 label。 Here is a sample:这是一个示例:

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

I have loaded this CSV into a pandas dataframe and fed it into an ImageDataGenerator :我已将此 CSV 加载到 pandas dataframe 并将其输入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)

Now I try to evaluate my model using:现在我尝试使用以下方法评估我的 model:

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

However, the accuracy doesn't change from 50 percent, but, my model had a 90 percent validation accuracy when trained.但是,准确率不会从 50% 变化,但是,我的 model 在训练时具有 90% 的验证准确率。

Here is what is returned:这是返回的内容:

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

I was able to ensure that my model worked and the generator was properly feeding data, by creating an ROC curve using matplotlib and scikit-learn, which produced a 90 percent AUC, so I'm not sure where the problem is:通过使用 matplotlib 和 scikit-learn 创建 ROC 曲线,我能够确保我的 model 正常工作并且生成器正确输入数据,所以我不确定问题出在哪里:

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()

Similar questions say that the problem came from setting shuffle parameter in the ImageDataGenerator to True , but mine has always been set to False .类似的问题说问题来自将 ImageDataGenerator 中的 shuffle 参数设置为True ,但我的一直设置为False Another similar problem was fixed by retraining with a sigmoid activation rather than softmax, but I used sigmoid in my final layer, so that can't be the problem另一个类似的问题是通过使用 sigmoid 激活而不是 softmax 重新训练来解决的,但是我在最后一层使用了 sigmoid,所以这不是问题

This is my first time using Keras.这是我第一次使用 Keras。 What did I do wrong?我做错了什么?

The problem was because of class_mode parameter in flow function.问题是因为流 function 中的class_mode参数。 Default is categorical .默认是categorical的。

Setting it as binary solved the problem.将其设置为binary解决了这个问题。 Corrected code:更正的代码:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM