(self, confidence, predicted_locations, labels, gt_locations)
return smooth_l1_loss / num_pos, classification_loss / num_pos
Compute classification loss and smooth l1 loss.

        Args:
            confidence (batch_size, num_priors, num_classes): class predictions.
            locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            boxes (batch_size, num_priors, 4): real boxes corresponding all the priors.
Compute classification loss and smooth l1 loss.
Compute classification loss and smooth l1 loss .
def forward(self, confidence, predicted_locations, labels, gt_locations):
        """Compute classification loss and smooth l1 loss.

        Args:
            confidence (batch_size, num_priors, num_classes): class predictions.
            locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            boxes (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        num_classes = confidence.size(2)
        with torch.no_grad():
            # derived from cross_entropy=sum(log(p))
            loss = -F.log_softmax(confidence, dim=2)[:, :, 0]
            mask = box_utils.hard_negative_mining(loss, labels, self.neg_pos_ratio)

        confidence = confidence[mask, :]
        classification_loss = F.cross_entropy(confidence.reshape(-1, num_classes), labels[mask], reduction='sum')
        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].reshape(-1, 4)
        gt_locations = gt_locations[pos_mask, :].reshape(-1, 4)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations, gt_locations, reduction='sum')  # smooth_l1_loss
        # smooth_l1_loss = F.mse_loss(predicted_locations, gt_locations, reduction='sum')  #l2 loss
        num_pos = gt_locations.size(0)
        return smooth_l1_loss / num_pos, classification_loss / num_pos
def forward ( self , confidence , predicted_locations , labels , gt_locations ) : num_classes = confidence . size ( 2 ) with torch . no_grad ( ) : # derived from cross_entropy=sum(log(p)) loss = - F . log_softmax ( confidence , dim = 2 ) [ : , : , 0 ] mask = box_utils . hard_negative_mining ( loss , labels , self . neg_pos_ratio ) confidence = confidence [ mask , : ] classification_loss = F . cross_entropy ( confidence . reshape ( - 1 , num_classes ) , labels [ mask ] , reduction = 'sum' ) pos_mask = labels > 0 predicted_locations = predicted_locations [ pos_mask , : ] . reshape ( - 1 , 4 ) gt_locations = gt_locations [ pos_mask , : ] . reshape ( - 1 , 4 ) smooth_l1_loss = F . smooth_l1_loss ( predicted_locations , gt_locations , reduction = 'sum' ) # smooth_l1_loss # smooth_l1_loss = F.mse_loss(predicted_locations, gt_locations, reduction='sum')  #l2 loss num_pos = gt_locations . size ( 0 ) return smooth_l1_loss / num_pos , classification_loss / num_pos