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=None, size_average=None, reduce=None, reduction='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:
where NN is the batch size. If reduction is not 'none' (default 'mean'), then
这用于测量重建的误差,例如自动编码器。注意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 (Tensor, optional) – 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 (bool, optional) – 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 fieldsize_averageis set toFalse, the losses are instead summed for each minibatch. Ignored when reduce isFalse. Default:True -
reduce (bool, optional) – Deprecated (see
reduction). By default, the losses are averaged or summed over observations for each minibatch depending onsize_average. WhenreduceisFalse, returns a loss per batch element instead and ignoressize_average. Default:True -
reduction (string, optional) – 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_averageandreduceare in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction. Default:'mean'
Shape:
-
Input: (N, *)(N,∗) where *∗ means, any number of additional dimensions
-
Target: (N, *)(N,∗) , same shape as the input
-
Output: scalar. If
reductionis'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()
8099

被折叠的 条评论
为什么被折叠?



