东方耀AI技术分享
标题:
同样是计算欧式距离的平方 float32比float16还要快N倍?
[打印本页]
作者:
东方耀
时间:
2020-5-29 14:12
标题:
同样是计算欧式距离的平方 float32比float16还要快N倍?
同样是计算欧式距离的平方 float32比float16还要快N倍?
import numpy as np
import time
# 同样是计算欧式距离的平方 float32比float16还要快N倍?
face_encodings = np.random.random(size=(300000, 512))
face_to_compare = np.random.random(size=(512, ))
face_encodings = face_encodings.astype(np.float32)
face_to_compare = face_to_compare.astype(np.float32)
# np.float32 1比所有200000个重点人脸库计算欧式距离的平方,耗时(s)= 0.502
# np.float16 1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 4.411
# np.float64 1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 1.539
print(type(face_encodings), face_encodings.dtype)
print(type(face_to_compare), face_to_compare.dtype)
# return np.linalg.norm(face_encodings - face_to_compare, axis=1)
print('重点库中的人脸编码个数=', len(face_encodings))
print('待比较的人脸编码的shape=', face_to_compare.shape)
start_time = time.time()
# all_distance = np.square(np.linalg.norm(face_encodings - face_to_compare, axis=1))
# 换成这样 时间效率高些
diff = np.subtract(face_encodings, face_to_compare)
all_distance = np.sum(np.square(diff), 1)
print('1比所有%d个重点人脸库计算欧式距离的平方,耗时(s)=' % len(face_encodings), time.time() - start_time)
print("结果距离的shape:", all_distance.shape, all_distance.dtype)
复制代码
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4