前言:
在目标检测的过程中,RPN生成的所有锚点(anchor)需要找到与之对应的在原图上标明的基准边框(gt)。在maskrcnn_benchmark中,这一匹配的过程由matcher.py来完成。该类定义了锚点与基准边框匹配的规则,将锚点与基准边框的IoU小于某个阈值的锚点设置为背景,大于另一个阈值的锚点设置为含目标的锚点,介于两个阈值之间的标记为第三种锚点。这种分类有助于RPN的后续操作,其代码与详细注释为:
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch
class Matcher(object):
"""
这个类主要实现将RPN提取出来的所有锚点(anchor)与标注的基准边框(ground truth box)进行匹配
每一个锚点都会匹配一个与之对应的基准边框,当锚点与基准边框的Iou小于一定值时,认定其找不到对应
的边框,认定其为背景。每一个基准边框对应0个或者多个锚点
这个匹配操作是基于计算过的各个锚点与基准边框之间的IoU的MxN矩阵(match_quality_matrix)来
进行的。其中M为基准边框的个数,N为锚点的个数。IoU矩阵的每一列表示某个锚点与所有各个基准框之间
的IoU,每一行表示每个基准边框与所有各个锚点之间的IoU
本Matcher类返回一个长度为N的向量,其表示每一个锚点的类型:背景-1,介于背景和目标之间-2以及
目标边框(各自对应的基准边框的索引)
This class assigns to each predicted "element" (e.g., a box) a ground-truth
element. Each predicted element will have exactly zero or one matches; each
ground-truth element may be assigned to zero or more predicted elements.
Matching is based on the MxN match_quality_matrix, that characterizes how well
each (ground-truth, predicted)-pair match. For example, if the elements are
boxes, the matrix may contain box IoU overlap values.
The matcher returns a tensor of size N containing the index of the ground-truth
element m that matches to prediction n. If there is no match, a negative value
is returned.
"""
# 如果锚点与基准边框的IoU小于某个阈值则让他对应的基准边框索引为