center_loss_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # MIT License
  2. #
  3. # Copyright (c) 2016 David Sandberg
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import unittest
  23. import tensorflow as tf
  24. import numpy as np
  25. import facenet
  26. class CenterLossTest(unittest.TestCase):
  27. def testCenterLoss(self):
  28. batch_size = 16
  29. nrof_features = 2
  30. nrof_classes = 16
  31. alfa = 0.5
  32. with tf.Graph().as_default():
  33. features = tf.placeholder(tf.float32, shape=(batch_size, nrof_features), name='features')
  34. labels = tf.placeholder(tf.int32, shape=(batch_size,), name='labels')
  35. # Define center loss
  36. center_loss, centers = facenet.center_loss(features, labels, alfa, nrof_classes)
  37. label_to_center = np.array( [
  38. [-3,-3], [-3,-1], [-3,1], [-3,3],
  39. [-1,-3], [-1,-1], [-1,1], [-1,3],
  40. [ 1,-3], [ 1,-1], [ 1,1], [ 1,3],
  41. [ 3,-3], [ 3,-1], [ 3,1], [ 3,3]
  42. ])
  43. sess = tf.Session()
  44. with sess.as_default():
  45. sess.run(tf.global_variables_initializer())
  46. np.random.seed(seed=666)
  47. for _ in range(0,100):
  48. # Create array of random labels
  49. lbls = np.random.randint(low=0, high=nrof_classes, size=(batch_size,))
  50. feats = create_features(label_to_center, batch_size, nrof_features, lbls)
  51. center_loss_, centers_ = sess.run([center_loss, centers], feed_dict={features:feats, labels:lbls})
  52. # After a large number of updates the estimated centers should be close to the true ones
  53. np.testing.assert_almost_equal(centers_, label_to_center, decimal=5, err_msg='Incorrect estimated centers')
  54. np.testing.assert_almost_equal(center_loss_, 0.0, decimal=5, err_msg='Incorrect center loss')
  55. def create_features(label_to_center, batch_size, nrof_features, labels):
  56. # Map label to center
  57. # label_to_center_dict = {
  58. # 0:(-3,-3), 1:(-3,-1), 2:(-3,1), 3:(-3,3),
  59. # 4:(-1,-3), 5:(-1,-1), 6:(-1,1), 7:(-1,3),
  60. # 8:( 1,-3), 9:( 1,-1), 10:( 1,1), 11:( 1,3),
  61. # 12:( 3,-3), 13:( 3,-1), 14:( 3,1), 15:( 3,3),
  62. # }
  63. # Create array of features corresponding to the labels
  64. feats = np.zeros((batch_size, nrof_features))
  65. for i in range(batch_size):
  66. cntr = label_to_center[labels[i]]
  67. for j in range(nrof_features):
  68. feats[i,j] = cntr[j]
  69. return feats
  70. if __name__ == "__main__":
  71. unittest.main()