yolo3-pytorch icon indicating copy to clipboard operation
yolo3-pytorch copied to clipboard

损失函数的计算

Open TTgogogo opened this issue 3 years ago • 2 comments

请问大牛能理解这种计算loss方法的方法吗?看起来简单很多,想先从简单的入手: def getLoss(yoloOut, yolo,bboxes): #yoloOut为模型前向传播结果,yolo为模型,bboxes为真实框 BCELoss = nn.BCELoss() MSELoss = nn.MSELoss() bboxes = torch.cat([bboxes, torch.zeros(bboxes.shape[0],1,device=CONST.device)], 1) #在bboxes中增加1列 anchorRelate = torch.tensor(CONST.anchor, device=CONST.device).view(-1,2) / 416 anchorRelate = torch.cat([torch.zeros_like(anchorRelate), anchorRelate], 1) boxesWH = torch.cat([torch.zeros_like(bboxes[:,4:6]), bboxes[:,4:6]], 1) for i,item in enumerate(boxesWH): bboxes[i][6] = torch.argmax(iou(item, anchorRelate)) # [bs, cls, x,y,w,h,an] #找到iou最大的anchor loss = 0 for l,output in enumerate(yoloOut): lastLayer = yolo.lastLayers[l]

    ###############
    ba,c,h,w = output.shape  #shape的结果不是2个值吗?为什么是4个值?output的具体格式是?
    output = output.view(ba,len(lastLayer.anchor),-1,h,w).permute(0,1,3,4,2).contiguous()  #为什么要将ouput交换维度?
    ###############        
    
    b, cls, boxesScaled, an, i, j = buildTarget(bboxes, lastLayer, l) 
    tConf = torch.zeros_like(output[..., 4], device=CONST.device)
    xLoss,yLoss,wLoss,hLoss,clsLoss = [0,0,0,0,0]
    if b.shape[0] != 0:        
    
     ##################
     pr = output[b, an, i, j] # type:torch.Tensor  #为什么b,an,i,j能取出pr?output的格式是什么?
     ##################                    
        
        tConf[b, an, i, j] = 1
        pr[:,:2] = pr[:,:2].sigmoid()
        xLoss = BCELoss(pr[..., 0], boxesScaled[...,0])
        yLoss = BCELoss(pr[..., 1], boxesScaled[...,1])
        wLoss = MSELoss(pr[..., 2], boxesScaled[...,2]) * 0.5
        hLoss = MSELoss(pr[..., 3], boxesScaled[...,3]) * 0.5
        clsLoss = BCELoss(pr[:,5:].sigmoid(), cls)
    confLoss = BCELoss(output[..., 4].sigmoid(),tConf)
    loss = loss + xLoss + yLoss + wLoss + hLoss + clsLoss + confLoss
return loss

TTgogogo avatar Jul 06 '22 00:07 TTgogogo

他这个只是事先算好了cls和tConf吧

bubbliiiing avatar Jul 06 '22 15:07 bubbliiiing

他这个只是事先算好了cls和tConf吧

@bubbliiiing 事先算好了cls和tConf?不是像你那样在YOLOLoss()里面动态计算吗?感觉你那样计算更准确,但也复杂了很多,还看不明白呢

TTgogogo avatar Jul 07 '22 00:07 TTgogogo