Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,7 +76,10 @@ def show_images(batch: th.Tensor):
|
|
| 76 |
display(Image.fromarray(reshaped.numpy()))
|
| 77 |
|
| 78 |
|
| 79 |
-
def compose_language_descriptions(prompt, guidance_scale):
|
|
|
|
|
|
|
|
|
|
| 80 |
# @markdown `prompt`: when composing multiple sentences, using `|` as the delimiter.
|
| 81 |
prompts = [x.strip() for x in prompt.split('|')]
|
| 82 |
|
|
@@ -240,12 +243,15 @@ clevr_model.load_state_dict(th.load(download_model('clevr_pos'), device))
|
|
| 240 |
print('total clevr_pos parameters', sum(x.numel() for x in clevr_model.parameters()))
|
| 241 |
|
| 242 |
|
| 243 |
-
def compose_clevr_objects(prompt, guidance_scale):
|
| 244 |
coordinates = [[float(x.split(',')[0].strip()), float(x.split(',')[1].strip())]
|
| 245 |
for x in prompt.split('|')]
|
| 246 |
coordinates += [[-1, -1]] # add unconditional score label
|
| 247 |
batch_size = 1
|
| 248 |
|
|
|
|
|
|
|
|
|
|
| 249 |
def model_fn(x_t, ts, **kwargs):
|
| 250 |
half = x_t[:1]
|
| 251 |
combined = th.cat([half] * kwargs['y'].size(0), dim=0)
|
|
@@ -281,24 +287,23 @@ def compose_clevr_objects(prompt, guidance_scale):
|
|
| 281 |
out_img = (out_img + 1) / 2
|
| 282 |
out_img = (out_img.detach().cpu() * 255.).to(th.uint8)
|
| 283 |
out_img = out_img.numpy()
|
| 284 |
-
Image.fromarray(out_img).convert('RGB').save('test.png')
|
| 285 |
|
| 286 |
return out_img
|
| 287 |
|
| 288 |
|
| 289 |
-
def stable_diffusion_compose(prompt, scale):
|
| 290 |
with autocast('cpu' if not has_cuda else 'cuda'):
|
| 291 |
-
image = pipe(prompt, guidance_scale=scale)["sample"][0]
|
| 292 |
return image
|
| 293 |
|
| 294 |
|
| 295 |
-
def compose(prompt, version, guidance_scale):
|
| 296 |
if version == 'GLIDE':
|
| 297 |
-
return compose_language_descriptions(prompt, guidance_scale)
|
| 298 |
elif version == 'Stable_Diffusion_1v_4':
|
| 299 |
-
return stable_diffusion_compose(prompt, guidance_scale)
|
| 300 |
else:
|
| 301 |
-
return compose_clevr_objects(prompt, guidance_scale)
|
| 302 |
|
| 303 |
|
| 304 |
examples_1 = 'a camel | a forest'
|
|
@@ -309,20 +314,28 @@ examples_5 = 'a white church on a hill | birds flying around the church'
|
|
| 309 |
examples_6 = 'a boat in a desert | a pink sky'
|
| 310 |
examples_7 = 'mountains in the background | a blue sky | cows on a pasture'
|
| 311 |
examples = [
|
| 312 |
-
[examples_7, 'Stable_Diffusion_1v_4', 10],
|
| 313 |
-
[examples_4, 'Stable_Diffusion_1v_4', 10],
|
| 314 |
-
[examples_5, 'Stable_Diffusion_1v_4', 10],
|
| 315 |
-
[examples_6, 'Stable_Diffusion_1v_4', 10],
|
| 316 |
-
[examples_1, 'GLIDE', 10],
|
| 317 |
-
[examples_2, 'GLIDE', 10],
|
| 318 |
-
[examples_3, 'CLEVR Objects', 10]
|
|
|
|
| 319 |
|
| 320 |
import gradio as gr
|
| 321 |
|
| 322 |
title = 'Compositional Visual Generation with Composable Diffusion Models'
|
| 323 |
-
description = '<p>Demo for Composable Diffusion<ul><li>~30s per GLIDE/Stable-Diffusion example</li><li>~10s per CLEVR Object example</li>(<b>Note</b>: time is varied depending on what gpu is used.)</ul></p><p>See more information from our <a href="https://energy-based-model.github.io/Compositional-Visual-Generation-with-Composable-Diffusion-Models/">Project Page</a>.</p><ul><li>One version is based on the released <a href="https://github.com/openai/glide-text2im">GLIDE</a> and <a href="https://github.com/CompVis/stable-diffusion/">Stable Diffusion</a> for composing natural language description.</li><li>Another is based on our pre-trained CLEVR Object Model for composing objects. <br>(<b>Note</b>: We recommend using <b><i>x</i></b> in range <b><i>[0.1, 0.9]</i></b> and <b><i>y</i></b> in range <b><i>[0.25, 0.7]</i></b>, since the training dataset labels are in given ranges.)</li></ul><p>When composing multiple sentences, use `|` as the delimiter, see given examples below.</p>'
|
| 324 |
-
|
| 325 |
-
iface = gr.Interface(compose,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
title=title, description=description, examples=examples)
|
| 327 |
|
| 328 |
iface.launch()
|
|
|
|
| 76 |
display(Image.fromarray(reshaped.numpy()))
|
| 77 |
|
| 78 |
|
| 79 |
+
def compose_language_descriptions(prompt, guidance_scale, steps):
|
| 80 |
+
options['timestep_respacing'] = str(steps)
|
| 81 |
+
_, diffusion = create_model_and_diffusion(**options)
|
| 82 |
+
|
| 83 |
# @markdown `prompt`: when composing multiple sentences, using `|` as the delimiter.
|
| 84 |
prompts = [x.strip() for x in prompt.split('|')]
|
| 85 |
|
|
|
|
| 243 |
print('total clevr_pos parameters', sum(x.numel() for x in clevr_model.parameters()))
|
| 244 |
|
| 245 |
|
| 246 |
+
def compose_clevr_objects(prompt, guidance_scale, steps):
|
| 247 |
coordinates = [[float(x.split(',')[0].strip()), float(x.split(',')[1].strip())]
|
| 248 |
for x in prompt.split('|')]
|
| 249 |
coordinates += [[-1, -1]] # add unconditional score label
|
| 250 |
batch_size = 1
|
| 251 |
|
| 252 |
+
clevr_options['timestep_respacing'] = str(int(steps))
|
| 253 |
+
_, clevr_diffusion = create_model_and_diffusion_for_clevr(**clevr_options)
|
| 254 |
+
|
| 255 |
def model_fn(x_t, ts, **kwargs):
|
| 256 |
half = x_t[:1]
|
| 257 |
combined = th.cat([half] * kwargs['y'].size(0), dim=0)
|
|
|
|
| 287 |
out_img = (out_img + 1) / 2
|
| 288 |
out_img = (out_img.detach().cpu() * 255.).to(th.uint8)
|
| 289 |
out_img = out_img.numpy()
|
|
|
|
| 290 |
|
| 291 |
return out_img
|
| 292 |
|
| 293 |
|
| 294 |
+
def stable_diffusion_compose(prompt, scale, steps):
|
| 295 |
with autocast('cpu' if not has_cuda else 'cuda'):
|
| 296 |
+
image = pipe(prompt, guidance_scale=scale, num_inference_steps=steps)["sample"][0]
|
| 297 |
return image
|
| 298 |
|
| 299 |
|
| 300 |
+
def compose(prompt, version, guidance_scale, steps):
|
| 301 |
if version == 'GLIDE':
|
| 302 |
+
return compose_language_descriptions(prompt, guidance_scale, steps)
|
| 303 |
elif version == 'Stable_Diffusion_1v_4':
|
| 304 |
+
return stable_diffusion_compose(prompt, guidance_scale, steps)
|
| 305 |
else:
|
| 306 |
+
return compose_clevr_objects(prompt, guidance_scale, steps)
|
| 307 |
|
| 308 |
|
| 309 |
examples_1 = 'a camel | a forest'
|
|
|
|
| 314 |
examples_6 = 'a boat in a desert | a pink sky'
|
| 315 |
examples_7 = 'mountains in the background | a blue sky | cows on a pasture'
|
| 316 |
examples = [
|
| 317 |
+
[examples_7, 'Stable_Diffusion_1v_4', 10, 50],
|
| 318 |
+
[examples_4, 'Stable_Diffusion_1v_4', 10, 50],
|
| 319 |
+
[examples_5, 'Stable_Diffusion_1v_4', 10, 50],
|
| 320 |
+
[examples_6, 'Stable_Diffusion_1v_4', 10, 50],
|
| 321 |
+
[examples_1, 'GLIDE', 10, 100],
|
| 322 |
+
[examples_2, 'GLIDE', 10, 100],
|
| 323 |
+
[examples_3, 'CLEVR Objects', 10, 100]
|
| 324 |
+
]
|
| 325 |
|
| 326 |
import gradio as gr
|
| 327 |
|
| 328 |
title = 'Compositional Visual Generation with Composable Diffusion Models'
|
| 329 |
+
description = '<p>Demo for Composable Diffusion<ul><li>~30s per GLIDE/Stable-Diffusion example</li><li>~10s per CLEVR Object example</li>(<b>Note</b>: time is varied depending on what gpu is used.)</ul></p><p>See more information from our <a href="https://energy-based-model.github.io/Compositional-Visual-Generation-with-Composable-Diffusion-Models/">Project Page</a>.</p><ul><li>One version is based on the released <a href="https://github.com/openai/glide-text2im">GLIDE</a> and <a href="https://github.com/CompVis/stable-diffusion/">Stable Diffusion</a> for composing natural language description.</li><li>Another is based on our pre-trained CLEVR Object Model for composing objects. <br>(<b>Note</b>: We recommend using <b><i>x</i></b> in range <b><i>[0.1, 0.9]</i></b> and <b><i>y</i></b> in range <b><i>[0.25, 0.7]</i></b>, since the training dataset labels are in given ranges.)</li></ul><p>When composing multiple sentences, use `|` as the delimiter, see given examples below.</p><p><b>Note</b>: When using more steps, the results can improve.</p>'
|
| 330 |
+
|
| 331 |
+
iface = gr.Interface(compose,
|
| 332 |
+
inputs=[
|
| 333 |
+
"text",
|
| 334 |
+
gr.Radio(['Stable_Diffusion_1v_4', 'GLIDE', 'CLEVR Objects'], type="value", label='version'),
|
| 335 |
+
gr.Slider(2, 15),
|
| 336 |
+
gr.Slider(10, 200)
|
| 337 |
+
],
|
| 338 |
+
outputs='image',
|
| 339 |
title=title, description=description, examples=examples)
|
| 340 |
|
| 341 |
iface.launch()
|