nielsr HF staff commited on
Commit
2790989
·
1 Parent(s): 09e8220

Add conditioned image

Browse files
Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -35,6 +35,10 @@ def process_image(image):
35
  n_px_crop = 16
36
  primers = samples.reshape(-1,n_px*n_px)[:,:n_px_crop*n_px] # crop top n_px_crop rows. These will be the conditioning tokens
37
 
 
 
 
 
38
  # generate (no beam search)
39
  context = np.concatenate((np.full((batch_size, 1), model.config.vocab_size - 1), primers), axis=1)
40
  context = torch.tensor(context).to(device)
@@ -52,16 +56,17 @@ def process_image(image):
52
  # return as PIL Image
53
  completion = Image.fromarray(result)
54
 
55
- return completion
56
 
57
  title = "Interactive demo: ImageGPT"
58
  description = "Demo for OpenAI's ImageGPT: Generative Pretraining from Pixels. To use it, simply upload an image or use the example image below and click 'submit'. Results will show up in a few seconds."
59
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>ImageGPT: Generative Pretraining from Pixels</a> | <a href='https://openai.com/blog/image-gpt/'>Official blog</a></p>"
60
  examples =[f"image_{idx}.png" for idx in range(len(urls))]
61
 
 
62
  iface = gr.Interface(fn=process_image,
63
  inputs=gr.inputs.Image(type="pil"),
64
- outputs=gr.outputs.Image(type="pil"),
65
  title=title,
66
  description=description,
67
  article=article,
 
35
  n_px_crop = 16
36
  primers = samples.reshape(-1,n_px*n_px)[:,:n_px_crop*n_px] # crop top n_px_crop rows. These will be the conditioning tokens
37
 
38
+ # get conditioned image (from first primer tensor) by converting color clusters back to pixels
39
+ primers_img = np.reshape(np.rint(127.5 * (clusters[primers[0]] + 1.0)), [n_px_crop,n_px, 3]).astype(np.uint8)
40
+ primers_img = Image.fromarray(primers_img)
41
+
42
  # generate (no beam search)
43
  context = np.concatenate((np.full((batch_size, 1), model.config.vocab_size - 1), primers), axis=1)
44
  context = torch.tensor(context).to(device)
 
56
  # return as PIL Image
57
  completion = Image.fromarray(result)
58
 
59
+ return [primers_img, completion]
60
 
61
  title = "Interactive demo: ImageGPT"
62
  description = "Demo for OpenAI's ImageGPT: Generative Pretraining from Pixels. To use it, simply upload an image or use the example image below and click 'submit'. Results will show up in a few seconds."
63
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>ImageGPT: Generative Pretraining from Pixels</a> | <a href='https://openai.com/blog/image-gpt/'>Official blog</a></p>"
64
  examples =[f"image_{idx}.png" for idx in range(len(urls))]
65
 
66
+ labels = ["Conditioned image:", "Completions:"]
67
  iface = gr.Interface(fn=process_image,
68
  inputs=gr.inputs.Image(type="pil"),
69
+ outputs=[gr.outputs.Image(type="pil", label=labels[idx]) for idx in range(2)],
70
  title=title,
71
  description=description,
72
  article=article,