zhiweili commited on
Commit
760912a
1 Parent(s): 125fda8

change control model

Browse files
Files changed (2) hide show
  1. app_haircolor_15.py +158 -0
  2. app_haircolor_inpaint_15.py +24 -11
app_haircolor_15.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import time
4
+ import torch
5
+ import numpy as np
6
+
7
+ from PIL import Image
8
+ from segment_utils import(
9
+ segment_image_withmask,
10
+ restore_result,
11
+ )
12
+ from diffusers import (
13
+ StableDiffusionControlNetImg2ImgPipeline,
14
+ ControlNetModel,
15
+ DDIMScheduler,
16
+ )
17
+
18
+ from controlnet_aux import (
19
+ CannyDetector,
20
+ )
21
+
22
+ BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
23
+
24
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
25
+
26
+ DEFAULT_EDIT_PROMPT = "a woman, blue hair, high detailed"
27
+ DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face"
28
+
29
+ DEFAULT_CATEGORY = "hair"
30
+
31
+ canny_detector = CannyDetector()
32
+
33
+ controlnet = [
34
+ ControlNetModel.from_pretrained(
35
+ "lllyasviel/control_v11p_sd15_inpaint",
36
+ torch_dtype=torch.float16,
37
+ ),
38
+ ControlNetModel.from_pretrained(
39
+ "diffusers/controlnet-canny-sdxl-1.0",
40
+ torch_dtype=torch.float16,
41
+ ),
42
+ ]
43
+
44
+ basepipeline = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
45
+ BASE_MODEL,
46
+ torch_dtype=torch.float16,
47
+ use_safetensors=True,
48
+ controlnet=controlnet,
49
+ )
50
+ basepipeline.scheduler = DDIMScheduler.from_config(basepipeline.scheduler.config)
51
+
52
+ basepipeline = basepipeline.to(DEVICE)
53
+
54
+ basepipeline.enable_model_cpu_offload()
55
+
56
+ @spaces.GPU(duration=30)
57
+ def image_to_image(
58
+ input_image: Image,
59
+ mask_image: Image,
60
+ edit_prompt: str,
61
+ seed: int,
62
+ num_steps: int,
63
+ guidance_scale: float,
64
+ generate_size: int,
65
+ lineart_scale: float = 1.0,
66
+ hed_scale: float = 0.5,
67
+ ):
68
+ run_task_time = 0
69
+ time_cost_str = ''
70
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
71
+ canny_image = canny_detector(input_image, int(generate_size*0.375), generate_size)
72
+ inpaint_image = make_inpaint_condition(input_image, mask_image)
73
+ control_image = [inpaint_image, canny_image]
74
+
75
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
76
+ generated_image = basepipeline(
77
+ generator=generator,
78
+ prompt=edit_prompt,
79
+ negative_prompt=DEFAULT_NEGATIVE_PROMPT,
80
+ image=input_image,
81
+ mask_image=mask_image,
82
+ control_image=control_image,
83
+ guidance_scale=guidance_scale,
84
+ num_inference_steps=num_steps,
85
+ controlnet_conditioning_scale=[inpaint_scale, canny_scale]
86
+ ).images[0]
87
+
88
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
89
+
90
+ return generated_image, time_cost_str
91
+
92
+ def make_inpaint_condition(image, image_mask):
93
+ image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
94
+ image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
95
+
96
+ assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
97
+ image[image_mask > 0.5] = -1.0 # set as masked pixel
98
+ image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
99
+ image = torch.from_numpy(image)
100
+ return image
101
+
102
+ def get_time_cost(run_task_time, time_cost_str):
103
+ now_time = int(time.time()*1000)
104
+ if run_task_time == 0:
105
+ time_cost_str = 'start'
106
+ else:
107
+ if time_cost_str != '':
108
+ time_cost_str += f'-->'
109
+ time_cost_str += f'{now_time - run_task_time}'
110
+ run_task_time = now_time
111
+ return run_task_time, time_cost_str
112
+
113
+ def create_demo() -> gr.Blocks:
114
+ with gr.Blocks() as demo:
115
+ croper = gr.State()
116
+ with gr.Row():
117
+ with gr.Column():
118
+ edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
119
+ generate_size = gr.Number(label="Generate Size", value=512)
120
+ with gr.Column():
121
+ num_steps = gr.Slider(minimum=1, maximum=100, value=15, step=1, label="Num Steps")
122
+ guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
123
+ with gr.Column():
124
+ with gr.Accordion("Advanced Options", open=False):
125
+ inpaint_scale = gr.Slider(minimum=0, maximum=3, value=1, step=0.1, label="Inpaint Scale")
126
+ canny_scale = gr.Slider(minimum=0, maximum=3, value=0.8, step=0.1, label="Canny Scale")
127
+ mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
128
+ mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
129
+ seed = gr.Number(label="Seed", value=8)
130
+ category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
131
+ g_btn = gr.Button("Edit Image")
132
+
133
+ with gr.Row():
134
+ with gr.Column():
135
+ input_image = gr.Image(label="Input Image", type="pil")
136
+ with gr.Column():
137
+ restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
138
+ with gr.Column():
139
+ origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
140
+ generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
141
+ generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
142
+ mask_image = gr.Image(label="Mask Image", type="pil", interactive=False)
143
+
144
+ g_btn.click(
145
+ fn=segment_image_withmask,
146
+ inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
147
+ outputs=[origin_area_image, mask_image, croper],
148
+ ).success(
149
+ fn=image_to_image,
150
+ inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, inpaint_scale, canny_scale],
151
+ outputs=[generated_image, generated_cost],
152
+ ).success(
153
+ fn=restore_result,
154
+ inputs=[croper, category, generated_image],
155
+ outputs=[restored_image],
156
+ )
157
+
158
+ return demo
app_haircolor_inpaint_15.py CHANGED
@@ -17,6 +17,9 @@ from diffusers import (
17
 
18
  from controlnet_aux import (
19
  CannyDetector,
 
 
 
20
  )
21
 
22
  BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
@@ -29,14 +32,22 @@ DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res,
29
  DEFAULT_CATEGORY = "hair"
30
 
31
  canny_detector = CannyDetector()
 
 
 
 
 
 
 
 
32
 
33
  controlnet = [
34
  ControlNetModel.from_pretrained(
35
- "lllyasviel/control_v11p_sd15_inpaint",
36
  torch_dtype=torch.float16,
37
  ),
38
  ControlNetModel.from_pretrained(
39
- "lllyasviel/control_v11p_sd15_canny",
40
  torch_dtype=torch.float16,
41
  ),
42
  ]
@@ -62,15 +73,17 @@ def image_to_image(
62
  num_steps: int,
63
  guidance_scale: float,
64
  generate_size: int,
65
- inpaint_scale: float = 1.0,
66
- canny_scale: float = 0.5,
67
  ):
68
  run_task_time = 0
69
  time_cost_str = ''
70
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
71
- canny_image = canny_detector(input_image, int(generate_size*0.375), generate_size)
72
- inpaint_image = make_inpaint_condition(input_image, mask_image)
73
- control_image = [inpaint_image, canny_image]
 
 
74
 
75
  generator = torch.Generator(device=DEVICE).manual_seed(seed)
76
  generated_image = basepipeline(
@@ -84,7 +97,7 @@ def image_to_image(
84
  width=generate_size,
85
  guidance_scale=guidance_scale,
86
  num_inference_steps=num_steps,
87
- controlnet_conditioning_scale=[inpaint_scale, canny_scale]
88
  ).images[0]
89
 
90
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
@@ -124,8 +137,8 @@ def create_demo() -> gr.Blocks:
124
  guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
125
  with gr.Column():
126
  with gr.Accordion("Advanced Options", open=False):
127
- inpaint_scale = gr.Slider(minimum=0, maximum=3, value=1, step=0.1, label="Inpaint Scale")
128
- canny_scale = gr.Slider(minimum=0, maximum=3, value=0.8, step=0.1, label="Canny Scale")
129
  mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
130
  mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
131
  seed = gr.Number(label="Seed", value=8)
@@ -149,7 +162,7 @@ def create_demo() -> gr.Blocks:
149
  outputs=[origin_area_image, mask_image, croper],
150
  ).success(
151
  fn=image_to_image,
152
- inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, inpaint_scale, canny_scale],
153
  outputs=[generated_image, generated_cost],
154
  ).success(
155
  fn=restore_result,
 
17
 
18
  from controlnet_aux import (
19
  CannyDetector,
20
+ LineartDetector,
21
+ PidiNetDetector,
22
+ HEDdetector,
23
  )
24
 
25
  BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
 
32
  DEFAULT_CATEGORY = "hair"
33
 
34
  canny_detector = CannyDetector()
35
+ lineart_detector = LineartDetector.from_pretrained("lllyasviel/Annotators")
36
+ lineart_detector = lineart_detector.to(DEVICE)
37
+
38
+ pidiNet_detector = PidiNetDetector.from_pretrained('lllyasviel/Annotators')
39
+ pidiNet_detector = pidiNet_detector.to(DEVICE)
40
+
41
+ hed_detector = HEDdetector.from_pretrained('lllyasviel/Annotators')
42
+ hed_detector = hed_detector.to(DEVICE)
43
 
44
  controlnet = [
45
  ControlNetModel.from_pretrained(
46
+ "lllyasviel/control_v11p_sd15_lineart",
47
  torch_dtype=torch.float16,
48
  ),
49
  ControlNetModel.from_pretrained(
50
+ "lllyasviel/control_v11p_sd15_softedge",
51
  torch_dtype=torch.float16,
52
  ),
53
  ]
 
73
  num_steps: int,
74
  guidance_scale: float,
75
  generate_size: int,
76
+ cond_scale1: float = 1.2,
77
+ cond_scale2: float = 1.2,
78
  ):
79
  run_task_time = 0
80
  time_cost_str = ''
81
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
82
+ # canny_image = canny_detector(input_image, int(generate_size*0.375), generate_size)
83
+ lineart_image = lineart_detector(input_image, int(generate_size*0.375), generate_size)
84
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
85
+ pidiNet_image = pidiNet_detector(input_image, int(generate_size*0.5), generate_size)
86
+ control_image = [lineart_image, pidiNet_image]
87
 
88
  generator = torch.Generator(device=DEVICE).manual_seed(seed)
89
  generated_image = basepipeline(
 
97
  width=generate_size,
98
  guidance_scale=guidance_scale,
99
  num_inference_steps=num_steps,
100
+ controlnet_conditioning_scale=[cond_scale1, cond_scale2]
101
  ).images[0]
102
 
103
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
 
137
  guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
138
  with gr.Column():
139
  with gr.Accordion("Advanced Options", open=False):
140
+ cond_scale1 = gr.Slider(minimum=0, maximum=3, value=1.2, step=0.1, label="Lineart Scale")
141
+ cond_scale2 = gr.Slider(minimum=0, maximum=3, value=1.2, step=0.1, label="PidiNet Scale")
142
  mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
143
  mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
144
  seed = gr.Number(label="Seed", value=8)
 
162
  outputs=[origin_area_image, mask_image, croper],
163
  ).success(
164
  fn=image_to_image,
165
+ inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, cond_scale1, cond_scale2],
166
  outputs=[generated_image, generated_cost],
167
  ).success(
168
  fn=restore_result,