當前位置: 首頁>>代碼示例>>Python>>正文


Python nn.SmoothL1Loss方法代碼示例

本文整理匯總了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 
開發者ID:varunagrawal,項目名稱:tiny-faces-pytorch,代碼行數:19,代碼來源:loss.py

示例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 
開發者ID:XiaohangZhan,項目名稱:conditional-motion-propagation,代碼行數:20,代碼來源:cmp.py

示例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 
開發者ID:egorzakharov,項目名稱:PerceptualGAN,代碼行數:24,代碼來源:perceptual_loss.py

示例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() 
開發者ID:svip-lab,項目名稱:PPGNet,代碼行數:22,代碼來源:test_common.py

示例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() 
開發者ID:CM-BF,項目名稱:FeatureFlow,代碼行數:20,代碼來源:layers.py

示例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)] 
開發者ID:orashi,項目名稱:CRL_pytorch,代碼行數:20,代碼來源:multiscale.py

示例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 
開發者ID:Zhongdao,項目名稱:Towards-Realtime-MOT,代碼行數:23,代碼來源:models.py

示例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') 
開發者ID:TheShadow29,項目名稱:zsgnet-pytorch,代碼行數:26,代碼來源:loss.py

示例9: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import SmoothL1Loss [as 別名]
def __init__(self):
        super().__init__()
        self.loss = nn.SmoothL1Loss() 
開發者ID:moemen95,項目名稱:Pytorch-Project-Template,代碼行數:5,代碼來源:huber_loss.py

示例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 
開發者ID:uci-cbcl,項目名稱:DeepLung,代碼行數:8,代碼來源:layers.py

示例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) 
開發者ID:ruinmessi,項目名稱:ASFF,代碼行數:39,代碼來源:yolov3_head.py

示例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 
開發者ID:XiaohangZhan,項目名稱:conditional-motion-propagation,代碼行數:16,代碼來源:losses.py

示例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 
開發者ID:snasiriany,項目名稱:leap,代碼行數:6,代碼來源:modules.py

示例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] 
開發者ID:egorzakharov,項目名稱:PerceptualGAN,代碼行數:12,代碼來源:utils.py

示例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 
開發者ID:hysts,項目名稱:pytorch_mpiigaze,代碼行數:12,代碼來源:losses.py


注:本文中的torch.nn.SmoothL1Loss方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。