torchreid.losses

Softmax

class torchreid.losses.cross_entropy_loss.CrossEntropyLoss(num_classes, epsilon=0.1, use_gpu=True, label_smooth=True)[source]

Cross entropy loss with label smoothing regularizer.

Reference:
Szegedy et al. Rethinking the Inception Architecture for Computer Vision. CVPR 2016.

With label smoothing, the label \(y\) for a class is computed by

\[\begin{equation} (1 - \epsilon) \times y + \frac{\epsilon}{K}, \end{equation}\]

where \(K\) denotes the number of classes and \(\epsilon\) is a weight. When \(\epsilon = 0\), the loss function reduces to the normal cross entropy.

Parameters:
  • num_classes (int) – number of classes.
  • epsilon (float, optional) – weight. Default is 0.1.
  • use_gpu (bool, optional) – whether to use gpu devices. Default is True.
  • label_smooth (bool, optional) – whether to apply label smoothing. Default is True.
forward(inputs, targets)[source]
Parameters:
  • inputs (torch.Tensor) – prediction matrix (before softmax) with shape (batch_size, num_classes).
  • targets (torch.LongTensor) – ground truth labels with shape (batch_size). Each position contains the label index.

Triplet

class torchreid.losses.hard_mine_triplet_loss.TripletLoss(margin=0.3)[source]

Triplet loss with hard positive/negative mining.

Reference:
Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.

Imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.

Parameters:margin (float, optional) – margin for triplet. Default is 0.3.
forward(inputs, targets)[source]
Parameters:
  • inputs (torch.Tensor) – feature matrix with shape (batch_size, feat_dim).
  • targets (torch.LongTensor) – ground truth labels with shape (num_classes).