推理过程中人脸嵌入比较的最佳度量

2024-10-06 12:29:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在闭路电视画面上进行人脸识别。在推理过程中,我实现了几个用于人脸嵌入比较的距离度量,如欧几里德距离、余弦距离、KDTree、SVM、L1&;L2距离,等等,但最后我只保留了前两个,因为我没有从中获得预期的准确性,很难找到一个好的阈值

def distance(self, embeddings1, embeddings2, distance_metric=0):
        if distance_metric == 0:
            # Euclidian distance
            embeddings1 = embeddings1/np.linalg.norm(embeddings1, axis=1, keepdims=True)
            embeddings2 = embeddings2/np.linalg.norm(embeddings2, axis=1, keepdims=True)
            dist = np.sqrt(np.sum(np.square(np.subtract(embeddings1, embeddings2))))
            return dist
        elif distance_metric == 1:
            # Distance based on cosine similarity
            dot = np.sum(np.multiply(embeddings1, embeddings2), axis=1)
            norm = np.linalg.norm(embeddings1, axis=1) * np.linalg.norm(embeddings2, axis=1)
            similarity = dot/norm
            dist = np.arccos(similarity) / math.pi
            return dist[0]
        else:
            raise 'Undefined distance metric %d' % distance_metric

Q1: What is the best metric in terms of accuracy for Face Embedding comparison and how to set a threshold in that?

Q2: What is the fastest and better way for Face Embedding comparison other than Euclidean and Cosine distance?


Tags: andtrue距离normdistnpmetricdistance
2条回答

我建议您在deepface中使用最先进的人脸识别模型。它提供了一个公共接口,您只需将模型名作为参数传递

#!pip install deepface
from deepface import DeepFace
models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace'
           , 'DeepID', 'Dlib', 'ArcFace']
metrics = ['cosine', 'euclidean', 'euclidean_l2']
obj = DeepFace.verify("img1.jpg", "img2.jpg"
   , model_name = models[6], distance_metric = metrics[0])
print(obj)

如果你指的是最先进的技术,那么这里的列表如下:https://paperswithcode.com/sota/face-verification-on-labeled-faces-in-the

目前,ArcFace是最好的评分模型

它使用加性角裕度损失作为人脸识别的高分辨特征

好的是,它可以很容易地进行推广,并且可以基于特征的角度表示和权重向量(包括三重态损耗)设计其他损耗函数

对于你的问题2,它似乎有点模糊。如果您的意思是如何使matchinf更快,那么您可以始终使用集群技术而不是线性搜索

本文还研究了一些加速研究

代码可从以下网址获取:

https://github.com/deepinsight/insightface(mxnet)

https://github.com/TreB1eN/InsightFace_Pytorch(pytorch)

https://github.com/happynear/AMSoftmax(caffe)

为了更快地进行推理,您可以只使用两个嵌入之间的L1距离,为了在推理阶段的实际目的,可以使用更简单的距离度量。我建议你通过实验观察哪一个在你训练过的模型中效果最好。其他一些距离度量https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics.pairwise

如何从嵌入数据库中检测哪张脸

  • 最简单的方法是线性扫描。因此,对于数据集中的所有嵌入,计算当前计算的人脸嵌入和来自嵌入数据库的距离度量。选择距离最小的一个。此外,您可能需要指定一个阈值以丢弃未知面。复杂性O(N)

  • 一个稍微好一点的方法是在人脸嵌入数据集上运行一个无监督的聚类算法来生成多个聚类。假设有k个簇,每个簇中平均有p个面嵌入

首先搜索所有簇以找到接近当前嵌入O(k)的簇,然后在该簇中运行线性扫描O(p)以找到合适的面

  • 之后,它变成了一个数据结构问题。您可以创建一个平衡树以进一步加快匹配速度

相关问题 更多 >