文章目录
1. 论文
1.1 Label Smoothing
- https://arxiv.org/abs/1906.02629 When Does Label Smoothing Help?
- https://arxiv.org/abs/1701.06548 Regularizing Neural Networks by Penalizing Confident Output Distributions
- https://arxiv.org/abs/1706.04599 On Calibration of Modern Neural Networks
1.2 Focal Loss
https://arxiv.org/abs/1708.02002 Focal Loss for Dense Object Detection
标签平滑: 提高模型的泛化能力,对于未知域任务,分类任务,可以提高精度。
2. 代码实现
2.1 Label Smoothing loss function
class LabelSmoothingCrossEntropy(nn.Module):
def __init__(self, eps=0.1, reduction='mean'):
super(LabelSmoothingCrossEntropy, self).__init__()
self.eps = eps
self.reduction = reduction
def forward(self, output, target):
c = output.size()[-1]
log_preds = F.log_softmax(output, dim=-1)
if self.reduction=='sum':
loss = -log_preds.sum()
else:
loss = -log_preds.sum(dim=-1)
if self.reduction=='mean':
loss = loss.mean()
return loss*self.eps/c + (1-self.eps) * F.nll_loss(log_preds, target, reduction=self.reduction)
2.2 labels mooth+类别不均衡loss
"""Pytorch implementation of Class-Balanced-Loss
Reference: "Class-Balanced Loss Based on Effective Number of Samples
"""
import numpy as np
import torch
import torch.nn.functional as F
def focal_loss(labels, logits, alpha, gamma):
"""Compute the focal loss between `logits` and the ground truth `labels`.
Focal loss = -alpha_t * (1-pt)^gamma * log(pt)
where pt is the probability of being classified to the true class.
pt = p (if true class), otherwise pt = 1 - p. p = sigmoid(logit).
Args:
labels: A float tensor of size [batch, num_classes].
logits: A float tensor of size [batch, num_classes].
alpha: A float tensor of size [batch_size]
specifying per-example weight for balanced cross entropy.
gamma: A float scalar modulating loss from hard and easy examples.
Returns:
focal_loss: A float32 scalar representing normalized total loss.
"""
BCLoss = F.binary_cross_entropy_with_logits(input = logits, target = labels,reduction = "none")
if gamma == 0.0:
modulator = 1.0
else:
modulator = torch.exp(-gamma * labels * logits - gamma * torch.log(1 +
torch.exp(-1.0 * logits)))
loss = modulator * BCLoss
weighted_loss = alpha * loss
focal_loss = torch.sum(weighted_loss)
focal_loss /= torch.sum(labels)
return focal_loss
import torch.nn as nn
class CB_loss(nn.Module):
def __init__(self,beta,gamma,epsilon=0.1):
super(CB_loss, self).__init__()
self.beta = beta
self.gamma = gamma
self.epsilon = epsilon
def forward(self,logits, labels,loss_type = 'softmax'):
"""Compute the Class Balanced Loss between `logits` and the ground truth `labels`.
Class Balanced Loss: ((1-beta)/(1-beta^n))*Loss(labels, logits)
where Loss is one of the standard losses used for Neural Networks.
Args:
labels: A int tensor of size [batch].
logits: A float tensor of size [batch, no_of_classes].
samples_per_cls: A python list of size [no_of_classes].
no_of_classes: total number of classes. int
loss_type: string. One of "sigmoid", "focal", "softmax".
beta: float. Hyperparameter for Class balanced loss.
gamma: float. Hyperparameter for Focal loss.
Returns:
cb_loss: A float tensor representing class balanced loss
"""
# self.epsilon = 0.1 #labelsmooth
beta = self.beta
gamma = self.gamma
no_of_classes = logits.shape[1]
samples_per_cls = torch.Tensor([sum(labels == i) for i in range(logits.shape[1])])
if torch.cuda.is_available():
samples_per_cls = samples_per_cls.cuda()
effective_num = 1.0 - torch.pow(beta, samples_per_cls)
weights = (1.0 - beta) / ((effective_num)+1e-8)
# print(weights)
weights = weights / torch.sum(weights) * no_of_classes
labels =labels.reshape(-1,1)
labels_one_hot = torch.zeros(len(labels), no_of_classes).scatter_(1, labels, 1)
weights = torch.tensor(weights).float()
if torch.cuda.is_available():
weights = weights.cuda()
labels_one_hot = torch.zeros(len(labels), no_of_classes).cuda().scatter_(1, labels, 1).cuda()
labels_one_hot = (1 - self.epsilon) * labels_one_hot + self.epsilon / no_of_classes
weights = weights.unsqueeze(0)
weights = weights.repeat(labels_one_hot.shape[0],1) * labels_one_hot
weights = weights.sum(1)
weights = weights.unsqueeze(1)
weights = weights.repeat(1,no_of_classes)
if loss_type == "focal":
cb_loss = focal_loss(labels_one_hot, logits, weights, gamma)
elif loss_type == "sigmoid":
cb_loss = F.binary_cross_entropy_with_logits(input = logits,target = labels_one_hot, pos_weight = weights)
elif loss_type == "softmax":
pred = logits.softmax(dim = 1)
cb_loss = F.binary_cross_entropy(input = pred, target = labels_one_hot, weight = weights)
return cb_loss
if __name__ == '__main__':
# no_of_classes = 5
# logits = torch.rand(10,no_of_classes).float()
# labels = torch.randint(0,no_of_classes, size = (10,))
# labels = torch.Tensor([1, 1, 4, 3, 1, 4, 0, 4, 3, 0]).long()
#
# beta = 0.9999
# gamma = 2.0
# samples_per_cls = [sum(labels==i) for i in range(no_of_classes)]
#
# loss_type = "sigmoid"
# cb_loss = CB_loss(labels, logits, samples_per_cls, no_of_classes,loss_type, beta, gamma)
# print(cb_loss)
beta = 0.9999
gamma = 2.0
criterion = CB_loss(beta, gamma)
if torch.cuda.is_available():
criterion = criterion.cuda()
########################################################################
labels = torch.Tensor([1, 1, 4, 3, 1, 4, 0, 4, 3, 0]).long()
logits = torch.ones((10, 5)).float()
loss = criterion(logits,labels)
print(loss)