本文整理汇总了Python中utils.net.smooth_l1_loss方法的典型用法代码示例。如果您正苦于以下问题:Python net.smooth_l1_loss方法的具体用法?Python net.smooth_l1_loss怎么用?Python net.smooth_l1_loss使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.net
的用法示例。
在下文中一共展示了net.smooth_l1_loss方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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)
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)
# 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
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
示例2: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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)
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, beta=1/3)
# 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
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
示例3: single_scale_rpn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
def single_scale_rpn_losses(
rpn_cls_logits, rpn_bbox_pred,
rpn_labels_int32_wide, rpn_bbox_targets_wide,
rpn_bbox_inside_weights_wide, rpn_bbox_outside_weights_wide):
"""Add losses for a single scale RPN model (i.e., no FPN)."""
h, w = rpn_cls_logits.shape[2:]
rpn_labels_int32 = rpn_labels_int32_wide[:, :, :h, :w] # -1 means ignore
h, w = rpn_bbox_pred.shape[2:]
rpn_bbox_targets = rpn_bbox_targets_wide[:, :, :h, :w]
rpn_bbox_inside_weights = rpn_bbox_inside_weights_wide[:, :, :h, :w]
rpn_bbox_outside_weights = rpn_bbox_outside_weights_wide[:, :, :h, :w]
if cfg.RPN.CLS_ACTIVATION == 'softmax':
B, C, H, W = rpn_cls_logits.size()
rpn_cls_logits = rpn_cls_logits.view(
B, 2, C // 2, H, W).permute(0, 2, 3, 4, 1).contiguous().view(-1, 2)
rpn_labels_int32 = rpn_labels_int32.contiguous().view(-1).long()
# the loss is averaged over non-ignored targets
loss_rpn_cls = F.cross_entropy(
rpn_cls_logits, rpn_labels_int32, ignore_index=-1)
else:
weight = (rpn_labels_int32 >= 0).float()
loss_rpn_cls = F.binary_cross_entropy_with_logits(
rpn_cls_logits, rpn_labels_int32.float(), weight, size_average=False)
loss_rpn_cls /= weight.sum()
loss_rpn_bbox = net_utils.smooth_l1_loss(
rpn_bbox_pred, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights,
beta=1/9)
return loss_rpn_cls, loss_rpn_bbox
示例4: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
def fast_rcnn_losses(cls_score, bbox_pred, label_int32, bbox_targets,
bbox_inside_weights, bbox_outside_weights, stage=0):
device_id = cls_score.get_device()
rois_label = Variable(torch.from_numpy(label_int32.astype('int64'))).cuda(device_id)
if cfg.CASCADE_RCNN.SCALE_LOSS:
loss_scalar = cfg.CASCADE_RCNN.STAGE_WEIGHTS[stage]
else:
loss_scalar = 1.0
if not cfg.FAST_RCNN.FOCAL_LOSS:
loss_cls = F.cross_entropy(cls_score, rois_label) * loss_scalar
else:
alpha = torch.ones((cls_score.shape[1], 1)) * cfg.FAST_RCNN.ALPHA
alpha[0] = 1 - cfg.FAST_RCNN.ALPHA
focalloss = FocalLoss(class_num=cls_score.shape[1], alpha=alpha, gamma=cfg.FAST_RCNN.GAMMA)
loss_cls = focalloss(cls_score, rois_label) * loss_scalar
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) * loss_scalar
# 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
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
示例5: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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)
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)
sl1_loss_bbox = net_utils.smooth_l1_loss(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights)
iou_loss_bbox, giou_loss_bbox = net_utils.compute_iou(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights,
transform_weights=cfg.MODEL.BBOX_REG_WEIGHTS)
if cfg.MODEL.LOSS_TYPE == 'smooth_l1':
loss_bbox = sl1_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'iou':
loss_bbox = iou_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'giou':
loss_bbox = giou_loss_bbox
else:
raise ValueError('Invalid loss type: ' + cfg.MODEL.LOSS_TYPE)
# 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, sl1_loss_bbox, iou_loss_bbox, giou_loss_bbox
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
示例6: single_scale_rpn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
def single_scale_rpn_losses(
rpn_cls_logits, rpn_bbox_pred,
rpn_labels_int32_wide, rpn_bbox_targets_wide,
rpn_bbox_inside_weights_wide, rpn_bbox_outside_weights_wide):
"""Add losses for a single scale RPN model (i.e., no FPN)."""
h, w = rpn_cls_logits.shape[2:]
rpn_labels_int32 = rpn_labels_int32_wide[:, :, :h, :w] # -1 means ignore
h, w = rpn_bbox_pred.shape[2:]
rpn_bbox_targets = rpn_bbox_targets_wide[:, :, :h, :w]
rpn_bbox_inside_weights = rpn_bbox_inside_weights_wide[:, :, :h, :w]
rpn_bbox_outside_weights = rpn_bbox_outside_weights_wide[:, :, :h, :w]
if cfg.RPN.CLS_ACTIVATION == 'softmax':
B, C, H, W = rpn_cls_logits.size()
rpn_cls_logits = rpn_cls_logits.view(
B, 2, C // 2, H, W).permute(0, 2, 3, 4, 1).contiguous().view(-1, 2)
rpn_labels_int32 = rpn_labels_int32.contiguous().view(-1).long()
# the loss is averaged over non-ignored targets
loss_rpn_cls = F.cross_entropy(
rpn_cls_logits, rpn_labels_int32, ignore_index=-1)
else:
weight = (rpn_labels_int32 >= 0).float()
loss_rpn_cls = F.binary_cross_entropy_with_logits(
rpn_cls_logits, rpn_labels_int32.float(), weight, reduction='sum')
loss_rpn_cls /= weight.sum()
loss_rpn_bbox = net_utils.smooth_l1_loss(
rpn_bbox_pred, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights,
beta=1/9)
return loss_rpn_cls, loss_rpn_bbox
示例7: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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
# if self.distill:
# soft_target = Variable(data[2].cuda())
# distill_loss = torch.mean(torch.sum(- nn.Softmax(dim=1)(soft_target/self.T) * nn.LogSoftmax(dim=1)(out_data/self.T), 1))
# loss += self.lbda*distill_loss
# self.writer.add_scalar('train/distill_loss', distill_loss, i_acc+i+1)
示例8: fpn_rpn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
def fpn_rpn_losses(**kwargs):
"""Add RPN on FPN specific losses."""
losses_cls = []
losses_bbox = []
for lvl in range(cfg.FPN.RPN_MIN_LEVEL, cfg.FPN.RPN_MAX_LEVEL + 1):
slvl = str(lvl)
# Spatially narrow the full-sized RPN label arrays to match the feature map shape
b, c, h, w = kwargs['rpn_cls_logits_fpn' + slvl].shape
rpn_labels_int32_fpn = kwargs['rpn_labels_int32_wide_fpn' + slvl][:, :, :h, :w]
h, w = kwargs['rpn_bbox_pred_fpn' + slvl].shape[2:]
rpn_bbox_targets_fpn = kwargs['rpn_bbox_targets_wide_fpn' + slvl][:, :, :h, :w]
rpn_bbox_inside_weights_fpn = kwargs[
'rpn_bbox_inside_weights_wide_fpn' + slvl][:, :, :h, :w]
rpn_bbox_outside_weights_fpn = kwargs[
'rpn_bbox_outside_weights_wide_fpn' + slvl][:, :, :h, :w]
if cfg.RPN.CLS_ACTIVATION == 'softmax':
rpn_cls_logits_fpn = kwargs['rpn_cls_logits_fpn' + slvl].view(
b, 2, c // 2, h, w).permute(0, 2, 3, 4, 1).contiguous().view(-1, 2)
rpn_labels_int32_fpn = rpn_labels_int32_fpn.contiguous().view(-1).long()
# the loss is averaged over non-ignored targets
loss_rpn_cls_fpn = F.cross_entropy(
rpn_cls_logits_fpn, rpn_labels_int32_fpn, ignore_index=-1)
else: # sigmoid
weight = (rpn_labels_int32_fpn >= 0).float()
loss_rpn_cls_fpn = F.binary_cross_entropy_with_logits(
kwargs['rpn_cls_logits_fpn' + slvl], rpn_labels_int32_fpn.float(), weight,
size_average=False)
loss_rpn_cls_fpn /= cfg.TRAIN.RPN_BATCH_SIZE_PER_IM * cfg.TRAIN.IMS_PER_BATCH
# Normalization by (1) RPN_BATCH_SIZE_PER_IM and (2) IMS_PER_BATCH is
# handled by (1) setting bbox outside weights and (2) SmoothL1Loss
# normalizes by IMS_PER_BATCH
loss_rpn_bbox_fpn = net_utils.smooth_l1_loss(
kwargs['rpn_bbox_pred_fpn' + slvl], rpn_bbox_targets_fpn,
rpn_bbox_inside_weights_fpn, rpn_bbox_outside_weights_fpn,
beta=1/9)
losses_cls.append(loss_rpn_cls_fpn)
losses_bbox.append(loss_rpn_bbox_fpn)
return losses_cls, losses_bbox
# ---------------------------------------------------------------------------- #
# FPN level info for stages 5, 4, 3, 2 for select models (more can be added)
# ---------------------------------------------------------------------------- #
示例9: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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)
if cfg.FAST_RCNN.LOSS_TYPE in ['cross_entropy', 'triplet_softmax']:
if cfg.FAST_RCNN.LOSS_TYPE == 'triplet_softmax':
cls_score = cls_score * 3 # This method is borrowed from ji zhang's large scale relationship detection
if not cfg.MODEL.TAGGING:
loss_cls = F.cross_entropy(cls_score, rois_label)
else:
loss_cls = F.cross_entropy(cls_score, rois_label, ignore_index=0)
if cfg.FAST_RCNN.LOSS_TYPE == 'triplet_softmax':
cls_score = cls_score / 3
else:
if cfg.FAST_RCNN.LOSS_TYPE == 'multi_margin':
loss_cls = F.multi_margin_loss(cls_score, rois_label,
margin=cfg.FAST_RCNN.MARGIN,
reduction='none')
elif cfg.FAST_RCNN.LOSS_TYPE == 'max_margin':
cls_score_with_high_target = cls_score.clone()
cls_score_with_high_target.scatter_(1, rois_label.view(-1, 1), 1e10) # This make sure the following variable always has the target in the first column
target_and_offender_index = cls_score_with_high_target.sort(1, True)[1][:, :2] # Target and the largest score excpet target
loss_cls = F.multi_margin_loss(cls_score.gather(1, target_and_offender_index), rois_label.data * 0,
margin=cfg.FAST_RCNN.MARGIN,
reduction='none')
loss_cls = loss_cls[rois_label > 0]
loss_cls = loss_cls.mean() if loss_cls.numel() > 0 else loss_cls.new_tensor(0)
# Secretly log the mean similarity!
if cfg.FAST_RCNN.LOSS_TYPE in ['triplet_softmax', 'max_margin', 'multi_margin']:
loss_cls.mean_similarity = cls_score[rois_label>0].gather(1, rois_label[rois_label>0].unsqueeze(1)).mean().detach() / 3
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)
if cfg.MODEL.TAGGING:
loss_bbox = torch.zeros_like(loss_bbox)
# class accuracy
cls_preds = cls_score.max(dim=1)[1].type_as(rois_label)
if not cfg.MODEL.TAGGING:
accuracy_cls = cls_preds.eq(rois_label).float().mean(dim=0)
else:
accuracy_cls = cls_preds[rois_label > 0].eq(rois_label[rois_label > 0]).float().mean(dim=0) # Ignore index 0
return loss_cls, loss_bbox, accuracy_cls
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
示例10: fast_rcnn_losses
# 需要导入模块: from utils import net [as 别名]
# 或者: from utils.net import smooth_l1_loss [as 别名]
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)
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)
sl1_loss_bbox = net_utils.smooth_l1_loss(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights)
iou_loss_bbox, giou_loss_bbox = net_utils.compute_giou(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights,
transform_weights=cfg.MODEL.BBOX_REG_WEIGHTS)
if cfg.MODEL.LOSS_TYPE == 'smooth_l1':
loss_bbox = sl1_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'iou':
loss_bbox = iou_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'giou':
loss_bbox = giou_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'diou':
_, diou_loss_bbox = net_utils.compute_diou(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights,
transform_weights=cfg.MODEL.BBOX_REG_WEIGHTS)
loss_bbox = diou_loss_bbox
elif cfg.MODEL.LOSS_TYPE == 'ciou':
_, ciou_loss_bbox = net_utils.compute_ciou(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights,
transform_weights=cfg.MODEL.BBOX_REG_WEIGHTS)
loss_bbox = ciou_loss_bbox
else:
raise ValueError('Invalid loss type: ' + cfg.MODEL.LOSS_TYPE)
# 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, sl1_loss_bbox, iou_loss_bbox, giou_loss_bbox
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #