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.
The first column of images shows true faces. The next columns illustrate how extremely randomized trees, k nearest neighbors, linear regression and ridge regression complete the lower half of those faces.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | print (__doc__) import numpy as np import matplotlib.pyplot as plt 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 # Test on a subset of people n_faces = 5 rng = check_random_state( 4 ) 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):] # Fit estimators ESTIMATORS = { "Extra trees" : ExtraTreesRegressor(n_estimators = 10 , max_features = 32 , random_state = 0 ), "K-nn" : KNeighborsRegressor(), "Linear regression" : LinearRegression(), "Ridge" : RidgeCV(), } 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) plt.figure(figsize = ( 2. * n_cols, 2.26 * n_faces)) plt.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])) if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 1 ) else : sub = plt.subplot(n_faces, n_cols, i * n_cols + 1 , title = "true faces" ) sub.axis( "off" ) sub.imshow(true_face.reshape(image_shape), cmap = plt.cm.gray, interpolation = "nearest" ) for j, est in enumerate ( sorted (ESTIMATORS)): completed_face = np.hstack((X_test[i], y_test_predict[est][i])) if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j) else : sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j, title = est) sub.axis( "off" ) sub.imshow(completed_face.reshape(image_shape), cmap = plt.cm.gray, interpolation = "nearest" ) plt.show() |
Total running time of the script: (0 minutes 4.875 seconds)
Download Python source code:
plot_multioutput_face_completion.py
Download IPython notebook:
plot_multioutput_face_completion.ipynb
Please login to continue.