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|
2 |
+
# python demo/fake_gan/run.py
|
3 |
+
import os
|
4 |
+
import random
|
5 |
import gradio as gr
|
6 |
+
import itertools
|
7 |
+
from PIL import Image, ImageFont, ImageDraw
|
8 |
+
import sys
|
9 |
+
|
10 |
+
sys.path.append("source")
|
11 |
+
|
12 |
+
import DirectedDiffusion
|
13 |
+
|
14 |
+
EX1 = [
|
15 |
+
"A painting of a tiger, on the wall in the living room",
|
16 |
+
"0.2,0.6,0.0,0.5",
|
17 |
+
"1,5",
|
18 |
+
5,
|
19 |
+
15,
|
20 |
+
1.0,
|
21 |
+
2094889,
|
22 |
+
]
|
23 |
+
|
24 |
+
|
25 |
+
def fake_gan(a, b, c):
|
26 |
+
print(a, b, c)
|
27 |
+
images = [
|
28 |
+
(
|
29 |
+
random.choice(
|
30 |
+
[
|
31 |
+
"https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
|
32 |
+
"https://images.unsplash.com/photo-1554151228-14d9def656e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=386&q=80",
|
33 |
+
"https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW4lMjBmYWNlfGVufDB8fDB8fA%3D%3D&w=1000&q=80",
|
34 |
+
"https://images.unsplash.com/photo-1546456073-92b9f0a8d413?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
|
35 |
+
"https://images.unsplash.com/photo-1601412436009-d964bd02edbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80",
|
36 |
+
]
|
37 |
+
),
|
38 |
+
f"label {i}" if i != 0 else "label" * 50,
|
39 |
+
)
|
40 |
+
for i in range(3)
|
41 |
+
]
|
42 |
+
return images
|
43 |
+
|
44 |
+
|
45 |
+
model_bundle = DirectedDiffusion.AttnEditorUtils.load_all_models(
|
46 |
+
model_path_diffusion="CompVis/stable-diffusion-v1-4"
|
47 |
+
)
|
48 |
+
|
49 |
+
|
50 |
+
def directed_diffusion(
|
51 |
+
in_prompt,
|
52 |
+
in_bb,
|
53 |
+
in_token_ids,
|
54 |
+
in_slider_trailings,
|
55 |
+
in_slider_ddsteps,
|
56 |
+
in_slider_gcoef,
|
57 |
+
in_seed,
|
58 |
+
is_draw_bbox,
|
59 |
+
):
|
60 |
+
str_arg_to_val = lambda arg, f: [
|
61 |
+
[f(b) for b in a.split(",")] for a in arg.split(" ")
|
62 |
+
]
|
63 |
+
roi = str_arg_to_val(in_bb, float)
|
64 |
+
attn_editor_bundle = {
|
65 |
+
"edit_index": str_arg_to_val(in_token_ids, int),
|
66 |
+
"roi": roi,
|
67 |
+
"num_trailing_attn": [in_slider_trailings] * len(roi),
|
68 |
+
"num_affected_steps": in_slider_ddsteps,
|
69 |
+
"noise_scale": [in_slider_gcoef] * len(roi),
|
70 |
+
}
|
71 |
+
img = DirectedDiffusion.Diffusion.stablediffusion(
|
72 |
+
model_bundle,
|
73 |
+
attn_editor_bundle=attn_editor_bundle,
|
74 |
+
guidance_scale=7.5,
|
75 |
+
prompt=in_prompt,
|
76 |
+
steps=50,
|
77 |
+
seed=in_seed,
|
78 |
+
is_save_attn=False,
|
79 |
+
is_save_recons=False,
|
80 |
+
)
|
81 |
+
print(img.size)
|
82 |
+
if is_draw_bbox and in_slider_ddsteps > 0:
|
83 |
+
for r in roi:
|
84 |
+
x0, y0, x1, y1 = [int(r_ * 512) for r_ in r]
|
85 |
+
print(x0, y0, x1, y1)
|
86 |
+
image_editable = ImageDraw.Draw(img)
|
87 |
+
image_editable.rectangle(
|
88 |
+
xy=[x0, y0, x1, y1], outline=(255, 0, 0, 255), width=5
|
89 |
+
)
|
90 |
+
|
91 |
+
return img
|
92 |
+
|
93 |
+
|
94 |
+
def run_it(
|
95 |
+
in_prompt,
|
96 |
+
in_bb,
|
97 |
+
in_token_ids,
|
98 |
+
in_slider_trailings,
|
99 |
+
in_slider_ddsteps,
|
100 |
+
in_slider_gcoef,
|
101 |
+
in_seed,
|
102 |
+
is_draw_bbox,
|
103 |
+
is_grid_search,
|
104 |
+
progress=gr.Progress(),
|
105 |
+
):
|
106 |
+
|
107 |
+
num_affected_steps = [in_slider_ddsteps]
|
108 |
+
noise_scale = [in_slider_gcoef]
|
109 |
+
num_trailing_attn = [in_slider_trailings]
|
110 |
+
if is_grid_search:
|
111 |
+
num_affected_steps = [5, 10]
|
112 |
+
#noise_scale = [1.0, 1.5, 2.5]
|
113 |
+
#num_trailing_attn = [10, 20, 30, 40]
|
114 |
+
|
115 |
+
param_list = [num_affected_steps, noise_scale, num_trailing_attn]
|
116 |
+
param_list = list(itertools.product(*param_list))
|
117 |
+
|
118 |
+
results = []
|
119 |
+
progress(0, desc="Starting...")
|
120 |
+
for i, element in enumerate(progress.tqdm(param_list)):
|
121 |
+
print("=========== Arguments ============")
|
122 |
+
print("Prompt:", in_prompt)
|
123 |
+
print("BoundingBox:", in_bb)
|
124 |
+
print("Token indices:", in_token_ids)
|
125 |
+
print("Num Trialings:", element[2])
|
126 |
+
print("Num DD steps:", element[0])
|
127 |
+
print("Gaussian coef:", element[1])
|
128 |
+
print("Seed:", in_seed)
|
129 |
+
print("===================================")
|
130 |
+
img = directed_diffusion(
|
131 |
+
in_prompt=in_prompt,
|
132 |
+
in_bb=in_bb,
|
133 |
+
in_token_ids=in_token_ids,
|
134 |
+
in_slider_trailings=element[2],
|
135 |
+
in_slider_ddsteps=element[0],
|
136 |
+
in_slider_gcoef=element[1],
|
137 |
+
in_seed=in_seed,
|
138 |
+
is_draw_bbox=is_draw_bbox,
|
139 |
+
)
|
140 |
+
results.append(
|
141 |
+
(
|
142 |
+
img,
|
143 |
+
"#Trailing:{},#DDSteps:{},GaussianCoef:{}".format(
|
144 |
+
element[2], element[0], element[1]
|
145 |
+
),
|
146 |
+
)
|
147 |
+
)
|
148 |
+
return results
|
149 |
+
|
150 |
+
|
151 |
+
with gr.Blocks() as demo:
|
152 |
+
with gr.Row(variant="panel"):
|
153 |
+
with gr.Column(variant="compact"):
|
154 |
+
in_prompt = gr.Textbox(
|
155 |
+
label="Enter your prompt",
|
156 |
+
show_label=False,
|
157 |
+
max_lines=1,
|
158 |
+
placeholder="Enter your prompt",
|
159 |
+
).style(
|
160 |
+
container=False,
|
161 |
+
)
|
162 |
+
with gr.Row(variant="compact"):
|
163 |
+
in_bb = gr.Textbox(
|
164 |
+
label="Bounding box",
|
165 |
+
show_label=True,
|
166 |
+
max_lines=1,
|
167 |
+
placeholder="e.g., 0.1,0.5,0.3,0.6",
|
168 |
+
)
|
169 |
+
in_token_ids = gr.Textbox(
|
170 |
+
label="Token idices",
|
171 |
+
show_label=True,
|
172 |
+
max_lines=1,
|
173 |
+
placeholder="e.g., 1,2,3",
|
174 |
+
)
|
175 |
+
in_seed = gr.Number(
|
176 |
+
value=2483964026821236, label="Random seed", interactive=True
|
177 |
+
)
|
178 |
+
with gr.Row(variant="compact"):
|
179 |
+
is_grid_search = gr.Checkbox(
|
180 |
+
value=False,
|
181 |
+
label="Grid search? (Checked then sliders are ignored)",
|
182 |
+
)
|
183 |
+
is_draw_bbox = gr.Checkbox(
|
184 |
+
value=True,
|
185 |
+
label="To draw the bounding box?",
|
186 |
+
)
|
187 |
+
with gr.Row(variant="compact"):
|
188 |
+
in_slider_trailings = gr.Slider(
|
189 |
+
minimum=1, maximum=30, value=10, step=1, label="#trailings"
|
190 |
+
)
|
191 |
+
in_slider_ddsteps = gr.Slider(
|
192 |
+
minimum=0, maximum=20, value=10, step=1, label="#DDSteps"
|
193 |
+
)
|
194 |
+
in_slider_gcoef = gr.Slider(
|
195 |
+
minimum=1, maximum=5, value=1.0, step=0.1, label="GaussianCoef"
|
196 |
+
)
|
197 |
+
btn = gr.Button("Generate image").style(full_width=False)
|
198 |
+
|
199 |
+
gallery = gr.Gallery(
|
200 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
201 |
+
).style(grid=[2], height="auto")
|
202 |
+
|
203 |
+
args = [
|
204 |
+
in_prompt,
|
205 |
+
in_bb,
|
206 |
+
in_token_ids,
|
207 |
+
in_slider_trailings,
|
208 |
+
in_slider_ddsteps,
|
209 |
+
in_slider_gcoef,
|
210 |
+
in_seed,
|
211 |
+
is_draw_bbox,
|
212 |
+
is_grid_search,
|
213 |
+
]
|
214 |
+
|
215 |
+
btn.click(
|
216 |
+
run_it,
|
217 |
+
inputs=args,
|
218 |
+
outputs=gallery,
|
219 |
+
)
|
220 |
|
221 |
+
examples = gr.Examples(
|
222 |
+
examples=[EX1],
|
223 |
+
inputs=args,
|
224 |
+
)
|
225 |
|
226 |
+
if __name__ == "__main__":
|
227 |
+
demo.queue().launch(share=True)
|