SteEsp commited on
Commit
87e3830
·
verified ·
1 Parent(s): f08c334

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -25
app.py CHANGED
@@ -82,33 +82,44 @@ for m in G.modules():
82
  m.recompute_scale_factor = None
83
 
84
 
85
- def generate_image(seed: int, truncation_psi: float):
86
  label = torch.zeros([1, G.c_dim], device=device)
87
  z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device).float()
88
  if hasattr(G.synthesis, 'input'):
89
  m = make_transform((0, 0), 0)
90
  m = np.linalg.inv(m)
91
  G.synthesis.input.transform.copy_(torch.from_numpy(m))
92
- img = G(z, label, truncation_psi=truncation_psi, noise_mode='const')
93
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
94
  return PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
95
 
96
 
97
- def inference(seed, truncation_psi):
98
  seed = int(seed)
99
- image = generate_image(seed, float(truncation_psi))
100
  return image, str(seed)
101
 
102
 
 
 
 
 
 
 
103
  def pick_random_seed():
104
  return random.randint(0, 5000)
105
 
106
 
 
 
 
 
107
  header_md = """
108
  # Projected GAN — Pokémon
109
 
110
  Generate Pokémon with the pretrained [Projected GAN](https://github.com/autonomousvision/projected_gan)
111
- from Sauer et al., NeurIPS 2021. Pick a seed for reproducibility, or roll the dice.
 
112
  """
113
 
114
  footer_md = """
@@ -117,48 +128,62 @@ Paper: [Projected GANs Converge Faster](http://www.cvlibs.net/publications/Sauer
117
  Code: [autonomousvision/projected_gan](https://github.com/autonomousvision/projected_gan)
118
  """
119
 
120
- with gr.Blocks(theme=gr.themes.Soft(), title="Projected GAN — Pokémon") as demo:
 
 
 
 
 
121
  gr.Markdown(header_md)
122
 
123
- with gr.Row():
124
- with gr.Column(scale=1):
 
 
125
  seed_slider = gr.Slider(
126
  label="Seed",
127
  minimum=0, maximum=5000, step=1, value=42,
128
- info="Same seed + same truncation = same image.",
129
- )
130
- trunc_slider = gr.Slider(
131
- label="Truncation ψ",
132
- minimum=0.0, maximum=1.0, step=0.05, value=1.0,
133
- info="Lower = more typical / less varied. Higher = more diverse / weirder.",
134
  )
135
  with gr.Row():
136
  random_btn = gr.Button("🎲 Random seed")
137
  generate_btn = gr.Button("Generate", variant="primary")
138
-
139
- with gr.Column(scale=1):
140
- image_out = gr.Image(label="Output", type="pil")
141
  seed_used = gr.Textbox(label="Seed used", interactive=False)
142
 
 
 
 
143
  gr.Examples(
144
- examples=[
145
- [0, 1.0], [1, 1.0], [10, 1.0], [20, 1.0], [30, 1.0],
146
- [42, 1.0], [50, 1.0], [60, 1.0], [77, 1.0], [102, 1.0],
147
- ],
148
- inputs=[seed_slider, trunc_slider],
149
  outputs=[image_out, seed_used],
150
  fn=inference,
151
  cache_examples=False,
152
  label="Curated seeds",
153
  )
154
 
 
 
 
 
 
 
 
 
155
  gr.Markdown(footer_md)
156
 
157
  random_btn.click(fn=pick_random_seed, outputs=seed_slider)
158
  generate_btn.click(
159
- fn=inference,
160
- inputs=[seed_slider, trunc_slider],
161
- outputs=[image_out, seed_used],
 
 
 
 
 
 
 
162
  )
163
 
164
  demo.queue().launch()
 
82
  m.recompute_scale_factor = None
83
 
84
 
85
+ def generate_image(seed: int):
86
  label = torch.zeros([1, G.c_dim], device=device)
87
  z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device).float()
88
  if hasattr(G.synthesis, 'input'):
89
  m = make_transform((0, 0), 0)
90
  m = np.linalg.inv(m)
91
  G.synthesis.input.transform.copy_(torch.from_numpy(m))
92
+ img = G(z, label, truncation_psi=1, noise_mode='const')
93
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
94
  return PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
95
 
96
 
97
+ def inference(seed):
98
  seed = int(seed)
99
+ image = generate_image(seed)
100
  return image, str(seed)
101
 
102
 
103
+ def run_and_log(seed, history):
104
+ image, seed_str = inference(seed)
105
+ history = (history or []) + [(image, f"seed={int(seed)}")]
106
+ return image, seed_str, history, history
107
+
108
+
109
  def pick_random_seed():
110
  return random.randint(0, 5000)
111
 
112
 
113
+ def clear_history():
114
+ return [], []
115
+
116
+
117
  header_md = """
118
  # Projected GAN — Pokémon
119
 
120
  Generate Pokémon with the pretrained [Projected GAN](https://github.com/autonomousvision/projected_gan)
121
+ from Sauer et al., NeurIPS 2021. Pick a seed, roll the dice, or click a curated example.
122
+ Each Generate adds to your session history below.
123
  """
124
 
125
  footer_md = """
 
128
  Code: [autonomousvision/projected_gan](https://github.com/autonomousvision/projected_gan)
129
  """
130
 
131
+ custom_css = """
132
+ .gradio-container { max-width: 1100px !important; }
133
+ #main-image img { object-fit: contain; }
134
+ """
135
+
136
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="Projected GAN — Pokémon") as demo:
137
  gr.Markdown(header_md)
138
 
139
+ gallery_state = gr.State([])
140
+
141
+ with gr.Row(equal_height=False):
142
+ with gr.Column(scale=2):
143
  seed_slider = gr.Slider(
144
  label="Seed",
145
  minimum=0, maximum=5000, step=1, value=42,
146
+ info="Same seed = same image.",
 
 
 
 
 
147
  )
148
  with gr.Row():
149
  random_btn = gr.Button("🎲 Random seed")
150
  generate_btn = gr.Button("Generate", variant="primary")
 
 
 
151
  seed_used = gr.Textbox(label="Seed used", interactive=False)
152
 
153
+ with gr.Column(scale=3):
154
+ image_out = gr.Image(label="Output", type="pil", height=512, elem_id="main-image")
155
+
156
  gr.Examples(
157
+ examples=[[0], [1], [10], [20], [30], [42], [50], [60], [77], [102]],
158
+ inputs=[seed_slider],
 
 
 
159
  outputs=[image_out, seed_used],
160
  fn=inference,
161
  cache_examples=False,
162
  label="Curated seeds",
163
  )
164
 
165
+ with gr.Row():
166
+ gr.Markdown("### Session history")
167
+ gallery = gr.Gallery(
168
+ label=None, show_label=False, columns=4, height=340,
169
+ )
170
+ with gr.Row():
171
+ clear_btn = gr.Button("🗑 Clear history", size="sm")
172
+
173
  gr.Markdown(footer_md)
174
 
175
  random_btn.click(fn=pick_random_seed, outputs=seed_slider)
176
  generate_btn.click(
177
+ fn=run_and_log,
178
+ inputs=[seed_slider, gallery_state],
179
+ outputs=[image_out, seed_used, gallery_state, gallery],
180
+ )
181
+ clear_btn.click(fn=clear_history, outputs=[gallery_state, gallery])
182
+
183
+ demo.load(
184
+ fn=run_and_log,
185
+ inputs=[seed_slider, gallery_state],
186
+ outputs=[image_out, seed_used, gallery_state, gallery],
187
  )
188
 
189
  demo.queue().launch()