1 Abstract
卷积神经网络(CNNs)已广泛应用于计算机视觉领域,显著提高了计算机视觉领域的技术水平。在大多数可用的cnn中,使用软tmax损失函数作为监督信号来训练深度模型。为了增强深度学习特征的识别能力,本文提出了一种新的监督信号,用于人脸识别任务。具体来说,中心损失同时学习每个类的深度特征的一个中心,并惩罚深度特征与其对应的类中心之间的距离。更重要的是,我们证明了所提出的中心损失函数在cnn中是可训练的和易于优化的。在软最大损失和中心损失的联合监督下,我们可以训练一个鲁棒的神经网络,尽可能地获得具有类间缺失和类内紧凑性两个关键学习目标的深度特征,这对人脸识别非常重要。令人鼓舞的是,我们的cnn(通过这样的联合监督)在几个重要的人脸识别基准上达到了最先进的准确性,野外标记人脸(LFW),
2 Algorithm
3 Optimization Strategy
3 Performance
3 code
import torch
import torch.nn as nnclass 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, use_gpu=True):super(CenterLoss, self).__init__()self.num_classes = num_classesself.feat_dim = feat_dimself.use_gpu = use_gpuif self.use_gpu:self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim).cuda())else:self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim))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)a = torch.pow(x, 2).sum(dim=1, keepdim=True).expand(batch_size, self.num_classes)b = torch.pow(self.centers, 2).sum(dim=1, keepdim=True).expand(self.num_classes, batch_size).t()# 这里相当于求x的平方与centers平方的和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())# 这里相当于求-2*(x + centers),与上一步相加即(x - centers)**2classes = torch.arange(self.num_classes).long()if self.use_gpu: 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_sizereturn loss