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


Python nn.NLLLoss2d方法代碼示例

本文整理匯總了Python中torch.nn.NLLLoss2d方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.NLLLoss2d方法的具體用法?Python nn.NLLLoss2d怎麽用?Python nn.NLLLoss2d使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torch.nn的用法示例。


在下文中一共展示了nn.NLLLoss2d方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def test():
    loss_nll = nn.NLLLoss2d()
    loss_focal = FocalLoss(gamma=0)
    target = torch.Tensor(2, 1, 5).random_(3).long()

    data = torch.rand(2, 3, 1, 5)
    input1 = torch.Tensor(data, requires_grad=True)
    loss1 = loss_nll(F.log_softmax(input1), target)
    loss1.backward()
    print(loss1)
    print(input1.grad)

    input2 = torch.Tensor(data, requires_grad=True)
    loss2 = loss_focal(F.log_softmax(input2), target)
    loss2.backward()
    print(loss2)
    print(input2.grad)


#test() 
開發者ID:akolishchak,項目名稱:doom-net-pytorch,代碼行數:22,代碼來源:focal_loss.py

示例2: loss_calc

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def loss_calc(out, label, gpu0):
    """
    This function returns cross entropy loss for semantic segmentation
    """
    # out shape batch_size x channels x h x w -> batch_size x channels x h x w
    # label shape h x w x 1 x batch_size  -> batch_size x 1 x h x w
    label = label[:,:,0,:].transpose(2,0,1)

    label = torch.from_numpy(label).long()
    if useGPU:
        label = Variable(label).cuda(gpu0)
        if onlyLesions:
            criterion = nn.NLLLoss2d(weight = torch.cuda.FloatTensor([1, 100000]))
        else:
            criterion = nn.NLLLoss2d(weight = torch.cuda.FloatTensor([1, 100000, 100000]))
    else:
        label = Variable(label)

        if onlyLesions:
            criterion = nn.NLLLoss2d(weight = torch.FloatTensor([1, 100000]))
        else:
            criterion = nn.NLLLoss2d(weight = torch.FloatTensor([1, 100000, 100000]))

    m = nn.LogSoftmax()
    out = m(out)

    return criterion(out,label) 
開發者ID:Achilleas,項目名稱:pytorch-mri-segmentation-3D,代碼行數:29,代碼來源:train_deeplab2D.py

示例3: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None, ignore = None):
        '''
        :param weight: 1D weight vector to deal with the class-imbalance
        '''

        super().__init__()
        if int(torch.__version__[2]) < 4:
            self.loss = nn.NLLLoss2d(weight, ignore_index=ignore)
        else:
            self.loss = nn.NLLLoss(weight, ignore_index=ignore) 
開發者ID:clovaai,項目名稱:ext_portrait_segmentation,代碼行數:12,代碼來源:Criteria.py

示例4: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None):
		super().__init__()
		self.loss = nn.NLLLoss2d(weight) 
開發者ID:mapleneverfade,項目名稱:pytorch-semantic-segmentation,代碼行數:5,代碼來源:criterion.py

示例5: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None):
        super().__init__()
        self.loss = nn.NLLLoss2d(weight) 
開發者ID:xdspacelab,項目名稱:sscdnet,代碼行數:5,代碼來源:criterion.py

示例6: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None):
        super().__init__()

        self.loss = nn.NLLLoss2d(weight) 
開發者ID:bodokaiser,項目名稱:piwise,代碼行數:6,代碼來源:criterion.py

示例7: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None, size_average=True, ignore_index=255):
        super(CrossEntropyLoss2d, self).__init__()
        self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index) 
開發者ID:zijundeng,項目名稱:pytorch-semantic-segmentation,代碼行數:5,代碼來源:misc.py

示例8: loss_calc

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def loss_calc(out, label,gpu0):
    """
    This function returns cross entropy loss for semantic segmentation
    """
    # out shape batch_size x channels x h x w -> batch_size x channels x h x w
    # label shape h x w x 1 x batch_size  -> batch_size x 1 x h x w
    label = label[:,:,0,:].transpose(2,0,1)
    label = torch.from_numpy(label).long()
    label = Variable(label).cuda(gpu0)
    m = nn.LogSoftmax()
    criterion = nn.NLLLoss2d()
    out = m(out)
    
    return criterion(out,label) 
開發者ID:isht7,項目名稱:pytorch-deeplab-resnet,代碼行數:16,代碼來源:train.py

示例9: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None, size_average=False, ignore_index=255):
        super(CrossEntropyLoss2d, self).__init__()
        self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index) 
開發者ID:laughtervv,項目名稱:DepthAwareCNN,代碼行數:5,代碼來源:losses.py

示例10: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weights=None):
        super(CrossEntropyLoss2d, self).__init__()

        self.loss = nn.NLLLoss2d(weight=weights)
        self.loss.cuda() 
開發者ID:saeedizadi,項目名稱:binseg_pytoch,代碼行數:7,代碼來源:criterion.py

示例11: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None, size_average=True):
        super(CrossEntropyLoss2d, self).__init__()
        self.nll_loss = nn.NLLLoss2d(weight, size_average) 
開發者ID:andyzeng,項目名稱:visual-pushing-grasping,代碼行數:5,代碼來源:utils.py

示例12: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None, ignore_label=255):
        '''
        :param weight: 1D weight vector to deal with the class-imbalance
        Obtaining log-probabilities in a neural network is easily achieved by adding a LogSoftmax layer in the last layer of your network. 
        You may use CrossEntropyLoss instead, if you prefer not to add an extra layer.
        '''
        super().__init__()

        # self.loss = nn.NLLLoss2d(weight, ignore_index=255)
        self.loss = nn.NLLLoss(weight, ignore_index=ignore_label) 
開發者ID:Reagan1311,項目名稱:DABNet,代碼行數:12,代碼來源:loss.py

示例13: test

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def test(model, data_loader):
    model.eval()

    epoch_loss_obj = 0
    epoch_loss_dist = 0
    epoch_accuracy_obj = 0
    epoch_accuracy_dist = 0
    batch = 0
    for batch, (screens, distances, objects) in enumerate(data_loader):
        screens, distances, objects = screens.to(device), distances.to(device), objects.to(device)

        pred_objects, pred_distances = model(screens)
        loss_obj = objects_criterion(pred_objects, objects)
        loss_dist = distances_criterion(pred_distances, distances)

        epoch_loss_obj += loss_obj.item()
        epoch_loss_dist += loss_dist.item()

        _, pred_objects = pred_objects.max(1)
        accuracy = (pred_objects == objects).float().mean()
        epoch_accuracy_obj += accuracy

        _, pred_distances = pred_distances.max(1)
        accuracy = (pred_distances == distances).float().mean()
        epoch_accuracy_dist += accuracy

    batch_num = batch + 1
    epoch_loss_obj /= batch_num
    epoch_loss_dist /= batch_num
    epoch_accuracy_obj /= batch_num
    epoch_accuracy_dist /= batch_num

    model.train()
    return (epoch_loss_obj, epoch_loss_dist), (epoch_accuracy_obj, epoch_accuracy_dist)

#objects_criterion = nn.NLLLoss2d()
#distances_criterion = nn.NLLLoss2d() 
開發者ID:akolishchak,項目名稱:doom-net-pytorch,代碼行數:39,代碼來源:map_train.py

示例14: __init__

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def __init__(self, weight=None):
        '''
        :param weight: 1D weight vector to deal with the class-imbalance
        '''
        super().__init__()

        self.loss = nn.NLLLoss2d(weight) 
開發者ID:sacmehta,項目名稱:ESPNet,代碼行數:9,代碼來源:Criteria.py

示例15: forward

# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import NLLLoss2d [as 別名]
def forward(self, output, target):
        """
        Forward pass
        :param output: torch.tensor (NxC)
        :param target: torch.tensor (N)
        :return: scalar
        """
        return self.nll_loss(output, target)


# class CrossEntropyLoss2d(nn.Module):
#     '''
#     This file defines a cross entropy loss for 2D images
#     '''
#
#     def __init__(self, weight=None, ignore_label=255):
#         '''
#         :param weight: 1D weight vector to deal with the class-imbalance
#         Obtaining log-probabilities in a neural network is easily achieved by adding a LogSoftmax layer in the last layer of your network.
#         You may use CrossEntropyLoss instead, if you prefer not to add an extra layer.
#         '''
#         super().__init__()
#
#         # self.loss = nn.NLLLoss2d(weight, ignore_index=255)
#         self.loss = nn.NLLLoss(weight, ignore_index=ignore_label)
#
#     def forward(self, outputs, targets):
#         return self.loss(F.log_softmax(outputs, dim=1), targets) 
開發者ID:xiaoyufenfei,項目名稱:Efficient-Segmentation-Networks,代碼行數:30,代碼來源:loss.py


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