Skip to content

Commit 83ce026

Browse files
committed
On branch ControlNet-v1-1
Changes to be committed: new file: ../../ppdiffusers/examples/controlnet/gradio_ip2p2image.py new file: ../../ppdiffusers/examples/controlnet/gradio_mlsd2image.py
1 parent dd11b56 commit 83ce026

File tree

2 files changed

+256
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)