Ejemplo n.º 1
0
    def forward(self, node_preds, edge_preds, gts):
        node_gts, edge_gts = [], []
        for gt in gts:
            node_gts.append(gt[:, 0])
            edge_gts.append(gt[:, 1:].contiguous().view(-1))
        node_gts = torch.cat(node_gts).long()
        edge_gts = torch.cat(edge_gts).long()

        node_valids = torch.nonzero(node_gts != self.ignore).view(-1)
        edge_valids = torch.nonzero(edge_gts != -1).view(-1)
        return dict(
            loss_node=self.node_weight * self.loss_node(node_preds, node_gts),
            loss_edge=self.edge_weight * self.loss_edge(edge_preds, edge_gts),
            acc_node=accuracy(node_preds[node_valids], node_gts[node_valids]),
            acc_edge=accuracy(edge_preds[edge_valids], edge_gts[edge_valids]))
Ejemplo n.º 2
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()

        if cls_score is not None:
            # avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
            # if cls_score.numel() > 0:
            #     losses['loss_cls'] = self.loss_cls(
            #         cls_score,
            #         labels,
            #         label_weights,
            #         avg_factor=avg_factor,
            #         reduction_override=reduction_override)
            #     losses['acc'] = accuracy(cls_score, labels)
            # Original label_weights is 1 for each roi.
            new_labels, new_weights, new_avgfactors = self._remap_labels(
                labels)
            new_preds = self._slice_preds(cls_score)

            num_bins = len(new_labels)
            for i in range(num_bins):
                losses['loss_cls_bin{}'.format(i)] = self.loss_bins[i](
                    new_preds[i],
                    new_labels[i].long(),
                    new_weights[i],
                    avg_factor=new_avgfactors[i],
                    reduction_override=reduction_override)
                losses['acc'] = accuracy(cls_score, labels)
        if bbox_pred is not None:
            bg_class_ind = self.num_classes
            # 0~self.num_classes-1 are FG, self.num_classes is BG
            pos_inds = (labels >= 0) & (labels < bg_class_ind)
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                if self.reg_decoded_bbox:
                    bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
                if self.reg_class_agnostic:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
                else:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), -1,
                        4)[pos_inds.type(torch.bool),
                           labels[pos_inds.type(torch.bool)]]
                losses['loss_bbox'] = self.loss_bbox(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=bbox_targets.size(0),
                    reduction_override=reduction_override)
            else:
                losses['loss_bbox'] = bbox_pred.sum() * 0
        return losses
 def loss(self,
          cls_score,
          bbox_pred,
          dist,
          rois,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          reduction_override=None,
          img_metas=None):
     losses = dict()
     if cls_score is not None:
         avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
         if cls_score.numel() > 0:
             losses['loss_cls'] = self.loss_cls(
                 cls_score,
                 labels,
                 label_weights,
                 avg_factor=avg_factor,
                 reduction_override=reduction_override)
             losses['acc'] = accuracy(cls_score, labels)
     if bbox_pred is not None:
         bg_class_ind = self.num_classes
         # 0~self.num_classes-1 are FG, self.num_classes is BG
         pos_inds = (labels >= 0) & (labels < bg_class_ind)
         # do not perform bounding box regression for BG anymore.
         if pos_inds.any():
             if self.reg_decoded_bbox:
                 bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
             if self.reg_class_agnostic:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
             else:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), -1,
                     4)[pos_inds.type(torch.bool),
                        labels[pos_inds.type(torch.bool)]]
             losses['loss_bbox'] = self.loss_bbox(
                 pos_bbox_pred,
                 bbox_targets[pos_inds.type(torch.bool)],
                 bbox_weights[pos_inds.type(torch.bool)],
                 avg_factor=bbox_targets.size(0),
                 reduction_override=reduction_override)
         else:
             losses['loss_bbox'] = bbox_pred[pos_inds].sum()
     # embedding loss for repmet
     '''
     if dist is not None:
         mask_label = torch.eye(self.num_classes + 1)[labels.long(),:].to(labels.device)
         gt_dist = torch.masked_select(dist, mask_label.bool())
         neg_dist = torch.masked_select(dist, ~mask_label.bool())
         neg_dist = neg_dist.reshape(labels.shape[0], self.num_classes)
         neg_dist = torch.min(neg_dist, dim = 1)[0]
         embedding_dist = gt_dist + self.alpha - neg_dist
         embedding_dist = torch.masked_select(embedding_dist, embedding_dist.ge(0))
         embedding_loss = torch.sum(embedding_dist) / embedding_dist.shape[0]
         losses['loss_embedding'] = embedding_loss
     '''
     return losses
Ejemplo n.º 4
0
    def loss_labels(self, outputs, targets, indices, num_boxes, log=True):
        """Classification loss (NLL)
        targets dicts must contain the key "labels" containing a tensor of dim [nb_target_boxes]
        """
        assert 'pred_logits' in outputs
        src_logits = outputs['pred_logits']

        idx = self._get_src_permutation_idx(indices)
        target_classes_o = torch.cat(
            [t["labels"][J] for t, (_, J) in zip(targets, indices)])
        target_classes = torch.full(src_logits.shape[:2],
                                    self.num_classes,
                                    dtype=torch.int64,
                                    device=src_logits.device)
        target_classes[idx] = target_classes_o

        loss_ce = F.cross_entropy(src_logits.transpose(1, 2), target_classes,
                                  self.empty_weight)
        losses = {'loss_ce': loss_ce}

        if log:
            # TODO this should probably be a separate loss, not hacked in this one here
            losses['class_error'] = 100 - accuracy(src_logits[idx],
                                                   target_classes_o)[0]
        return losses
Ejemplo n.º 5
0
 def loss(self,
          cls_score,
          bbox_pred,
          rois,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          reduction_override=None):
     losses = dict()
     if cls_score is not None:
         avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
         if cls_score.numel() > 0:
             if self.loss_cls_type == "CBLoss":
                 # fg_inds = (labels >= 0) & (labels < self.num_classes)
                 # fg_labels = labels[fg_inds]
                 unique_labels, count = torch.unique(labels,
                                                     return_counts=True)
                 samples_per_class = torch.zeros(self.num_classes + 1,
                                                 dtype=torch.int64).cuda()
                 samples_per_class[unique_labels] = count
                 losses['loss_cls'] = self.loss_cls(labels, cls_score,
                                                    samples_per_class,
                                                    self.num_classes + 1)
             else:
                 losses['loss_cls'] = self.loss_cls(
                     cls_score,
                     labels,
                     label_weights,
                     avg_factor=avg_factor,
                     reduction_override=reduction_override)
             losses['acc'] = accuracy(cls_score, labels)
     if bbox_pred is not None:
         bg_class_ind = self.num_classes
         # 0~self.num_classes-1 are FG, self.num_classes is BG
         pos_inds = (labels >= 0) & (labels < bg_class_ind)
         # do not perform bounding box regression for BG anymore.
         if pos_inds.any():
             if self.reg_decoded_bbox:
                 bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
             if self.reg_class_agnostic:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
             else:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), -1,
                     4)[pos_inds.type(torch.bool),
                        labels[pos_inds.type(torch.bool)]]
             # print("pos_bbox_pred:", pos_bbox_pred)
             # print("pos_bbox_targets:", bbox_targets[pos_inds.type(torch.bool)])
             losses['loss_bbox'] = self.loss_bbox(
                 pos_bbox_pred,
                 bbox_targets[pos_inds.type(torch.bool)],
                 bbox_weights[pos_inds.type(torch.bool)],
                 avg_factor=bbox_targets.size(0),
                 reduction_override=reduction_override)
         else:
             losses['loss_bbox'] = bbox_pred.sum() * 0
     return losses
 def loss(self,
          cls_score,
          bbox_pred,
          rois,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          reduction_override=None):
     losses = dict()
     if cls_score is not None:
         avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
         if cls_score.numel() > 0:
             loss_cls_ = self.loss_cls(
                 cls_score,
                 labels,
                 label_weights,
                 avg_factor=avg_factor,
                 reduction_override=reduction_override)
             if isinstance(loss_cls_, dict):
                 losses.update(loss_cls_)
             else:
                 losses['loss_cls'] = loss_cls_
             if self.custom_activation:
                 acc_ = self.loss_cls.get_accuracy(cls_score, labels)
                 losses.update(acc_)
             else:
                 losses['acc'] = accuracy(cls_score, labels)
     if bbox_pred is not None:
         bg_class_ind = self.num_classes
         # 0~self.num_classes-1 are FG, self.num_classes is BG
         pos_inds = (labels >= 0) & (labels < bg_class_ind)
         # do not perform bounding box regression for BG anymore.
         if pos_inds.any():
             if self.reg_decoded_bbox:
                 # When the regression loss (e.g. `IouLoss`,
                 # `GIouLoss`, `DIouLoss`) is applied directly on
                 # the decoded bounding boxes, it decodes the
                 # already encoded coordinates to absolute format.
                 bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
             if self.reg_class_agnostic:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
             else:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), -1,
                     4)[pos_inds.type(torch.bool),
                        labels[pos_inds.type(torch.bool)]]
             losses['loss_bbox'] = self.loss_bbox(
                 pos_bbox_pred,
                 bbox_targets[pos_inds.type(torch.bool)],
                 bbox_weights[pos_inds.type(torch.bool)],
                 avg_factor=bbox_targets.size(0),
                 reduction_override=reduction_override)
         else:
             losses['loss_bbox'] = bbox_pred[pos_inds].sum()
     return losses
Ejemplo n.º 7
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             gt_bboxes=None,
             img_metas=None,
             stage=None,
             reduction_override=None):

        # # rank, _ = get_dist_info()
        # # if self.if_save_image and rank == 0 and self.iteration % self.save_interval == 0:
        if 1:
            roi_loss_save_images(img_metas, cls_score, bbox_pred, rois, labels,
                                 gt_bboxes, self.bbox_coder, stage)

        losses = dict()
        if cls_score is not None:
            avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
            if cls_score.numel() > 0:
                losses['loss_cls'] = self.loss_cls(
                    cls_score,
                    labels,
                    label_weights,
                    avg_factor=avg_factor,
                    reduction_override=reduction_override)
                losses['acc'] = accuracy(cls_score, labels)
        if bbox_pred is not None:
            bg_class_ind = self.num_classes
            # 0~self.num_classes-1 are FG, self.num_classes is BG
            pos_inds = (labels >= 0) & (labels < bg_class_ind)
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                if self.reg_decoded_bbox:
                    bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
                if self.reg_class_agnostic:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
                else:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), -1,
                        4)[pos_inds.type(torch.bool),
                           labels[pos_inds.type(torch.bool)]]
                losses['loss_bbox'] = self.loss_bbox(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=bbox_targets.size(0),
                    reduction_override=reduction_override)
            else:
                losses['loss_bbox'] = bbox_pred[pos_inds].sum()
        return losses
Ejemplo n.º 8
0
 def loss(self,
          cls_score,
          bbox_pred,
          rois,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          reduction_override=None):
     losses = dict()
     if cls_score is not None:
         avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
         if cls_score.numel() > 0:
             losses['loss_cls'] = self.loss_cls(
                 cls_score,
                 labels,
                 label_weights,
                 avg_factor=avg_factor,
                 reduction_override=reduction_override)
             losses['acc'] = accuracy(cls_score, labels)
     if bbox_pred is not None:
         bg_class_ind = self.num_classes
         # print('bg ind are ')
         # print(bg_class_ind)
         # print('labels are')
         # print(np.unique(labels.cpu()))
         # 0~self.num_classes-1 are FG, self.num_classes is BG
         pos_inds = (labels >= 0) & (labels < bg_class_ind)
         # do not perform bounding box regression for BG anymore.
         if pos_inds.any():
             if self.reg_decoded_bbox:
                 bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
             if self.reg_class_agnostic:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
             else:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), -1,
                     4)[pos_inds.type(torch.bool),
                        labels[pos_inds.type(torch.bool)]]
             losses['loss_bbox'] = self.loss_bbox(
                 pos_bbox_pred,
                 bbox_targets[pos_inds.type(torch.bool)],
                 bbox_weights[pos_inds.type(torch.bool)],
                 avg_factor=bbox_targets.size(0),
                 reduction_override=reduction_override)
         else:
             losses['loss_bbox'] = bbox_pred.sum() * 0
     return losses
Ejemplo n.º 9
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()
        if cls_score is not None:
            avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
            losses['loss_cls'] = self.loss_cls(
                cls_score,
                labels,
                label_weights,
                avg_factor=avg_factor,
                reduction_override=reduction_override)
            losses['acc'] = accuracy(cls_score, labels)

        if bbox_pred is not None:
            bucket_cls_preds, bucket_offset_preds = bbox_pred
            bucket_cls_targets, bucket_offset_targets = bbox_targets
            bucket_cls_weights, bucket_offset_weights = bbox_weights
            # edge cls
            bucket_cls_preds = bucket_cls_preds.view(-1, self.side_num)
            bucket_cls_targets = bucket_cls_targets.view(-1, self.side_num)
            bucket_cls_weights = bucket_cls_weights.view(-1, self.side_num)
            losses['loss_bbox_cls'] = self.loss_bbox_cls(
                bucket_cls_preds,
                bucket_cls_targets,
                bucket_cls_weights,
                avg_factor=bucket_cls_targets.size(0),
                reduction_override=reduction_override)

            losses['loss_bbox_reg'] = self.loss_bbox_reg(
                bucket_offset_preds,
                bucket_offset_targets,
                bucket_offset_weights,
                avg_factor=bucket_offset_targets.size(0),
                reduction_override=reduction_override)

        return losses
 def loss(self,
          cls_score,
          bbox_pred,
          dist,
          rois,
          labels,
          label_weights,
          bbox_targets,
          bbox_weights,
          reduction_override=None,
          img_metas=None):
     losses = dict()
     if cls_score is not None:
         avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
         if cls_score.numel() > 0:
             #大类别分类
             cls_score_all = []
             for class_index in range(self.num_classes):
                 cls_score_all.append(
                     torch.max(cls_score[:, class_index *
                                         2:(class_index + 1) * 2],
                               dim=1)[0].unsqueeze(1))
             cls_score_all.append(cls_score[:, cls_score.shape[1] -
                                            1].unsqueeze(1))
             cls_score_all = torch.cat(cls_score_all,
                                       dim=1).to(labels.device)
             losses['loss_cls'] = self.loss_cls(
                 cls_score_all,
                 labels,
                 label_weights,
                 avg_factor=avg_factor,
                 reduction_override=reduction_override)
             losses['acc'] = accuracy(cls_score_all, labels)
             #风格分类
             proposal_num_per_pic = 512
             styles = torch.zeros(labels.shape[0] // proposal_num_per_pic)
             for i, meta in enumerate(img_metas):
                 pic = meta['filename'].split('.')[0].split('/')[-1]
                 styles[i] = int(self.file_to_style[pic])
             style_labels, cls_score_style = [], []
             for proposal in range(labels.shape[0]):
                 if labels[proposal] != 19:
                     # 当前proposal所属的风格
                     # 每个pic512个proposal
                     style_index = proposal // 512
                     cur_style = styles[style_index]
                     style_labels.append(cur_style.unsqueeze(0))
                     cls_score_style.append(
                         cls_score[proposal]
                         [labels[proposal]:labels[proposal] +
                          2].unsqueeze(0))
             style_labels = torch.cat(style_labels,
                                      dim=0).to(cls_score.device).long()
             cls_score_style = torch.cat(cls_score_style,
                                         dim=0).to(cls_score.device)
             label_weights = torch.ones(style_labels.shape[0]).to(
                 cls_score.device)
             avg_factor = max(
                 torch.sum(label_weights > 0).float().item(), 1.)
             losses['loss_cls_style'] = self.loss_cls(
                 cls_score_style,
                 style_labels,
                 label_weights,
                 avg_factor=avg_factor,
                 reduction_override=reduction_override)
     if bbox_pred is not None:
         bg_class_ind = self.num_classes
         # 0~self.num_classes-1 are FG, self.num_classes is BG
         pos_inds = (labels >= 0) & (labels < bg_class_ind)
         # do not perform bounding box regression for BG anymore.
         if pos_inds.any():
             if self.reg_decoded_bbox:
                 bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
             if self.reg_class_agnostic:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
             else:
                 pos_bbox_pred = bbox_pred.view(
                     bbox_pred.size(0), -1,
                     4)[pos_inds.type(torch.bool),
                        labels[pos_inds.type(torch.bool)]]
             losses['loss_bbox'] = self.loss_bbox(
                 pos_bbox_pred,
                 bbox_targets[pos_inds.type(torch.bool)],
                 bbox_weights[pos_inds.type(torch.bool)],
                 avg_factor=bbox_targets.size(0),
                 reduction_override=reduction_override)
         else:
             losses['loss_bbox'] = bbox_pred[pos_inds].sum()
     return losses
Ejemplo n.º 11
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()
        if cls_score is not None:
            avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
            if cls_score.numel() > 0:
                losses['loss_cls'] = self.loss_cls(
                    cls_score,
                    labels,
                    label_weights,
                    avg_factor=avg_factor,
                    reduction_override=reduction_override)
                losses['acc'] = accuracy(cls_score, labels)
        if bbox_pred is not None:
            bg_class_ind = self.num_classes
            # 0~self.num_classes-1 are FG, self.num_classes is BG
            pos_inds = (labels >= 0) & (labels < bg_class_ind)
            # print(pos_inds)
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                if self.reg_decoded_bbox:
                    bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
                if self.reg_class_agnostic:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), 5)[pos_inds.type(torch.bool)]
                else:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), -1,
                        5)[pos_inds.type(torch.bool),
                           labels[pos_inds.type(torch.bool)]]
                losses['loss_bbox'] = self.loss_bbox(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=bbox_targets.size(0),
                    reduction_override=reduction_override)
                # print(pos_bbox_pred,
                #     bbox_targets[pos_inds.type(torch.bool)],
                #     bbox_weights[pos_inds.type(torch.bool)])
            else:
                losses['loss_bbox'] = bbox_pred.sum() * 0
        # ###########################################################
        from mmdet.MARK import PRINT_RBBOX_HEAD_RS_LOSS
        if torch.sum(pos_inds) > 0:
            # pos_inds = pos_inds[0:min(len(pos_inds), 10)]
            pred_score = cls_score[pos_inds].softmax(1)
            pred_score, pred_label = torch.max(pred_score, dim=1)

            # 前景标签
            pred_f_indices = pred_label != bg_class_ind

            # from EXP_CONCONFIG.MARK import PRINT_RBBOX_HEAD
            # PRINT_RBBOX_HEAD = True
            pos_loss = self.loss_cls(cls_score[pos_inds], labels[pos_inds],
                                     label_weights[pos_inds])
            pos_acc = accuracy(cls_score[pos_inds], labels[pos_inds])

            if torch.sum(pred_f_indices) > 0:
                print('#' * 80)
                print('pred_score:',
                      pred_score[pred_f_indices].detach().cpu().numpy())
                print('pred_label:',
                      pred_label[pred_f_indices].detach().cpu().numpy())
                print('gt_label:',
                      labels[pos_inds][pred_f_indices].detach().cpu().numpy())
                print('pos loss: %.2f' % float(pos_loss),
                      '   || pos acc: %.2f' % float(pos_acc))
                print('total_loss:', float(losses['loss_cls']))
                print('%d, %d' % (int(torch.sum(pos_inds)), len(cls_score)))
                print(torch.sum(label_weights))

                print('#' * 80)
            # ###########################################################
        return losses
Ejemplo n.º 12
0
    def loss(self,
             cls_score,
             bbox_pred,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             imgs_whwh=None,
             reduction_override=None,
             **kwargs):
        """"Loss function of DIIHead, get loss of all images.

        Args:
            cls_score (Tensor): Classification prediction
                results of all class, has shape
                (batch_size * num_proposals_single_image, num_classes)
            bbox_pred (Tensor): Regression prediction results,
                has shape
                (batch_size * num_proposals_single_image, 4), the last
                dimension 4 represents [tl_x, tl_y, br_x, br_y].
            labels (Tensor): Label of each proposals, has shape
                (batch_size * num_proposals_single_image
            label_weights (Tensor): Classification loss
                weight of each proposals, has shape
                (batch_size * num_proposals_single_image
            bbox_targets (Tensor): Regression targets of each
                proposals, has shape
                (batch_size * num_proposals_single_image, 4),
                the last dimension 4 represents
                [tl_x, tl_y, br_x, br_y].
            bbox_weights (Tensor): Regression loss weight of each
                proposals's coordinate, has shape
                (batch_size * num_proposals_single_image, 4),
            imgs_whwh (Tensor): imgs_whwh (Tensor): Tensor with\
                shape (batch_size, num_proposals, 4), the last
                dimension means
                [img_width,img_height, img_width, img_height].
            reduction_override (str, optional): The reduction
                method used to override the original reduction
                method of the loss. Options are "none",
                "mean" and "sum". Defaults to None,

            Returns:
                dict[str, Tensor]: Dictionary of loss components
        """
        losses = dict()
        bg_class_ind = self.num_classes
        # note in spare rcnn num_gt == num_pos
        pos_inds = (labels >= 0) & (labels < bg_class_ind)
        num_pos = pos_inds.sum().float()
        avg_factor = reduce_mean(num_pos)
        if cls_score is not None:
            if cls_score.numel() > 0:
                losses['loss_cls'] = self.loss_cls(
                    cls_score,
                    labels,
                    label_weights,
                    avg_factor=avg_factor,
                    reduction_override=reduction_override)
                losses['pos_acc'] = accuracy(cls_score[pos_inds],
                                             labels[pos_inds])
        if bbox_pred is not None:
            # 0~self.num_classes-1 are FG, self.num_classes is BG
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                pos_bbox_pred = bbox_pred.reshape(bbox_pred.size(0),
                                                  4)[pos_inds.type(torch.bool)]
                imgs_whwh = imgs_whwh.reshape(bbox_pred.size(0),
                                              4)[pos_inds.type(torch.bool)]
                losses['loss_bbox'] = self.loss_bbox(
                    pos_bbox_pred / imgs_whwh,
                    bbox_targets[pos_inds.type(torch.bool)] / imgs_whwh,
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=avg_factor)
                losses['loss_iou'] = self.loss_iou(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=avg_factor)
            else:
                losses['loss_bbox'] = bbox_pred.sum() * 0
                losses['loss_iou'] = bbox_pred.sum() * 0
        return losses
Ejemplo n.º 13
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()

        if bbox_pred is not None:
            bg_class_ind = self.num_classes
            # 0~self.num_classes-1 are FG, self.num_classes is BG
            pos_inds = (labels >= 0) & (labels < bg_class_ind)
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                if self.reg_decoded_bbox:
                    bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
                if self.reg_class_agnostic:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
                else:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), -1,
                        4)[pos_inds.type(torch.bool),
                           labels[pos_inds.type(torch.bool)]]
                losses['loss_bbox'] = self.loss_bbox(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=bbox_targets.size(0),
                    reduction_override=reduction_override)
            else:
                losses['loss_bbox'] = bbox_pred[pos_inds].sum()

        if cls_score is not None:
            avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
            if cls_score.numel() > 0:
                argspec = inspect.getargspec(self.loss_cls.forward)
                pos_ious = bbox_overlaps(pos_bbox_pred,
                                         bbox_targets[pos_inds.type(
                                             torch.bool)],
                                         is_aligned=True).clamp(min=1e-7)
                if 'pos_ious' not in argspec.args:
                    losses['loss_cls'] = self.loss_cls(
                        cls_score,
                        labels,
                        label_weights,
                        avg_factor=avg_factor,
                        reduction_override=reduction_override)
                else:
                    losses['loss_cls'] = self.loss_cls(
                        cls_score,
                        labels,
                        pos_ious=pos_ious,
                        weight=label_weights,
                        avg_factor=avg_factor,
                        reduction_override=reduction_override)
                losses['acc'] = accuracy(cls_score, labels)
        return losses
Ejemplo n.º 14
0
    def loss(self,
             similarity_logits,
             track_id_targets,
             track_id_weights,
             reduction_override=None):
        """Calculate the loss in a batch.

        Args:
            similarity_logits (list[Tensor]): The predicted similarity_logits
                of each pair of key image and reference image.
            track_id_targets (list[Tensor]): The instance ids of Gt_labels for
                all proposals in a batch, each tensor in list has shape
                (num_proposals,).
            track_id_weights (list[Tensor]): Labels_weights for
                all proposals in a batch, each tensor in list has shape
                (num_proposals,).
            reduction_override (str, optional): The method used to reduce the
                loss. Options are "none", "mean" and "sum".

        Returns:
            dict[str, Tensor]: a dictionary of loss components.
        """
        assert isinstance(similarity_logits, list)
        assert isinstance(track_id_targets, list)
        assert isinstance(track_id_weights, list)
        assert len(similarity_logits) == len(track_id_targets)
        assert len(track_id_weights) == len(track_id_targets)
        losses = defaultdict(list)

        for similarity_logit, track_id_target, track_id_weight in zip(
                similarity_logits, track_id_targets, track_id_weights):
            avg_factor = max(torch.sum(track_id_target > 0).float().item(), 1.)
            if similarity_logit.numel() > 0:
                loss_match = self.loss_match(
                    similarity_logit,
                    track_id_target,
                    track_id_weight,
                    avg_factor=avg_factor,
                    reduction_override=reduction_override)
                if isinstance(loss_match, dict):
                    for key, value in loss_match.items():
                        losses[key].append(value)
                else:
                    losses['loss_match'].append(loss_match)

                valid_index = track_id_weight > 0
                valid_similarity_logit = similarity_logit[valid_index]
                valid_track_id_target = track_id_target[valid_index]
                if self.custom_activation:
                    match_accuracy = self.loss_match.get_accuracy(
                        valid_similarity_logit, valid_track_id_target)
                    for key, value in match_accuracy.items():
                        losses[key].append(value)
                else:
                    losses['match_accuracy'].append(
                        accuracy(valid_similarity_logit,
                                 valid_track_id_target))

        for key, value in losses.items():
            losses[key] = sum(losses[key]) / len(similarity_logits)
        return losses
Ejemplo n.º 15
0
    def loss(self,
             cls_score,
             bbox_pred,
             fix_pred,
             ratio_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             fix_targets,
             fix_weights,
             ratio_targets,
             ratio_weights,
             reduction_override=None):
        losses = dict()
        avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
        if cls_score.numel() > 0:
            losses['loss_cls'] = self.loss_cls(
                cls_score,
                labels,
                label_weights,
                avg_factor=avg_factor,
                reduction_override=reduction_override)
            losses['acc'] = accuracy(cls_score, labels)

        bg_class_ind = self.num_classes
        # 0~self.num_classes-1 are FG, self.num_classes is BG
        pos_inds = (labels >= 0) & (labels < bg_class_ind)
        # do not perform bounding box regression for BG anymore.
        if pos_inds.any():
            if self.reg_class_agnostic:
                pos_bbox_pred = bbox_pred.view(bbox_pred.size(0),
                                               4)[pos_inds.type(torch.bool)]
            else:
                pos_bbox_pred = bbox_pred.view(
                    bbox_pred.size(0), -1,
                    4)[pos_inds.type(torch.bool),
                       labels[pos_inds.type(torch.bool)]]
            losses['loss_bbox'] = self.loss_bbox(
                pos_bbox_pred,
                bbox_targets[pos_inds.type(torch.bool)],
                bbox_weights[pos_inds.type(torch.bool)],
                avg_factor=bbox_targets.size(0),
                reduction_override=reduction_override)
        else:
            losses['loss_bbox'] = bbox_pred.sum() * 0

        if pos_inds.any():
            if self.reg_class_agnostic:
                pos_fix_pred = fix_pred.view(fix_pred.size(0),
                                             4)[pos_inds.type(torch.bool)]
            else:
                pos_fix_pred = fix_pred.view(
                    fix_pred.size(0), -1, 4)[pos_inds.type(torch.bool),
                                             labels[pos_inds.type(torch.bool)]]
            losses['loss_fix'] = self.loss_fix(
                pos_fix_pred,
                fix_targets[pos_inds.type(torch.bool)],
                fix_weights[pos_inds.type(torch.bool)],
                avg_factor=fix_targets.size(0),
                reduction_override=reduction_override)
        else:
            losses['loss_fix'] = fix_pred.sum() * 0

        if pos_inds.any():
            if self.reg_class_agnostic:
                pos_ratio_pred = ratio_pred.view(ratio_pred.size(0),
                                                 1)[pos_inds.type(torch.bool)]
            else:
                pos_ratio_pred = ratio_pred.view(
                    ratio_pred.size(0), -1,
                    1)[pos_inds.type(torch.bool),
                       labels[pos_inds.type(torch.bool)]]
            losses['loss_ratio'] = self.loss_ratio(
                pos_ratio_pred,
                ratio_targets[pos_inds.type(torch.bool)],
                ratio_weights[pos_inds.type(torch.bool)],
                avg_factor=ratio_targets.size(0),
                reduction_override=reduction_override)
        else:
            losses['loss_ratio'] = ratio_pred.sum() * 0

        return losses
Ejemplo n.º 16
0
    def loss(self,
             cls_score,
             bbox_pred,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()
        # if cls_score is not None:
        #     avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)
        #     if cls_score.numel() > 0:
        #         losses['loss_cls'] = self.loss_cls(
        #             cls_score,
        #             labels,
        #             label_weights,
        #             avg_factor=avg_factor,
        #             reduction_override=reduction_override)
        #         pos_inds = (labels >= 0) & (labels < self.num_classes)
        #         losses['acc'] = pos_inds.type(torch.float32).sum()
        #         # losses['acc'] = accuracy(cls_score, labels)
        # if bbox_pred is not None:
        #     bg_class_ind = self.num_classes
        #     # 0~self.num_classes-1 are FG, self.num_classes is BG
        #     pos_inds = (labels >= 0) & (labels < bg_class_ind)
        #     # do not perform bounding box regression for BG anymore.
        #     if pos_inds.any():
        #         if self.reg_decoded_bbox:
        #             bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
        #         if self.reg_class_agnostic:
        #             pos_bbox_pred = bbox_pred.view(
        #                 bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
        #         else:
        #             pos_bbox_pred = bbox_pred.view(
        #                 bbox_pred.size(0), -1,
        #                 4)[pos_inds.type(torch.bool),
        #                    labels[pos_inds.type(torch.bool)]]
        #         losses['loss_bbox'] = self.loss_bbox(
        #             pos_bbox_pred,
        #             bbox_targets[pos_inds.type(torch.bool)],
        #             bbox_weights[pos_inds.type(torch.bool)],
        #             avg_factor=bbox_targets.size(0),
        #             reduction_override=reduction_override)
        #     else:
        #         losses['loss_bbox'] = bbox_pred[pos_inds].sum()

        bg_class_ind = self.num_classes
        pos_inds = (labels >= 0) & (labels < bg_class_ind)
        if pos_inds.any():
            if self.reg_decoded_bbox:
                bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
            if self.reg_class_agnostic:
                pos_bbox_pred = bbox_pred.view(
                    bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
            else:
                pos_bbox_pred = bbox_pred.view(-1, 4)

            losses['loss_bbox'] = self.loss_bbox(
                pos_bbox_pred[pos_inds.type(torch.bool)],
                bbox_targets[pos_inds.type(torch.bool)],
                bbox_weights[pos_inds.type(torch.bool)],
                avg_factor=bbox_targets.size(0),
                reduction_override=reduction_override)

            pos_pred_bboxes = self.bbox_coder.decode(rois[pos_inds, 1:], bbox_pred[pos_inds])
            pos_target_bboxes = self.bbox_coder.decode(rois[pos_inds, 1:], bbox_targets[pos_inds])
            from mmdet.core import bbox_overlaps
            score = label_weights.new_zeros(labels.shape)
            score[pos_inds] = bbox_overlaps(pos_pred_bboxes,
                            pos_target_bboxes, is_aligned=True)


            avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)

            losses['loss_cls'] = self.loss_cls(
                cls_score, (labels, score),
                weight=label_weights,
                avg_factor=avg_factor,
                reduction_override=reduction_override)

            losses['num'] = pos_inds.type(torch.float32).sum()
            losses['acc'] = accuracy(cls_score, labels)

        return losses
Ejemplo n.º 17
0
    def loss(self,
             okpd_outs,
             rois,
             labels,
             label_weights,
             bbox_targets,
             bbox_weights,
             reduction_override=None):
        losses = dict()
        cls_score = okpd_outs[-2]
        bbox_pred = okpd_outs[-1]
        bg_class_ind = self.num_classes
        pos_inds = (labels >= 0) & (labels < bg_class_ind)
        avg_factor = max(torch.sum(label_weights > 0).float().item(), 1.)

        if cls_score.numel() > 0:
            losses['loss_okpd_cls'] = self.loss_cls(
                cls_score,
                labels,
                label_weights,
                avg_factor=avg_factor,
                reduction_override=reduction_override)
            losses['okpd_acc'] = accuracy(cls_score, labels)

        if bbox_pred is not None:
            # do not perform bounding box regression for BG anymore.
            if pos_inds.any():
                if self.reg_decoded_bbox:
                    bbox_pred = self.bbox_coder.decode(rois[:, 1:], bbox_pred)
                if self.reg_class_agnostic:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), 4)[pos_inds.type(torch.bool)]
                else:
                    pos_bbox_pred = bbox_pred.view(
                        bbox_pred.size(0), -1,
                        4)[pos_inds.type(torch.bool),
                           labels[pos_inds.type(torch.bool)]]
                losses['loss_okpd_bbox'] = self.loss_bbox(
                    pos_bbox_pred,
                    bbox_targets[pos_inds.type(torch.bool)],
                    bbox_weights[pos_inds.type(torch.bool)],
                    avg_factor=bbox_targets.size(0),
                    reduction_override=reduction_override)
            else:
                losses['loss_bbox'] = bbox_pred.sum() * 0

        if self.num_kp > 0:
            kp_max_target = torch.zeros_like(okpd_outs[0])
            kp_max_target[pos_inds, ...] = 1.
            kp_max_wts = torch.zeros_like(okpd_outs[0]) + 0.5
            fn_inds = (kp_max_target > 0.) & (okpd_outs[0] < 0.5)
            fp_inds = (kp_max_target < 1.) & (okpd_outs[0] > 0.5)
            kp_max_wts[fp_inds | fn_inds] = 1.0
            Ld_loss = smooth_l1_loss(okpd_outs[0] * kp_max_wts,
                                     kp_max_target * kp_max_wts,
                                     beta=0.05)

            pos_mask = pos_inds.float()[:, None, None, None]
            kp_ch_sum_target = torch.zeros_like(okpd_outs[1])
            kp_ch_sum_target[pos_inds, ...] = 1.
            Lu_loss = smooth_l1_loss(okpd_outs[1] * pos_mask,
                                     kp_ch_sum_target * pos_mask,
                                     beta=1.0)
            losses['kp_loss'] = 0.03 * (Ld_loss + Lu_loss)

        return losses