代码是网上找的,运行出来效果不错
class CenterLoss(nn.Module):
"""Center loss.
Reference:
Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016.
Args:
num_classes (int): number of classes.
feat_dim (int): feature dimension.
"""
def __init__(self, num_classes=10, feat_dim=2 ):
super(CenterLoss, self).__init__()
self.num_classes = num_classes
self.feat_dim = feat_dim
self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim).cuda())
def forward(self, x, labels):
"""
Args:
x: feature matrix with shape (batch_size, feat_dim).
labels: ground truth labels with shape (batch_size).
"""
batch_size = x.size(0)
distmat = torch.pow(x, 2).sum(dim=1, keepdim=True).expand(batch_size, self.num_classes) + \
torch.pow(self.centers, 2).sum(dim=1, keepdim=True).expand(self.num_classes, batch_size).t()
distmat.addmm_(1, -2, x, self.centers.t()) # x + (-2) * x * center.t --> [batchsize,num_classes]
#上面部分是计算欧几里得距离的公式(x-center)^2的展开
classes = torch.arange(self.num_classes).long()
classes = classes.cuda()
labels = labels.unsqueeze(1).expand(batch_size, self.num_classes)
mask = labels.eq(classes.expand(batch_size, self.num_classes))
dist = distmat * mask.float()
loss = dist.clamp(min=1e-12, max=1e+12).sum() / batch_size
return loss
其中distmat为每个样本在每个类别的距离,长为batchsize,宽为classnum。 模拟各个环节的计算结果可以更好的理解该代码。 label为样本的真实标签。 通过下列式子可以得到一个mask矩阵,有助于之后取出每个样本真实类别的距离。 可见,将距离矩阵中的真实距离取出来了 之后将所有值加起来求平均值就是要求的loss值了。
|