anchor_target_layer函数,在network.py中作为一个层被调用到,network并且还加了一些其他的处理,封装在_anchor_target_layer函数里面。
这里面有三个函数方法,一个主要的就是anchor_target_layer,其他两个方法都是为这个函数方法服务的。
anchor_target_layer主要针对RPN的输出进行处理,对RPN的输出结果加工,对anchor打上标签,然后通过与groundtrue的信息比对,计算出与真实框的偏差,这些都指向了为计算loss误差做准备。
# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick and Xinlei Chen
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from model.config import cfg
import numpy as np
import numpy.random as npr
from utils.cython_bbox import bbox_overlaps
from model.bbox_transform import bbox_transform
#输入:rpn分类得分,gt、图像、所有框坐标和索引、总框数量
def anchor_target_layer(rpn_cls_score, gt_boxes, im_info, _feat_stride, all_anchors, num_anchors):
"""Same as the anchor target layer in original Fast/er RCNN """
A = num_anchors#A=总anchor数量
total_anchors = all_anchors.shape[0]#用shape[0]来统计行数从而统计框数量
K = total_anchors / num_anchors#框数量和anchor数量的比例,统计一下平均每一个anchor center 有几个框被选取。
# allow boxes to sit over the edge by a small amount
_allowed_border = 0#允许框是紧贴图像边缘的
# map of shape (..., H, W)
height, width = rpn_cls_score.shape[1:3]#RPN输出得分图的长和宽,应该是第二个reshape之后的,wxh是特征图的大小,代表的是anchor的数量。
# only keep anchors inside the image
#过滤掉不在图像范围内的Boxes,首先用where函数加条件筛选出索引
inds_inside = np.where(
(all_anchors[:, 0] >= -_allowed_border) &
(all_anchors[:, 1] >= -_allowed_border) &
(all_anchors[:, 2] <