in evaluation/tiny_benchmark/maskrcnn_benchmark/modeling/rpn/retinanet_fa/retinanet_detail_infer.py [0:0]
def forward_for_single_feature_map(self, anchors, box_cls, box_regression):
"""
Arguments:
anchors: list[BoxList]
box_cls: tensor of size N, A * C, H, W
box_regression: tensor of size N, A * 4, H, W
"""
device = box_cls.device
N, _ , H, W = box_cls.shape
A = int(box_regression.size(1) / 4)
C = int(box_cls.size(1) / A)
# put in the same format as anchors
box_cls = box_cls.view(N, -1, C, H, W).permute(0, 3, 4, 1, 2)
box_cls = box_cls.reshape(N, -1, C)
box_cls = box_cls.sigmoid()
box_regression = box_regression.view(N, -1, 4, H, W)
box_regression = box_regression.permute(0, 3, 4, 1, 2)
box_regression = box_regression.reshape(N, -1, 4)
num_anchors = A * H * W
results = [[] for _ in range(N)]
pre_nms_thresh = self.pre_nms_thresh
candidate_inds = box_cls > self.pre_nms_thresh
if candidate_inds.sum().item() == 0:
return results
pre_nms_top_n = candidate_inds.view(N, -1).sum(1)
pre_nms_top_n = pre_nms_top_n.clamp(max=self.pre_nms_top_n)
for batch_idx, (per_box_cls, per_box_regression, per_pre_nms_top_n, \
per_candidate_inds, per_anchors) in enumerate(zip(
box_cls,
box_regression,
pre_nms_top_n,
candidate_inds,
anchors)):
# Sort and select TopN
per_box_cls = per_box_cls[per_candidate_inds]
per_candidate_nonzeros = per_candidate_inds.nonzero()
per_box_loc = per_candidate_nonzeros[:, 0]
per_class = per_candidate_nonzeros[:, 1]
per_class += 1
if per_candidate_inds.sum().item() > per_pre_nms_top_n.item():
per_box_cls, top_k_indices = \
per_box_cls.topk(per_pre_nms_top_n, sorted=False)
per_box_loc = per_box_loc[top_k_indices]
per_class = per_class[top_k_indices]
detections = self.box_coder.decode(
per_box_regression[per_box_loc, :].view(-1, 4),
per_anchors.bbox[per_box_loc, :].view(-1, 4)
)
boxlist = BoxList(detections, per_anchors.size, mode="xyxy")
boxlist.add_field("labels", per_class)
boxlist.add_field("scores", per_box_cls)
boxlist.add_field("sparse_off", per_box_loc / 9)
boxlist.add_field("sparse_anchor_idx", per_box_loc % 9)
boxlist.add_field("sparse_anchors",
per_anchors.bbox[per_box_loc, :].view(-1, 4))
boxlist.add_field("sparse_batch",
per_box_loc.clone().fill_(batch_idx))
boxlist = boxlist.clip_to_image(remove_empty=False)
boxlist = remove_small_boxes(boxlist, self.min_size)
results[batch_idx] = boxlist
return results