Shuang59 commited on
Commit
2b95c91
β€’
1 Parent(s): c52d702

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -31,6 +31,7 @@ from composable_diffusion.model_creation import model_and_diffusion_defaults as
31
 
32
  has_cuda = th.cuda.is_available()
33
  device = th.device('cpu' if not has_cuda else 'cuda')
 
34
 
35
  # Create base model.
36
  timestep_respacing = 100 #@param{type: 'number'}
@@ -63,12 +64,11 @@ def show_images(batch: th.Tensor):
63
  reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
64
  display(Image.fromarray(reshaped.numpy()))
65
 
66
- def compose_language_descriptions(prompt):
67
  #@markdown `prompt`: when composing multiple sentences, using `|` as the delimiter.
68
  prompts = [x.strip() for x in prompt.split('|')]
69
 
70
  batch_size = 1
71
- guidance_scale = 10 #@param{type: 'number'}
72
  # Tune this parameter to control the sharpness of 256x256 images.
73
  # A value of 1.0 is sharper, but sometimes results in grainy artifacts.
74
  upsample_temp = 0.980 #@param{type: 'number'}
@@ -229,13 +229,12 @@ clevr_model.to(device)
229
  clevr_model.load_state_dict(th.load(download_model('clevr_pos'), device))
230
  print('total clevr_pos parameters', sum(x.numel() for x in clevr_model.parameters()))
231
 
232
- def compose_clevr_objects(prompt):
233
  print(prompt)
234
  coordinates = [[float(x.split(',')[0].strip()), float(x.split(',')[1].strip())]
235
  for x in prompt.split('|')]
236
  coordinates += [[-1, -1]] # add unconditional score label
237
  batch_size = 1
238
- guidance_scale = 10
239
 
240
  def model_fn(x_t, ts, **kwargs):
241
  half = x_t[:1]
@@ -274,22 +273,22 @@ def compose_clevr_objects(prompt):
274
  return out_img
275
 
276
 
277
- def compose(prompt, ver):
278
  if ver == 'GLIDE':
279
- return compose_language_descriptions(prompt)
280
  else:
281
- return compose_clevr_objects(prompt)
282
 
283
  examples_1 = 'a camel | a forest'
284
  examples_2 = 'A cloudy blue sky | A mountain in the horizon | Cherry Blossoms in front of the mountain'
285
  examples_3 = '0.1, 0.5 | 0.3, 0.5 | 0.5, 0.5 | 0.7, 0.5 | 0.9, 0.5'
286
- examples = [[examples_1, 'GLIDE'], [examples_2, 'GLIDE'], [examples_3, 'CLEVR Objects']]
287
 
288
  import gradio as gr
289
 
290
  title = 'Compositional Visual Generation with Composable Diffusion Models'
291
  description = '<p>Demo for Composable Diffusion (~20s per example)</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> 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>'
292
 
293
- iface = gr.Interface(compose, ['text', gr.inputs.Radio(['GLIDE','CLEVR Objects'], type="value", default='GLIDE', label='version')], 'image', title=title, description=description, examples=examples)
294
 
295
  iface.launch()
 
31
 
32
  has_cuda = th.cuda.is_available()
33
  device = th.device('cpu' if not has_cuda else 'cuda')
34
+ print(device)
35
 
36
  # Create base model.
37
  timestep_respacing = 100 #@param{type: 'number'}
 
64
  reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
65
  display(Image.fromarray(reshaped.numpy()))
66
 
67
+ def compose_language_descriptions(prompt, guidance_scale):
68
  #@markdown `prompt`: when composing multiple sentences, using `|` as the delimiter.
69
  prompts = [x.strip() for x in prompt.split('|')]
70
 
71
  batch_size = 1
 
72
  # Tune this parameter to control the sharpness of 256x256 images.
73
  # A value of 1.0 is sharper, but sometimes results in grainy artifacts.
74
  upsample_temp = 0.980 #@param{type: 'number'}
 
229
  clevr_model.load_state_dict(th.load(download_model('clevr_pos'), device))
230
  print('total clevr_pos parameters', sum(x.numel() for x in clevr_model.parameters()))
231
 
232
+ def compose_clevr_objects(prompt, guidance_scale):
233
  print(prompt)
234
  coordinates = [[float(x.split(',')[0].strip()), float(x.split(',')[1].strip())]
235
  for x in prompt.split('|')]
236
  coordinates += [[-1, -1]] # add unconditional score label
237
  batch_size = 1
 
238
 
239
  def model_fn(x_t, ts, **kwargs):
240
  half = x_t[:1]
 
273
  return out_img
274
 
275
 
276
+ def compose(prompt, ver, guidance_scale):
277
  if ver == 'GLIDE':
278
+ return compose_language_descriptions(prompt, guidance_scale)
279
  else:
280
+ return compose_clevr_objects(prompt, guidance_scale)
281
 
282
  examples_1 = 'a camel | a forest'
283
  examples_2 = 'A cloudy blue sky | A mountain in the horizon | Cherry Blossoms in front of the mountain'
284
  examples_3 = '0.1, 0.5 | 0.3, 0.5 | 0.5, 0.5 | 0.7, 0.5 | 0.9, 0.5'
285
+ examples = [[examples_1, 'GLIDE', 10], [examples_2, 'GLIDE', 10], [examples_3, 'CLEVR Objects', 10]]
286
 
287
  import gradio as gr
288
 
289
  title = 'Compositional Visual Generation with Composable Diffusion Models'
290
  description = '<p>Demo for Composable Diffusion (~20s per example)</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> 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>'
291
 
292
+ iface = gr.Interface(compose, inputs=["text", gr.inputs.Radio(['GLIDE','CLEVR Objects'], type="value", default='GLIDE', label='version'), gr.Slider(1, 10)], outputs='image', title=title, description=description, examples=examples)
293
 
294
  iface.launch()