Example #1
0
    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.
            predicted_locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            gt_locations (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        num_classes = confidence.size(2)
        with torch.no_grad():
            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.view(-1, num_classes),
                                              labels[mask],
                                              reduction='sum')
        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].view(-1, 4)
        gt_locations = gt_locations[pos_mask, :].view(-1, 4)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations,
                                          gt_locations,
                                          reduction='sum')
        num_pos = gt_locations.size(0)
        return smooth_l1_loss / num_pos, classification_loss / num_pos
Example #2
0
    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.
            predicted_locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            gt_locations (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        # print('confidence.size1:', confidence.size())  # [bs,num_anchors,2]
        # print('labels.size:', labels.size())  # [bs,num_anchors]
        num_classes = 2
        #classification_loss = self.focalloss(confidence,labels)
        # classification_loss = F.cross_entropy(confidence.view(-1,num_classes),labels.view(-1).long(),reduce='mean')
        with torch.no_grad():
            #     # derived from cross_entropy=sum(log(p))
            loss = -F.log_softmax(confidence, dim=2)[:, :, 0]
            # num_pos = torch.sum(labels==1)
            # print('num_pos:',num_pos)
            # print('num_posx4:', num_pos*4)
        # print('self.neg_pos_ratio:',self.neg_pos_ratio)
        mask = box_utils.hard_negative_mining(loss, labels, self.neg_pos_ratio)

        confidence = confidence[mask, :]
        # print('confidence.size:', confidence.size())  # [bs,num_anchors,2]

        classification_loss = F.cross_entropy(confidence.view(-1, num_classes),
                                              labels[mask].long(),
                                              reduction='sum')

        pos_mask = labels > 0
        # print('predicted_locations.size:',predicted_locations.size())
        predicted_locations = predicted_locations[pos_mask, :].view(-1, 8)
        gt_locations = gt_locations[pos_mask, :].view(-1, 8)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations,
                                          gt_locations.float(),
                                          reduction='sum')
        #l2_loss = F.mse_loss(predicted_locations, gt_locations.float(),reduction='sum')
        num_pos = gt_locations.size(0)
        return smooth_l1_loss / num_pos, classification_loss / num_pos
Example #3
0
    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.
            predicted_locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            gt_locations (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, :]
        #         print("confidence:",confidence)
        #         print("labels",labels[mask])
        classification_loss = F.cross_entropy(confidence.view(-1, num_classes),
                                              labels[mask],
                                              reduction='sum')

        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].view(-1, 4)
        gt_locations = gt_locations[pos_mask, :].view(-1, 4)
        #         print("predicted_locations",predicted_locations)
        #         print(predicted_locations.shape)
        #         print("gt_locations",gt_locations)
        #         print(gt_locations.shape)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations,
                                          gt_locations,
                                          reduction='sum')
        num_pos = gt_locations.size(0)
        #         info="s_loss is %f c_loss is %f \n num_pos is %d"%(smooth_l1_loss,classification_loss,num_pos)
        #         print(info)
        return smooth_l1_loss / num_pos, classification_loss / num_pos
Example #4
0
    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.
            predicted_locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            gt_locations (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        priors = PriorBox()().to(predicted_locations.device)
        priors = priors.unsqueeze(0).expand(predicted_locations.shape[0], -1,
                                            -1)
        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.view(-1, num_classes),
                                              labels[mask],
                                              reduction='sum')

        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].view(-1, 4)
        gt_locations = gt_locations[pos_mask, :].view(-1, 4)
        priors = priors[pos_mask, :].view(-1, 4)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations,
                                          gt_locations,
                                          reduction='sum')
        #smooth_l1_loss = self.Giou_loss(predicted_locations, gt_locations, reduction='mean')
        iou_loss = self.iouloss(predicted_locations, gt_locations, priors)
        #print(iou_loss)
        num_pos = gt_locations.size(0)
        return iou_loss / num_pos, classification_loss / num_pos
Example #5
0
    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.
            predicted_locations (batch_size, num_priors, 4): predicted locations.
            labels (batch_size, num_priors): real labels of all the priors.
            gt_locations (batch_size, num_priors, 4): real boxes corresponding all the priors.
        """
        #pdb.set_trace()
        num_classes = confidence.size(2)  #class = 5
        with torch.no_grad():
            # derived from cross_entropy=sum(log(p))
            loss = -F.log_softmax(confidence,
                                  dim=2)[:, :, 0]  #0 is the bcakground class

            mask = box_utils.hard_negative_mining(
                loss, labels, self.neg_pos_ratio
            )  #mask is to conduct the hear_negative_mining for reducing the negative labels.
        confidence = confidence[
            mask, :]  #In the frist loop, the confidence.shape is torch.Size([3108,5])
        classification_loss = F.cross_entropy(
            confidence.view(-1, num_classes), labels[mask], reduction='sum'
        )  #.view(-1,c)'s -1 means that the number of row can be any number since you are not sure!
        ###--------------try to modify cross-entropy into focal loss--------------------------
        p = torch.exp(-classification_loss)
        focal_loss = (1 - p)**self.gamma * classification_loss
        ###----------------modefied by Xiaoyu------------------------------------------------
        pos_mask = labels > 0
        predicted_locations = predicted_locations[pos_mask, :].view(-1, 4)
        gt_locations = gt_locations[pos_mask, :].view(-1, 4)
        smooth_l1_loss = F.smooth_l1_loss(predicted_locations,
                                          gt_locations,
                                          reduction='sum')
        num_pos = gt_locations.size(0)
        return smooth_l1_loss / num_pos, focal_loss / num_pos  #classification_loss / num_pos