center_loss.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(shape=[self.num_classes, self.feat_dim]).astype(
  32. "float64"
  33. )
  34. if center_file_path is not None:
  35. assert os.path.exists(
  36. center_file_path
  37. ), f"center path({center_file_path}) must exist when it is not None."
  38. with open(center_file_path, "rb") as f:
  39. char_dict = pickle.load(f)
  40. for key in char_dict.keys():
  41. self.centers[key] = paddle.to_tensor(char_dict[key])
  42. def __call__(self, predicts, batch):
  43. assert isinstance(predicts, (list, tuple))
  44. features, predicts = predicts
  45. feats_reshape = paddle.reshape(features, [-1, features.shape[-1]]).astype(
  46. "float64"
  47. )
  48. label = paddle.argmax(predicts, axis=2)
  49. label = paddle.reshape(label, [label.shape[0] * label.shape[1]])
  50. batch_size = feats_reshape.shape[0]
  51. # calc l2 distance between feats and centers
  52. square_feat = paddle.sum(paddle.square(feats_reshape), axis=1, keepdim=True)
  53. square_feat = paddle.expand(square_feat, [batch_size, self.num_classes])
  54. square_center = paddle.sum(paddle.square(self.centers), axis=1, keepdim=True)
  55. square_center = paddle.expand(
  56. square_center, [self.num_classes, batch_size]
  57. ).astype("float64")
  58. square_center = paddle.transpose(square_center, [1, 0])
  59. distmat = paddle.add(square_feat, square_center)
  60. feat_dot_center = paddle.matmul(
  61. feats_reshape, paddle.transpose(self.centers, [1, 0])
  62. )
  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. )
  69. mask = paddle.equal(
  70. paddle.expand(classes, [batch_size, self.num_classes]), label
  71. ).astype("float64")
  72. dist = paddle.multiply(distmat, mask)
  73. loss = paddle.sum(paddle.clip(dist, min=1e-12, max=1e12)) / batch_size
  74. return {"loss_center": loss}