损失函数的定义在src\lib\trainer.py23行左右,首先介绍损失函数的初始化部分
注:损失函数的调用在src\lib\trainer.py 134行
def _get_losses(self, opt):
loss_order = ['hm', 'wh', 'reg', 'ltrb', 'hps', 'hm_hp', \
'hp_offset', 'dep', 'dep_sec', 'dim', 'rot', 'rot_sec',
'amodel_offset', 'ltrb_amodal', 'tracking', 'nuscenes_att', 'velocity']
#首先定义最后模型需要回归的头,之后根据这里的头来调用相应的损失函数进行回归
loss_states = ['tot'] + [k for k in loss_order if k in opt.heads]
loss = GenericLoss(opt) #这里就是损失函数的调用,都封装在了GenericLoss中
return loss_states, loss
接下来详细介绍下GenericLoss这个类
class GenericLoss(torch.nn.Module):
def __init__(self, opt):
super(GenericLoss, self).__init__()
self.crit = FastFocalLoss(opt=opt) #FocalLoss用于之后的分类损失
这里只放了fastfocalloss类的初始化部分,之后在介绍函数具体运算的时候会放上所有的forward函数以及对应需要调用的函数,在该部分对于之后要用到的类也是主要介绍init函数。
class FastFocalLoss(nn.Module):
'''
Reimplemented focal loss, exactly the same as the CornerNet version.
Faster and costs much less memory.
'''
def __init__(self, opt=None):
super(FastFocalLoss, self).__init__()
self.only_neg_loss = _only_neg_loss #这里调用了负样本的损失函数
论文中对于负样本的损失为(1−Yxyz)β(Y^xyz)αlog(1−Y^xyz){(1 - {Y_{xyz}})^\beta }{({\widehat Y_{xyz}})^\alpha }\log (1 - {\widehat Y_{xyz}})(1−Yxyz)β(Y xyz)αlog(1−Y xyz),其中$ Y_{xyz}为groundtruth,为groundtruth,为groundtruth,\widehat Y_{xyz}$为预测的标签。下面的函数就是根据这个公式进行编写的。
def _only_neg_loss(pred, gt):
gt = torch.

本文介绍了在深度学习模型中损失函数的定义和使用,特别是FastFocalLoss的实现,该损失函数用于解决分类问题,特别是类别不平衡时的梯度消失问题。损失函数还包括其他如回归损失、深度损失等,通过不同的模块计算不同任务的损失并进行加权求和,以优化网络的训练过程。
最低0.47元/天 解锁文章
1366

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



