center_loss.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. #Licensed under the Apache License, Version 2.0 (the "License");
  4. #you may not use this file except in compliance with the License.
  5. #You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. # This code is refer from: https://github.com/KaiyangZhou/pytorch-center-loss
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import os
  19. import pickle
  20. import paddle
  21. import paddle.nn as nn
  22. import paddle.nn.functional as F
  23. class CenterLoss(nn.Layer):
  24. """
  25. Reference: Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016.
  26. """
  27. def __init__(self, num_classes=6625, feat_dim=96, center_file_path=None):
  28. super().__init__()
  29. self.num_classes = num_classes
  30. self.feat_dim = feat_dim
  31. self.centers = paddle.randn(
  32. shape=[self.num_classes, self.feat_dim]).astype("float64")
  33. if center_file_path is not None:
  34. assert os.path.exists(
  35. center_file_path
  36. ), f"center path({center_file_path}) must exist when it is not None."
  37. with open(center_file_path, 'rb') as f:
  38. char_dict = pickle.load(f)
  39. for key in char_dict.keys():
  40. self.centers[key] = paddle.to_tensor(char_dict[key])
  41. def __call__(self, predicts, batch):
  42. assert isinstance(predicts, (list, tuple))
  43. features, predicts = predicts
  44. feats_reshape = paddle.reshape(
  45. features, [-1, features.shape[-1]]).astype("float64")
  46. label = paddle.argmax(predicts, axis=2)
  47. label = paddle.reshape(label, [label.shape[0] * label.shape[1]])
  48. batch_size = feats_reshape.shape[0]
  49. #calc l2 distance between feats and centers
  50. square_feat = paddle.sum(paddle.square(feats_reshape),
  51. axis=1,
  52. keepdim=True)
  53. square_feat = paddle.expand(square_feat, [batch_size, self.num_classes])
  54. square_center = paddle.sum(paddle.square(self.centers),
  55. axis=1,
  56. keepdim=True)
  57. square_center = paddle.expand(
  58. square_center, [self.num_classes, batch_size]).astype("float64")
  59. square_center = paddle.transpose(square_center, [1, 0])
  60. distmat = paddle.add(square_feat, square_center)
  61. feat_dot_center = paddle.matmul(feats_reshape,
  62. paddle.transpose(self.centers, [1, 0]))
  63. distmat = distmat - 2.0 * feat_dot_center
  64. #generate the mask
  65. classes = paddle.arange(self.num_classes).astype("int64")
  66. label = paddle.expand(
  67. paddle.unsqueeze(label, 1), (batch_size, self.num_classes))
  68. mask = paddle.equal(
  69. paddle.expand(classes, [batch_size, self.num_classes]),
  70. label).astype("float64")
  71. dist = paddle.multiply(distmat, mask)
  72. loss = paddle.sum(paddle.clip(dist, min=1e-12, max=1e+12)) / batch_size
  73. return {'loss_center': loss}