I am trying to use Chinese whispers algorithm for face clustering. I have used dlib and python for extracting features for each face and mapped into 128 D vector as described by Davisking at https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp.
Then I constructed a graph following the instructions given there. I implemented Chinese whispers algorithm and applied to this graph. Can anyone tell me what mistake I have done? Can anyone upload python code for face clustering using chinese whispers algorithm? Here is my code of chinese whispers :
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)
Here is the code of facial feature extraction and encoding of each faces in 128 D vector and from these construction of graph for applying chinese whispers.
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)
I don't understand what wrong I am doing.Can anyone help me about this? Thanks in advance.