东方耀AI技术分享

标题: 同样是计算欧式距离的平方 float32比float16还要快N倍? [打印本页]

作者: 东方耀    时间: 2020-5-29 14:12
标题: 同样是计算欧式距离的平方 float32比float16还要快N倍?
同样是计算欧式距离的平方 float32比float16还要快N倍?



  1. import numpy as np
  2. import time

  3. # 同样是计算欧式距离的平方 float32比float16还要快N倍?

  4. face_encodings = np.random.random(size=(300000, 512))
  5. face_to_compare = np.random.random(size=(512, ))

  6. face_encodings = face_encodings.astype(np.float32)
  7. face_to_compare = face_to_compare.astype(np.float32)
  8. # np.float32  1比所有200000个重点人脸库计算欧式距离的平方,耗时(s)= 0.502
  9. # np.float16  1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 4.411
  10. # np.float64  1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 1.539
  11. print(type(face_encodings), face_encodings.dtype)
  12. print(type(face_to_compare), face_to_compare.dtype)
  13. # return np.linalg.norm(face_encodings - face_to_compare, axis=1)
  14. print('重点库中的人脸编码个数=', len(face_encodings))
  15. print('待比较的人脸编码的shape=', face_to_compare.shape)
  16. start_time = time.time()
  17. # all_distance = np.square(np.linalg.norm(face_encodings - face_to_compare, axis=1))
  18. # 换成这样 时间效率高些
  19. diff = np.subtract(face_encodings, face_to_compare)
  20. all_distance = np.sum(np.square(diff), 1)
  21. print('1比所有%d个重点人脸库计算欧式距离的平方,耗时(s)=' % len(face_encodings), time.time() - start_time)
  22. print("结果距离的shape:", all_distance.shape, all_distance.dtype)
复制代码







欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4