YOLO的损失函数

yolo的损失函数,面试的时候被问到了。之前确实式看过,但是忘记了,三项一共答出来两项。昨天一直拖着不想弄,事实上每一次的面试也是一次进步的机会。主要是yolov3论文里没有提到具体的损失函数。那我就从pytorch那边来简单的分析下yolov3的损失函数把。

            #顾名思义这里的x,y,z,w都是采用的最小均方差来评判误差
            #位置损失
            loss_x = self.mse_loss(x[obj_mask], tx[obj_mask])
            loss_y = self.mse_loss(y[obj_mask], ty[obj_mask])
            loss_w = self.mse_loss(w[obj_mask], tw[obj_mask])
            loss_h = self.mse_loss(h[obj_mask], th[obj_mask])
            #我们进去看看
            #nn.MSELoss()
            #调用的nn的模块

            #置信度损失,即有没有这个物体的概率
            #bce_loss是什么东西,我们进入瞧瞧nn.BCELoss(),采用二进制交叉熵
            loss_conf_obj = self.bce_loss(pred_conf[obj_mask], tconf[obj_mask])
            loss_conf_noobj = self.bce_loss(pred_conf[noobj_mask], tconf[noobj_mask])
            loss_conf = self.obj_scale * loss_conf_obj + self.noobj_scale * loss_conf_noobj
            
            #分类的损失 二分类交叉熵,bce_loss其实是个迭代器计算所有的误差然后求和。
            loss_cls = self.bce_loss(pred_cls[obj_mask], tcls[obj_mask])
            #all
            total_loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls

下面介绍下BCELoss 二分类交叉熵 Binary Cross Entropy

torch.nn.BCELoss(weight=Nonesize_average=Nonereduce=Nonereduction='mean')

创建一个衡量目标和输出的binary_cross_entropy误差

Creates a criterion that measures the Binary Cross Entropy between the target and the output:

没有educed的loss可以描述为

The unreduced (i.e. with reduction set to 'none') loss can be described as:

\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right],ℓ(x,y)=L={l1​,…,lN​}⊤,ln​=−wn​[yn​⋅logxn​+(1−yn​)⋅log(1−xn​)],

where NN is the batch size. If reduction is not 'none' (default 'mean'), then

\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}ℓ(x,y)={mean(L),sum(L),​if reduction=’mean’;if reduction=’sum’.​

这用于测量重建的误差,例如自动编码器。注意y的范围为0-1

This is used for measuring the error of a reconstruction in for example an auto-encoder. Note that the targets yy should be numbers between 0 and 1.

Parametersyong

  • weight (Tensoroptional) – a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size nbatch.

  • 一个控制缩放因子,控制每一个batch中占的权重

  • size_average (booloptional) – Deprecated (see reduction). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: True

  • reduce (booloptional) – Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: True

  • reduction (stringoptional) – Specifies the reduction to apply to the output:'none' | 'mean' | 'sum''none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: 'mean'

Shape:

  • Input: (N, *)(N,∗) where *∗ means, any number of additional dimensions

  • Target: (N, *)(N,∗) , same shape as the input

  • Output: scalar. If reduction is 'none', then (N, *)(N,∗) , same shape as input.

Examples:

>>> m = nn.Sigmoid()
>>> loss = nn.BCELoss()
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> output = loss(m(input), target)
>>> output.backward()

你可能感兴趣的:(工作记录)