本文整理匯總了Python中torch.nn.SmoothL1Loss方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.SmoothL1Loss方法的具體用法?Python nn.SmoothL1Loss怎麽用?Python nn.SmoothL1Loss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.SmoothL1Loss方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, n_templates=25, reg_weight=1, pos_fraction=0.5):
super().__init__()
# We don't want per element averaging.
# We want to normalize over the batch or positive samples.
self.regression_criterion = nn.SmoothL1Loss(reduction='none')
self.classification_criterion = nn.SoftMarginLoss(reduction='none')
self.n_templates = n_templates
self.reg_weight = reg_weight
self.pos_fraction = pos_fraction
self.class_average = AvgMeter()
self.reg_average = AvgMeter()
self.masked_class_loss = None
self.masked_reg_loss = None
self.total_loss = None
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, params, dist_model=False):
super(CMP, self).__init__(params, dist_model)
model_params = params['module']
# define loss
if model_params['flow_criterion'] == 'L1':
self.flow_criterion = nn.SmoothL1Loss()
elif model_params['flow_criterion'] == 'L2':
self.flow_criterion = nn.MSELoss()
elif model_params['flow_criterion'] == 'DiscreteLoss':
self.flow_criterion = losses.DiscreteLoss(
nbins=model_params['nbins'], fmax=model_params['fmax'])
else:
raise Exception("No such flow loss: {}".format(model_params['flow_criterion']))
self.fuser = utils.Fuser(nbins=model_params['nbins'],
fmax=model_params['fmax'])
self.model_params = model_params
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(
self,
matching_type='features',
matching_loss='L1',
average_loss=True):
super(Matcher, self).__init__()
# Matched statistics
if matching_type == 'features':
self.get_stats = self.gram_matrix
elif matching_type == 'features':
self.get_stats = lambda x: x
# Loss function
matching_loss = matching_loss.lower()
if matching_loss == 'mse':
self.criterion = nn.MSELoss()
elif matching_loss == 'smoothl1':
self.criterion = nn.SmoothL1Loss()
elif matching_loss == 'l1':
self.criterion = nn.L1Loss()
self.average_loss = average_loss
示例4: setUp
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def setUp(self):
th.manual_seed(789)
self.net1 = nn.Sequential(
nn.Conv2d(16, 32, 3, 1, 1, bias=True),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
)
self.net2 = nn.Sequential(
nn.Conv2d(32, 16, 3, 1, 1, bias=True),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.Conv2d(16, 1, 3, 1, 1, bias=True),
)
self.test_data = dict(
X = th.rand(100, 16, 32, 32),
Y = th.rand(100, 1, 32, 32),
)
self.net1.apply(common.weights_init)
self.net2.apply(common.weights_init)
self.crit = nn.SmoothL1Loss()
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self):
super(DetailEnhance, self).__init__()
self.feature_level = 3
self.extract_features = model.ValidationFeatures()
self.extract_aligned_features = model.ExtractAlignedFeatures(n_res=5) # 4 5
self.pcd_align = model.PCD_Align(groups=8) # 4 8
self.tsa_fusion = model.TSA_Fusion(nframes=3, center=1)
self.reconstruct = model.Reconstruct(n_res=20) # 5 40
# Loss calculate
self.L1_lossFn = nn.L1Loss()
self.sL1_lossFn = nn.SmoothL1Loss()
self.cL1_lossFn = loss.CharbonnierLoss()
self.MSE_LossFn = nn.MSELoss()
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, scales, downscale, weights=None, loss='MSE'):
super(MultiScaleLoss, self).__init__()
self.downscale = downscale
self.weights = torch.Tensor(scales).fill_(1) if weights is None else torch.Tensor(weights)
assert (len(weights) == scales)
if type(loss) is str:
assert (loss in ['L1', 'MSE', 'SmoothL1'])
if loss == 'L1':
self.loss = nn.L1Loss()
elif loss == 'MSE':
self.loss = nn.MSELoss()
elif loss == 'SmoothL1':
self.loss = nn.SmoothL1Loss()
else:
self.loss = loss
self.multiScales = [nn.AvgPool2d(self.downscale * (2 ** i), self.downscale * (2 ** i)) for i in range(scales)]
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, anchors, nC, nID, nE, img_size, yolo_layer):
super(YOLOLayer, self).__init__()
self.layer = yolo_layer
nA = len(anchors)
self.anchors = torch.FloatTensor(anchors)
self.nA = nA # number of anchors (3)
self.nC = nC # number of classes (80)
self.nID = nID # number of identities
self.img_size = 0
self.emb_dim = nE
self.shift = [1, 3, 5]
self.SmoothL1Loss = nn.SmoothL1Loss()
self.SoftmaxLoss = nn.CrossEntropyLoss(ignore_index=-1)
self.CrossEntropyLoss = nn.CrossEntropyLoss()
self.IDLoss = nn.CrossEntropyLoss(ignore_index=-1)
self.s_c = nn.Parameter(-4.15*torch.ones(1)) # -4.15
self.s_r = nn.Parameter(-4.85*torch.ones(1)) # -4.85
self.s_id = nn.Parameter(-2.3*torch.ones(1)) # -2.3
self.emb_scale = math.sqrt(2) * math.log(self.nID-1) if self.nID>1 else 1
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, ratios, scales, cfg):
super().__init__()
self.cfg = cfg
self.ratios = ratios
self.scales = scales
self.alpha = cfg['alpha']
self.gamma = cfg['gamma']
# Which loss fucntion to use
self.use_focal = cfg['use_focal']
self.use_softmax = cfg['use_softmax']
self.use_multi = cfg['use_multi']
self.lamb_reg = cfg['lamb_reg']
self.loss_keys = ['loss', 'cls_ls', 'box_ls']
self.anchs = None
self.get_anchors = partial(
create_anchors, ratios=self.ratios,
scales=self.scales, flatten=True)
self.box_loss = nn.SmoothL1Loss(reduction='none')
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self):
super().__init__()
self.loss = nn.SmoothL1Loss()
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, num_hard = 0):
super(Loss, self).__init__()
self.sigmoid = nn.Sigmoid()
self.classify_loss = nn.BCELoss()
self.regress_loss = nn.SmoothL1Loss()
self.num_hard = num_hard
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, anch_mask, n_classes, stride, in_ch=1024, ignore_thre=0.7, label_smooth = False, rfb=False, sep=False):
super(YOLOv3Head, self).__init__()
self.anchors = [
(10, 13), (16, 30), (33, 23),
(30, 61), (62, 45), (42, 119),
(116, 90), (156, 198), (121, 240) ]
if sep:
self.anchors = [
(10, 13), (16, 30), (33, 23),
(30, 61), (62, 45), (42, 119),
(116, 90), (156, 198), (373, 326)]
self.anch_mask = anch_mask
self.n_anchors = 4
self.n_classes = n_classes
self.guide_wh = nn.Conv2d(in_channels=in_ch,
out_channels=2*self.n_anchors, kernel_size=1, stride=1, padding=0)
self.Feature_adaption=FeatureAdaption(in_ch, in_ch, self.n_anchors, rfb, sep)
self.conv = nn.Conv2d(in_channels=in_ch,
out_channels=self.n_anchors*(self.n_classes+5), kernel_size=1, stride=1, padding=0)
self.ignore_thre = ignore_thre
self.l1_loss = nn.L1Loss(reduction='none')
#self.smooth_l1_loss = nn.SmoothL1Loss(reduction='none')
self.bcewithlog_loss = nn.BCEWithLogitsLoss(reduction='none')
self.bce_loss = nn.BCELoss(reduction='none')
self.iou_loss = IOUloss(reduction='none')
self.iou_wh_loss = IOUWH_loss(reduction='none')
self.stride = stride
self._label_smooth = label_smooth
self.all_anchors_grid = self.anchors
self.masked_anchors = [self.all_anchors_grid[i]
for i in self.anch_mask]
self.ref_anchors = np.zeros((len(self.all_anchors_grid), 4))
self.ref_anchors[:, 2:] = np.array(self.all_anchors_grid)
self.ref_anchors = torch.FloatTensor(self.ref_anchors)
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, nc=2, loss_type="L1", reduction='mean'):
assert loss_type in ['L1', 'BCE'], "Undefined loss type: {}".format(loss_type)
self.nc = nc
self.loss_type = loss_type
self.kernelx = Variable(torch.Tensor([[1,0,-1],[2,0,-2],[1,0,-1]]).cuda())
self.kernelx = self.kernelx.repeat(nc,1,1,1)
self.kernely = Variable(torch.Tensor([[1,2,1],[0,0,0],[-1,-2,-1]]).cuda())
self.kernely = self.kernely.repeat(nc,1,1,1)
self.bias = Variable(torch.zeros(nc).cuda())
self.reduction = reduction
if loss_type == 'L1':
self.loss = nn.SmoothL1Loss(reduction=reduction)
elif loss_type == 'BCE':
self.loss = self.bce2d
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self, delta=1):
super().__init__()
self.huber_loss_delta1 = nn.SmoothL1Loss()
self.delta = delta
示例14: get_loss_layer
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def get_loss_layer(loss_type, encoder=None):
return {
'l1': nn.L1Loss(),
'l2': nn.MSELoss(),
'huber': nn.SmoothL1Loss(),
'perceptual': PerceptualLoss(
input_range='tanh',
average_loss=False,
extractor=encoder)
}[loss_type]
示例15: create_loss
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def create_loss(config: yacs.config.CfgNode) -> nn.Module:
loss_name = config.train.loss
if loss_name == LossType.L1.name:
return nn.L1Loss(reduction='mean')
elif loss_name == LossType.L2.name:
return nn.MSELoss(reduction='mean')
elif loss_name == LossType.SmoothL1.name:
return nn.SmoothL1Loss(reduction='mean')
else:
raise ValueError