Yolov1 源码讲解 loss.py

news/2024/7/10 23:53:56 标签: YOLO, 深度学习, python

结构

1.lt rb我觉得不是很合适 正确来说是lb rt 因为比较出来的都是左下和右上坐标

比如前两个,都是max出来的 选两个box左下坐标中最大的,  后两个则是右上坐标中最小的 那也就形成了交集面积   

但是代码中仍然是lt rb我也就直接这样说

而算出lt和rb之后 算他们差可以算出高宽,只要没有交集 w或者h必定为负,可以画图验证一下

后面就是普通的iou算法

python">    def compute_iou(self, bbox1, bbox2):
        """ Compute the IoU (Intersection over Union) of two set of bboxes, each bbox format: [x1, y1, x2, y2].
        Args:
            bbox1: (Tensor) bounding bboxes, sized [N, 4].
            bbox2: (Tensor) bounding bboxes, sized [M, 4].
        Returns:
            (Tensor) IoU, sized [N, M].
        """
        N = bbox1.size(0)
        M = bbox2.size(0)

        # Compute left-top coordinate of the intersections
        lt = torch.max(
            bbox1[:, :2].unsqueeze(1).expand(N, M, 2), # [N, 2] -> [N, 1, 2] -> [N, M, 2]
            bbox2[:, :2].unsqueeze(0).expand(N, M, 2)  # [M, 2] -> [1, M, 2] -> [N, M, 2]
        )
        # Conpute right-bottom coordinate of the intersections
        rb = torch.min(
            bbox1[:, 2:].unsqueeze(1).expand(N, M, 2), # [N, 2] -> [N, 1, 2] -> [N, M, 2]
            bbox2[:, 2:].unsqueeze(0).expand(N, M, 2)  # [M, 2] -> [1, M, 2] -> [N, M, 2]
        )
        # Compute area of the intersections from the coordinates
        wh = rb - lt   # width and height of the intersection, [N, M, 2]
        wh[wh < 0] = 0 # clip at 0
        inter = wh[:, :, 0] * wh[:, :, 1] # [N, M]

        # Compute area of the bboxes
        area1 = (bbox1[:, 2] - bbox1[:, 0]) * (bbox1[:, 3] - bbox1[:, 1]) # [N, ]
        area2 = (bbox2[:, 2] - bbox2[:, 0]) * (bbox2[:, 3] - bbox2[:, 1]) # [M, ]
        area1 = area1.unsqueeze(1).expand_as(inter) # [N, ] -> [N, 1] -> [N, M]
        area2 = area2.unsqueeze(0).expand_as(inter) # [M, ] -> [1, M] -> [N, M]

        # Compute IoU from the areas
        union = area1 + area2 - inter # [N, M, 2]
        iou = inter / union           # [N, M, 2]

        return iou

2.比较难的部分也就是重头戏

python">        coord_mask = target_tensor[..., 4] > 0 #三个点自动判断维度 自动找到最后一维 用4找出第五个 也就是置信度,为什么30维 第二个框是怎么样的 等下再看
        #没有目标的张量[n_batch, S, S]
        noobj_mask = target_tensor[..., 4] == 0 
        #扩展维度的布尔值相同,[n_batch, S, S] -> [n_batch, S, S, N]
        coord_mask = coord_mask.unsqueeze(-1).expand_as(target_tensor)  
        noobj_mask = noobj_mask.unsqueeze(-1).expand_as(target_tensor)  

target中具有真实的置信度 有就是1没有就是0   而不是训练时的train值 train值是从0-1模糊的与网络输出值     

而这里的置信度赋值是从voc的encode方法中赋值的

这里筛选出存在物体与不存在物体的部分分别为coord_mask和noobj_mask,大小也分别为(batch_size,S,S) 意思是这个batch批次中,这个像素是否存在物体, 值为True False

扩充维数,使之对应(batch_size,S,S,30)  30维度

而负责的话这30维全为true或者不负责全为false

下一部分

python">        #预测值里含有目标的张量取出来,[n_coord, N] view类似于reshape 这里可以当作reshape看 就是变形
        coord_pred = pred_tensor[coord_mask].view(-1, N)        
        
        #提取bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_pred = coord_pred[:, :5*B].contiguous().view(-1, 5)   #防止内存不连续报错
        # 预测值的分类信息[n_coord, C]
        class_pred = coord_pred[:, 5*B:]                            

        #含有目标的标签张量,[n_coord, N]
        coord_target = target_tensor[coord_mask].view(-1, N)        
        
        #提取标签bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_target = coord_target[:, :5*B].contiguous().view(-1, 5) 
        #标签的分类信息
        class_target = coord_target[:, 5*B:]                        

 从网络中输出的预测张量中,这里叫他预测值(但这只是训练网络输出的预测值,而不是detect的预测值),我们在target中所有的含有物体的像素的地方,从预测中取出来,叫coord_pred

coord_pred也就是对应着真实存在的像素的张量,下面把他分割出bbox和class出来。

target也分割出bbox和class出来用于待会比较 切成10和20长度

ps:coord_pred的是取出结果后将前面三个维度拉平了, 只留最后一个维度N也就是30,

以整体来看pred_tensor.view(-1, N) 形状是(batch_size*S*S,N)  coord_pred就是取出来之后的类比于这个形状的张量 (所有batch中各自对应的图片中所有含有物体的像素的cell,N),人话来说就是所有batch中真实框个数总和

python">#没有目标的处理
        #找到预测值里没有目标的网格张量[n_noobj, N],n_noobj=SxS-n_coord
        noobj_pred = pred_tensor[noobj_mask].view(-1, N)         
        #标签的没有目标的网格张量 [n_noobj, N]                                                     
        noobj_target = target_tensor[noobj_mask].view(-1, N)            
        
        noobj_conf_mask = torch.cuda.BoolTensor(noobj_pred.size()).fill_(0) # [n_noobj, N]
        for b in range(B):
            noobj_conf_mask[:, 4 + b*5] = 1 # 没有目标置信度置1,noobj_conf_mask[:, 4] = 1; noobj_conf_mask[:, 9] = 1 目标是下面把置信度拿出来再并排
        

        noobj_pred_conf = noobj_pred[noobj_conf_mask]       # [n_noobj x 2=len([conf1, conf2])] 这里目标是
        noobj_target_conf = noobj_target[noobj_conf_mask]   # [n_noobj x 2=len([conf1, conf2])]
        #计算没有目标的置信度损失 加法》? #如果 reduction 参数未指定,默认值为 'mean',表示对所有元素的误差求平均值。
        #loss_noobj=F.mse_loss(noobj_pred_conf, noobj_target_conf,)*len(noobj_pred_conf)
        loss_noobj = F.mse_loss(noobj_pred_conf, noobj_target_conf, reduction='sum')

这里是取出所有不含物体的预测张量的部分,那个for循环是提前把位置赋予1标出来 后面用来提取找出这部分。

找出对应两个部分置信度之后做mse,平方差损失得到 不负责物体的像素置信度的损失,实际上是(0-预测出来的置信度)^2 论文中还要带个权重, 原因是不负责的像素太多了为了公平性,基于比较低的权重

python">        coord_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(0)    # [n_coord x B, 5]
        coord_not_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(1)# [n_coord x B, 5]
        bbox_target_iou = torch.zeros(bbox_target.size()).cuda()                    # [n_coord x B, 5], only the last 1=(conf,) is used

初始化下面循环需要用到的变量

python">        for i in range(0, bbox_target.size(0), B):
            pred = bbox_pred[i:i+B] # predicted bboxes at i-th cell, [B, 5=len([x, y, w, h, conf])]
            pred_xyxy = Variable(torch.FloatTensor(pred.size())) # [B, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=pred[:, 2] and (w,h)=pred[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            pred_xyxy[:,  :2] = pred[:, :2]/float(S) - 0.5 * pred[:, 2:4]
            pred_xyxy[:, 2:4] = pred[:, :2]/float(S) + 0.5 * pred[:, 2:4]

            target = bbox_target[i] # target bbox at i-th cell. Because target boxes contained by each cell are identical in current implementation, enough to extract the first one.
            target = bbox_target[i].view(-1, 5) # target bbox at i-th cell, [1, 5=len([x, y, w, h, conf])]
            target_xyxy = Variable(torch.FloatTensor(target.size())) # [1, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=target[:, 2] and (w,h)=target[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            target_xyxy[:,  :2] = target[:, :2]/float(S) - 0.5 * target[:, 2:4]
            target_xyxy[:, 2:4] = target[:, :2]/float(S) + 0.5 * target[:, 2:4]

            iou = self.compute_iou(pred_xyxy[:, :4], target_xyxy[:, :4]) # [B, 1]
            max_iou, max_index = iou.max(0)
            max_index = max_index.data.cuda()

            coord_response_mask[i+max_index] = 1
            coord_not_response_mask[i+max_index] = 0

            # "we want the confidence score to equal the intersection over union (IOU) between the predicted box and the ground truth"
            # from the original paper of YOLO.
            bbox_target_iou[i+max_index, torch.LongTensor([4]).cuda()] = (max_iou).data.cuda()

训练是进行预测框中每两个循环一次的比较,

预测值两个框是独立不一样的,而真实值target中在voc我们给予两个框一样的值。

pred_xyxy[:,  :2] = pred[:, :2]/float(S) - 0.5 * pred[:, 2:4]
pred_xyxy[:, 2:4] = pred[:, :2]/float(S) + 0.5 * pred[:, 2:4]但是这里除以S,我也没搞懂 我就先跳了

这里preds和target数量是一一对应的

而pred和target中坐标分别为 中心x和y还有w和h,target这四个都是相对图片归一化的 可以从voc解析中知道,而pred则是网络预测的随机值

iou需要传入的参数是左上,右下坐标 这里需要计算 

于是在某次循环中,得出最大iou,将那个iou的索引,也就是iou最大是第几个框拿出来在coord_response_mask 和coord_notresponse_mask,这两个变量形状完全与bbox相同

bbox_target_iou在gpu存下结果,用于下面计算损失,循环结束

下面是循环外内容

python">        bbox_target_iou = Variable(bbox_target_iou).cuda()

        # BBox location/size and objectness loss for the response bboxes.
        bbox_pred_response = bbox_pred[coord_response_mask].view(-1, 5)      # [n_response, 5]
        bbox_target_response = bbox_target[coord_response_mask].view(-1, 5)  # [n_response, 5], only the first 4=(x, y, w, h) are used
        target_iou = bbox_target_iou[coord_response_mask].view(-1, 5)        # [n_response, 5], only the last 1=(conf,) is used
        loss_xy = F.mse_loss(bbox_pred_response[:, :2], bbox_target_response[:, :2], reduction='sum')
        loss_wh = F.mse_loss(torch.sqrt(bbox_pred_response[:, 2:4]), torch.sqrt(bbox_target_response[:, 2:4]), reduction='sum')
        loss_obj = F.mse_loss(bbox_pred_response[:, 4], target_iou[:, 4], reduction='sum')

        ################################################################################


        # Class probability loss for the cells which contain objects.
        loss_class = F.mse_loss(class_pred, class_target, reduction='sum')

        # Total loss
        loss = self.lambda_coord * (loss_xy + loss_wh) + loss_obj + self.lambda_noobj * loss_noobj + loss_class
        loss = loss / float(batch_size)

负责物体部分的损失 

xy坐标求损失,因为含物体的像素少,论文中给予权重5加大损失倾向,

wh求损失,因为大框和小框之间差会非常大,容易造成损失,所以论文中加入根号,使之差不会过于太大,(大框差的值加在小框上是好几倍甚至几十倍这是无法容忍的),这里也因为像素少,给予权重5

置信度损失,此处网络输出出来的预测值是随机的,论文的想法是应该逼近她所应该位于的值,也就是真实的置信度。他位置在哪就应该给予在哪的置信度。这里体现在与真实框的iou交并比。但是yolov3中将这个与真实框的iou交并比的标签值换成了1,不懂这里也不用纠结。

这里是单纯计算损失  计算方法保持论文中提到的要求,mse_loss也是默认参数不给的话会自动求平均,加个参数sum单纯求平方差的和

返回的loss最终是除以batch_size的一个平均值

python">    def forward(self, pred_tensor, target_tensor):#target_tensor[2,0,0,:]
        """ Compute loss for YOLO training. #
        Args:
            pred_tensor: (Tensor) predictions, sized [n_batch, S, S, Bx5+C], 5=len([x, y, w, h, conf]).
            target_tensor: (Tensor) targets, sized [n_batch, S, S, Bx5+C].
        Returns:
            (Tensor): loss, sized [1, ].
        """
        # TODO: Romove redundant dimensions for some Tensors.
        #获取网格参数S=7,每个网格预测的边框数目B=2,和分类数C=20
        S, B, C = self.S, self.B, self.C
        N = 5 * B + C    # 5=len([x, y, w, h, conf],N=30

        #批的大小
        batch_size = pred_tensor.size(0)
        #有目标的张量[n_batch, S, S]
        coord_mask = target_tensor[..., 4] > 0 #三个点自动判断维度 自动找到最后一维 用4找出第五个 也就是置信度,为什么30维 第二个框是怎么样的 等下再看
        #没有目标的张量[n_batch, S, S]
        noobj_mask = target_tensor[..., 4] == 0 
        #扩展维度的布尔值相同,[n_batch, S, S] -> [n_batch, S, S, N]
        coord_mask = coord_mask.unsqueeze(-1).expand_as(target_tensor)  
        noobj_mask = noobj_mask.unsqueeze(-1).expand_as(target_tensor)  

        #int8-->bool
        noobj_mask = noobj_mask.bool()  #不是已经bool了?
        coord_mask = coord_mask.bool()  

        ##################################################
        #预测值里含有目标的张量取出来,[n_coord, N]
        coord_pred = pred_tensor[coord_mask].view(-1, N)        
        
        #提取bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_pred = coord_pred[:, :5*B].contiguous().view(-1, 5)   #防止内存不连续报错
        # 预测值的分类信息[n_coord, C]
        class_pred = coord_pred[:, 5*B:]                            

        #含有目标的标签张量,[n_coord, N]
        coord_target = target_tensor[coord_mask].view(-1, N)        
        
        #提取标签bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_target = coord_target[:, :5*B].contiguous().view(-1, 5) 
        #标签的分类信息
        class_target = coord_target[:, 5*B:]                         
        ######################################################

        # ##################################################
        #没有目标的处理
        #找到预测值里没有目标的网格张量[n_noobj, N],n_noobj=SxS-n_coord
        noobj_pred = pred_tensor[noobj_mask].view(-1, N)         
        #标签的没有目标的网格张量 [n_noobj, N]                                                     
        noobj_target = target_tensor[noobj_mask].view(-1, N)            
        
        noobj_conf_mask = torch.cuda.BoolTensor(noobj_pred.size()).fill_(0) # [n_noobj, N]
        for b in range(B):
            noobj_conf_mask[:, 4 + b*5] = 1 # 没有目标置信度置1,noobj_conf_mask[:, 4] = 1; noobj_conf_mask[:, 9] = 1 目标是下面把置信度拿出来再并排
        

        noobj_pred_conf = noobj_pred[noobj_conf_mask]       # [n_noobj x 2=len([conf1, conf2])] 这里目标是
        noobj_target_conf = noobj_target[noobj_conf_mask]   # [n_noobj x 2=len([conf1, conf2])]
        #计算没有目标的置信度损失 加法》? #如果 reduction 参数未指定,默认值为 'mean',表示对所有元素的误差求平均值。
        #loss_noobj=F.mse_loss(noobj_pred_conf, noobj_target_conf,)*len(noobj_pred_conf)
        loss_noobj = F.mse_loss(noobj_pred_conf, noobj_target_conf, reduction='sum')
        #################################################################################

        #################################################################################
        # Compute loss for the cells with objects.
        coord_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(0)    # [n_coord x B, 5]
        coord_not_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(1)# [n_coord x B, 5]
        bbox_target_iou = torch.zeros(bbox_target.size()).cuda()                    # [n_coord x B, 5], only the last 1=(conf,) is used

        # Choose the predicted bbox having the highest IoU for each target bbox.
        for i in range(0, bbox_target.size(0), B):
            pred = bbox_pred[i:i+B] # predicted bboxes at i-th cell, [B, 5=len([x, y, w, h, conf])]
            pred_xyxy = Variable(torch.FloatTensor(pred.size())) # [B, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=pred[:, 2] and (w,h)=pred[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            pred_xyxy[:,  :2] = pred[:, :2]/float(S) - 0.5 * pred[:, 2:4]
            pred_xyxy[:, 2:4] = pred[:, :2]/float(S) + 0.5 * pred[:, 2:4]

            target = bbox_target[i] # target bbox at i-th cell. Because target boxes contained by each cell are identical in current implementation, enough to extract the first one.
            target = bbox_target[i].view(-1, 5) # target bbox at i-th cell, [1, 5=len([x, y, w, h, conf])]
            target_xyxy = Variable(torch.FloatTensor(target.size())) # [1, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=target[:, 2] and (w,h)=target[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            target_xyxy[:,  :2] = target[:, :2]/float(S) - 0.5 * target[:, 2:4]
            target_xyxy[:, 2:4] = target[:, :2]/float(S) + 0.5 * target[:, 2:4]

            iou = self.compute_iou(pred_xyxy[:, :4], target_xyxy[:, :4]) # [B, 1]
            max_iou, max_index = iou.max(0)
            max_index = max_index.data.cuda()

            coord_response_mask[i+max_index] = 1
            coord_not_response_mask[i+max_index] = 0

            # "we want the confidence score to equal the intersection over union (IOU) between the predicted box and the ground truth"
            # from the original paper of YOLO.
            bbox_target_iou[i+max_index, torch.LongTensor([4]).cuda()] = (max_iou).data.cuda()
        bbox_target_iou = Variable(bbox_target_iou).cuda()

        # BBox location/size and objectness loss for the response bboxes.
        bbox_pred_response = bbox_pred[coord_response_mask].view(-1, 5)      # [n_response, 5]
        bbox_target_response = bbox_target[coord_response_mask].view(-1, 5)  # [n_response, 5], only the first 4=(x, y, w, h) are used
        target_iou = bbox_target_iou[coord_response_mask].view(-1, 5)        # [n_response, 5], only the last 1=(conf,) is used
        loss_xy = F.mse_loss(bbox_pred_response[:, :2], bbox_target_response[:, :2], reduction='sum')
        loss_wh = F.mse_loss(torch.sqrt(bbox_pred_response[:, 2:4]), torch.sqrt(bbox_target_response[:, 2:4]), reduction='sum')
        loss_obj = F.mse_loss(bbox_pred_response[:, 4], target_iou[:, 4], reduction='sum')

        ################################################################################


        # Class probability loss for the cells which contain objects.
        loss_class = F.mse_loss(class_pred, class_target, reduction='sum')

        # Total loss
        loss = self.lambda_coord * (loss_xy + loss_wh) + loss_obj + self.lambda_noobj * loss_noobj + loss_class
        loss = loss / float(batch_size)

        return loss

 

总的

python">import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable


class Loss(nn.Module):

    def __init__(self, feature_size=7, num_bboxes=2, num_classes=20, lambda_coord=5.0, lambda_noobj=0.5):
        """ Constructor.
        Args:
            feature_size: (int) size of input feature map.
            num_bboxes: (int) number of bboxes per each cell.
            num_classes: (int) number of the object classes.
            lambda_coord: (float) weight for bbox location/size losses.
            lambda_noobj: (float) weight for no-objectness loss.
        """
        super(Loss, self).__init__()

        self.S = feature_size
        self.B = num_bboxes
        self.C = num_classes
        self.lambda_coord = lambda_coord
        self.lambda_noobj = lambda_noobj


    def compute_iou(self, bbox1, bbox2):
        """ Compute the IoU (Intersection over Union) of two set of bboxes, each bbox format: [x1, y1, x2, y2].
        Args:
            bbox1: (Tensor) bounding bboxes, sized [N, 4].
            bbox2: (Tensor) bounding bboxes, sized [M, 4].
        Returns:
            (Tensor) IoU, sized [N, M].
        """
        N = bbox1.size(0)
        M = bbox2.size(0)

        # Compute left-top coordinate of the intersections
        lt = torch.max(
            bbox1[:, :2].unsqueeze(1).expand(N, M, 2), # [N, 2] -> [N, 1, 2] -> [N, M, 2]
            bbox2[:, :2].unsqueeze(0).expand(N, M, 2)  # [M, 2] -> [1, M, 2] -> [N, M, 2]
        )
        # Conpute right-bottom coordinate of the intersections
        rb = torch.min(
            bbox1[:, 2:].unsqueeze(1).expand(N, M, 2), # [N, 2] -> [N, 1, 2] -> [N, M, 2]
            bbox2[:, 2:].unsqueeze(0).expand(N, M, 2)  # [M, 2] -> [1, M, 2] -> [N, M, 2]
        )
        # Compute area of the intersections from the coordinates
        wh = rb - lt   # width and height of the intersection, [N, M, 2]
        wh[wh < 0] = 0 # clip at 0
        inter = wh[:, :, 0] * wh[:, :, 1] # [N, M]

        # Compute area of the bboxes
        area1 = (bbox1[:, 2] - bbox1[:, 0]) * (bbox1[:, 3] - bbox1[:, 1]) # [N, ]
        area2 = (bbox2[:, 2] - bbox2[:, 0]) * (bbox2[:, 3] - bbox2[:, 1]) # [M, ]
        area1 = area1.unsqueeze(1).expand_as(inter) # [N, ] -> [N, 1] -> [N, M]
        area2 = area2.unsqueeze(0).expand_as(inter) # [M, ] -> [1, M] -> [N, M]

        # Compute IoU from the areas
        union = area1 + area2 - inter # [N, M, 2]
        iou = inter / union           # [N, M, 2]

        return iou

    def forward(self, pred_tensor, target_tensor):#target_tensor[2,0,0,:]
        """ Compute loss for YOLO training. #
        Args:
            pred_tensor: (Tensor) predictions, sized [n_batch, S, S, Bx5+C], 5=len([x, y, w, h, conf]).
            target_tensor: (Tensor) targets, sized [n_batch, S, S, Bx5+C].
        Returns:
            (Tensor): loss, sized [1, ].
        """
        # TODO: Romove redundant dimensions for some Tensors.
        #获取网格参数S=7,每个网格预测的边框数目B=2,和分类数C=20
        S, B, C = self.S, self.B, self.C
        N = 5 * B + C    # 5=len([x, y, w, h, conf],N=30

        #批的大小
        batch_size = pred_tensor.size(0)
        #有目标的张量[n_batch, S, S]
        coord_mask = target_tensor[..., 4] > 0 #三个点自动判断维度 自动找到最后一维 用4找出第五个 也就是置信度,为什么30维 第二个框是怎么样的 等下再看
        #没有目标的张量[n_batch, S, S]
        noobj_mask = target_tensor[..., 4] == 0 
        #扩展维度的布尔值相同,[n_batch, S, S] -> [n_batch, S, S, N]
        coord_mask = coord_mask.unsqueeze(-1).expand_as(target_tensor)  
        noobj_mask = noobj_mask.unsqueeze(-1).expand_as(target_tensor)  

        #int8-->bool
        noobj_mask = noobj_mask.bool()  #不是已经bool了?
        coord_mask = coord_mask.bool()  

        ##################################################
        #预测值里含有目标的张量取出来,[n_coord, N]
        coord_pred = pred_tensor[coord_mask].view(-1, N)        
        
        #提取bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_pred = coord_pred[:, :5*B].contiguous().view(-1, 5)   #防止内存不连续报错
        # 预测值的分类信息[n_coord, C]
        class_pred = coord_pred[:, 5*B:]                            

        #含有目标的标签张量,[n_coord, N]
        coord_target = target_tensor[coord_mask].view(-1, N)        
        
        #提取标签bbox和C,[n_coord x B, 5=len([x, y, w, h, conf])]
        bbox_target = coord_target[:, :5*B].contiguous().view(-1, 5) 
        #标签的分类信息
        class_target = coord_target[:, 5*B:]                         
        ######################################################

        # ##################################################
        #没有目标的处理
        #找到预测值里没有目标的网格张量[n_noobj, N],n_noobj=SxS-n_coord
        noobj_pred = pred_tensor[noobj_mask].view(-1, N)         
        #标签的没有目标的网格张量 [n_noobj, N]                                                     
        noobj_target = target_tensor[noobj_mask].view(-1, N)            
        
        noobj_conf_mask = torch.cuda.BoolTensor(noobj_pred.size()).fill_(0) # [n_noobj, N]
        for b in range(B):
            noobj_conf_mask[:, 4 + b*5] = 1 # 没有目标置信度置1,noobj_conf_mask[:, 4] = 1; noobj_conf_mask[:, 9] = 1 目标是下面把置信度拿出来再并排
        

        noobj_pred_conf = noobj_pred[noobj_conf_mask]       # [n_noobj x 2=len([conf1, conf2])] 这里目标是
        noobj_target_conf = noobj_target[noobj_conf_mask]   # [n_noobj x 2=len([conf1, conf2])]
        #计算没有目标的置信度损失 加法》? #如果 reduction 参数未指定,默认值为 'mean',表示对所有元素的误差求平均值。
        #loss_noobj=F.mse_loss(noobj_pred_conf, noobj_target_conf,)*len(noobj_pred_conf)
        loss_noobj = F.mse_loss(noobj_pred_conf, noobj_target_conf, reduction='sum')
        #################################################################################

        #################################################################################
        # Compute loss for the cells with objects.
        coord_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(0)    # [n_coord x B, 5]
        coord_not_response_mask = torch.cuda.BoolTensor(bbox_target.size()).fill_(1)# [n_coord x B, 5]
        bbox_target_iou = torch.zeros(bbox_target.size()).cuda()                    # [n_coord x B, 5], only the last 1=(conf,) is used

        # Choose the predicted bbox having the highest IoU for each target bbox.
        for i in range(0, bbox_target.size(0), B):
            pred = bbox_pred[i:i+B] # predicted bboxes at i-th cell, [B, 5=len([x, y, w, h, conf])]
            pred_xyxy = Variable(torch.FloatTensor(pred.size())) # [B, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=pred[:, 2] and (w,h)=pred[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            pred_xyxy[:,  :2] = pred[:, :2]/float(S) - 0.5 * pred[:, 2:4]
            pred_xyxy[:, 2:4] = pred[:, :2]/float(S) + 0.5 * pred[:, 2:4]

            target = bbox_target[i] # target bbox at i-th cell. Because target boxes contained by each cell are identical in current implementation, enough to extract the first one.
            target = bbox_target[i].view(-1, 5) # target bbox at i-th cell, [1, 5=len([x, y, w, h, conf])]
            target_xyxy = Variable(torch.FloatTensor(target.size())) # [1, 5=len([x1, y1, x2, y2, conf])]
            # Because (center_x,center_y)=target[:, 2] and (w,h)=target[:,2:4] are normalized for cell-size and image-size respectively,
            # rescale (center_x,center_y) for the image-size to compute IoU correctly.
            target_xyxy[:,  :2] = target[:, :2]/float(S) - 0.5 * target[:, 2:4]
            target_xyxy[:, 2:4] = target[:, :2]/float(S) + 0.5 * target[:, 2:4]

            iou = self.compute_iou(pred_xyxy[:, :4], target_xyxy[:, :4]) # [B, 1]
            max_iou, max_index = iou.max(0)
            max_index = max_index.data.cuda()

            coord_response_mask[i+max_index] = 1
            coord_not_response_mask[i+max_index] = 0

            # "we want the confidence score to equal the intersection over union (IOU) between the predicted box and the ground truth"
            # from the original paper of YOLO.
            bbox_target_iou[i+max_index, torch.LongTensor([4]).cuda()] = (max_iou).data.cuda()
        bbox_target_iou = Variable(bbox_target_iou).cuda()

        # BBox location/size and objectness loss for the response bboxes.
        bbox_pred_response = bbox_pred[coord_response_mask].view(-1, 5)      # [n_response, 5]
        bbox_target_response = bbox_target[coord_response_mask].view(-1, 5)  # [n_response, 5], only the first 4=(x, y, w, h) are used
        target_iou = bbox_target_iou[coord_response_mask].view(-1, 5)        # [n_response, 5], only the last 1=(conf,) is used
        loss_xy = F.mse_loss(bbox_pred_response[:, :2], bbox_target_response[:, :2], reduction='sum')
        loss_wh = F.mse_loss(torch.sqrt(bbox_pred_response[:, 2:4]), torch.sqrt(bbox_target_response[:, 2:4]), reduction='sum')
        loss_obj = F.mse_loss(bbox_pred_response[:, 4], target_iou[:, 4], reduction='sum')

        ################################################################################


        # Class probability loss for the cells which contain objects.
        loss_class = F.mse_loss(class_pred, class_target, reduction='sum')

        # Total loss
        loss = self.lambda_coord * (loss_xy + loss_wh) + loss_obj + self.lambda_noobj * loss_noobj + loss_class
        loss = loss / float(batch_size)

        return loss


http://www.niftyadmin.cn/n/289755.html

相关文章

Gateway案例

官网:Spring Cloud Gateway 中文文档:Spring Cloud Gateway 2.1.0 中文官网文档 - 腾讯云开发者社区-腾讯云 一、网关介绍: 网关就是当前微服务的统一入口 通常在微服务项目中,只有网关项目是暴露在网络里的,其他服务一般都是在内网里, 用户访问网关,网关根据访问的路径,来进…

超细Redis(二)

五大数据类型 官方文档&#xff1a; 翻译&#xff1a; Redis 是一个开源&#xff08;BSD 许可&#xff09;内存数据结构存储系统&#xff0c;用作数据库、缓存、消息代理和流引擎。Redis 提供数据结构&#xff0c;例如字符串、哈希、列表、集、带有范围查询的排序集、位图、超…

软件著作权审查时间、软件导刊审稿周期、计算机工程与应用审稿周期、计算机技术与发展审稿周期、电子测量与仪器学报审稿周期

目录 《软件著作权》审查时间《软件导刊》审稿周期《计算机工程与应用》审稿周期《计算机技术与发展》审稿周期《电子测量与仪器学报》审稿周期 《软件著作权》审查时间 2022年申请软著的时间节点&#xff1a; 11.15受理通知书 11.15审查中 12.3审批中 12.20审查中 12.24待发放…

这就是实力~ 腾讯云大咖亲码 “redis深度笔记” 无废话全精华

前言 作为这个时代码代码的秃头人员&#xff0c;对Redis肯定是不陌生的&#xff0c;如果连Redis都没用过&#xff0c;还真不好意思出去面试&#xff0c;指不定被面试官吊打多少次。 毕竟现在互联网公司和一些创业公司都要用到Redis&#xff0c;像亚马逊、谷歌、阿里、腾讯都要…

科普, API 是这么演变而来的

API&#xff0c;全称为 Application Programming Interface&#xff0c;中文翻译为应用程序编程接口&#xff0c;是为了方便应用程序之间的数据和功能交互而设计的一些标准方法。API 的使用让开发者能够快速、高效地构建应用程序&#xff0c;从而加速了应用程序的开发速度。在 …

linux下raid1、raid5和raid10创建命令

linux下raid1和raid5创建命令 //创建命令 [rootraid ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 39.5G 0 part ├─cen…

[LeetCode周赛复盘] 第 343 场周赛20230430

[LeetCode周赛复盘] 第 343 场周赛20230430 一、本周周赛总结2660. 保龄球游戏的获胜者1. 题目描述2. 思路分析3. 代码实现 2661. 找出叠涂元素1. 题目描述2. 思路分析3. 代码实现 2662. 前往目标的最小代价1. 题目描述2. 思路分析3. 代码实现 2663. 字典序最小的美丽字符串1.…

我的服务器被挖矿了,原因竟是。。。

「作者简介」&#xff1a;CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」&#xff1a;对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 挖矿木马应急响应 一、什么是挖矿二、被挖矿主机现象三、挖矿木马处置思路1&#xff09;隔…