gluonfr.loss

gluonfr.loss.get_loss Return the loss by name.
gluonfr.loss.get_loss_list Get the entire list of loss names in losses.

API Reference

Custom losses

gluonfr.loss.get_loss(name, **kwargs)[source]

Return the loss by name.

Parameters:
  • name (str.) – Available losses name in gluon face
  • kwargs (str.) – Check the docs for details.
Returns:

The loss.

Return type:

HybridBlock

gluonfr.loss.get_loss_list()[source]

Get the entire list of loss names in losses.

Returns:Entire list of loss names in losses.
Return type:list of str.
class gluonfr.loss.ArcLoss

ArcLoss from “ArcFace: Additive Angular Margin Loss for Deep Face Recognition” paper.

Parameters:
  • classes (int.) – Number of classes.
  • m (float.) – Margin parameter for loss.
  • s (int.) – Scale parameter for loss.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.TripletLoss

Calculates triplet loss given three input tensors and a positive margin. Triplet loss measures the relative similarity between prediction, a positive example and a negative example:

\[L = \sum_i \max(\Vert {pred}_i - {pos_i} \Vert_2^2 - \Vert {pred}_i - {neg_i} \Vert_2^2 + {margin}, 0)\]

pred, positive and negative can have arbitrary shape as long as they have the same number of elements.

Parameters:
  • margin (float) – Margin of separation between correct and incorrect pair.
  • weight (float or None) – Global scalar weight for loss.
  • batch_axis (int, default 0) – The axis that represents mini-batch.
Inputs:
  • pred: prediction tensor with arbitrary shape
  • positive: positive example tensor with arbitrary shape. Must have the same size as pred.
  • negative: negative example tensor with arbitrary shape Must have the same size as pred.
Outputs:
  • loss: loss tensor with shape (batch_size,).
class gluonfr.loss.RingLoss

Computes the Ring Loss from “Ring loss: Convex Feature Normalization for Face Recognition” paper.

\[L = -\sum_i \log \softmax({pred})_{i,{label}_i} + \frac{\lambda}{2m} \sum_{i=1}^{m} (\Vert \mathcal{F}({x}_i)\Vert_2 - R )^2\]
Parameters:
  • lamda (float.) – The loss weight enforcing a trade-off between the softmax loss and ring loss.
  • r_init (float.) – The initial value of Hyper Parameter R.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.CosLoss
CosLoss from

“CosFace: Large Margin Cosine Loss for Deep Face Recognition” paper.

It is also AM-Softmax from “Additive Margin Softmax for Face Verification” paper.

Parameters:
  • classes (int.) – Number of classes.
  • m (float, default 0.4) – Margin parameter for loss.
  • s (int, default 64) – Scale parameter for loss.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.L2Softmax

L2Softmax from “L2-constrained Softmax Loss for Discriminative Face Verification” paper.

Parameters:
  • classes (int.) – Number of classes.
  • alpha (float.) – The scaling parameter, a hypersphere with small alpha will limit surface area for embedding features.
  • p (float, default is 0.9.) – The expected average softmax probability for correctly classifying a feature.
  • from_normx (bool, default is False.) – Whether input has already been normalized.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.ASoftmax

ASoftmax from “SphereFace: Deep Hypersphere Embedding for Face Recognition” paper. input(weight, x) has already been normalized

Parameters:
  • classes (int.) – Number of classes.
  • m (float.) – Margin parameter for loss.
  • s (int.) – Scale parameter for loss.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.CenterLoss

Computes the Center Loss from “A Discriminative Feature Learning Approach for Deep Face Recognition” paper.

Implementation is refer to “https://github.com/ShownX/mxnet-center-loss/blob/master/center_loss.py

Parameters:
  • classes (int.) – Number of classes.
  • lamda (float) – The loss weight enforcing a trade-off between the softmax loss and center loss.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.ContrastiveLoss

Computes the contrastive loss. See “Dimensionality Reduction by Learning an Invariant Mapping” paper. This loss encourages the embedding to be close to each other for the samples of the same label and the embedding to be far apart at least by the margin constant for the samples of different labels.

Parameters:margin (float, default is 1.) – Margin term in the loss definition.
Inputs:
  • anchor: prediction tensor. Embeddings should be l2 normalized.
  • positive: positive example tensor with arbitrary shape. Must have the same size as anchor. Embeddings should be l2 normalized.
  • labels: array with shape (batch_size,) of binary labels indicating positive vs negative pair.
Outputs:
  • loss: loss tensor with shape (batch_size,).Dimensions other than batch_axis are averaged out.
class gluonfr.loss.LGMLoss

LGM Loss from “Rethinking Feature Distribution for Loss Functions in Image Classification” paper.

Implementation is refer to https://github.com/LeeJuly30/L-GM-Loss-For-Gluon/blob/master/L_GM.py

Parameters:
  • num_classes (int.) – The num of classes.
  • embedding_size (int.) – The size of embedding feature.
  • alpha (float.) – A non-negative parameter controlling the size of the expected margin between two classes on the training set.
  • lamda (float.) – A non-negative weighting coefficient.
  • lr_mult (float.) – Var updating need a relatively low learning rate compared to the overall learning rate.
class gluonfr.loss.MPSLoss

Computes the MPS Loss from “DocFace: Matching ID Document Photos to Selfies” paper.

Parameters:m (float) – Margin parameter for loss.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.GitLoss

Computes the Git Loss from “Git Loss for Deep Face Recognition” paper.

This implementation require the batch size not changing in training or validation. Commonly, it is ok, as when we train models last batch discard is applied, and no need for validation to compute the loss.

Parameters:
  • classes (int.) – Number of classes.
  • embedding_size (int.) – Size of feature.
  • lamda_c (float.) – The loss weight enforcing a trade-off between the softmax loss and center loss.
  • lamda_g (float.) – The loss weight enforcing a trade-off between the softmax loss and git loss.
  • batch_size_per_gpu (int.) – This size is sample numbers in each gpu or device, not total batch size
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.COCOLoss

Computes the COCO Loss from “Rethinking Feature Discrimination and Polymerization for Large-scale Recognition” paper.

This loss can be replaced by NormDense with Softmax, it is not recommended to use this.

Parameters:
  • classes (int.) – Number of classes.
  • embedding_size (int.) – Size of feature.
  • alpha (float.) – The scaling parameter, a hypersphere with small alpha will limit surface area for embedding features.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.
class gluonfr.loss.SVXSoftmax

SVXSoftmax from “Support Vector Guided Softmax Loss for Face Recognition” paper.

When use default parameter, the designed SV-X-Softmax loss becomes identical to the original softmax loss.

Parameters:
  • classes (int.) – Number of classes.
  • s (int.) – Scale parameter for loss.
  • t (float.) – Indicator parameter of SV.
  • m1 (float.) – Margin parameter for sphere softmax.
  • m2 (float.) – Margin parameter for cos/am softmax.
  • m3 (float.) – Margin parameter for arc softmax.
Outputs:
  • loss: loss tensor with shape (batch_size,). Dimensions other than batch_axis are averaged out.