2013-07-16 21:21:08 +08:00
|
|
|
"""
|
2013-07-17 17:07:24 +08:00
|
|
|
=============================================
|
|
|
|
|
Face completion with a multi-output estimator
|
|
|
|
|
=============================================
|
2013-07-16 21:21:08 +08:00
|
|
|
|
|
|
|
|
This example shows the use of multi-output estimator to complete images.
|
|
|
|
|
The goal is to predict the lower half of a face given its upper half.
|
|
|
|
|
|
2013-07-17 17:07:24 +08:00
|
|
|
The first column of images shows true faces. The next columns illustrate
|
|
|
|
|
how k nearest neighbors, extremely randomized trees, linear
|
|
|
|
|
regression and ridge regression complete the lower half of those faces.
|
2013-07-16 21:21:08 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
print(__doc__)
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
|
|
|
|
|
from sklearn.datasets import fetch_olivetti_faces
|
|
|
|
|
from sklearn.utils.validation import check_random_state
|
|
|
|
|
|
|
|
|
|
from sklearn.ensemble import ExtraTreesRegressor
|
|
|
|
|
from sklearn.neighbors import KNeighborsRegressor
|
|
|
|
|
from sklearn.linear_model import LinearRegression
|
|
|
|
|
from sklearn.linear_model import RidgeCV
|
|
|
|
|
|
|
|
|
|
# Load the faces datasets
|
|
|
|
|
data = fetch_olivetti_faces()
|
|
|
|
|
targets = data.target
|
|
|
|
|
|
|
|
|
|
data = data.images.reshape((len(data.images), -1))
|
|
|
|
|
train = data[targets < 30]
|
|
|
|
|
test = data[targets >= 30] # Test on independent people
|
|
|
|
|
|
2013-07-17 17:07:24 +08:00
|
|
|
# Test on a subset of people
|
2013-07-16 21:21:08 +08:00
|
|
|
n_faces = 5
|
|
|
|
|
rng = check_random_state(4)
|
2013-07-17 17:07:24 +08:00
|
|
|
face_ids = rng.randint(test.shape[0], size=(n_faces, ))
|
|
|
|
|
test = test[face_ids, :]
|
|
|
|
|
|
|
|
|
|
n_pixels = data.shape[1]
|
|
|
|
|
X_train = train[:, :np.ceil(0.5 * n_pixels)] # Upper half of the faces
|
|
|
|
|
y_train = train[:, np.floor(0.5 * n_pixels):] # Lower half of the faces
|
|
|
|
|
X_test = test[:, :np.ceil(0.5 * n_pixels)]
|
|
|
|
|
y_test = test[:, np.floor(0.5 * n_pixels):]
|
2013-07-16 21:21:08 +08:00
|
|
|
|
2013-07-17 17:07:24 +08:00
|
|
|
# Fit estimators
|
2013-07-16 21:21:08 +08:00
|
|
|
ESTIMATORS = {
|
2013-07-17 17:07:24 +08:00
|
|
|
"Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32,
|
2013-07-16 21:21:08 +08:00
|
|
|
random_state=0),
|
2013-07-17 17:07:24 +08:00
|
|
|
"K-nn": KNeighborsRegressor(),
|
|
|
|
|
"Linear regression": LinearRegression(),
|
|
|
|
|
"Ridge": RidgeCV(),
|
2013-07-16 21:21:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y_test_predict = dict()
|
|
|
|
|
for name, estimator in ESTIMATORS.items():
|
|
|
|
|
estimator.fit(X_train, y_train)
|
|
|
|
|
y_test_predict[name] = estimator.predict(X_test)
|
|
|
|
|
|
|
|
|
|
# Plot the completed faces
|
|
|
|
|
image_shape = (64, 64)
|
|
|
|
|
|
|
|
|
|
n_cols = 1 + len(ESTIMATORS)
|
|
|
|
|
pl.figure(figsize=(2. * n_cols, 2.26 * n_faces))
|
|
|
|
|
pl.suptitle("Face completion with multi-output estimators", size=16)
|
|
|
|
|
|
|
|
|
|
for i in range(n_faces):
|
|
|
|
|
true_face = np.hstack((X_test[i], y_test[i]))
|
|
|
|
|
|
2013-07-18 22:30:48 +08:00
|
|
|
if i:
|
2013-07-16 21:21:08 +08:00
|
|
|
sub = pl.subplot(n_faces, n_cols, i * n_cols + 1,
|
|
|
|
|
title="true faces")
|
2013-07-18 22:30:48 +08:00
|
|
|
else:
|
|
|
|
|
sub = pl.subplot(n_faces, n_cols, i * n_cols + 1)
|
|
|
|
|
|
2013-07-16 21:21:08 +08:00
|
|
|
sub.axis("off")
|
|
|
|
|
sub.imshow(true_face.reshape(image_shape),
|
|
|
|
|
cmap=pl.cm.gray,
|
|
|
|
|
interpolation="nearest")
|
|
|
|
|
|
|
|
|
|
for j, est in enumerate(sorted(ESTIMATORS)):
|
|
|
|
|
completed_face = np.hstack((X_test[i], y_test_predict[est][i]))
|
|
|
|
|
|
2013-07-18 22:30:48 +08:00
|
|
|
if i:
|
2013-07-16 21:21:08 +08:00
|
|
|
sub = pl.subplot(n_faces, n_cols, i * n_cols + 2 + j)
|
2013-07-18 22:30:48 +08:00
|
|
|
|
2013-07-16 21:21:08 +08:00
|
|
|
else:
|
|
|
|
|
sub = pl.subplot(n_faces, n_cols, i * n_cols + 2 + j,
|
|
|
|
|
title=est)
|
2013-07-18 22:30:48 +08:00
|
|
|
|
2013-07-16 21:21:08 +08:00
|
|
|
sub.axis("off")
|
|
|
|
|
sub.imshow(completed_face.reshape(image_shape),
|
|
|
|
|
cmap=pl.cm.gray,
|
|
|
|
|
interpolation="nearest")
|
|
|
|
|
|
|
|
|
|
pl.show()
|