目标检测:交并比计算和bbox回归操作

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import logging
import numpy as np

import paddle.fluid as fluid

__all__ = ["bbox_overlaps", "box_to_delta"]

logger = logging.getLogger(__name__)


def bbox_overlaps(boxes_1, boxes_2):
    '''
    ex_roi 用来回归的anchor
    gt_roi 每个anchor对应的ground truth
    在进行回归前,保证每个需要回归的anchor都有一个gtbox作为回归的目标
    计算dx,dy时,使用的是anchor和gtbox的中心点,比如中心点x方向距离/anchor的w
    计算dw,dh时,使用的是对数log形式 np.log(gt_widths / ex_widths)
    bbox_overlaps
        boxes_1: x1, y, x2, y2
        boxes_2: x1, y, x2, y2
    '''
    assert boxes_1.shape[1] == 4 and boxes_2.shape[1] == 4

    num_1 = boxes_1.shape[0]
    num_2 = boxes_2.shape[0]

    x1_1 = boxes_1[:, 0:1]
    y1_1 = boxes_1[:, 1:2]
    x2_1 = boxes_1[:, 2:3]
    y2_1 = boxes_1[:, 3:4]
    area_1 = (x2_1 - x1_1 + 1) * (y2_1 - y1_1 + 1)

    x1_2 = boxes_2[:, 0].transpose()
    y1_2 = boxes_2[:, 1].transpose()
    x2_2 = boxes_2[:, 2].transpose()
    y2_2 = boxes_2[:, 3].transpose()
    area_2 = (x2_2 - x1_2 + 1) * (y2_2 - y1_2 + 1)

    xx1 = np.maximum(x1_1, x1_2)
    yy1 = np.maximum(y1_1, y1_2)
    xx2 = np.minimum(x2_1, x2_2)
    yy2 = np.minimum(y2_1, y2_2)

    w = np.maximum(0.0, xx2 - xx1 + 1)
    h = np.maximum(0.0, yy2 - yy1 + 1)
    inter = w * h

    ovr = inter / (area_1 + area_2 - inter)
    return ovr


def box_to_delta(ex_boxes, gt_boxes, weights):
    """ box_to_delta """
    ex_w = ex_boxes[:, 2] - ex_boxes[:, 0] + 1
    ex_h = ex_boxes[:, 3] - ex_boxes[:, 1] + 1
    ex_ctr_x = ex_boxes[:, 0] + 0.5 * ex_w
    ex_ctr_y = ex_boxes[:, 1] + 0.5 * ex_h

    gt_w = gt_boxes[:, 2] - gt_boxes[:, 0] + 1
    gt_h = gt_boxes[:, 3] - gt_boxes[:, 1] + 1
    gt_ctr_x = gt_boxes[:, 0] + 0.5 * gt_w
    gt_ctr_y = gt_boxes[:, 1] + 0.5 * gt_h

    dx = (gt_ctr_x - ex_ctr_x) / ex_w / weights[0]
    dy = (gt_ctr_y - ex_ctr_y) / ex_h / weights[1]
    dw = (np.log(gt_w / ex_w)) / weights[2]
    dh = (np.log(gt_h / ex_h)) / weights[3]

    targets = np.vstack([dx, dy, dw, dh]).transpose()
    return targets

 

你可能感兴趣的:(数字图像处理,图像处理,深度学习)