我尝试使用中文耳语算法进行人脸聚类。我使用dlib和python为每个人脸提取特征,并映射成128d向量,如Davisking在https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp所描述的那样。在
然后我按照上面给出的说明构造了一个图。我实现了中文耳语算法并应用到这个图中。谁能告诉我我犯了什么错误吗?有人可以上传使用中文耳语算法的python代码进行人脸聚类吗?以下是我的中文耳语密码:
import networkx as nx
import random
from random import shuffle
import math
def chinese_whispers(nodes,edges,iterations):
G = nx.Graph()
G.add_nodes_from(nodes)
#print(G.node)
for n, v in enumerate(nodes):
G.node[n]['class'] = v
#print(n,v)
G.add_edges_from(edges)
#gn=G.nodes()
#for node in gn:
#print((node,G[node],G.node,G.node[node]))
#(0, {16: {'weight': 0.49846761956907698}, 14: {'weight': 0.55778036559581601}, 7: {'weight': 0.43902511314524784}}, {'class': 0})
for z in range(0, iterations):
gn = G.nodes()
# I randomize the nodes to give me an arbitrary start point
shuffle(gn)
for node in gn:
neighs = G[node]
classes = {}
# do an inventory of the given nodes neighbours and edge weights
for ne in neighs:
if isinstance(ne, int):
key=G.node[ne]['class']
if key in classes:
classes[key] += G[node][ne]['weight']
else:
classes[key] = G[node][ne]['weight']
# find the class with the highest edge weight sum
max = 0
maxclass = 0
for c in classes:
if classes[c] > max:
max = classes[c]
maxclass = c
# set the class of target node to the winning local class
G.node[node]['class'] = maxclass
n_clusters = []
for node in G.nodes():
n_clusters.append(G.node[node]['class'])
return(n_clusters)
本文给出了在128d矢量中对每个人脸进行特征提取和编码的代码,并从这些图形的构造中应用汉语耳语。在
^{pr2}$我不明白我错了什么做。可以有人帮我吗? 提前谢谢。在
我以前用过Dlib进行人脸聚类。在
对不起,我没听懂你的问题。 你得到的是错误还是没有得到准确的结果?在
假设您没有得到正确的结果,我建议使用shape_predictor_5_face_landmarks.dat而不是64人脸地标,因为当使用中文耳语算法进行聚类时,它会得到更好的结果。在
你也可以试试DLib自己的中文耳语聚类功能,看看效果是否更好。在
示例-face_clustering.py
您还可以更改阈值和迭代次数,以查看它是否能提供更好的结果。在
希望这有帮助。在
相关问题 更多 >
编程相关推荐