zhiweili commited on
Commit
3226a63
·
1 Parent(s): f687b4d

add pre enhance

Browse files
Files changed (4) hide show
  1. app_base.py +16 -13
  2. enhance_utils.py +7 -41
  3. inversion_run_base.py +4 -0
  4. requirements.txt +2 -1
app_base.py CHANGED
@@ -10,8 +10,8 @@ from segment_utils import(
10
  )
11
  from enhance_utils import enhance_image
12
 
13
- DEFAULT_SRC_PROMPT = "a woman, photo"
14
- DEFAULT_EDIT_PROMPT = "a beautiful woman, photo, hollywood style face, 8k, high quality"
15
 
16
  DEFAULT_CATEGORY = "face"
17
 
@@ -31,13 +31,17 @@ def create_demo() -> gr.Blocks:
31
  start_step: int,
32
  guidance_scale: float,
33
  generate_size: int,
34
- enhance_scale: int,
35
- enhance_face: bool = True,
36
  ):
37
  w2 = 1.0
38
  run_task_time = 0
39
  time_cost_str = ''
40
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
 
 
 
 
41
  run_model = base_run
42
  res_image = run_model(
43
  input_image,
@@ -52,8 +56,7 @@ def create_demo() -> gr.Blocks:
52
  guidance_scale,
53
  )
54
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
55
- enhance_mode = 0 if enhance_face else 2
56
- enhanced_image = enhance_image(res_image, enhance_scale, enhance_mode)
57
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
58
 
59
  return enhanced_image, res_image, time_cost_str
@@ -77,18 +80,18 @@ def create_demo() -> gr.Blocks:
77
  edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
78
  category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
79
  with gr.Column():
80
- num_steps = gr.Slider(minimum=1, maximum=100, value=30, step=1, label="Num Steps")
81
- start_step = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Start Step")
82
  with gr.Accordion("Advanced Options", open=False):
83
  guidance_scale = gr.Slider(minimum=0, maximum=20, value=0, step=0.5, label="Guidance Scale")
84
- generate_size = gr.Number(label="Generate Size", value=1024)
85
  mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
86
  mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
87
- enhance_scale = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="Enhance Scale")
88
- enhance_face = gr.Checkbox(label="Enhance Face", value=False)
89
  with gr.Column():
90
  seed = gr.Number(label="Seed", value=8)
91
- w1 = gr.Number(label="W1", value=2)
92
  g_btn = gr.Button("Edit Image")
93
 
94
  with gr.Row():
@@ -109,7 +112,7 @@ def create_demo() -> gr.Blocks:
109
  outputs=[origin_area_image, croper],
110
  ).success(
111
  fn=image_to_image,
112
- inputs=[origin_area_image, input_image_prompt, edit_prompt,seed,w1, num_steps, start_step, guidance_scale, generate_size, enhance_scale, enhance_face],
113
  outputs=[enhanced_image, generated_image, generated_cost],
114
  ).success(
115
  fn=restore_result,
 
10
  )
11
  from enhance_utils import enhance_image
12
 
13
+ DEFAULT_SRC_PROMPT = "a person"
14
+ DEFAULT_EDIT_PROMPT = "a person with perfect face"
15
 
16
  DEFAULT_CATEGORY = "face"
17
 
 
31
  start_step: int,
32
  guidance_scale: float,
33
  generate_size: int,
34
+ pre_enhance: bool = True,
35
+ pre_enhance_scale: int = 2,
36
  ):
37
  w2 = 1.0
38
  run_task_time = 0
39
  time_cost_str = ''
40
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
41
+ if pre_enhance:
42
+ input_image = enhance_image(input_image, enhance_face=True, scale=pre_enhance_scale)
43
+ input_image = input_image.resize((generate_size, generate_size))
44
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
45
  run_model = base_run
46
  res_image = run_model(
47
  input_image,
 
56
  guidance_scale,
57
  )
58
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
59
+ enhanced_image = enhance_image(res_image)
 
60
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
61
 
62
  return enhanced_image, res_image, time_cost_str
 
80
  edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
81
  category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
82
  with gr.Column():
83
+ num_steps = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Num Steps")
84
+ start_step = gr.Slider(minimum=1, maximum=100, value=30, step=1, label="Start Step")
85
  with gr.Accordion("Advanced Options", open=False):
86
  guidance_scale = gr.Slider(minimum=0, maximum=20, value=0, step=0.5, label="Guidance Scale")
87
+ generate_size = gr.Number(label="Generate Size", value=512)
88
  mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
89
  mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
90
+ pre_enhance = gr.Checkbox(label="Pre Enhance", value=True)
91
+ pre_enhance_scale = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="Pre Enhance Scale")
92
  with gr.Column():
93
  seed = gr.Number(label="Seed", value=8)
94
+ w1 = gr.Number(label="W1", value=1.5)
95
  g_btn = gr.Button("Edit Image")
96
 
97
  with gr.Row():
 
112
  outputs=[origin_area_image, croper],
113
  ).success(
114
  fn=image_to_image,
115
+ inputs=[origin_area_image, input_image_prompt, edit_prompt,seed,w1, num_steps, start_step, guidance_scale, generate_size, pre_enhance, pre_enhance_scale],
116
  outputs=[enhanced_image, generated_image, generated_cost],
117
  ).success(
118
  fn=restore_result,
enhance_utils.py CHANGED
@@ -38,55 +38,21 @@ upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, ti
38
 
39
  face_enhancer = GFPGANer(model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2)
40
 
41
-
42
  def enhance_image(
43
- input_image: Image,
44
- scale: int,
45
- enhance_mode: int,
46
- keep_size: bool = False,
47
  ):
48
- only_face = enhance_mode == 1
49
- enhance_face = enhance_mode != 2
50
-
51
- if enhance_mode == 1:
52
- face_enhancer.upscale = scale
53
- face_enhancer.bg_upsampler = None
54
- elif enhance_mode == 2:
55
- pass
56
- else:
57
- face_enhancer.upscale = scale
58
- face_enhancer.bg_upsampler = upsampler
59
-
60
- img = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR)
61
 
62
  h, w = img.shape[0:2]
63
  if h < 300:
64
  img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
65
-
66
- max_size = 3480 / scale
67
- if h > max_size:
68
- w = int(w * max_size / h)
69
- h = max_size
70
-
71
- if w > max_size:
72
- h = int(h * max_size / w)
73
- w = max_size
74
-
75
- if h != img.shape[0] or w != img.shape[1]:
76
- img = cv2.resize(img, (w, h), interpolation=cv2.INTER_LANCZOS4)
77
-
78
  if enhance_face:
79
- _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=only_face, paste_back=True)
80
  else:
81
- output, _ = upsampler.enhance(img, outscale=scale)
82
-
83
- h, w = img.shape[0:2]
84
- interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
85
- if keep_size:
86
- output = cv2.resize(output, (w, h), interpolation=interpolation)
87
- elif scale != 2:
88
- output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
89
-
90
  pil_output = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
91
 
92
  return pil_output
 
38
 
39
  face_enhancer = GFPGANer(model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2)
40
 
 
41
  def enhance_image(
42
+ pil_image: Image,
43
+ enhance_face: bool = False,
44
+ scale: int = 2,
 
45
  ):
46
+ face_enhancer.upscale = scale
47
+ img = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  h, w = img.shape[0:2]
50
  if h < 300:
51
  img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  if enhance_face:
53
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=True, paste_back=True)
54
  else:
55
+ output, _ = upsampler.enhance(img, outscale=2)
 
 
 
 
 
 
 
 
56
  pil_output = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
57
 
58
  return pil_output
inversion_run_base.py CHANGED
@@ -11,6 +11,8 @@ from config import get_config, get_num_steps_actual
11
  from functools import partial
12
  from compel import Compel, ReturnedEmbeddingsType
13
 
 
 
14
  class Object(object):
15
  pass
16
 
@@ -55,6 +57,8 @@ pipeline.scheduler = DDPMScheduler.from_pretrained(
55
  subfolder="scheduler",
56
  )
57
 
 
 
58
  config = get_config(args)
59
 
60
  compel_proc = Compel(
 
11
  from functools import partial
12
  from compel import Compel, ReturnedEmbeddingsType
13
 
14
+ from hidiffusion import apply_hidiffusion, remove_hidiffusion
15
+
16
  class Object(object):
17
  pass
18
 
 
57
  subfolder="scheduler",
58
  )
59
 
60
+ apply_hidiffusion(pipeline)
61
+
62
  config = get_config(args)
63
 
64
  compel_proc = Compel(
requirements.txt CHANGED
@@ -14,4 +14,5 @@ git+https://github.com/XPixelGroup/BasicSR@master
14
  facexlib
15
  realesrgan
16
  controlnet_aux
17
- peft
 
 
14
  facexlib
15
  realesrgan
16
  controlnet_aux
17
+ peft
18
+ hidiffusion