Deep learning 从分类损失到回归损失的修正 一般的

Deep learning 从分类损失到回归损失的修正 一般的,deep-learning,pytorch,Deep Learning,Pytorch,我正在跟踪这个目标检测的报告 动机 对象检测网络通常执行两个任务。对于图像中的每个对象,输出一个类置信度分数和边界框回归分数。对于我的任务以及这两个输出,对于每个对象,我希望输出一个介于0-5之间的多个回归分数 问题陈述 我采取的方法是,由于网络已经进行了分类,我想我会修改一些部分,使其成为回归损失。上述回购中使用的损失是焦点损失。分类损失的部分内容如下所述,我想将其修改为回归损失 targets = torch.ones(classification.shape) * -1#Her

我正在跟踪这个目标检测的报告

动机 对象检测网络通常执行两个任务。对于图像中的每个对象,输出一个类置信度分数和边界框回归分数。对于我的任务以及这两个输出,对于每个对象,我希望输出一个介于0-5之间的多个回归分数

问题陈述 我采取的方法是,由于网络已经进行了分类,我想我会修改一些部分,使其成为回归损失。上述回购中使用的损失是焦点损失。分类损失的部分内容如下所述,我想将其修改为回归损失

     targets = torch.ones(classification.shape) * -1#Here classification shape is = torch.Size([114048, 1])
    targets = targets.cuda()
    targets[torch.lt(IoU_max, 0.4), :] = 0#Iou_max is a tensor of shape = torch.Size([114048]) which looks like tensor([0., 0., 0.,  ..., 0., 0., 0.], device='cuda:0')
    positive_indices = torch.ge(IoU_max, 0.5)
    num_positive_anchors = positive_indices.sum()
    assigned_annotations = bbox_annotation[IoU_argmax, :]#Here bbox_annotation has the shape torch.Size([6, 6]) and IoU_argmax has the shape torch.Size([114048])
    targets[positive_indices, :] = 0#Here positive indices has the shape torch.Size([114048])
    targets[positive_indices, assigned_annotations[positive_indices, 4].long()] = 1
    alpha_factor = torch.ones(targets.shape).cuda() * alpha#here alpha is 0.25
    alpha_factor = torch.where(torch.eq(targets, 1.), alpha_factor, 1. - alpha_factor)
    focal_weight = torch.where(torch.eq(targets, 1.), 1. - classification, classification)
    focal_weight = alpha_factor * torch.pow(focal_weight, gamma)#here gamma is 2.0

    bce = -(targets * torch.log(classification) + (1.0 - targets) * torch.log(1.0 - classification))
    cls_loss = focal_weight * bce
    cls_loss = torch.where(torch.ne(targets, -1.0), cls_loss,torch.zeros(cls_loss.shape).cuda())
  classification_losses.append(cls_loss.sum()/torch.clamp(num_positive_anchors.float(), min=1.0))
所以我试着修改上面描述的一些位。但即使代码没有中断,这也没有学到任何东西。这里侵蚀只是一个张量,但侵蚀分数是我们想要回归的东西

    targets = torch.ones(erosion.shape) * -1#Here erosion shape is torch.Size([114048, 1])
    targets = targets.cuda()
    targets[torch.lt(IoU_max, 0.4), :] = 0#This part remains the same as above
    positive_indices = torch.ge(IoU_max, 0.5)
    num_positive_anchors = positive_indices.sum()
    assigned_annotations = bbox_annotation[IoU_argmax, :]
    targets[positive_indices, :] = 0
    targets[assigned_annotations[positive_indices, 5].long()] = 1#Here i have indexed along the 5th dimension because the 5th dimension contains annotations for erosion.
    criterion = nn.MSELoss()#This part is where i use the mean squared loss for regression. 
    loss = torch.sqrt(criterion(targets, erosion))
    erosion_losses.append(loss)
这不会抛出错误,但也不会学到任何东西

有没有人能帮我正确地计算这个损失来进行回归

更多信息
如果我理解正确,您希望添加另一个回归参数,该参数的值应在[0-5]中

您可以将其添加到长方体回归部分,而不是尝试更改分类部分。因此,您可以为每个锚点再添加一个参数进行预测

是回归参数的损失计算。如果向每个锚点添加一个参数(可能是通过增加最后一层中的过滤器计数),则必须像提取其他参数一样“提取”它。您可能还需要考虑值的激活,例如sigmoid,这样您可以得到介于0和1之间的值,如果您愿意,可以重新缩放到0-5

在你想添加这样的东西之后

anchor_erosion_pi = anchor_erosion[positive_indices]
然后你必须以某种方式获得gt,也许可以将其重新缩放到0-1,这样你就可以使用sigmoid激活的输出来获得该值。 然后你必须设定目标。这只是:

targets_erosion = gt_erosion
如果你不依赖于锚,情况似乎就是这样。 在目标堆叠之后,您必须添加新的目标值并添加权重,您必须决定并尝试是否需要在此处添加权重。我可能会在张量上加上0.2或其他什么。也许你可以看看报纸里面其他权重是如何选择的

targets = targets/torch.Tensor([[0.1, 0.1, 0.2, 0.2, 0.2]]).cuda()
在这之后,计算出的值可能不需要更改任何内容


你可能还有更多的事要做。只需查看代码,查看其他参数的位置,并尝试将您的参数添加到代码中。

好的,谢谢,让我检查一下,在接受此答案之前,我将等待更多的答案。检查我问题中的“更多信息”标题,您认为如何?不要真正获得
loss*torch.tensor(5.0).cuda()
。它本身并没有做类似的事情,但我想你的意思是
loss*=torch.tensor(5.0).cuda()
。你要么想把网络的输出乘以5,要么除以地面真值。我仍然会把侵蚀量加到其他回归值中,然后按照你已有的代码进行操作。通过这种方式,您可以确保您没有遗漏任何步骤,并且github中的代码非常干净,易于理解。感谢您的评论,但是由于某些原因,这种丢失并不能让您了解。我正在检查原因
targets = targets/torch.Tensor([[0.1, 0.1, 0.2, 0.2, 0.2]]).cuda()