代码来自
github: https://github.com/smallcorgi/Faster-RCNN_TF
# ex_roi 用来回归的anchor
# gt_roi 每个anchor对应的ground truth
# 在进行回归前,保证每个需要回归的anchor都有一个gtbox作为回归的目标
def bbox_transform(ex_rois, gt_rois):
#计算dx,dy时,使用的是anchor和gtbox的中心点,比如中心点x方向距离/anchor的w
#计算dw,dh时,使用的是对数log形式 np.log(gt_widths / ex_widths)
ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0
ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0
ex_ctr_x = ex_rois[:, 0] + 0.5 * ex_widths
ex_ctr_y = ex_rois[:, 1] + 0.5 * ex_heights
gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0
gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0
gt_ctr_x = gt_rois[:, 0] + 0.5 * gt_widths
gt_ctr_y = gt_rois[:, 1] + 0.5 * gt_heights
targets_dx = (gt_ctr_x - ex_ctr_x) / ex_widths
targets_dy = (gt_ctr_y - ex_ctr_y) / ex_heights
targets_dw = np.log(gt_widths / ex_widths)
targets_dh = np.log(gt_heights / ex_heights)
targets = np.vstack(
(targets_dx, targets_dy, targets_dw, targets_dh)).transpose()
return targets