nielsr HF staff commited on
Commit
e793580
1 Parent(s): 2790989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -24,8 +24,8 @@ for idx, url in enumerate(urls):
24
  image.save(f"image_{idx}.png")
25
 
26
  def process_image(image):
27
- # prepare 8 images, shape (8, 1024)
28
- batch_size = 8
29
  encoding = feature_extractor([image for _ in range(batch_size)], return_tensors="pt")
30
 
31
  # create primers
@@ -35,9 +35,9 @@ 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
- # 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)
@@ -48,6 +48,8 @@ def process_image(image):
48
  samples = output[:,1:].cpu().detach().numpy()
49
  samples_img = [np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples]
50
 
 
 
51
  # stack images horizontally
52
  row1 = np.hstack(samples_img[:4])
53
  row2 = np.hstack(samples_img[4:])
@@ -56,17 +58,16 @@ def process_image(image):
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,
24
  image.save(f"image_{idx}.png")
25
 
26
  def process_image(image):
27
+ # prepare 7 images, shape (7, 1024)
28
+ batch_size = 7
29
  encoding = feature_extractor([image for _ in range(batch_size)], return_tensors="pt")
30
 
31
  # create primers
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), padded with black pixels to be 32x32
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 = np.pad(primers_img, pad_width=((0,16), (0,0), (0,0)), mode="constant")
41
 
42
  # generate (no beam search)
43
  context = np.concatenate((np.full((batch_size, 1), model.config.vocab_size - 1), primers), axis=1)
48
  samples = output[:,1:].cpu().detach().numpy()
49
  samples_img = [np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [n_px, n_px, 3]).astype(np.uint8) for s in samples]
50
 
51
+ samples_img = [primers_img] + samples_img
52
+
53
  # stack images horizontally
54
  row1 = np.hstack(samples_img[:4])
55
  row2 = np.hstack(samples_img[4:])
58
  # return as PIL Image
59
  completion = Image.fromarray(result)
60
 
61
+ return completion
62
 
63
  title = "Interactive demo: ImageGPT"
64
  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."
65
  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>"
66
  examples =[f"image_{idx}.png" for idx in range(len(urls))]
67
 
 
68
  iface = gr.Interface(fn=process_image,
69
  inputs=gr.inputs.Image(type="pil"),
70
+ outputs=gr.outputs.Image(type="pil", label="Model input + completions"),
71
  title=title,
72
  description=description,
73
  article=article,