[docs]classVeloLSTMLoss(Loss):"""Loss term for VeloLSTM."""def__init__(self,loc_dim:int=7,smooth_weight:float=0.001)->None:"""Initialize the loss term."""super().__init__()self.loc_dim=loc_dimself.smooth_weight=smooth_weight
[docs]@staticmethoddeflinear_motion_loss(outputs:Tensor)->Tensor:"""Linear motion loss. Loss: |(loc_t - loc_t-1), (loc_t-1, loc_t-2)|_1 for t = [2, s_len] """s_len=outputs.shape[1]loss=outputs.new_zeros(1)past_motion=outputs[:,1,:]-outputs[:,0,:]foridxinrange(2,s_len,1):curr_motion=outputs[:,idx,:]-outputs[:,idx-1,:]loss+=F.l1_loss(past_motion,curr_motion,reduction="mean")past_motion=curr_motionreturnloss/(s_len-2)
[docs]defforward(self,loc_preds:Tensor,loc_refines:Tensor,gt_traj:Tensor)->LossesType:"""Loss term for VeloLSTM."""refine_loss=F.smooth_l1_loss(loc_refines,gt_traj[:,1:,:self.loc_dim],reduction="mean")pred_loss=F.smooth_l1_loss(loc_preds[:,:-1,:],gt_traj[:,2:,:self.loc_dim],reduction="mean",)linear_loss=self.linear_motion_loss(loc_preds[:,:-1,:])return{"refine_loss":refine_loss,"pred_loss":pred_loss,"linear_loss":torch.mul(self.smooth_weight,linear_loss),}