Fabrice-TIERCELIN commited on
Commit
752f198
1 Parent(s): 35c63ae
Files changed (1) hide show
  1. gradio_demo.py +27 -14
gradio_demo.py CHANGED
@@ -72,19 +72,30 @@ def stage1_process(input_image, gamma_correction):
72
  if torch.cuda.device_count() == 0:
73
  gr.Warning('Set this space to GPU config to make it work.')
74
  return None
 
75
  torch.cuda.set_device(SUPIR_device)
 
76
  LQ = HWC3(input_image)
 
77
  LQ = fix_resize(LQ, 512)
 
78
  # stage1
79
  LQ = np.array(LQ) / 255 * 2 - 1
 
80
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
 
81
  LQ = model.batchify_denoise(LQ, is_stage1=True)
 
82
  LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
83
  # gamma correction
84
  LQ = LQ / 255.0
 
85
  LQ = np.power(LQ, gamma_correction)
 
86
  LQ *= 255.0
 
87
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
 
88
  return LQ
89
 
90
  @spaces.GPU(duration=120)
@@ -165,6 +176,7 @@ def stage2_process(input_image, prompt, a_prompt, n_prompt, num_samples, upscale
165
  Image.fromarray(result).save(f'./history/{event_id[:5]}/{event_id[5:]}/HQ_{i}.png')
166
  return [input_image] + results, event_id, 3, ''
167
 
 
168
  def load_and_reset(param_setting):
169
  if torch.cuda.device_count() == 0:
170
  gr.Warning('Set this space to GPU config to make it work.')
@@ -215,7 +227,7 @@ if torch.cuda.device_count() == 0:
215
 
216
  <p style="background-color: red;"><big><big><big><b>⚠️To use SUPIR, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true">Duplicate this space</a> and set a GPU with 30 GB VRAM.</b>
217
 
218
- You can't use SUPIR directly here because this space runs on a CPU, which not enough for SUPIR. This is a template space. Please provide feedback if you have issues.
219
  </big></big></big></p>
220
  """
221
  else:
@@ -224,8 +236,8 @@ else:
224
 
225
  ⚠️SUPIR is still a research project under tested and is not yet a stable commercial product.
226
 
227
- <a href="https://arxiv.org/abs/2401.13627">Paper</a> &emsp; <a href="http://supir.xpixel.group/">Project Page</a> &emsp; <a href="https://github.com/Fanghua-Yu/SUPIR/blob/master/assets/DemoGuide.png">How to play</a>
228
- <p style="background-color: orange;">For now, only the stage 2 is working (the most important one). The stage 1 and LLaVa are failing. LLaVa is disabled.</p>
229
  """
230
 
231
 
@@ -243,6 +255,7 @@ The service is a research preview intended for non-commercial use only, subject
243
  with gr.Blocks(title='SUPIR') as interface:
244
  with gr.Row():
245
  gr.HTML(title_md)
 
246
  with gr.Row():
247
  with gr.Column():
248
  with gr.Row(equal_height=True):
@@ -250,11 +263,11 @@ with gr.Blocks(title='SUPIR') as interface:
250
  gr.Markdown("<center>Input</center>")
251
  input_image = gr.Image(type="numpy", elem_id="image-input", height=400, width=400)
252
  with gr.Column():
253
- gr.Markdown("<center>Stage1 Output</center>")
254
  denoise_image = gr.Image(type="numpy", elem_id="image-s1", height=400, width=400)
255
- prompt = gr.Textbox(label="Prompt", value="", placeholder="A person, walking, in a town, Summer, photorealistic")
256
 
257
- with gr.Accordion("Stage1 options", open=False):
258
  gamma_correction = gr.Slider(label="Gamma Correction", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
259
 
260
  with gr.Accordion("LLaVA options", open=False):
@@ -262,15 +275,15 @@ with gr.Blocks(title='SUPIR') as interface:
262
  top_p = gr.Slider(label="Top P", info = "Percent of tokens shortlisted", minimum=0., maximum=1.0, value=0.7, step=0.1)
263
  qs = gr.Textbox(label="Question", info="Describe the image and its style in a very detailed manner", placeholder="The image is a realistic photography, not an art painting.")
264
 
265
- with gr.Accordion("Stage2 options", open=False):
266
  num_samples = gr.Slider(label="Num Samples", info="Number of generated results; I discourage to increase because the process is limited to 2 min", minimum=1, maximum=4 if not args.use_image_slider else 1
267
  , value=1, step=1)
268
  upscale = gr.Slider(label="Upscale", info="The resolution increase factor", minimum=1, maximum=8, value=1, step=1)
269
  edm_steps = gr.Slider(label="Steps", info="lower=faster, higher=more details", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
270
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
271
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
272
- s_stage2 = gr.Slider(label="Stage2 Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
273
- s_stage1 = gr.Slider(label="Stage1 Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
274
  seed = gr.Slider(label="Seed", info="-1=Different each time, other=Reproducible", minimum=-1, maximum=2147483647, step=1, randomize=True)
275
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
276
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
@@ -292,7 +305,7 @@ with gr.Blocks(title='SUPIR') as interface:
292
  spt_linear_CFG = gr.Slider(label="CFG Start", minimum=1.0,
293
  maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
294
  with gr.Column():
295
- linear_s_stage2 = gr.Checkbox(label="Linear Stage2 Guidance", value=False)
296
  spt_linear_s_stage2 = gr.Slider(label="Guidance Start", minimum=0.,
297
  maximum=1., value=0., step=0.05)
298
  with gr.Row():
@@ -310,18 +323,18 @@ with gr.Blocks(title='SUPIR') as interface:
310
  interactive=True)
311
 
312
  with gr.Column():
313
- gr.Markdown("<center>Stage2 Output</center>")
314
  if not args.use_image_slider:
315
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery1")
316
  else:
317
  result_gallery = ImageSlider(label='Output', show_label=False, elem_id="gallery1")
318
  with gr.Row():
319
  with gr.Column():
320
- denoise_button = gr.Button(value="Stage1 Run (out of work)")
321
  with gr.Column():
322
- llave_button = gr.Button(value="LlaVa Run")
323
  with gr.Column():
324
- diffusion_button = gr.Button(value="Stage2 Run (actual upscaling)", variant = "primary")
325
  with gr.Row():
326
  with gr.Column():
327
  param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="Param Setting", value="Quality")
 
72
  if torch.cuda.device_count() == 0:
73
  gr.Warning('Set this space to GPU config to make it work.')
74
  return None
75
+ print ("stage1_process 1")
76
  torch.cuda.set_device(SUPIR_device)
77
+ print ("stage1_process 2")
78
  LQ = HWC3(input_image)
79
+ print ("stage1_process 3")
80
  LQ = fix_resize(LQ, 512)
81
+ print ("stage1_process 4")
82
  # stage1
83
  LQ = np.array(LQ) / 255 * 2 - 1
84
+ print ("stage1_process 5")
85
  LQ = torch.tensor(LQ, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(SUPIR_device)[:, :3, :, :]
86
+ print ("stage1_process 6")
87
  LQ = model.batchify_denoise(LQ, is_stage1=True)
88
+ print ("stage1_process 7")
89
  LQ = (LQ[0].permute(1, 2, 0) * 127.5 + 127.5).cpu().numpy().round().clip(0, 255).astype(np.uint8)
90
  # gamma correction
91
  LQ = LQ / 255.0
92
+ print ("stage1_process 8")
93
  LQ = np.power(LQ, gamma_correction)
94
+ print ("stage1_process 9")
95
  LQ *= 255.0
96
+ print ("stage1_process 10")
97
  LQ = LQ.round().clip(0, 255).astype(np.uint8)
98
+ print ("stage1_process 11")
99
  return LQ
100
 
101
  @spaces.GPU(duration=120)
 
176
  Image.fromarray(result).save(f'./history/{event_id[:5]}/{event_id[5:]}/HQ_{i}.png')
177
  return [input_image] + results, event_id, 3, ''
178
 
179
+ @spaces.GPU(duration=120)
180
  def load_and_reset(param_setting):
181
  if torch.cuda.device_count() == 0:
182
  gr.Warning('Set this space to GPU config to make it work.')
 
227
 
228
  <p style="background-color: red;"><big><big><big><b>⚠️To use SUPIR, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR?duplicate=true">Duplicate this space</a> and set a GPU with 30 GB VRAM.</b>
229
 
230
+ You can't use SUPIR directly here because this space runs on a CPU, which is not enough for SUPIR. This is a template space. Please provide feedback if you have issues.
231
  </big></big></big></p>
232
  """
233
  else:
 
236
 
237
  ⚠️SUPIR is still a research project under tested and is not yet a stable commercial product.
238
 
239
+ <a href="https://arxiv.org/abs/2401.13627">Paper</a> &emsp; <a href="http://supir.xpixel.group/">Project Page</a> &emsp; <a href="https://github.com/Fanghua-Yu/SUPIR/blob/master/assets/DemoGuide.png">How to play</a> &emsp; <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a>
240
+ <p style="background-color: blue;">For now, only the stage 2 is working (the most important one). The stage 1 and LLaVa are failing. LLaVa is disabled.</p>
241
  """
242
 
243
 
 
255
  with gr.Blocks(title='SUPIR') as interface:
256
  with gr.Row():
257
  gr.HTML(title_md)
258
+
259
  with gr.Row():
260
  with gr.Column():
261
  with gr.Row(equal_height=True):
 
263
  gr.Markdown("<center>Input</center>")
264
  input_image = gr.Image(type="numpy", elem_id="image-input", height=400, width=400)
265
  with gr.Column():
266
+ gr.Markdown("<center>Pre-denoising Output</center>")
267
  denoise_image = gr.Image(type="numpy", elem_id="image-s1", height=400, width=400)
268
+ prompt = gr.Textbox(label="Image description", value="", placeholder="A person, walking, in a town, Summer, photorealistic")
269
 
270
+ with gr.Accordion("Pre-denoising options", open=False):
271
  gamma_correction = gr.Slider(label="Gamma Correction", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
272
 
273
  with gr.Accordion("LLaVA options", open=False):
 
275
  top_p = gr.Slider(label="Top P", info = "Percent of tokens shortlisted", minimum=0., maximum=1.0, value=0.7, step=0.1)
276
  qs = gr.Textbox(label="Question", info="Describe the image and its style in a very detailed manner", placeholder="The image is a realistic photography, not an art painting.")
277
 
278
+ with gr.Accordion("Restoring options", open=False):
279
  num_samples = gr.Slider(label="Num Samples", info="Number of generated results; I discourage to increase because the process is limited to 2 min", minimum=1, maximum=4 if not args.use_image_slider else 1
280
  , value=1, step=1)
281
  upscale = gr.Slider(label="Upscale", info="The resolution increase factor", minimum=1, maximum=8, value=1, step=1)
282
  edm_steps = gr.Slider(label="Steps", info="lower=faster, higher=more details", minimum=1, maximum=200, value=default_setting.edm_steps if torch.cuda.device_count() > 0 else 1, step=1)
283
  s_cfg = gr.Slider(label="Text Guidance Scale", info="lower=follow the image, higher=follow the prompt", minimum=1.0, maximum=15.0,
284
  value=default_setting.s_cfg_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.1)
285
+ s_stage2 = gr.Slider(label="Restoring Guidance Strength", minimum=0., maximum=1., value=1., step=0.05)
286
+ s_stage1 = gr.Slider(label="Pre-denoising Guidance Strength", minimum=-1.0, maximum=6.0, value=-1.0, step=1.0)
287
  seed = gr.Slider(label="Seed", info="-1=Different each time, other=Reproducible", minimum=-1, maximum=2147483647, step=1, randomize=True)
288
  s_churn = gr.Slider(label="S-Churn", minimum=0, maximum=40, value=5, step=1)
289
  s_noise = gr.Slider(label="S-Noise", minimum=1.0, maximum=1.1, value=1.003, step=0.001)
 
305
  spt_linear_CFG = gr.Slider(label="CFG Start", minimum=1.0,
306
  maximum=9.0, value=default_setting.spt_linear_CFG_Quality if torch.cuda.device_count() > 0 else 1.0, step=0.5)
307
  with gr.Column():
308
+ linear_s_stage2 = gr.Checkbox(label="Linear Restoring Guidance", value=False)
309
  spt_linear_s_stage2 = gr.Slider(label="Guidance Start", minimum=0.,
310
  maximum=1., value=0., step=0.05)
311
  with gr.Row():
 
323
  interactive=True)
324
 
325
  with gr.Column():
326
+ gr.Markdown("<center>Restoring Output</center>")
327
  if not args.use_image_slider:
328
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery1")
329
  else:
330
  result_gallery = ImageSlider(label='Output', show_label=False, elem_id="gallery1")
331
  with gr.Row():
332
  with gr.Column():
333
+ denoise_button = gr.Button(value="Pre-denoise (KO)")
334
  with gr.Column():
335
+ llave_button = gr.Button(value="Auto-generate description (LlaVa)")
336
  with gr.Column():
337
+ diffusion_button = gr.Button(value="🚀 Restore", variant = "primary")
338
  with gr.Row():
339
  with gr.Column():
340
  param_setting = gr.Radio(["Quality", "Fidelity"], interactive=True, label="Param Setting", value="Quality")