Beispiel #1
0
def smooth_label_loss(cls_score,
                      label_int32,
                      gt_score,
                      gt_source,
                      dist_T=1.0,
                      dist_lambda=0.5,
                      tracker_score=0.0,
                      cls_id=1):
    """
    Compute the label smoothing loss given network predictions (logits), hard 
    labels and soft labels. This currently supports a single category only.

    soft_label = (1-dist_lambda)*label + dist_lambda*gt_score
    Loss = BinaryCrossEntropy(predicted_scores, soft_label)

    Inputs: cls_score (network predictions), label_int32 (hard labels), 
            gt_score (baseline detector scores as groundtruth), 
            gt_source (1: detector, 2: tracker)

    """
    # TODO: single generalized multi-class distillation loss!
    if DEBUG:
        print('\tDistillation loss')

    assert all(gt_score >= 0) & all(gt_score <= 1)  # sanity-check
    assert gt_score.shape == label_int32.shape
    if not cfg.TRAIN.DISTILL_ATTN:
        assert dist_lambda > 0
    assert dist_T > 0
    pred = cls_score[:, cls_id] / dist_T  # tempered prediction logits
    device_id = pred.get_device()
    gt_score[gt_source == 2] = tracker_score
    label = torch.from_numpy(label_int32.astype('float32'))
    gt_score = torch.from_numpy(gt_score.astype('float32'))

    if not cfg.TRAIN.DISTILL_ATTN:
        # Blend "baseline gt score" and "label" with temperature
        soft_label = (1 - dist_lambda) * label + dist_lambda * gt_score
        soft_label = soft_label**(1 /
                                  dist_T) / (soft_label**(1 / dist_T) +
                                             (1 - soft_label)**(1 / dist_T))
        soft_label = Variable(soft_label).cuda(device_id)
    else:
        # "dist_lambda" is a learnable Variable
        label = Variable(label).cuda(device_id)
        gt_score = Variable(gt_score).cuda(device_id)
        soft_label = (1 - torch.squeeze(dist_lambda)
                      ) * label + torch.squeeze(dist_lambda) * gt_score
        soft_label = soft_label**(1 /
                                  dist_T) / (soft_label**(1 / dist_T) +
                                             (1 - soft_label)**(1 / dist_T))

    loss_distill = F.binary_cross_entropy_with_logits(pred, soft_label)
    assert not net_utils.is_nan_loss(loss_distill)
    return loss_distill
Beispiel #2
0
def fast_rcnn_losses(cls_score, bbox_pred, label_int32, bbox_targets,
                     bbox_inside_weights, bbox_outside_weights):
    device_id = cls_score.get_device()
    rois_label = Variable(torch.from_numpy(
        label_int32.astype('int64'))).cuda(device_id)
    loss_cls = F.cross_entropy(cls_score, rois_label)
    assert not net_utils.is_nan_loss(loss_cls)

    bbox_targets = Variable(torch.from_numpy(bbox_targets)).cuda(device_id)
    bbox_inside_weights = Variable(
        torch.from_numpy(bbox_inside_weights)).cuda(device_id)
    bbox_outside_weights = Variable(
        torch.from_numpy(bbox_outside_weights)).cuda(device_id)
    loss_bbox = net_utils.smooth_l1_loss(bbox_pred, bbox_targets,
                                         bbox_inside_weights,
                                         bbox_outside_weights)
    assert not net_utils.is_nan_loss(loss_bbox)

    # class accuracy
    cls_preds = cls_score.max(dim=1)[1].type_as(rois_label)
    accuracy_cls = cls_preds.eq(rois_label).float().mean(dim=0)

    return loss_cls, loss_bbox, accuracy_cls
Beispiel #3
0
def domain_loss_roi(pred, domain_label):
    """
    ROI-level domain adversarial loss
    
    """
    if DEBUG:
        print('\tDA-ROI loss')
    device_id = pred.get_device()
    target_label = Variable(
        torch.FloatTensor(pred.data.size()).fill_(
            float(domain_label))).cuda(device_id)
    loss_da_roi = F.binary_cross_entropy_with_logits(pred, target_label)
    if net_utils.is_nan_loss(loss_da_roi):
        loss_da_roi *= 0
    return loss_da_roi