目标检测assigner部分,匹配之前要计算一下哪些预测box与GT框匹配,可以算一下预测框与GT框的关系
以下get_in_boxes_info
用于判断Box与GT的关系,主要分为两部分,预测box中心是否在GT框内部 和是否在一个圆中(其实不是)
def get_in_boxes_info(self, boxes, target_gts, expanded_strides):
xy_target_gts = bbox_cxcywh_to_xyxy(target_gts) # (x1, y1, x2, y2)
anchor_center_x = boxes[:, 0].unsqueeze(1)
anchor_center_y = boxes[:, 1].unsqueeze(1)
# whether the center of each anchor is inside a gt box
b_l = anchor_center_x > xy_target_gts[:, 0].unsqueeze(0)
b_r = anchor_center_x < xy_target_gts[:, 2].unsqueeze(0)
b_t = anchor_center_y > xy_target_gts[:, 1].unsqueeze(0)
b_b = anchor_center_y < xy_target_gts[:, 3].unsqueeze(0)
# (b_l.long()+b_r.long()+b_t.long()+b_b.long())==4 [300,num_gt] ,
is_in_boxes = ((b_l.cast("int64") + b_r.cast("int64") + \
b_t.cast("int64") + b_b.cast("int64")) == 4)
is_in_boxes_all = is_in_boxes.sum(1) > 0 # [num_query]
# in fixed center
center_radius = 2.5
# Modified to self-adapted sampling --- the center size depends on the size of the gt boxes
# https://github.com/dulucas/UVO_Challenge/blob/main/Track1/detection/mmdet/core/bbox/assigners/rpn_sim_ota_assigner.py#L212
b_l = anchor_center_x > (target_gts[:, 0] - (center_radius * (
xy_target_gts[:, 2] - xy_target_gts[:, 0]))).unsqueeze(0)
b_r = anchor_center_x < (target_gts[:, 0] + (center_radius * (
xy_target_gts[:, 2] - xy_target_gts[:, 0]))).unsqueeze(0)
b_t = anchor_center_y > (target_gts[:, 1] - (center_radius * (
xy_target_gts[:, 3] - xy_target_gts[:, 1]))).unsqueeze(0)
b_b = anchor_center_y < (target_gts[:, 1] + (center_radius * (
xy_target_gts[:, 3] - xy_target_gts[:, 1]))).unsqueeze(0)
is_in_centers = ((b_l.cast("int64") + b_r.cast("int64") + \
b_t.cast("int64") + b_b.cast("int64")) == 4)
is_in_centers_all = is_in_centers.sum(1) > 0
is_in_boxes_anchor = is_in_boxes_all | is_in_centers_all
is_in_boxes_and_center = (is_in_boxes & is_in_centers)
return is_in_boxes_anchor, is_in_boxes_and_center
1. 预测box中心是否在GT框内部
anchor_center_*
是预测框的中心,xy_target_gts
是GT框的坐标
# whether the center of each anchor is inside a gt box
b_l = anchor_center_x > xy_target_gts[:, 0].unsqueeze(0)
b_r = anchor_center_x < xy_target_gts[:, 2].unsqueeze(0)
b_t = anchor_center_y > xy_target_gts[:, 1].unsqueeze(0)
b_b = anchor_center_y < xy_target_gts[:, 3].unsqueeze(0)
# (b_l.long()+b_r.long()+b_t.long()+b_b.long())==4 [300,num_gt] ,
is_in_boxes = ((b_l.cast("int64") + b_r.cast("int64") + \
b_t.cast("int64") + b_b.cast("int64")) == 4)
is_in_boxes_all = is_in_boxes.sum(1) > 0 # [num_query]
2. 是否在一个5倍的矩形框之内
center_radius = 2.5
# Modified to self-adapted sampling --- the center size depends on the size of the gt boxes
# https://github.com/dulucas/UVO_Challenge/blob/main/Track1/detection/mmdet/core/bbox/assigners/rpn_sim_ota_assigner.py#L212
b_l = anchor_center_x > (target_gts[:, 0] - (center_radius * (
xy_target_gts[:, 2] - xy_target_gts[:, 0]))).unsqueeze(0)
b_r = anchor_center_x < (target_gts[:, 0] + (center_radius * (
xy_target_gts[:, 2] - xy_target_gts[:, 0]))).unsqueeze(0)
b_t = anchor_center_y > (target_gts[:, 1] - (center_radius * (
xy_target_gts[:, 3] - xy_target_gts[:, 1]))).unsqueeze(0)
b_b = anchor_center_y < (target_gts[:, 1] + (center_radius * (
xy_target_gts[:, 3] - xy_target_gts[:, 1]))).unsqueeze(0)
下图绿色框是GT,上边这几行代码就是判断是否在蓝色框中,蓝色框是绿色框的五倍,二者中心重合
所以is_in_boxes_all
中为True的部分 应该是 is_in_centers_all
中为True的部分的子集
也就是:
>>> (is_in_boxes_all.cast("float32") <= is_in_centers_all.cast("float32")).all()
Tensor(shape=[1], dtype=bool, place=Place(gpu:0), stop_gradient=True,
[True])