前言
? ? ?損失函數(shù)是用來(lái)評(píng)價(jià)模型的預(yù)測(cè)值和真實(shí)值不一樣的程度,損失函數(shù)越小,通常模型的性能越好。不同的模型用的損失函數(shù)一般也不一樣。
? ? ?損失函數(shù)的使用主要是在模型的訓(xùn)練階段,如果我們想讓預(yù)測(cè)值無(wú)限接近于真實(shí)值,就需要將損失值降到最低,在這個(gè)過(guò)程中就需要引入損失函數(shù)。而損失函數(shù)的選擇又是十分關(guān)鍵的。
一些常見(jiàn)的損失函數(shù)大家可以看我的這篇文章:Pytorch學(xué)習(xí)筆記(6):模型的權(quán)值初始化與損失函數(shù)
這篇我們主要講IOU系列損失函數(shù)。
友情提示:本文干貨滿(mǎn)滿(mǎn),適合剛?cè)腴T(mén)的小白,可以先點(diǎn)再慢慢看哦~
在yolov5中改進(jìn)的方法:YOLOv5改進(jìn)系列(11)——添加損失函數(shù)之EIoU、AlphaIoU、SIoU、WIoU?
目錄
前言
???一、IOU(Intersection over Union)
1.1 簡(jiǎn)介
1.2 公式
1.3 不足
1.4 pytorch代碼
???二、GIOU(Generalized-IoU)
2.1 簡(jiǎn)介
2.2 公式
2.3 不足
2.4 pytorch代碼
???三、DIoU(Distance-IoU)
3.1 簡(jiǎn)介
3.2 公式
3.3 不足
3.4 pytorch代碼
???四、CIoU(Complete-IoU)
4.1 簡(jiǎn)介
4.2 公式
4.3 不足
4.4 pytorch代碼
?? 五、EIoU(Efficient-IoU)
5.1 簡(jiǎn)介
5.2 公式
5.3 pytorch代碼
?? 六、α IoU(Alpha-IoU)
6.1 簡(jiǎn)介
6.2 公式
6.3 pytorch代碼
?? 七、SIoU(SCYLLA-IoU)
7.1 簡(jiǎn)介
7.2 公式
7.3 不足?
7.4 pytorch代碼
?? 八、WIoU(Wise-IoU)
8.1 簡(jiǎn)介
8.2 公式
8.3 pytorch代碼
??????總結(jié)
???一、IOU(Intersection over Union)
論文原文:?《UnitBox: An Advanced Object Detection Network》
1.1 簡(jiǎn)介
IoU全稱(chēng)Intersection over Union,交并比。
IoU是一種測(cè)量在特定數(shù)據(jù)集中檢測(cè)相應(yīng)物體準(zhǔn)確度的一個(gè)標(biāo)準(zhǔn)。只要是在輸出中得出一個(gè)預(yù)測(cè)范圍(bounding boxes)的任務(wù)都可以用IoU來(lái)進(jìn)行測(cè)量。
IoU算法是使用最廣泛的算法,大部分的檢測(cè)算法都是使用的這個(gè)算法。在目標(biāo)識(shí)別中,我們的預(yù)測(cè)框與實(shí)際框的某種比值就是IoU。
1.2 公式
1.3 不足
- 如果兩個(gè)框沒(méi)有相交,根據(jù)定義,IoU=0,不能反映兩者的距離大?。ㄖ睾隙龋?/span>。同時(shí)因?yàn)閘oss=0,沒(méi)有梯度回傳,無(wú)法進(jìn)行學(xué)習(xí)訓(xùn)練。(如圖(a)所示)
- 當(dāng)預(yù)測(cè)框和真實(shí)框的交并比相同,但是預(yù)測(cè)框所在位置不同,因?yàn)?span style="color:#1c7331;">計(jì)算出來(lái)的損失一樣,所以這樣并不能判斷哪種預(yù)測(cè)框更加準(zhǔn)確。(如圖(b)(c)所示)
1.4 pytorch代碼
def IoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
w = np.maximum(0.0, yy2 - yy1)
h = np.maximum(0.0, xx2 - xx1)
inter = w * h
IoU = inter/((b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter)
print("IoU: ", IoU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
IoU(box1, box2)
???二、GIOU(Generalized-IoU)
?論文原文:?《Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression》
2.1 簡(jiǎn)介
GIoU比IoU多了一個(gè)‘Generalized’,能在更廣義的層面上計(jì)算IoU。當(dāng)檢測(cè)框和真實(shí)框沒(méi)有出現(xiàn)重疊的時(shí)候IoU的loss都是一樣的,因此GIoU就引入了最小封閉形狀C(C可以把A,B包含在內(nèi)),在不重疊情況下能讓預(yù)測(cè)框盡可能朝著真實(shí)框前進(jìn),這樣就可以解決檢測(cè)框和真實(shí)框沒(méi)有重疊的問(wèn)題 。
IoU取值[0,1],但是GIoU有對(duì)稱(chēng)區(qū)間,取值范圍[-1,1]。在兩者重合的時(shí)候取最大值1,在兩者無(wú)交集且無(wú)限遠(yuǎn)的時(shí)候取最小值-1,因此GIoU是一個(gè)非常好的距離度量指標(biāo)。
GIoU不僅可以關(guān)注重疊區(qū)域,還可以關(guān)注其他非重合區(qū)域,能比較好的反映兩個(gè)框在閉包區(qū)域中的相交情況。
2.2 公式
如上圖所示,綠框是Prediction框就記為A框,橙框是Ground truth框就記為B框,最外面的藍(lán)框是將這兩個(gè)矩形用最小矩形框起來(lái)的邊界就記為C框,然后計(jì)算?,計(jì)算這個(gè)值與C面積的比值,最后用AB的IoU減去這個(gè)比值得到GIoU。
公式如下:
2.3 不足
- 對(duì)每個(gè)預(yù)測(cè)框與真實(shí)框均要去計(jì)算最小外接矩形,計(jì)算及收斂速度受到限制
- 在兩個(gè)預(yù)測(cè)框完全重疊的情況下,不能反映出實(shí)際情況,這時(shí)GIoU就退化為IoU。
2.4 pytorch代碼
def GIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, yy2 - yy1)
inter_h = np.maximum(0.0, xx2 - xx1)
inter = inter_w * inter_h
Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter
# GIOU
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
C_area = (C_xx2 - C_xx1) * (C_yy2 - C_yy1)
IOU = inter / Union
GIOU = IOU - abs((C_area-Union)/C_area)
print("GIOU:", GIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
GIoU(box1, box2)
???三、DIoU(Distance-IoU)
論文原文:《Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression》
3.1 簡(jiǎn)介
DIoU考慮到GIoU的缺點(diǎn),也是增加了C檢測(cè)框,將真實(shí)框和預(yù)測(cè)框都包含了進(jìn)來(lái),但是DIoU計(jì)算的不是框之間的交并,而是計(jì)算的每個(gè)檢測(cè)框之間的歐氏距離。
DIoU要比GIou更加符合目標(biāo)框回歸的機(jī)制,將目標(biāo)與anchor之間的距離,重疊率以及尺度都考慮進(jìn)去,使得目標(biāo)框回歸變得更加穩(wěn)定,不會(huì)像IoU和GIoU一樣出現(xiàn)訓(xùn)練過(guò)程中發(fā)散等問(wèn)題。
3.2 公式
其中d=ρ(A,B)是A框與B框中心點(diǎn)坐標(biāo)的歐式距離,而c則是包住它們的最小方框的對(duì)角線(xiàn)距離。
3.3 不足
DIoU考慮了重疊面積和中心點(diǎn)距離,當(dāng)目標(biāo)框包裹預(yù)測(cè)框的時(shí)候,直接度量2個(gè)框的距離,因此DIoU收斂的更快,但并沒(méi)有考慮到長(zhǎng)寬比。
3.4 pytorch代碼
def DIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, xx2 - xx1)
inter_h = np.maximum(0.0, yy2 - yy1)
inter = inter_w * inter_h
Union = (b1_x2 - b1_x1)*(b1_y2 - b1_y1) + (b2_x2 - b2_x1)*(b2_y2 - b2_y1) - inter
# DISTANCE
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
C_area = (C_xx2 - C_xx1) * (C_yy2 - C_yy1)
center_b_x = (b1_x1+b1_x2)/2
center_b_y = (b1_y1+b1_y2)/2
center_gtb_x = (b2_x1+b2_x2)/2
center_gtb_y = (b2_y1+b2_y2)/2
center_distance = (center_gtb_x-center_b_x)**2 + (center_gtb_y-center_b_y)**2
c_distance = (C_xx2 - C_xx1)**2 + (C_yy2 - C_yy1)**2
IOU = inter/Union
DIOU = IOU - center_distance /c_distance
print("DIOU:", DIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
DIoU(box1, box2)
???四、CIoU(Complete-IoU)
?論文原文(和DIoU同一篇論文):《Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression》
4.1 簡(jiǎn)介
CIoU就是在DIoU的基礎(chǔ)上增加了檢測(cè)框尺度的loss,增加了長(zhǎng)和寬的loss,使得目標(biāo)框回歸更加穩(wěn)定,不會(huì)像IoU和GIoU一樣出現(xiàn)訓(xùn)練過(guò)程中發(fā)散等問(wèn)題。
4.2 公式
?公式中,A,B代表兩個(gè)框,?代表A和B的中心點(diǎn)。
所以CIOU的前兩部分和DIOU是一致的(這里的LOSS就是1-CIOU)。唯一增加的部分是后面的av,這個(gè)就是對(duì)長(zhǎng)寬比的考量。
4.3 不足
- 如果預(yù)測(cè)框和gt框的長(zhǎng)寬比是相同的,那么長(zhǎng)寬比的懲罰項(xiàng)恒為0,不合理
- 觀(guān)察CIoU中w, h相對(duì)于v的梯度,發(fā)現(xiàn)這兩個(gè)梯度是一對(duì)相反數(shù),也就是說(shuō),w和h不能同時(shí)增大或減小,這顯然也不夠合理的。
4.4 pytorch代碼
def CIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, xx2 - xx1)
inter_h = np.maximum(0.0, yy2 - yy1)
inter = inter_w*inter_h
Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter
IOU = inter/Union
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
# DISTANCE
center_b_x = (b1_x1 + b1_x2)/2
center_b_y = (b1_y1 + b1_y2)/2
center_gtb_x = (b2_x1 + b2_x2)/2
center_gtb_y = (b2_y1 + b2_y2)/2
C_area = (C_xx2-C_xx1)*(C_yy2-C_yy1)
Distance = (center_gtb_x-center_b_x)**2 + (center_gtb_y-center_b_y)**2
Distance_area = Distance/C_area**2
# aspect ratio
pred_w = b1_y2 - b1_y1
pred_h = b1_x2 - b1_x1
gt_w = b2_y2 - b2_y1
gt_h = b2_x2 - b2_x1
v = (4/(np.pi)**2)*(np.arctan(gt_w/gt_h) - np.arctan(pred_w/pred_h))**2
alpha = v/((1-IOU) + v)
CIOU = IOU - Distance_area - alpha*v
print("CIOU:", CIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
CIoU(box1, box2)
?? 五、EIoU(Efficient-IoU)
論文原文:《Focal and Efficient IOU Loss for Accurate Bounding Box Regression》
5.1 簡(jiǎn)介
EIOU 是在 CIOU 的懲罰項(xiàng)基礎(chǔ)上將預(yù)測(cè)框和真實(shí)框的縱橫比的影響因子拆開(kāi),分別計(jì)算預(yù)測(cè)框和真實(shí)框的長(zhǎng)和寬,來(lái)解決 CIOU 存在的問(wèn)題。
EIoU包括三個(gè)部分:IoU損失、距離損失、高寬損失(重疊面積、中心點(diǎn)舉例、高寬比)。高寬損失直接最小化了預(yù)測(cè)目標(biāo)邊界框和真實(shí)邊界框的高度和寬度的差異,使其有更快的收斂速度和更好的定位結(jié)果。
5.2 公式
?其中,wc和hc是預(yù)測(cè)邊界框與真實(shí)邊界框的最小外接矩形的寬度和高度。p是兩點(diǎn)之間的歐氏距離。
5.3 pytorch代碼
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=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 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 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:
rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
cw2 = cw ** 2 + eps
ch2 = ch ** 2 + eps
return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2)
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
?? 六、α IoU(Alpha-IoU)
論文原文:《Alpha-IoU: A Family of Power Intersection over Union Losses for Bounding Box Regression》
6.1 簡(jiǎn)介
作者將現(xiàn)有的基于IoU Loss推廣到一個(gè)新的Power IoU系列 Loss,該系列具有一個(gè)Power IoU項(xiàng)和一個(gè)附加的Power正則項(xiàng),具有單個(gè)Power參數(shù)α,稱(chēng)這種新的損失系列為α-IoU Loss。
通過(guò)調(diào)節(jié)α,使檢測(cè)器在實(shí)現(xiàn)不同水平的bbox回歸精度方面具有更大的靈活性。并且α-IoU 對(duì)小數(shù)據(jù)集和噪聲的魯棒性更強(qiáng)。
通過(guò)實(shí)驗(yàn)發(fā)現(xiàn),在大多數(shù)情況下,取α=3 的效果最好。
6.2 公式
?通過(guò)上圖的公式我們可以看出,α-IoU 簡(jiǎn)單說(shuō)就是對(duì)IoU loss家族做了冪次運(yùn)算。
6.3 pytorch代碼
def bbox_alpha_iou(box1, box2, x1y1x2y2=False, GIoU=False, DIoU=False, CIoU=False, alpha=3, eps=1e-7):
# Returns tsqrt_he 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
# change iou into pow(iou+eps)
# iou = inter / union
iou = torch.pow(inter/union + eps, alpha)
# beta = 2 * alpha
if GIoU or DIoU or CIoU:
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 CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal
rho_x = torch.abs(b2_x1 + b2_x2 - b1_x1 - b1_x2)
rho_y = torch.abs(b2_y1 + b2_y2 - b1_y1 - b1_y2)
rho2 = ((rho_x ** 2 + rho_y ** 2) / 4) ** alpha # center distance
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_ciou = v / ((1 + eps) - inter / union + v)
# return iou - (rho2 / c2 + v * alpha_ciou) # CIoU
return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
# c_area = cw * ch + eps # convex area
# return iou - (c_area - union) / c_area # GIoU
c_area = torch.max(cw * ch + eps, union) # convex area
return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU
else:
return iou # torch.log(iou+eps) or iou
?? 七、SIoU(SCYLLA-IoU)
論文原文:《SIoU Loss: More Powerful Learning for Bounding Box Regression》
?7.1 簡(jiǎn)介
SIoU考慮到期望回歸之間向量的角度,重新定義角度懲罰度量,它可以使預(yù)測(cè)框快速漂移到最近的軸,隨后則只需要回歸一個(gè)坐標(biāo)(X或Y),這有效地減少了自由度的總數(shù)。
7.2 公式
-
Angle cost
(
角度損失):
?描述了中心點(diǎn)(圖1)連接與x-y軸之間的最小角度,當(dāng)中心點(diǎn)在x軸或y軸上對(duì)齊時(shí),Λ = 0。當(dāng)中心點(diǎn)連接到x軸45°時(shí),Λ = 1。這一懲罰可以引導(dǎo)anchor box移動(dòng)到目標(biāo)框的最近的軸上,減少了BBR的總自由度數(shù)。
-
Distance cost
(
距離損失):
描述了中心點(diǎn)之間的距離,其懲罰代價(jià)與角度代價(jià)呈正相關(guān),當(dāng)??→0時(shí),Distance cost的貢獻(xiàn)大大降低。相反,??越接近pi/4,Distance cost貢獻(xiàn)越大。
-
Shape cost(形狀損失):這里作者考慮的兩框之間的長(zhǎng)寬比,是通過(guò)計(jì)算兩框之間寬之差和二者之間最大寬之比(長(zhǎng)同理)來(lái)定義的,大體思路和CIOU類(lèi)似,只不過(guò)CIOU可以的考慮是兩框整體形狀的收斂,而SIoU是以長(zhǎng)、寬兩個(gè)邊收斂來(lái)達(dá)到整體形狀收斂的效果。
-
IoU cost(IoU損失):
7.3 不足?
- 模型評(píng)估結(jié)果的可重復(fù)性問(wèn)題:Scylla-IoU采用了多個(gè)不同的IoU閾值,這意味著對(duì)于同一組測(cè)試數(shù)據(jù),在不同的IoU閾值下,同一個(gè)模型的評(píng)估結(jié)果可能會(huì)有所不同。這會(huì)給模型的評(píng)估和比較帶來(lái)困難。
- IoU閾值的選擇問(wèn)題:Scylla-IoU需要指定多個(gè)IoU閾值,而這些閾值的選擇通常需要根據(jù)具體的數(shù)據(jù)集和任務(wù)進(jìn)行調(diào)整。不同的IoU閾值選擇可能會(huì)導(dǎo)致評(píng)估結(jié)果的不同,從而使得模型的比較變得困難。
- 可解釋性問(wèn)題:Scylla-IoU的計(jì)算過(guò)程較為復(fù)雜,需要對(duì)多個(gè)IoU閾值進(jìn)行計(jì)算并進(jìn)行加權(quán)平均。這可能會(huì)使得結(jié)果難以解釋?zhuān)o結(jié)果的可信度帶來(lái)一定影響。
7.4 pytorch代碼
def calculate_iou(boxes_a, boxes_b):
"""
Calculate Intersection over Union (IoU) of two bounding box tensors.
:param boxes_a: Tensor of shape (N, 4) representing bounding boxes (x1, y1, x2, y2)
:param boxes_b: Tensor of shape (M, 4) representing bounding boxes (x1, y1, x2, y2)
:return: Tensor of shape (N, M) representing IoU between all pairs of boxes_a and boxes_b
"""
# Calculate intersection coordinates
x1 = torch.max(boxes_a[:, 0].unsqueeze(1), boxes_b[:, 0].unsqueeze(0))
y1 = torch.max(boxes_a[:, 1].unsqueeze(1), boxes_b[:, 1].unsqueeze(0))
x2 = torch.min(boxes_a[:, 2].unsqueeze(1), boxes_b[:, 2].unsqueeze(0))
y2 = torch.min(boxes_a[:, 3].unsqueeze(1), boxes_b[:, 3].unsqueeze(0))
# Calculate intersection area
intersection_area = torch.clamp(x2 - x1 + 1, min=0) * torch.clamp(y2 - y1 + 1, min=0)
# Calculate box areas
boxes_a_area = (boxes_a[:, 2] - boxes_a[:, 0] + 1) * (boxes_a[:, 3] - boxes_a[:, 1] + 1)
boxes_b_area = (boxes_b[:, 2] - boxes_b[:, 0] + 1) * (boxes_b[:, 3] - boxes_b[:, 1] + 1)
# Calculate union area
union_area = boxes_a_area.unsqueeze(1) + boxes_b_area.unsqueeze(0) - intersection_area
# Calculate IoU
iou = intersection_area / union_area
return iou
def scylla_iou_loss(pred_boxes, target_boxes):
"""
Compute the SCYLLA-IoU loss between predicted and target bounding boxes.
:param pred_boxes: Tensor of shape (N, 4) representing predicted bounding boxes (x1, y1, x2, y2)
:param target_boxes: Tensor of shape (N, 4) representing target bounding boxes (x1, y1, x2, y2)
:return: SCYLLA-IoU loss
"""
iou = calculate_iou(pred_boxes, target_boxes)
# Compute SIoU terms
si = torch.min(pred_boxes[:, 2], target_boxes[:, 2]) - torch.max(pred_boxes[:, 0], target_boxes[:, 0]) + 1
sj = torch.min(pred_boxes[:, 3], target_boxes[:, 3]) - torch.max(pred_boxes[:, 1], target_boxes[:, 1]) + 1
s_union = (pred_boxes[:, 2] - pred_boxes[:, 0] + 1) * (pred_boxes[:, 3] - pred_boxes[:, 1] + 1) + \
(target_boxes[:, 2] - target_boxes[:, 0] + 1) * (target_boxes[:, 3] - target_boxes[:, 1] + 1)
s_intersection = si * sj
# Compute SCYLLA-IoU
siou = iou - (s_intersection / s_union)
# Compute loss
?? 八、WIoU(Wise-IoU)
論文原文:《Wise-IoU: Bounding Box Regression Loss with Dynamic Focusing Mechanism》
(論文一作是這位大佬@荷碧TongZJ,這里放上作者的論文解讀:?Wise-IoU 作者導(dǎo)讀:基于動(dòng)態(tài)非單調(diào)聚焦機(jī)制的邊界框損失)
8.1 簡(jiǎn)介
傳統(tǒng)的Intersection over Union(IoU)只考慮了預(yù)測(cè)框和真實(shí)框的重疊部分,沒(méi)有考慮兩者之間的區(qū)域,導(dǎo)致在評(píng)估結(jié)果時(shí)可能存在偏差?;谶@一思想,作者提出了一種基于IoU的損失,該損失具有動(dòng)態(tài)非單調(diào)FM,名為Wise IoU(WIoU)。
8.2 公式
Wise-IoU v1:根據(jù)距離度量構(gòu)建了距離注意力,得到了具有兩層注意力機(jī)制的 WIoU v1:
Wise-IoU v2: 參照Focal Loss 設(shè)計(jì)了一種針對(duì)交叉熵的單調(diào)聚焦機(jī)制WIoU v2,有效降低了簡(jiǎn)單示例對(duì)損失值的貢獻(xiàn)。這使得模型能夠聚焦于困難示例,獲得分類(lèi)性能的提升。
?Wise-IoU v3:利用?β?構(gòu)造了一個(gè)非單調(diào)聚焦系數(shù)并將其應(yīng)用于 WIoU v1就得到了具有動(dòng)態(tài)非單調(diào)FM的WIoU v3。利用動(dòng)態(tài)非單調(diào)FM的明智的梯度增益分配策略,WIoU v3獲得了優(yōu)越的性能。
8.3 pytorch代碼
def wiou(prediction, target, weight):
"""其中,prediction和target是大小為[batch_size, height, width]的張量,
weight是大小為[batch_size, height, width]的張量,表示每個(gè)像素的權(quán)重。"""
intersection = torch.min(prediction, target) * weight
union = torch.max(prediction, target) * weight
numerator = intersection.sum(dim=(1, 2))
denominator = union.sum(dim=(1, 2)) + intersection.sum(dim=(1, 2))
iou = numerator / denominator
wiou = (1 - iou ** 2) * iou
return wiou.mean().item()
import torch
def w_iou_loss(pred_boxes, target_boxes, weight=None):
"""
Compute the Weighted IoU loss between predicted and target bounding boxes.
其中,輸入pred_boxes和target_boxes分別是形狀為(N, 4)的預(yù)測(cè)邊界框和目標(biāo)邊界框張量。
如果需要使用權(quán)重,則輸入形狀為(N,)的權(quán)重張量weight,否則默認(rèn)為None。函數(shù)返回一個(gè)標(biāo)量,表示計(jì)算出的加權(quán)IoU損失。
Args:
pred_boxes (torch.Tensor): Predicted bounding boxes, with shape (N, 4).
target_boxes (torch.Tensor): Target bounding boxes, with shape (N, 4).
weight (torch.Tensor, optional): Weight tensor with shape (N,). Defaults to None.
Returns:
torch.Tensor: Weighted IoU loss scalar.
"""
# Compute the intersection over union (IoU) between the predicted and target boxes.
x1 = torch.max(pred_boxes[:, 0], target_boxes[:, 0])
y1 = torch.max(pred_boxes[:, 1], target_boxes[:, 1])
x2 = torch.min(pred_boxes[:, 2], target_boxes[:, 2])
y2 = torch.min(pred_boxes[:, 3], target_boxes[:, 3])
intersection_area = torch.clamp(x2 - x1, min=0) * torch.clamp(y2 - y1, min=0)
pred_boxes_area = (pred_boxes[:, 2] - pred_boxes[:, 0]) * (pred_boxes[:, 3] - pred_boxes[:, 1])
target_boxes_area = (target_boxes[:, 2] - target_boxes[:, 0]) * (target_boxes[:, 3] - target_boxes[:, 1])
union_area = pred_boxes_area + target_boxes_area - intersection_area
iou = intersection_area / union_area
# Compute the Weighted IoU loss using the IoU and weight tensors.
if weight is None:
w_iou = 1 - iou.mean()
else:
w_iou = (1 - iou) * weight
w_iou = w_iou.sum() / weight.sum()
return w_iou
??????總結(jié)
邊界框回歸的三大幾何因素:重疊面積、中心點(diǎn)距離、縱橫比文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-492455.html
- IOU Loss:主要考慮檢測(cè)框和目標(biāo)框重疊面積。
- GIOU Loss:在IOU的基礎(chǔ)上,解決邊界框不相交時(shí)loss等于0的問(wèn)題。
- DIOU Loss:在IOU和GIOU的基礎(chǔ)上,考慮邊界框中心點(diǎn)距離的信息。
- CIOU Loss:在DIOU的基礎(chǔ)上,考慮邊界框?qū)捀弑鹊某叨?/span>信息。
- EIOU Loss:在CIOU的基礎(chǔ)上,解決了縱橫比的模糊定義,并添加Focal Loss解決BBox回歸中的樣本不平衡問(wèn)題。
- αIOU Loss:通過(guò)調(diào)節(jié)α,使探測(cè)器更靈活地實(shí)現(xiàn)不同水平的bbox回歸精度
- SIOU Loss:在EIOU的基礎(chǔ)上,加入了類(lèi)別信息的權(quán)重因子,以提高檢測(cè)模型的分類(lèi)準(zhǔn)確率。
- WIOU Loss:解決質(zhì)量較好和質(zhì)量較差的樣本間的BBR平衡問(wèn)題
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-492455.html
到了這里,關(guān)于損失函數(shù):IoU、GIoU、DIoU、CIoU、EIoU、alpha IoU、SIoU、WIoU超詳細(xì)精講及Pytorch實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!