【全网首发】 DIoU使用Tensorflow实现

【全网首发】 DIoU使用Tensorflow实现

  计算机视觉方向的人员都知道DIoU是啥,在此不做解释,全网DIoU所查资料全为Torch版本,博主今天将DIoU用 Tensorflow 实现,绝对全网首发!!
下面附上Torch版本实现:

def Diou(bboxes1, bboxes2):
    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    dious = torch.zeros((rows, cols))
    if rows * cols == 0:#
        return dious
    exchange = False
    if bboxes1.shape[0] > bboxes2.shape[0]:
        bboxes1, bboxes2 = bboxes2, bboxes1
        dious = torch.zeros((cols, rows))
        exchange = True
    # #xmin,ymin,xmax,ymax->[:,0],[:,1],[:,2],[:,3]
    w1 = bboxes1[:, 2] - bboxes1[:, 0]
    h1 = bboxes1[:, 3] - bboxes1[:, 1] 
    w2 = bboxes2[:, 2] - bboxes2[:, 0]
    h2 = bboxes2[:, 3] - bboxes2[:, 1]
    
    area1 = w1 * h1
    area2 = w2 * h2

    center_x1 = (bboxes1[:, 2] + bboxes1[:, 0]) / 2 
    center_y1 = (bboxes1[:, 3] + bboxes1[:, 1]) / 2 
    center_x2 = (bboxes2[:, 2] + bboxes2[:, 0]) / 2
    center_y2 = (bboxes2[:, 3] + bboxes2[:, 1]) / 2

    inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:]) 
    inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2]) 
    out_max_xy = torch.max(bboxes1[:, 2:],bboxes2[:, 2:]) 
    out_min_xy = torch.min(bboxes1[:, :2],bboxes2[:, :2])

    inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)
    inter_area = inter[:, 0] * inter[:, 1]
    inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2
    outer = torch.clamp((out_max_xy - out_min_xy), min=0)
    outer_diag = (outer[:, 0] ** 2) + (outer[:, 1] ** 2)
    union = area1+area2-inter_area
    dious = inter_area / union - (inter_diag) / outer_diag
    dious = torch.clamp(dious,min=-1.0,max = 1.0)
    if exchange:
        dious = dious.T
    return dious

接下来是博主用 Tensorflow 复现的代码:

def DIoU(boxes1, boxes2):
    b1 = tf.reshape(tf.tile(tf.expand_dims(boxes1, 1),
                                [1, 1, tf.shape(boxes2)[0]]), [-1, 4])

    b2 = tf.tile(boxes2, [tf.shape(boxes1)[0], 1])

    # Compute intersections
    b1_y1, b1_x1, b1_y2, b1_x2 = tf.split(b1, 4, axis=1)
    b2_y1, b2_x1, b2_y2, b2_x2 = tf.split(b2, 4, axis=1)
    y1 = tf.maximum(b1_y1, b2_y1)
    x1 = tf.maximum(b1_x1, b2_x1)
    y2 = tf.minimum(b1_y2, b2_y2)
    x2 = tf.minimum(b1_x2, b2_x2)

    out_max_xy = tf.maximum(b1[:, 2:], b2[:, 2:])
    out_min_xy = tf.minimum(b1[:, :2], b2[:, :2])

    c_h = tf.maximum(out_max_xy[:, 0] - out_min_xy[:, 0], 0)
    c_w = tf.maximum(out_max_xy[:, 1] - out_min_xy[:, 1], 0)

    # 求中心点
    center_x1 = (b1[:, 3] + b1[:, 1]) / 2
    center_y1 = (b1[:, 2] + b1[:, 0]) / 2
    center_x2 = (b2[:, 3] + b2[:, 1]) / 2
    center_y2 = (b2[:, 2] + b2[:, 0]) / 2

    # 求ρ的平方
    p2 = (center_x2 - center_x1) ** 2 + (center_y2 - center_y1) ** 2
    p2 = tf.expand_dims(p2, axis=-1)

    # 求c的平方
    c2 = c_w ** 2 + c_h ** 2
    c2 = tf.expand_dims(c2, axis=-1)

    intersection = tf.maximum(x2 - x1, 0) * tf.maximum(y2 - y1, 0)
    # Compute unions
    b1_area = (b1_y2 - b1_y1) * (b1_x2 - b1_x1)
    b2_area = (b2_y2 - b2_y1) * (b2_x2 - b2_x1)
    union = b1_area + b2_area - intersection

    # 4. Compute IoU and reshape to [boxes1, boxes2]
    diou = intersection / union - p2 / tf.cast(c2, dtype=tf.float64)
    diou = tf.reshape(diou, [tf.shape(boxes1)[0], tf.shape(boxes2)[0]])

    return diou

大家有什么疑问欢迎交流,最后转载本博客请注明出处。同时也希望大家积极复现GIoU以及CIoU。

你可能感兴趣的:(IoU,tensorflow,pytorch,深度学习)