如何从 Faster-RCNN 计算 F1 分数和其他分类指标? (PyTorch 中的对象检测)
我正在尝试解决这个问题,但很难理解如何在对象检测任务中计算 f1 分数。
理想情况下,我想知道图像中每个目标的假阳性、真阳性、假阴性和真阴性(这是一个二元问题,图像中的对象作为一类,背景作为另一类)。
最终我还想从图像中提取误报边界框。我不确定这是否有效,但我会将图像名称和 bbox 预测以及它们是否误报等保存到 numpy 文件中。
我目前将批量大小设置为 1,这样我就可以对每个图像应用非极大值抑制算法:
def apply_nms(orig_prediction, iou_thresh=0.3):
# torchvision returns the indices of the bboxes to keep
keep = torchvision.ops.nms(orig_prediction['boxes'], orig_prediction['scores'], iou_thresh)
final_prediction = orig_prediction
final_prediction['boxes'] = final_prediction['boxes'][keep]
final_prediction['scores'] = final_prediction['scores'][keep]
final_prediction['labels'] = final_prediction['labels'][keep]
return final_prediction
cpu_device = torch.device("cpu")
model.eval()
with torch.no_grad():
for images, targets in valid_data_loader:
images = list(img.to(device) for img in images)
outputs = model(images)
outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs]
predictions = apply_nms(outputs[0], iou_thresh=0.3)
关于如何确定上述分类指标和 f1 分数,有什么想法吗?
我在 torchvision 提供的评估代码中遇到了这一行,想知道它是否会帮助我继续前进:
res = {target["image_id"].item(): output for target, output in zip(targets, outputs)}
I'm trying to wrap my head around this but struggling to understand how I can compute the f1-score in an object detection task.
Ideally, I would like to know false positives, true positives, false negatives and true negatives for every target in the image (it's a binary problem with an object in the image as one class and the background as the other class).
Eventually I would also like to extract the false positive bounding boxes from the image. I'm not sure if this is efficient but I'd save the image names and bbox predictions and whether they are false positives etc. into a numpy file.
I currently have this set up with a batch size of 1 so I can apply a non-maximum suppression algorithm per image:
def apply_nms(orig_prediction, iou_thresh=0.3):
# torchvision returns the indices of the bboxes to keep
keep = torchvision.ops.nms(orig_prediction['boxes'], orig_prediction['scores'], iou_thresh)
final_prediction = orig_prediction
final_prediction['boxes'] = final_prediction['boxes'][keep]
final_prediction['scores'] = final_prediction['scores'][keep]
final_prediction['labels'] = final_prediction['labels'][keep]
return final_prediction
cpu_device = torch.device("cpu")
model.eval()
with torch.no_grad():
for images, targets in valid_data_loader:
images = list(img.to(device) for img in images)
outputs = model(images)
outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs]
predictions = apply_nms(outputs[0], iou_thresh=0.3)
Any idea on how I can determine the aforementioned classification metrics and f1-score?
I've come across this line in an evaluation code provided by torchvision and wondering whether it would help me going forward:
res = {target["image_id"].item(): output for target, output in zip(targets, outputs)}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,我实现了全局计算的 f1 分数 - 即针对整个数据集。
下面的实现给出了确定验证集的 f1 分数的示例。
模型的输出采用字典格式,因此我们需要将它们放入张量中,如下所示:
train_index:特定 bbox 来自的图像的索引
class_prediction:表示类别预测的整数值
prob_score:bbox 输出的客观性分数
x1,y1,x2,y2:(x1,y1) 和 (x2,y2) bbox 坐标
其中
prob_score
只是地面实况输入的1
(它可以是任何值)实际上只要指定并填写该尺寸即可)。IoU 也在 torchvision 中实现,这使得一切变得更加容易。
我希望这对其他人有帮助,因为我在其他地方找不到对象检测中 f1 分数的另一个实现。
So I've implemented the f1 score to be calculated globally- that is for the entire dataset.
The implementation below gives an example of determining the f1-score for a validation set.
The outputs of the model are in a dictionary format, and so we need to place them into tensors like this:
train_index: index of image that the specific bbox comes from
class_prediction: integer value representing class prediction
prob_score: outputed objectiveness score for a bbox
x1,y1,x2,y2: (x1, y1) and (x2,y2) bbox coordinates
Where
prob_score
is just1
for the ground truth inputs (it could be anything really as long as that dimension is specified and filled in).IoU is also implemented in torchvision which makes everything a lot easier.
I hope this helps others as I couldn't find another implementation of f1 score in object detection anywhere else.
目标检测中术语精度、召回率和 F1 分数的使用有点令人困惑,因为这些指标最初用于二元评估任务(例如分类)。无论如何,在对象检测中它们的含义略有不同:
让:
TP - 与地面真实对象成功匹配的预测对象集(高于您使用的任何数据集的 IOU 阈值,通常为 0.5 或 0.7)
FP - 未与地面真实对象成功匹配的预测对象集
FN - 未成功与预测对象匹配的地面实况对象集
您可以找到许多匹配步骤的实现(匹配地面实况和预测对象),通常提供用于评估的数据集,或者您可以自己实现。我建议使用 py-motmetrics 存储库。
IOU 计算的简单实现可能如下所示:
The use of the terms precision, recall, and F1 score in object detection are slightly confusing because these metrics were originally used for binary evaluation tasks (e.g. classifiation). In any case, in object detection they have slightly different meanings:
let:
TP - set of predicted objects that are successfully matched to a ground truth object (above IOU threshold for whatever dataset you're using, generally 0.5 or 0.7)
FP - set of predicted objects that were not successfully matched to a ground truth object
FN - set of ground truth objects that were not successfully matched to a predicted object
You can find many an implementation of the matching step (matching ground truth and predicted objects) generally provided with an dataset for evaluation, or you can implement it yourself. I'll suggest the py-motmetrics repository.
A simple implementation of the IOU calculation might look like: