2017-06-05 73 views
5

我正在嘗試使用中文whispering算法進行人臉聚類。我已經使用dlib和python爲每個臉部提取特徵,並將其映射到128 D矢量,如Davisking在https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp中所述。使用中文Whispers算法的人臉聚類

然後我按照給出的指示構建了一張圖。我實現了中國耳語算法並應用於此圖。任何人都可以告訴我我做了什麼錯誤嗎?任何人都可以使用中文耳語算法上傳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) 

這裏被面部特徵提取和在128 d矢量,並從這些構造圖的用於施加中國低語各面的編碼的代碼。

from sklearn import cluster 
import cv2 
import sys 
import os 
import dlib 
import glob 
from skimage import io 
import numpy as np 
from sklearn.cluster import KMeans 
from sklearn.manifold import TSNE 
from matplotlib import pyplot as plt 
import chinese 
from chinese import chinese_whispers 
predictor_path = "/home/deeplearning/Desktop/face_recognition 
examples/shape_predictor_68_face_landmarks.dat" 
face_rec_model_path = "/home/deeplearning/Desktop/face_recognition 
examples/dlib_face_recognition_resnet_model_v1.dat" 
faces_folder_path = "/home/deeplearning/Desktop/face_recognition 
examples/test11/" 

# Load all the models we need: a detector to find the faces, a shape predictor 
# to find face landmarks so we can precisely localize the face, and finally the 
# face recognition model. 
detector = dlib.get_frontal_face_detector() 
#print (detector) 
sp = dlib.shape_predictor(predictor_path) 
facerec = dlib.face_recognition_model_v1(face_rec_model_path) 

#win = dlib.image_window() 

# Now process all the images 
dict={} 
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")): 
    print("Processing file: {}".format(f)) 
    img = io.imread(f) 
    dets = detector(img, 3) 
    for k, d in enumerate(dets): 
     shape = sp(img, d) 
     face_descriptor = facerec.compute_face_descriptor(img, shape) 
     a=np.array(face_descriptor) 
     dict[(f,d)] = (a,f) 



answ=np.array(list(dict.values())) 
tmp=answ.shape[0] 
ans=np.zeros((tmp,128)) 
for i in range(tmp): 
    ans[i]=np.array(answ[i][0]) 
nodes=[] 
for i in range(tmp): 
    nodes.append(i) 
edges=[] 
for i in range(tmp): 
    for j in range(i+1,tmp): 
     dist=np.sqrt(np.sum((ans[i]-ans[j])**2)) 
     if dist < 0.6: 
      edges.append((i,j,{'weight': dist})) 


iterations=10 
cluster=chinese_whispers(nodes,edges,iterations) 

我不明白我在做什麼錯誤。誰能幫我解決這個問題? 在此先感謝。

+2

歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –

+1

「但我沒有得到很好的準確性」不是一個非常有用的問題描述。堆棧溢出最好用於解決特定問題和這類問題 – Rook

回答

0

我已經使用dlib進行相同的人臉識別任務。我發現Scikit-learn的SVM與RBF內核在準確性方面給出了很好的結果。您可以嘗試GridSearchCV來調整SVM分類器以獲得最佳的C值和伽瑪值。

from sklearn import svm 
from sklearn.metrics import confusion_matrix, accuracy_score 
from sklearn.preprocessing import LabelEncoder 
from sklearn.model_selection import train_test_split 
from sklearn.metrics import classification_report 
from sklearn.model_selection import GridSearchCV 

# reps is a list of all faces embeddings (array of 128D vectors) 
# labels is a list of all face labels (['bob', 'mary'...] etc) 

X_train, X_test, y_train, y_test = train_test_split(
    reps, labels, test_size=0.3, random_state=0) 

le = LabelEncoder().fit(labels) 

# Set the parameters by cross-validation 
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1, 10, 100], 
        'C': [0.1, 1, 10, 100, 1000]} 
        ] 

scores = ['precision', 'recall'] 

for score in scores: 
    _LOGGER.info("# Tuning hyper-parameters for %s" % score) 

    clf = GridSearchCV(svm.SVC(C=1), tuned_parameters, cv=5, 
         scoring='%s_macro' % score) 

    stime = time.time() 
    clf.fit(X_train, y_train) 
    etime = time.time() - stime 
    _LOGGER.info("Fitting took %s seconds" % time.strftime("%H:%M:%S", time.gmtime(etime))) 

    _LOGGER.info("Best parameters set found on development set:") 

    _LOGGER.info(clf.best_params_) 

    _LOGGER.info("Grid scores on development set:") 

    means = clf.cv_results_['mean_test_score'] 
    stds = clf.cv_results_['std_test_score'] 
    for mean, std, params in zip(means, stds, clf.cv_results_['params']): 
     _LOGGER.info("%0.3f (+/-%0.03f) for %r" 
        % (mean, std * 2, params)) 

    _LOGGER.info("Detailed classification report:") 

    _LOGGER.info("The model is trained on the full development set.") 
    _LOGGER.info("The scores are computed on the full evaluation set.") 

    y_true, y_pred = y_test, clf.predict(X_test) 
    _LOGGER.info(classification_report(y_true, y_pred)) 

    _LOGGER.info("Accuracy : %.2f" % (accuracy_score(y_true, y_pred) * 100.0))