笔者使用numpy实现了GIOU,废话不多说,直接贴上代码:
import numpy as np
def GIOU (boxes1 , boxes2 ):
"calculate GIOU "
'''
boxes1 shape : shape (n, 4)
boxes2 shape : shape (k, 4)
gious: shape (n, k)
'''
IOU = []
GIOU = []
num = (boxes1[:,0]).size
x1 = boxes1[:,0]
y1 = boxes1[:,1]
x2 = boxes1[:,2]
y2 = boxes1[:,3]
xx1=boxes2[:,0]
yy1=boxes2[:,1]
xx2=boxes2[:,2]
yy2=boxes2[:,3]
area1 = (x2 -x1) * (y2 -y1) #求取框的面积
area2 = (xx2-xx1) * (yy2- yy1)
for i in range (num):
inter_max_x = np.minimum(x2[i], xx2[:]) #求取重合的坐标及面积
inter_max_y = np.minimum(y2[i], yy2[:])
inter_min_x = np.maximum(x1[i], xx1[:])
inter_min_y = np.maximum(y1[i], yy1[:])
inter_w = np.maximum(0 ,inter_max_x-inter_min_x)
inter_h = np.maximum(0 ,inter_max_y-inter_mi