Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +121 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import libraries
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
|
5 |
+
from sklearn.datasets import fetch_olivetti_faces
|
6 |
+
from sklearn.utils.validation import check_random_state
|
7 |
+
|
8 |
+
from sklearn.ensemble import ExtraTreesRegressor
|
9 |
+
from sklearn.neighbors import KNeighborsRegressor
|
10 |
+
from sklearn.linear_model import LinearRegression
|
11 |
+
from sklearn.linear_model import RidgeCV
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
# Load the faces datasets
|
16 |
+
data, targets = fetch_olivetti_faces(return_X_y=True)
|
17 |
+
|
18 |
+
train = data[targets < 30]
|
19 |
+
|
20 |
+
n_pixels = data.shape[1]
|
21 |
+
|
22 |
+
# Training data
|
23 |
+
# Upper half of the faces
|
24 |
+
X_train = train[:, : (n_pixels + 1) // 2]
|
25 |
+
# Lower half of the faces
|
26 |
+
y_train = train[:, n_pixels // 2 :]
|
27 |
+
|
28 |
+
# Fit estimators -> The problem (given half the image/features extrapolate the rest of the image/features)
|
29 |
+
ESTIMATORS = {
|
30 |
+
"Extra trees": ExtraTreesRegressor(
|
31 |
+
n_estimators=10, max_features=32, random_state=0
|
32 |
+
),
|
33 |
+
"K-nn": KNeighborsRegressor(),
|
34 |
+
"Linear regression": LinearRegression(),
|
35 |
+
"Ridge": RidgeCV(),
|
36 |
+
}
|
37 |
+
|
38 |
+
for name, estimator in ESTIMATORS.items():
|
39 |
+
estimator.fit(X_train, y_train)
|
40 |
+
|
41 |
+
test = data[targets >= 30]
|
42 |
+
n_faces = 15
|
43 |
+
rng = check_random_state(4)
|
44 |
+
face_ids = rng.randint(test.shape[0], size=(n_faces,))
|
45 |
+
test = test[face_ids, :]
|
46 |
+
|
47 |
+
# Function for returning 64*64 image, given the image index
|
48 |
+
def imageFromIndex(index):
|
49 |
+
return test[int(index)].reshape(1,-1).reshape(64, 64)
|
50 |
+
|
51 |
+
# Function for extrapolating face
|
52 |
+
def extrapolateFace(index, ESTIMATORS=ESTIMATORS):
|
53 |
+
image = test[int(index)].reshape(1,-1)
|
54 |
+
image_shape = (64, 64)
|
55 |
+
n_cols = 1 + len(ESTIMATORS)
|
56 |
+
n_faces = 1
|
57 |
+
|
58 |
+
n_pixels = image.shape[1]
|
59 |
+
|
60 |
+
# Upper half of the face
|
61 |
+
X_upper = image[:, : (n_pixels + 1) // 2]
|
62 |
+
# Lower half of the face
|
63 |
+
y_ground_truth = image[:, n_pixels // 2 :]
|
64 |
+
|
65 |
+
# y_predict: Dictionary of predicted lower-faces
|
66 |
+
y_predict = dict()
|
67 |
+
for name, estimator in ESTIMATORS.items():
|
68 |
+
y_predict[name] = estimator.predict(X_upper)
|
69 |
+
|
70 |
+
|
71 |
+
plt.figure(figsize=(2.0 * n_cols, 2.5 * n_faces))
|
72 |
+
# plt.suptitle("Face completion with multi-output estimators", size=16)
|
73 |
+
|
74 |
+
|
75 |
+
true_face = np.hstack((X_upper, y_ground_truth))
|
76 |
+
|
77 |
+
sub = plt.subplot(n_faces, n_cols, 1, title="true face")
|
78 |
+
|
79 |
+
sub.axis("off")
|
80 |
+
sub.imshow(
|
81 |
+
true_face.reshape(image_shape), cmap=plt.cm.gray, interpolation="nearest"
|
82 |
+
)
|
83 |
+
|
84 |
+
for j, est in enumerate(sorted(ESTIMATORS)):
|
85 |
+
completed_face = np.hstack((X_upper[0], y_predict[est][0]))
|
86 |
+
|
87 |
+
sub = plt.subplot(n_faces, n_cols, 2 + j, title=est)
|
88 |
+
sub.axis("off")
|
89 |
+
sub.imshow(
|
90 |
+
completed_face.reshape(image_shape),
|
91 |
+
cmap=plt.cm.gray,
|
92 |
+
interpolation="nearest",
|
93 |
+
)
|
94 |
+
|
95 |
+
return plt
|
96 |
+
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
link = "https://scikit-learn.org/stable/auto_examples/miscellaneous/plot_multioutput_face_completion.html#sphx-glr-auto-examples-miscellaneous-plot-multioutput-face-completion-py"
|
99 |
+
title = "Face completion with a multi-output estimators"
|
100 |
+
gr.Markdown(f"# {title}")
|
101 |
+
gr.Markdown(f"This demo is based on this [scikit-learn example]({link}).")
|
102 |
+
gr.Markdown("In this demo, we compare 4 multi-output estimators to complete images. \
|
103 |
+
The goal is to predict the lower half of a face given its upper half.")
|
104 |
+
|
105 |
+
gr.Markdown("#### Use the below slider to choose a face's image. \
|
106 |
+
Consequently, observe how the four estimators complete the lower half of that face.")
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column(scale=1):
|
110 |
+
image_index = gr.Slider(1,15,1,step=1, label="Image Index", info="Choose an image")
|
111 |
+
face_image = gr.Image()
|
112 |
+
with gr.Column(scale=2):
|
113 |
+
plot = gr.Plot(label=f"Face completion with multi-output estimators")
|
114 |
+
|
115 |
+
image_index.change(imageFromIndex, inputs=[image_index], outputs=[face_image])
|
116 |
+
image_index.change(extrapolateFace, inputs=[image_index], outputs=[plot])
|
117 |
+
demo.load(imageFromIndex, inputs=[image_index], outputs=[face_image])
|
118 |
+
demo.load(extrapolateFace, inputs=[image_index], outputs=[plot])
|
119 |
+
|
120 |
+
if __name__ == "__main__":
|
121 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
matplotlib
|
3 |
+
scikit-learn
|
4 |
+
gradio
|