Facial Clustering Using Chinese Whispers

I am trying to use Chinese whisper algorithm to cluster faces. I used dlib and python to extract functions for each face and mapped to a 128 D vector, as described by Davisking, at https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp .

Then I plotted following the instructions given there. I implemented the Chinese whisper algorithm and applied it to this graph. Can someone tell me what mistake I made? Can someone download python code to cluster faces using Chinese whisper algorithm? Here is my Chinese whisper code:

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)

, . - ? .

+6
1

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))
-1

All Articles