ohayonguy
commited on
Commit
•
bfc1040
1
Parent(s):
3dc9cef
improved interface
Browse files- app.py +168 -27
- requirements.txt +2 -1
app.py
CHANGED
@@ -5,9 +5,12 @@ os.environ['K_DIFFUSION_USE_COMPILE'] = "0"
|
|
5 |
import spaces
|
6 |
import cv2
|
7 |
import gradio as gr
|
|
|
8 |
import torch
|
9 |
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
10 |
from basicsr.utils import img2tensor, tensor2img
|
|
|
|
|
11 |
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
12 |
from realesrgan.utils import RealESRGANer
|
13 |
|
@@ -15,7 +18,7 @@ from lightning_models.mmse_rectified_flow import MMSERectifiedFlow
|
|
15 |
|
16 |
torch.set_grad_enabled(False)
|
17 |
|
18 |
-
|
19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
|
21 |
os.makedirs('pretrained_models', exist_ok=True)
|
@@ -60,7 +63,6 @@ def generate_reconstructions(pmrf_model, x, y, non_noisy_z0, num_flow_steps, dev
|
|
60 |
@spaces.GPU()
|
61 |
def enhance_face(img, face_helper, has_aligned, num_flow_steps, only_center_face=False, paste_back=True, scale=2):
|
62 |
face_helper.clean_all()
|
63 |
-
|
64 |
if has_aligned: # the inputs are already aligned
|
65 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
66 |
face_helper.cropped_faces = [img]
|
@@ -108,7 +110,10 @@ def enhance_face(img, face_helper, has_aligned, num_flow_steps, only_center_face
|
|
108 |
|
109 |
@torch.inference_mode()
|
110 |
@spaces.GPU()
|
111 |
-
def inference(img, aligned, scale, num_flow_steps):
|
|
|
|
|
|
|
112 |
if scale > 4:
|
113 |
scale = 4 # avoid too large scale value
|
114 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
@@ -145,13 +150,20 @@ def inference(img, aligned, scale, num_flow_steps):
|
|
145 |
cv2.imwrite(save_path, output)
|
146 |
|
147 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
Please refer to our project's page for more details: https://pmrf-ml.github.io/.
|
157 |
|
@@ -162,12 +174,33 @@ You may use this demo to enhance the quality of any image which contains faces.
|
|
162 |
1. If your input image has only one face and it is aligned, please mark "Yes" to the answer below.
|
163 |
2. Otherwise, your image may contain any number of faces (>=1), and the quality of each face will be enhanced separately.
|
164 |
|
165 |
-
|
166 |
|
167 |
1. Our model is designed to restore aligned face images, but here we incorporate mechanisms that allow restoring the quality of any image that contains any number of faces. Thus, the resulting quality of such general images is not guaranteed.
|
168 |
2. Images that are too large won't work due to memory constraints.
|
169 |
"""
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
article = r"""
|
173 |
|
@@ -196,24 +229,132 @@ Redistribution and use for non-commercial purposes should follow this license.
|
|
196 |
|
197 |
If you have any questions, please feel free to contact me at <b>[email protected]</b>.
|
198 |
"""
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
200 |
"""
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
gr.
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
|
218 |
demo.queue()
|
219 |
-
demo.launch(state_session_capacity=15)
|
|
|
5 |
import spaces
|
6 |
import cv2
|
7 |
import gradio as gr
|
8 |
+
import random
|
9 |
import torch
|
10 |
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
11 |
from basicsr.utils import img2tensor, tensor2img
|
12 |
+
from gradio_imageslider import ImageSlider
|
13 |
+
from pytorch_lightning.utilities.seed import seed_everything
|
14 |
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
15 |
from realesrgan.utils import RealESRGANer
|
16 |
|
|
|
18 |
|
19 |
torch.set_grad_enabled(False)
|
20 |
|
21 |
+
MAX_SEED = 1000000
|
22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
|
24 |
os.makedirs('pretrained_models', exist_ok=True)
|
|
|
63 |
@spaces.GPU()
|
64 |
def enhance_face(img, face_helper, has_aligned, num_flow_steps, only_center_face=False, paste_back=True, scale=2):
|
65 |
face_helper.clean_all()
|
|
|
66 |
if has_aligned: # the inputs are already aligned
|
67 |
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
|
68 |
face_helper.cropped_faces = [img]
|
|
|
110 |
|
111 |
@torch.inference_mode()
|
112 |
@spaces.GPU()
|
113 |
+
def inference(seed, randomize_seed, img, aligned, scale, num_flow_steps):
|
114 |
+
if randomize_seed:
|
115 |
+
seed = random.randint(0, MAX_SEED)
|
116 |
+
seed_everything(seed)
|
117 |
if scale > 4:
|
118 |
scale = 4 # avoid too large scale value
|
119 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
|
|
150 |
cv2.imwrite(save_path, output)
|
151 |
|
152 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
153 |
+
orig_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
154 |
+
orig_input = cv2.resize(orig_input, (output.shape[0], output.shape[1]), interpolation=cv2.INTER_LINEAR)
|
155 |
+
return [[orig_input, output, seed], save_path]
|
156 |
+
|
157 |
+
intro = """
|
158 |
+
<h2 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">Posterior-Mean Rectified Flow: Towards Minimum MSE Photo-Realistic Image Restoration</h2>
|
159 |
+
<h3 style="margin-bottom: 10px; text-align: center;">
|
160 |
+
<a href="https://arxiv.org/abs/2410.00418">[Paper]</a> |
|
161 |
+
<a href="https://pmrf-ml.github.io/">[Project Page]</a> |
|
162 |
+
<a href="https://github.com/ohayonguy/PMRF">[Code]</a>
|
163 |
+
</h3>
|
164 |
+
"""
|
165 |
+
markdown_top = """
|
166 |
+
Gradio demo for the blind face image restoration version of [Posterior-Mean Rectified Flow: Towards Minimum MSE Photo-Realistic Image Restoration](https://arxiv.org/abs/2410.00418).
|
167 |
|
168 |
Please refer to our project's page for more details: https://pmrf-ml.github.io/.
|
169 |
|
|
|
174 |
1. If your input image has only one face and it is aligned, please mark "Yes" to the answer below.
|
175 |
2. Otherwise, your image may contain any number of faces (>=1), and the quality of each face will be enhanced separately.
|
176 |
|
177 |
+
*Notes*:
|
178 |
|
179 |
1. Our model is designed to restore aligned face images, but here we incorporate mechanisms that allow restoring the quality of any image that contains any number of faces. Thus, the resulting quality of such general images is not guaranteed.
|
180 |
2. Images that are too large won't work due to memory constraints.
|
181 |
"""
|
182 |
|
183 |
+
#
|
184 |
+
# title = "Posterior-Mean Rectified Flow: Towards Minimum MSE Photo-Realistic Image Restoration"
|
185 |
+
#
|
186 |
+
# description = r"""
|
187 |
+
# Gradio demo for the blind face image restoration version of <a href='https://arxiv.org/abs/2410.00418' target='_blank'><b>Posterior-Mean Rectified Flow: Towards Minimum MSE Photo-Realistic Image Restoration</b></a>.
|
188 |
+
#
|
189 |
+
# Please refer to our project's page for more details: https://pmrf-ml.github.io/.
|
190 |
+
#
|
191 |
+
# ---
|
192 |
+
#
|
193 |
+
# You may use this demo to enhance the quality of any image which contains faces.
|
194 |
+
#
|
195 |
+
# 1. If your input image has only one face and it is aligned, please mark "Yes" to the answer below.
|
196 |
+
# 2. Otherwise, your image may contain any number of faces (>=1), and the quality of each face will be enhanced separately.
|
197 |
+
#
|
198 |
+
# <b>NOTEs</b>:
|
199 |
+
#
|
200 |
+
# 1. Our model is designed to restore aligned face images, but here we incorporate mechanisms that allow restoring the quality of any image that contains any number of faces. Thus, the resulting quality of such general images is not guaranteed.
|
201 |
+
# 2. Images that are too large won't work due to memory constraints.
|
202 |
+
# """
|
203 |
+
|
204 |
|
205 |
article = r"""
|
206 |
|
|
|
229 |
|
230 |
If you have any questions, please feel free to contact me at <b>[email protected]</b>.
|
231 |
"""
|
232 |
+
|
233 |
+
css = """
|
234 |
+
#col-container {
|
235 |
+
margin: 0 auto;
|
236 |
+
max-width: 512px;
|
237 |
+
}
|
238 |
"""
|
239 |
|
240 |
+
|
241 |
+
|
242 |
+
with gr.Blocks(css=css) as demo:
|
243 |
+
gr.HTML(intro)
|
244 |
+
gr.Markdown(markdown_top)
|
245 |
+
|
246 |
+
with gr.Row():
|
247 |
+
run_button = gr.Button(value="Run")
|
248 |
+
|
249 |
+
with gr.Row():
|
250 |
+
with gr.Column(scale=4):
|
251 |
+
input_im = gr.Image(label="Input Image", type="pil")
|
252 |
+
with gr.Column(scale=1):
|
253 |
+
num_inference_steps = gr.Slider(
|
254 |
+
label="Number of Inference Steps",
|
255 |
+
minimum=1,
|
256 |
+
maximum=200,
|
257 |
+
step=1,
|
258 |
+
value=25,
|
259 |
+
)
|
260 |
+
upscale_factor = gr.Slider(
|
261 |
+
label="Scale factor for the background upsampler. Applicable only to non-aligned face images.",
|
262 |
+
minimum=1,
|
263 |
+
maximum=4,
|
264 |
+
step=0.1,
|
265 |
+
value=1,
|
266 |
+
)
|
267 |
+
seed = gr.Slider(
|
268 |
+
label="Seed",
|
269 |
+
minimum=0,
|
270 |
+
maximum=MAX_SEED,
|
271 |
+
step=1,
|
272 |
+
value=42,
|
273 |
+
)
|
274 |
+
|
275 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
276 |
+
aligned = gr.Checkbox(label="The input is an aligned face image", value=True)
|
277 |
+
|
278 |
+
with gr.Row():
|
279 |
+
result = ImageSlider(label="Input / Output", type="numpy", interactive=True)
|
280 |
+
with gr.Row():
|
281 |
+
file = gr.File(label="Download the output image")
|
282 |
+
|
283 |
+
# examples = gr.Examples(
|
284 |
+
# examples=[
|
285 |
+
# # [42, False, "examples/image_1.jpg", 28, 4, 0.6],
|
286 |
+
# # [42, False, "examples/image_2.jpg", 28, 4, 0.6],
|
287 |
+
# # [42, False, "examples/image_3.jpg", 28, 4, 0.6],
|
288 |
+
# # [42, False, "examples/image_4.jpg", 28, 4, 0.6],
|
289 |
+
# # [42, False, "examples/image_5.jpg", 28, 4, 0.6],
|
290 |
+
# # [42, False, "examples/image_6.jpg", 28, 4, 0.6],
|
291 |
+
# ],
|
292 |
+
# inputs=[
|
293 |
+
# seed,
|
294 |
+
# randomize_seed,
|
295 |
+
# input_im,
|
296 |
+
# num_inference_steps,
|
297 |
+
# upscale_factor,
|
298 |
+
# controlnet_conditioning_scale,
|
299 |
+
# ],
|
300 |
+
# fn=infer,
|
301 |
+
# outputs=result,
|
302 |
+
# cache_examples="lazy",
|
303 |
+
# )
|
304 |
+
|
305 |
+
# examples = gr.Examples(
|
306 |
+
# examples=[
|
307 |
+
# #[42, False, "examples/image_1.jpg", 28, 4, 0.6],
|
308 |
+
# [42, False, "examples/image_2.jpg", 28, 4, 0.6],
|
309 |
+
# #[42, False, "examples/image_3.jpg", 28, 4, 0.6],
|
310 |
+
# #[42, False, "examples/image_4.jpg", 28, 4, 0.6],
|
311 |
+
# [42, False, "examples/image_5.jpg", 28, 4, 0.6],
|
312 |
+
# [42, False, "examples/image_6.jpg", 28, 4, 0.6],
|
313 |
+
# [42, False, "examples/image_7.jpg", 28, 4, 0.6],
|
314 |
+
# ],
|
315 |
+
# inputs=[
|
316 |
+
# seed,
|
317 |
+
# randomize_seed,
|
318 |
+
# input_im,
|
319 |
+
# num_inference_steps,
|
320 |
+
# upscale_factor,
|
321 |
+
# controlnet_conditioning_scale,
|
322 |
+
# ],
|
323 |
+
# )
|
324 |
+
|
325 |
+
|
326 |
+
gr.on(
|
327 |
+
[run_button.click],
|
328 |
+
fn=inference,
|
329 |
+
inputs=[
|
330 |
+
seed,
|
331 |
+
randomize_seed,
|
332 |
+
input_im,
|
333 |
+
aligned,
|
334 |
+
upscale_factor,
|
335 |
+
num_inference_steps,
|
336 |
+
],
|
337 |
+
outputs=[result, file],
|
338 |
+
show_api=False,
|
339 |
+
# show_progress="minimal",
|
340 |
+
)
|
341 |
+
|
342 |
+
|
343 |
+
# demo = gr.Interface(
|
344 |
+
# inference, [
|
345 |
+
# gr.Image(type="filepath", label="Input"),
|
346 |
+
# gr.Radio(['Yes', 'No'], type="value", value='aligned', label='Is the input an aligned face image?'),
|
347 |
+
# gr.Slider(label="Scale factor for the background upsampler. Applicable only to non-aligned face images.", minimum=1, maximum=4, value=2, step=0.1, interactive=True),
|
348 |
+
# gr.Number(label="Number of flow steps. A higher value should result in better image quality, but will inference will take a longer time.", value=25),
|
349 |
+
# ], [
|
350 |
+
# gr.ImageSlider(type="numpy", label="Input / Output", interactive=True),
|
351 |
+
# gr.File(label="Download the output image")
|
352 |
+
# ],
|
353 |
+
# title=title,
|
354 |
+
# description=description,
|
355 |
+
# article=article,
|
356 |
+
# )
|
357 |
|
358 |
|
359 |
demo.queue()
|
360 |
+
demo.launch(state_session_capacity=15, show_api=False, share=False)
|
requirements.txt
CHANGED
@@ -20,4 +20,5 @@ timm
|
|
20 |
torchmetrics
|
21 |
torch-fidelity==0.3.0
|
22 |
-f https://shi-labs.com/natten/wheels/
|
23 |
-
natten==0.17.1+torch240cu121
|
|
|
|
20 |
torchmetrics
|
21 |
torch-fidelity==0.3.0
|
22 |
-f https://shi-labs.com/natten/wheels/
|
23 |
+
natten==0.17.1+torch240cu121
|
24 |
+
gradio-imageslider
|