SubCenterArcFaceLoss¶
Sub-center ArcFace: Boosting Face Recognition by Large-scale Noisy Web Faces
losses.SubCenterArcFaceLoss(num_classes, embedding_size, margin=0.5, scale=64, sub_centers=3,
*, weight_regularizer=None, weight_reg_weight=1.0, weight_init_func=None, **options)
Equation:
Here \(\theta_{j,k}\) is the angle between an embedding and the \(k\)-th of the \(K\) sub-centers of class \(j\). Each class's logit uses its closest sub-center, then the ArcFaceLoss margin and scale apply.
Implementation notes: The target-logit treatment for
\(\theta_{y_i} > \pi - m\) and the miner weights
are those of ArcFaceLoss. get_logits(embeddings) returns \(s \cos\theta_j\)
over the closest sub-centers, shaped (N, num_classes); see
classification logits. This loss derives
from ArcFaceLoss and BaseProxyLoss.
get_outliers(embeddings, labels, threshold_radians=math.radians(75)) finds
outliers and dominant sub-centers as described in the paper, whose drop
threshold is 75 degrees. It returns the named tuple
types.SubCenterOutliers(outliers, dominant_centers).
outliers holds the indices of embeddings whose similarity to their class's
dominant sub-center is below cos(threshold_radians); the threshold is in
radians, like margin. dominant_centers has shape
(num_classes, embedding_size); a class's dominant sub-center is the one
nearest to the most of its embeddings in embeddings. The method runs without
gradients.
Example:
import torch
from pytorch_metric_learning import losses
loss_func = losses.SubCenterArcFaceLoss(num_classes=10, embedding_size=128)
optimizer = torch.optim.SGD(loss_func.parameters(), lr=0.01)
embeddings = torch.randn(32, 128, requires_grad=True)
labels = torch.randint(0, 10, (32,))
loss = loss_func(embeddings, labels)
loss.backward()
optimizer.step()
outliers, dominant_centers = loss_func.get_outliers(embeddings, labels)
Parameters:
- num_classes: The number of classes in the training set. Labels must be in
0..num_classes - 1. - embedding_size: The size of the embeddings passed to the loss.
- margin: The additive angular margin \(m\) in radians. The paper uses 0.5.
- scale: The scale \(s\). The paper uses 64.
- sub_centers: \(K\), the number of sub-centers per class. The paper uses 3.
- weight_regularizer, weight_reg_weight, weight_init_func: See
learnable weight arguments. The
sub-centers are
weight, shaped(num_classes * sub_centers, embedding_size)and initialized withtorch.nn.init.normal_; rowsc * sub_centersthrough(c + 1) * sub_centers - 1belong to classc. Includeloss_func.parameters()in an optimizer.
Default distance:
CosineSimilarity(). This is the only compatible distance; another distance class raisesTypeError.
Default reducer:
Reducer input:
- loss: One loss per embedding, indexed by embedding position.