2017-06-05 12 views
5

का उपयोग कर फेस क्लस्टरिंग मैं चेहरे क्लस्टरिंग के लिए चीनी फुसफुसाए एल्गोरिदम का उपयोग करने की कोशिश कर रहा हूं। मैंने प्रत्येक चेहरे के लिए सुविधाओं को निकालने के लिए dlib और पायथन का उपयोग किया है और https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp पर डेविडिंग द्वारा वर्णित 128 डी वेक्टर में मैप किया गया है।चीनी व्हिस्पर एल्गोरिदम

फिर मैंने वहां दिए गए निर्देशों के बाद एक ग्राफ बनाया। मैंने चीनी फुसफुसाए एल्गोरिदम लागू किया और इस ग्राफ पर लागू किया। क्या कोई मुझे बता सकता है कि मैंने क्या गलती की है? क्या कोई चीनी व्हिस्पर एल्गोरिदम का उपयोग कर चेहरे क्लस्टरिंग के लिए पाइथन कोड अपलोड कर सकता है?

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 डी वेक्टर में और चीनी फुसफुसाते हुए आवेदन करने के लिए ग्राफ के इन निर्माण से प्रत्येक चेहरे के एन्कोडिंग का कोड है: यहाँ चीनी फुसफुसाते हुए की मेरी कोड है।

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

स्टैक ओवरफ़्लो में आपका स्वागत है। कृपया [टूर] (http://stackoverflow.com/tour) पढ़ने के लिए समय लें और [सहायता केंद्र] (http://stackoverflow.com/help/asking) से सामग्री देखें और आप कैसे कर सकते हैं यहाँ पूछो –

+1

"लेकिन मुझे अच्छी सटीकता नहीं मिली" एक बहुत उपयोगी समस्या विवरण नहीं है। स्टैक ओवरफ़्लो का उपयोग विशिष्ट समस्याओं, और इस प्रकार के प्रश्न को हल करने के लिए सबसे अच्छा किया जाता है – Rook

उत्तर

0

मैंने उसी चेहरे भेदभाव कार्यों के लिए dlib का उपयोग किया है। मुझे सटीकता के संदर्भ में अच्छे परिणाम देने के लिए आरबीएफ कर्नेल के साथ विज्ञान-सीखने का एसवीएम मिला। आप सबसे अच्छा सी और गामा मूल्यों के लिए एसवीएम वर्गीकरण को ट्यून करने के लिए ग्रिडशर्चसीवी को भी आजमा सकते हैं।

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)) 
संबंधित मुद्दे