YOLOV5代碼詳解之損失函數(shù)的計(jì)算
摘要:
神經(jīng)網(wǎng)絡(luò)的訓(xùn)練的主要流程包括圖像輸入神經(jīng)網(wǎng)絡(luò), 得到模型的輸出結(jié)果,計(jì)算模型的輸出與真實(shí)值的損失, 計(jì)算損失值的梯度,最后用梯度下降算法更新模型參數(shù)。損失函數(shù)值的計(jì)算是非常關(guān)鍵的一個(gè)步驟。
本博客將對(duì)yolov5損失值的計(jì)算過(guò)程代碼的實(shí)現(xiàn)做簡(jiǎn)要的理解。
def compute_loss(p, targets, model): # predictions, targets, model device = targets.device lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([h['cls_pw']])).to(device) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([h['obj_pw']])).to(device) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 cp, cn = smooth_BCE(eps=0.0) # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) 。。。。。。
yolov5代碼用IOU指標(biāo)評(píng)價(jià)目標(biāo)框和預(yù)測(cè)框的位置損失損失。yolov5代碼用nn.BCEWithLogitsLoss或FocalLoss評(píng)價(jià)目標(biāo)框和預(yù)測(cè)框的類損失和置信度損失 .
yolov5代碼用寬高比選擇對(duì)應(yīng)真實(shí)框的預(yù)測(cè)框,且每一個(gè)真實(shí)框?qū)?yīng)三個(gè)預(yù)測(cè)框 。
1、位置損失
yolov5代碼用IOU值評(píng)價(jià)預(yù)測(cè)框和真實(shí)框的位置損失, 本文介紹CIoU指標(biāo).
公式如下截圖:
公式中參數(shù)代表的意義如下:
IOU: 預(yù)測(cè)框和真實(shí)框的叫并比
v是衡量長(zhǎng)寬比一致性的參數(shù),我們也可以定義為:
代碼實(shí)現(xiàn):
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-9): # 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: 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 + 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 / ((1 + eps) - iou + v) return iou - (rho2 / c2 + v * 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 else: return iou # IoU
2、置信度損失和類損失
yolov5代碼用nn.BCEWithLogitsLoss或FocalLoss評(píng)價(jià)目標(biāo)框和預(yù)測(cè)框的類損失和置信度損失,本節(jié)一一介紹這兩個(gè)損失函數(shù)。
- nn.BCEWithLogitsLoss:
首先對(duì)預(yù)測(cè)輸出作sigmoid變換,然后求變換后的結(jié)果與真實(shí)值的二值交叉熵.
假設(shè)預(yù)測(cè)輸出是3分類,預(yù)測(cè)輸出:
預(yù)測(cè)輸出sigmoid變換:
假設(shè)真實(shí)輸出是:
兩者的二值交叉熵的計(jì)算方法:
接口函數(shù)驗(yàn)證下上面的結(jié)果:
- FocalLoss損失:
FocalLoss損失考慮的是:目標(biāo)檢測(cè)中正負(fù)樣本嚴(yán)重不均衡的一種策略。該損失函數(shù)的設(shè)計(jì)思想類似于boosting,降低容易分類的樣本對(duì)損失函數(shù)的影響,注重較難分類的樣本的訓(xùn)練.
簡(jiǎn)而言之,FocalLoss更加關(guān)注的是比較難分的樣本,何謂難分?若某一個(gè)真實(shí)類預(yù)測(cè)的概率只有0.2,我們認(rèn)為它比較難分,相反若該真實(shí)類的預(yù)測(cè)概率是0.95,則容易分類.
FocalLoss通過(guò)提高難分類別的損失函數(shù)來(lái)實(shí)現(xiàn),公式如下:
圖像如下:
可以看出預(yù)測(cè)真實(shí)類概率越大,則損失函數(shù)越小,即實(shí)現(xiàn)了之前的想法.
為了能夠平衡正負(fù)樣本的重要性,我們可以給各個(gè)類別添加一個(gè)權(quán)重常數(shù) α ,比如想使正樣本初始權(quán)重為0.8,負(fù)樣本就為0.2.
代碼實(shí)現(xiàn)為:
class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super(FocalLoss, self).__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss
其中成員函數(shù)loss_fcn為nn.BCEWithLogitsLoss。
總結(jié)
到此這篇關(guān)于YOLOV5代碼詳解之損失函數(shù)計(jì)算的文章就介紹到這了,更多相關(guān)YOLOV5損失函數(shù)計(jì)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python tkinter實(shí)現(xiàn)屏保程序
這篇文章主要為大家詳細(xì)介紹了python tkinter實(shí)現(xiàn)屏保程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Python利用PsUtil實(shí)現(xiàn)實(shí)時(shí)監(jiān)控系統(tǒng)狀態(tài)
PSUtil是一個(gè)跨平臺(tái)的Python庫(kù),用于檢索有關(guān)正在運(yùn)行的進(jìn)程和系統(tǒng)利用率(CPU,內(nèi)存,磁盤,網(wǎng)絡(luò),傳感器)的信息。本文就來(lái)用PsUtil實(shí)現(xiàn)實(shí)時(shí)監(jiān)控系統(tǒng)狀態(tài),感興趣的可以跟隨小編一起學(xué)習(xí)一下2023-04-04詳解Python虛擬機(jī)是如何實(shí)現(xiàn)閉包的
Python中的閉包是一個(gè)強(qiáng)大的概念,允許函數(shù)捕獲和訪問(wèn)其周圍的作用域,即使這些作用域在函數(shù)執(zhí)行完畢后也能被訪問(wèn),這篇文章將著重討論P(yáng)ython虛擬機(jī)是如何實(shí)現(xiàn)閉包的,文中有相關(guān)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12python使用matplotlib的savefig保存時(shí)圖片保存不完整的問(wèn)題
這篇文章主要介紹了python使用matplotlib的savefig保存時(shí)圖片保存不完整的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01詳解用pyecharts Geo實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)熱力圖城市找不到問(wèn)題解決
這篇文章主要介紹了詳解用pyecharts Geo實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)熱力圖城市找不到問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Python實(shí)現(xiàn)Event回調(diào)機(jī)制的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)Event回調(diào)機(jī)制的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python字典循環(huán)添加一鍵多值的用法實(shí)例
今天小編就為大家分享一篇Python字典循環(huán)添加一鍵多值的用法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01