我使用以下行来获取我的测试数据集:
test_ds = keras.utils.image_dataset_from_directory(img_path, image_size=image_size, batch_size = batch_size)
当我在此上运行我的模型时,我会得到以下统计数据:Accuracy = 0.5214362272240086, Precision = 0.5950113378684807, F1-score = 0.5434962717481359
然而,当我以这种方式加载我的数据集时:
_, new_images = keras.utils.image_dataset_from_directory(img_path, shuffle=True, subset="both", seed=1, validation_split=0.9999, image_size=image_size, batch_size = batch_size)
性能统计数据为:Accuracy = 0.9635388739946381, Precision = 0.9658291457286432, F1-score = 0.96875
为什么会发生这种情况?有类似的经历吗?
编辑-代码获取上述指标:
predict = model.predict(new_images)
actual = tf.concat([y for x, y in new_images], axis=0).numpy().tolist()
# Get optimal threshold
fpr, tpr, thresholds = sklearn.metrics.roc_curve(actual, predict)
# Youden's index
J = tpr - fpr
# Optimal threshold
threshold = thresholds[np.argmax(J)]
# Use threshold
predicted = [1 if res > threshold else 0 for res in predict]
# Metrics
print(sklearn.metrics.accuracy_score(actual, predicted), sklearn.metrics.f1_score(actual, predicted), sklearn.metrics.precision_score(actual, predicted)