triplet.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4. import paddle
  5. import paddle.nn as nn
  6. class TripletLossV2(nn.Layer):
  7. """Triplet loss with hard positive/negative mining.
  8. Args:
  9. margin (float): margin for triplet.
  10. """
  11. def __init__(self, margin=0.5, normalize_feature=True):
  12. super(TripletLossV2, self).__init__()
  13. self.margin = margin
  14. self.ranking_loss = paddle.nn.loss.MarginRankingLoss(margin=margin)
  15. self.normalize_feature = normalize_feature
  16. def forward(self, input, target):
  17. """
  18. Args:
  19. inputs: feature matrix with shape (batch_size, feat_dim)
  20. target: ground truth labels with shape (num_classes)
  21. """
  22. inputs = input["features"]
  23. if self.normalize_feature:
  24. inputs = 1. * inputs / (paddle.expand_as(
  25. paddle.norm(
  26. inputs, p=2, axis=-1, keepdim=True), inputs) + 1e-12)
  27. bs = inputs.shape[0]
  28. # compute distance
  29. dist = paddle.pow(inputs, 2).sum(axis=1, keepdim=True).expand([bs, bs])
  30. dist = dist + dist.t()
  31. dist = paddle.addmm(
  32. input=dist, x=inputs, y=inputs.t(), alpha=-2.0, beta=1.0)
  33. dist = paddle.clip(dist, min=1e-12).sqrt()
  34. # hard negative mining
  35. is_pos = paddle.expand(target, (
  36. bs, bs)).equal(paddle.expand(target, (bs, bs)).t())
  37. is_neg = paddle.expand(target, (
  38. bs, bs)).not_equal(paddle.expand(target, (bs, bs)).t())
  39. # `dist_ap` means distance(anchor, positive)
  40. ## both `dist_ap` and `relative_p_inds` with shape [N, 1]
  41. '''
  42. dist_ap, relative_p_inds = paddle.max(
  43. paddle.reshape(dist[is_pos], (bs, -1)), axis=1, keepdim=True)
  44. # `dist_an` means distance(anchor, negative)
  45. # both `dist_an` and `relative_n_inds` with shape [N, 1]
  46. dist_an, relative_n_inds = paddle.min(
  47. paddle.reshape(dist[is_neg], (bs, -1)), axis=1, keepdim=True)
  48. '''
  49. dist_ap = paddle.max(paddle.reshape(
  50. paddle.masked_select(dist, is_pos), (bs, -1)),
  51. axis=1,
  52. keepdim=True)
  53. # `dist_an` means distance(anchor, negative)
  54. # both `dist_an` and `relative_n_inds` with shape [N, 1]
  55. dist_an = paddle.min(paddle.reshape(
  56. paddle.masked_select(dist, is_neg), (bs, -1)),
  57. axis=1,
  58. keepdim=True)
  59. # shape [N]
  60. dist_ap = paddle.squeeze(dist_ap, axis=1)
  61. dist_an = paddle.squeeze(dist_an, axis=1)
  62. # Compute ranking hinge loss
  63. y = paddle.ones_like(dist_an)
  64. loss = self.ranking_loss(dist_an, dist_ap, y)
  65. return {"TripletLossV2": loss}
  66. class TripletLoss(nn.Layer):
  67. """Triplet loss with hard positive/negative mining.
  68. Reference:
  69. Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.
  70. Code imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.
  71. Args:
  72. margin (float): margin for triplet.
  73. """
  74. def __init__(self, margin=1.0):
  75. super(TripletLoss, self).__init__()
  76. self.margin = margin
  77. self.ranking_loss = paddle.nn.loss.MarginRankingLoss(margin=margin)
  78. def forward(self, input, target):
  79. """
  80. Args:
  81. inputs: feature matrix with shape (batch_size, feat_dim)
  82. target: ground truth labels with shape (num_classes)
  83. """
  84. inputs = input["features"]
  85. bs = inputs.shape[0]
  86. # Compute pairwise distance, replace by the official when merged
  87. dist = paddle.pow(inputs, 2).sum(axis=1, keepdim=True).expand([bs, bs])
  88. dist = dist + dist.t()
  89. dist = paddle.addmm(
  90. input=dist, x=inputs, y=inputs.t(), alpha=-2.0, beta=1.0)
  91. dist = paddle.clip(dist, min=1e-12).sqrt()
  92. mask = paddle.equal(
  93. target.expand([bs, bs]), target.expand([bs, bs]).t())
  94. mask_numpy_idx = mask.numpy()
  95. dist_ap, dist_an = [], []
  96. for i in range(bs):
  97. # dist_ap_i = paddle.to_tensor(dist[i].numpy()[mask_numpy_idx[i]].max(),dtype='float64').unsqueeze(0)
  98. # dist_ap_i.stop_gradient = False
  99. # dist_ap.append(dist_ap_i)
  100. dist_ap.append(
  101. max([
  102. dist[i][j] if mask_numpy_idx[i][j] == True else float(
  103. "-inf") for j in range(bs)
  104. ]).unsqueeze(0))
  105. # dist_an_i = paddle.to_tensor(dist[i].numpy()[mask_numpy_idx[i] == False].min(), dtype='float64').unsqueeze(0)
  106. # dist_an_i.stop_gradient = False
  107. # dist_an.append(dist_an_i)
  108. dist_an.append(
  109. min([
  110. dist[i][k] if mask_numpy_idx[i][k] == False else float(
  111. "inf") for k in range(bs)
  112. ]).unsqueeze(0))
  113. dist_ap = paddle.concat(dist_ap, axis=0)
  114. dist_an = paddle.concat(dist_an, axis=0)
  115. # Compute ranking hinge loss
  116. y = paddle.ones_like(dist_an)
  117. loss = self.ranking_loss(dist_an, dist_ap, y)
  118. return {"TripletLoss": loss}