forked from PaddlePaddle/PaddleMIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradio_hed2image.py
147 lines (133 loc) · 5.06 KB
/
gradio_hed2image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import cv2
import gradio as gr
import paddle
from annotator.hed import HEDdetector
from annotator.util import HWC3, resize_image
from paddlenlp.trainer import set_seed as seed_everything
from ppdiffusers import ControlNetModel, StableDiffusionControlNetPipeline
apply_hed = HEDdetector()
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-hed")
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
def process(
input_image,
prompt,
a_prompt,
n_prompt,
num_samples,
image_resolution,
detect_resolution,
ddim_steps,
guess_mode,
strength,
scale,
seed,
eta,
):
with paddle.no_grad():
input_image = HWC3(input_image)
detected_map = apply_hed(resize_image(input_image, detect_resolution))
detected_map = HWC3(detected_map)
img = resize_image(input_image, image_resolution)
H, W, C = img.shape
detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)
control = paddle.to_tensor(detected_map.copy(), dtype=paddle.float32) / 255.0
control = control.unsqueeze(0).transpose([0, 3, 1, 2])
control_scales = strength * (0.825 ** float(12)) if guess_mode else float(strength)
if seed == -1:
seed = random.randint(0, 65535)
seed_everything(seed)
results = []
for _ in range(num_samples):
img = pipe(
prompt + ", " + a_prompt,
negative_prompt=n_prompt,
image=control,
num_inference_steps=ddim_steps,
height=H,
width=W,
eta=eta,
controlnet_conditioning_scale=control_scales,
guidance_scale=scale,
).images[0]
results.append(img)
return [detected_map] + results
block = gr.Blocks().queue()
with block:
with gr.Row():
gr.Markdown("## Control Stable Diffusion with HED Maps")
with gr.Row():
with gr.Column():
input_image = gr.Image(source="upload", type="numpy")
prompt = gr.Textbox(label="Prompt")
run_button = gr.Button(label="Run")
with gr.Accordion("Advanced options", open=False):
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
image_resolution = gr.Slider(
label="Image Resolution",
minimum=256,
maximum=768,
value=512,
step=64,
)
strength = gr.Slider(
label="Control Strength",
minimum=0.0,
maximum=2.0,
value=1.0,
step=0.01,
)
guess_mode = gr.Checkbox(label="Guess Mode", value=False)
detect_resolution = gr.Slider(label="HED Resolution", minimum=128, maximum=1024, value=512, step=1)
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
scale = gr.Slider(
label="Guidance Scale",
minimum=0.1,
maximum=30.0,
value=9.0,
step=0.1,
)
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
eta = gr.Number(label="eta (DDIM)", value=0.0)
a_prompt = gr.Textbox(label="Added Prompt", value="best quality, extremely detailed")
n_prompt = gr.Textbox(
label="Negative Prompt",
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
)
with gr.Column():
result_gallery = gr.Gallery(label="Output", show_label=False, elem_id="gallery").style(
grid=2, height="auto"
)
ips = [
input_image,
prompt,
a_prompt,
n_prompt,
num_samples,
image_resolution,
detect_resolution,
ddim_steps,
guess_mode,
strength,
scale,
seed,
eta,
]
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
block.launch(server_name="0.0.0.0", server_port=8235)