In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

Get Face Datas


In [2]:
faces = fetch_olivetti_faces()
print("total data : {}".format(len(faces.data)))
print("total class : {}".format(len(np.unique(faces.target))))


total data : 400
total class : 40

Display Datas


In [3]:
def display_datas(N, M=10):
    np.random.seed(0)
    fig = plt.figure(figsize=(20, 20/(10/N)))
    plt.subplots_adjust()
    for i in range(N):
        for j in range(M):
            k = i*M+j
            ax = fig.add_subplot(N, M, i*M+j+1)
            ax.imshow(faces.images[k], cmap=plt.cm.bone)
            ax.xaxis.set_ticks([])
            ax.yaxis.set_ticks([])
            plt.title(faces.target[k]) # add target on title 
    plt.tight_layout()
    plt.show()

In [20]:
display_datas(2)


Split test data & training data


In [4]:
X_train, X_test, y_train, y_test = train_test_split(faces.data, faces.target, test_size=0.2, random_state=0)
print("Training Data : {}".format(len(X_train)))
print("Test Data : {}".format(len(X_test)))


Training Data : 320
Test Data : 80

Train SVM


In [5]:
svc_model = SVC(kernel='linear').fit(X_train, y_train)

In [6]:
y_pred = svc_model.predict(X_test)
print(accuracy_score(y_test, y_pred))


0.9875

Predict Face


In [7]:
def display_datas2(k):
    fig = plt.figure(figsize=(20, 2))
    plt.subplots_adjust()
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(faces.images[k], cmap=plt.cm.bone)
    ax.xaxis.set_ticks([])
    ax.yaxis.set_ticks([])
    plt.tight_layout()
    plt.show()
    
    result = svc_model.predict(faces.data)[k]
    result_str = "data : {} / predict : {}".format(faces.target[k], result)
    print(result_str)

In [15]:
display_datas2(259)


data : 25 / predict : 25

Predict Glasses


In [10]:
glasses = [
    ( 10,  19), ( 30,  32), ( 37,  38), ( 50,  59), ( 63,  64),
    ( 69,  69), (120, 121), (124, 129), (130, 139), (160, 161),
    (164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
    (194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
    (330, 339), (358, 359), (360, 369)
]

def create_target(segments):
    y = np.zeros(faces.target.shape[0])
    for (start, end) in segments:
        y[start:end + 1] = 1
    return y

target_glasses = create_target(glasses)
X_train, X_test, y_train, y_test = train_test_split(faces.data, target_glasses, test_size=0.25, random_state=0)

glassesModel = SVC(kernel='linear').fit(X_train, y_train)

In [11]:
y_pred = glassesModel.predict(X_test)
print(accuracy_score(y_test, y_pred))


0.99

In [19]:
def predict(k):
    fig = plt.figure(figsize=(3,20))
    ax = fig.add_subplot(10, 1, 1)
    ax.imshow(faces.images[k].reshape(64,64), cmap=plt.cm.bone);
    ax.grid(False)
    ax.xaxis.set_ticks([])
    ax.yaxis.set_ticks([])
    plt.tight_layout()
    plt.show()
    result = glassesModel.predict(faces.data)[k]
    print("glasses" if result == 1 else "no glasses")
predict(32)


glasses

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: