【參考文檔】江大白的yolo解析
后面會給出我的完整代碼,先來分段看看!
轉化格式
if x1y1x2y2: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
else: # transform from xywh to xyxy
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
轉換成這種格式:
IOU
這個應該都很熟了
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
#.clamp:將小于0的元素修改為0,截斷元素的取值空
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + ep
iou = inter / union
clamp
def clamp(self, min: _float=-inf, max: _float=inf, *, out: Optional[Tensor]=None) -> Tensor: ...
inf:無窮大
-inf:負無窮
out:輸出,默認即可,不用設定
在 yolov5的使用中,應該是截斷掉小于0的部分
(torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0)
torch.clamp
DIOU
在正式進入各種iou之前
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # 最小包裹矩形寬度
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # 最小包裹矩形高度
cw :最小外包矩形寬度
ch :最小外包矩形高度
分子部分的一次項代表:GT框和bbox框中心點的距離
c:兩個框對角線的距離
考慮了重疊面積和中心點距離
c2 = cw ** 2 + ch ** 2 + eps # 勾股定理,使用兩邊的平方和來代替斜邊的平方
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4
if DIoU:
return iou - rho2 / c2 # DIoU
c2: 勾股定理,使用兩邊的平方和來代替斜邊的平方
GIOU
c_area = cw * ch + eps # convex area
return iou - (c_area - union) / c_area # GIoU
CIoU
CIOU_Loss和DIOU_Loss前面的公式都是一樣的,不過在此基礎上還增加了一個影響因子,將預測框和目標框的長寬比都考慮了進去。
其中v是衡量長寬比一致性的參數,我們也可以定義為:
這樣CIOU_Loss就將目標框回歸函數應該考慮三個重要幾何因素:重疊面積、中心點距離,長寬比全都考慮進去了。
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU
EIOU
【參考博文】IOU、GIOU、DIOU、CIOU、EIOU、Focal EIOU、alpha IOU損失函數
前兩部分延續(xù)CIOU中的方法,但是寬高損失直接使目標盒與錨盒的寬度和高度之差最小,使得收斂速度更快。
該損失函數包含三個部分:重疊損失,中心距離損失,寬高損失
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
elif EIoU:
w=(w1-w2)*(w1-w2)
h=(h1-h2)*(h1-h2)
return iou-(rho2/c2+w/(cw**2)+h/(ch**2))
w:寬度差的平方
h:高度差的平方
SIOU
直接看這篇的解析吧。就不重復寫了
然后上一下完整代碼
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU = False, eps=1e-7):
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2 = box2.T
# Get the coordinates of bounding boxes
if x1y1x2y2: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
else: # transform from xywh to xyxy
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
# Intersection area
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps
iou = inter / union
if GIoU or DIoU or CIoU or SIoU or EIoU:
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
if SIoU: # SIoU Loss 2022.08.01
sigma = torch.pow(cw ** 2 + ch ** 2, 0.5)
sin_alpha_1 = ch / sigma
sin_alpha_2 = cw / sigma
threshold = pow(2, 0.5) / 2
sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
# angle_cost = 1 - 2 * torch.pow( torch.sin(torch.arcsin(sin_alpha) - np.pi/4), 2)
angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - np.pi / 2)
rho_x = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) / cw) ** 2
rho_y = ((b2_y1 + b2_y2 - b1_y1 - b1_y2) / ch) ** 2
gamma = 2 - angle_cost
distance_cost = 2 - torch.exp(-1 * gamma * rho_x) - torch.exp(-1 * gamma * rho_y)
omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
return iou - 0.5 * (distance_cost + shape_cost)
elif CIoU or DIoU or EIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
if DIoU:
return iou - rho2 / c2 # DIoU
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU
elif EIoU:
w=(w1-w2)*(w1-w2)
h=(h1-h2)*(h1-h2)
return iou-(rho2/c2+w/(cw**2)+h/(ch**2))#EIOU
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
c_area = cw * ch + eps # convex area
return iou - (c_area - union) / c_area # GIoU
else:
return iou # IoU
在yolov5使用方法
1.metrics.py修改一下注釋掉原來的bbox_iou,復制上面的完整代碼
2.loss.py中把想要使用的iou設置為True(下圖以EIOU為例)文章來源:http://www.zghlxwxcb.cn/news/detail-461586.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-461586.html
到了這里,關于yolov5 優(yōu)化方法(四)修改bbox損失函數(補充EIOU,SIOU)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!