如何在训练 SSD 后预测 Precision、Recall 和 F1 分数

How to predict Precision, Recall and F1 score after training SSD

我是深度学习的新手,我希望能够使用 F1 分数评估经过特定时期训练的模型。我认为这需要先计算精度和召回率。

训练的模型是SSD-300-Tensorflow。是否有代码或其他东西可以产生这些结果?我没有使用 sci-kit 或任何东西,因为我不确定计算分数是否需要这样做,但欢迎任何指导。

文件夹 tf_extended 中有一个名为 metrics.py 的评估文件,其中包含精确度和召回率代码。训练我的文件后,我在日志文件夹中有检查点。我如何计算我的指标?由于硬件限制(GPU 问题),我正在使用 Google collab

您应该首先计算假阳性、假阴性、真阳​​性和真阴性。要获得这些值,您必须使用测试数据集评估您的模型。 This link 可能有帮助

使用这些公式,您可以计算精度和召回率,下面是一些示例代码:

y_hat = []
y = []
threshold = 0.5
for data, label in test_dataset:
    y_hat.extend(model.predict(data))
    y.extend(label.numpy()[:, 1])
    y_hat = np.asarray(y_hat)
    y = np.asarray(y)
    m = len(y)
    y_hat = np.asarray([1 if i > threshold else 0 for i in y_hat[:, 1]])
    true_positive = np.logical_and(y, y_hat).sum()
    true_negative = np.logical_and(np.logical_not(y_hat), np.logical_not(y)).sum()
    false_positive = np.logical_and(np.logical_not(y), y_hat).sum()
    false_negative = np.logical_and(np.logical_not(y_hat), y).sum()
    total = true_positive + true_negative + false_negative + false_positive
    assert total == m
    precision = true_positive / (true_positive + false_positive)
    recall = true_positive / (true_positive + false_negative)
    accuracy = (true_positive + true_negative) / total
    f1score = 2 * precision * recall / (precision + recall)

如果您使用的是 Tensorflow 对象检测 API,它提供了一种 运行 模型评估方法,可以针对不同的指标进行配置。有关如何执行此操作的教程是 here.

COCO 评估指标包括对象检测用例的精确度和召回率的类似度量。 here 很好地概述了这些指标。精确度和召回率的概念需要针对对象检测场景进行一些调整,因为您必须定义预测的边界框需要与地面实况边界框匹配“多近”才能被视为真阳性。

我不确定 F1 分数的模拟对于对象检测场景是什么。通常,我看到使用 mAP 作为单一评估指标比较模型。