Faces recognition using ICA and SVMs

The dataset used in this example is a preprocessed excerpt of the "Labeled Faces in the Wild", aka LFW_:

http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz (233MB)

LFW: http://vis-www.cs.umass.edu/lfw/


In [1]:
%matplotlib inline
from time import time
import logging
import matplotlib.pyplot as plt

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn import manifold
from sklearn.decomposition import FastICA

print(__doc__)

# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

# introspect the images arrays to find the shapes (for plotting)
n_samples, h, w = lfw_people.images.shape

# for machine learning we use the 2 data directly (as relative pixel
# positions info is ignored by this model)
X = lfw_people.data
n_features = X.shape[1]

# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]

print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)

# split into a training and testing set
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42)


2017-03-19 21:52:08,788 Loading LFW people faces from /home/chandu/scikit_learn_data/lfw_home
Automatically created module for IPython interactive environment
Total dataset size:
n_samples: 1288
n_features: 1850
n_classes: 7

In [2]:
n_components_1 = np.arange(150,240,3)
accuracies = []
components = []
# for i in xrange(len(n_components_1)):
n_components = 198

ica = FastICA(n_components=n_components)
S_ = ica.fit_transform(X)
A_ = ica.mixing_

X_train_ica = ica.transform(X_train)
X_test_ica = ica.transform(X_test)

param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
                  'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
clf = clf.fit(X_train_ica, y_train)
y_pred = clf.predict(X_test_ica)

accuracies.append(float(np.sum(y_test==y_pred))/len(y_pred))
components.append(n_components)

print('For '+str(n_components)+' components, accuracy is '+str(float(np.sum(y_test==y_pred))/len(y_pred))+' confusion matrix is: ')
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
print(classification_report(y_test, y_pred, target_names=target_names))


/home/chandu/anaconda2/lib/python2.7/site-packages/sklearn/decomposition/fastica_.py:116: UserWarning: FastICA did not converge. Consider increasing tolerance or the maximum number of iterations.
  warnings.warn('FastICA did not converge. Consider increasing '
For 198 components, accuracy is 0.841614906832 confusion matrix is: 
[[  6   6   1   0   0   0   0]
 [  0  54   1   0   3   1   1]
 [  0   1  24   1   0   0   1]
 [  4   3   2 128   3   4   2]
 [  0   0   1   1  19   1   3]
 [  0   0   0   1   2  11   1]
 [  0   2   2   2   1   0  29]]
                   precision    recall  f1-score   support

     Ariel Sharon       0.60      0.46      0.52        13
     Colin Powell       0.82      0.90      0.86        60
  Donald Rumsfeld       0.77      0.89      0.83        27
    George W Bush       0.96      0.88      0.92       146
Gerhard Schroeder       0.68      0.76      0.72        25
      Hugo Chavez       0.65      0.73      0.69        15
       Tony Blair       0.78      0.81      0.79        36

      avg / total       0.85      0.84      0.84       322


In [3]:
icafaces = ica.components_.reshape((n_components, h, w))
def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
    """Helper function to plot a gallery of portraits"""
#     plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
    plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
    for i in range(n_row * n_col):
        plt.subplot(n_row, n_col, i + 1)
        plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
        plt.title(titles[i], size=12)
        plt.xticks(())
        plt.yticks(())
        
icafaces_titles = ["ica comp %d" % i for i in range(icafaces.shape[0])]
plot_gallery(icafaces, icafaces_titles, h, w)
plt.show()



In [ ]: