深度学习手撕系列整理(待完善)

import numpy as np
import torch

def IOU(box1, box2, wh=False):
    if wh == False:
        xmin1,ymin1,xmax1,ymax1 = box1
        xmin2,ymin2,xmax2,ymax2 = box2
    else:
        xmin1,ymin1 = int(box1[0] - box1[2] / 2.0), int(box1[1] - box1[3] / 2.0)
        xmax1,ymax1 = int(box1[0] + box1[2] / 2.0), int(box1[1] + box1[3] / 2.0)
        xmin2,ymin2 = int(box2[0] - box2[2] / 2.0), int(box2[1] - box2[3] / 2.0)
        xmax2,ymax2 = int(box2[0] + box2[2] / 2.0), int(box2[1] + box2[3] / 2.0)
    
    xx1 = np.max([xmin1,xmin2])
    yy1 = np.max([xmin1,ymin2])
    xx2 = np.min([xmax1,xmax2])
    yy2 = np.min([ymax1,ymax2])

    area1 = (xmax1 - xmin1) * (ymax1 - ymin1)
    area2 = (xmax2 - xmin2) * (ymax2 - ymin2)
    inter_area = (np.max([0,xx2-xx1]) * np.max([0,yy2-yy1]))
    iou = inter_area / (area1 + area2 - inter_area)
    return iou

def GIOU(box1,box2):
    xmin1,ymin1,xmax1,ymax1 = box1
    xmin2,ymin2,xmax2,ymax2 = box2
    iou = IOU(box1,box2)
    area_C = (max(xmin1,xmax2,xmin2,xmax1) - min(xmin1,xmax2,xmin2,xmax1)) * (max(ymax1,ymax2,ymin1,ymin2) - min(ymax1,ymax2,ymin1,ymin2))
    area_1 = (xmax1-xmin1) * (ymin1 - ymax1)
    area_2 = (xmax2-xmin1) * (ymin2 - ymax2)
    sum_area = area_1 + area_2

    w1 = xmax1 - xmin1
    w2 = xmax2 - xmin2
    h1 = ymin1 - ymax2
    h2 = ymin2 - ymax2
    W = min(xmin1,xmin2,xmax1,xmax2) - max(xmin1,xmin2,xmax1,xmax2)
    H = min(ymin1,ymin2,ymax1,ymax2) - max(ymin1,ymin2,ymax1,ymax2)

    Area = W * H
    add_area = sum_area - Area

    end_area = (area_C - add_area) / area_C
    giou = iou - end_area
    
    return giou


def DIOU(box1,box2):
    rows = box1.shape[0]
    cols = box2.shape[0]

    dious = torch.zeros((rows,cols))
    if rows * cols == 0:
        return dious
    exchange = False
    if box1.shape[0] > box2.shape[0]:
        box1,box2 = box2,box1
        dious = torch.zeros((cols,rows))
        exchange = True
    w1 = box1[:2] - box1[:0]
    h1 = box1[:3] - box1[:1]
    w2 = box2[:2] - box2[:0]
    h2 = box2[:3] - box2[:1]

    area1 = w1 * h1
    area2 = w2 * h2

    center_x1 = (box1[:2]+box1[:0]) / 2.0
    center_y1 = (box1[:1]+box1[:3]) / 2.0
    center_x2 = (box2[:2]+box2[:0]) / 2.0
    center_y2 = (box2[:1]+box2[:3]) / 2.0

    inter_max_xy = torch.min(box1[:,2:],box2[:,2:])
    inter_min_xy = torch.max(box1[:,:2],box2[:,:2])
    out_max_xy = torch.max(box1[:,2:],box2[:,2:])
    out_min_xy = torch.min(box1[:,:2],box2[:,: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


def auc(pred,gt):
    for i in range(len(gt) - 1):
        for j in range(len(gt)):
            if(gt[i] != gt[j]):
                d += 1
                if(gt[i] > gt[j] and pred[i] > pred[j]) or (gt[i] < gt[j] and pred[i] < pred[j]):
                    n+=1
    return n / d

 

posted @ 2022-05-12 20:52  fourmii  阅读(91)  评论(0编辑  收藏  举报