【全網首發】 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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章