forked from tnwei/vqgan-clip-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
665 lines (584 loc) · 24.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
This script is organized like so:
+ `if __name__ == "__main__" sets up the Streamlit UI elements
+ `generate_image` houses interactions between UI and the CLIP image
generation models
+ Core model code is abstracted in `logic.py` and imported in `generate_image`
"""
import streamlit as st
from pathlib import Path
import sys
import datetime
import shutil
import torch
import json
import os
import base64
import traceback
import argparse
sys.path.append("./taming-transformers")
from PIL import Image
from typing import Optional, List
from omegaconf import OmegaConf
import imageio
import numpy as np
# Catch import issue, introduced in version 1.1
# Deprecate in a few minor versions
try:
import cv2
except ModuleNotFoundError:
st.warning(
"Version 1.1 onwards requires opencv. Please update your Python environment as defined in `environment.yml`"
)
from logic import VQGANCLIPRun
# Optional
try:
import git
except ModuleNotFoundError:
pass
def generate_image(
text_input: str = "the first day of the waters",
vqgan_ckpt: str = "vqgan_imagenet_f16_16384",
num_steps: int = 300,
image_x: int = 300,
image_y: int = 300,
init_image: Optional[Image.Image] = None,
image_prompts: List[Image.Image] = [],
continue_prev_run: bool = False,
seed: Optional[int] = None,
mse_weight: float = 0,
mse_weight_decay: float = 0,
mse_weight_decay_steps: int = 0,
tv_loss_weight: float = 1e-3,
use_scrolling_zooming: bool = False,
translation_x: int = 0,
translation_y: int = 0,
rotation_angle: float = 0,
zoom_factor: float = 1,
transform_interval: int = 10,
use_cutout_augmentations: bool = True,
device: Optional[torch.device] = None,
) -> None:
### Init -------------------------------------------------------------------
run = VQGANCLIPRun(
text_input=text_input,
vqgan_ckpt=vqgan_ckpt,
num_steps=num_steps,
image_x=image_x,
image_y=image_y,
seed=seed,
init_image=init_image,
image_prompts=image_prompts,
continue_prev_run=continue_prev_run,
mse_weight=mse_weight,
mse_weight_decay=mse_weight_decay,
mse_weight_decay_steps=mse_weight_decay_steps,
tv_loss_weight=tv_loss_weight,
use_scrolling_zooming=use_scrolling_zooming,
translation_x=translation_x,
translation_y=translation_y,
rotation_angle=rotation_angle,
zoom_factor=zoom_factor,
transform_interval=transform_interval,
use_cutout_augmentations=use_cutout_augmentations,
device=device,
)
### Load model -------------------------------------------------------------
if continue_prev_run is True:
run.load_model(
prev_model=st.session_state["model"],
prev_perceptor=st.session_state["perceptor"],
)
prev_run_id = st.session_state["run_id"]
else:
# Remove the cache first! CUDA out of memory
if "model" in st.session_state:
del st.session_state["model"]
if "perceptor" in st.session_state:
del st.session_state["perceptor"]
st.session_state["model"], st.session_state["perceptor"] = run.load_model()
prev_run_id = None
# Generate random run ID
# Used to link runs linked w/ continue_prev_run
# ref: https://stackoverflow.com/a/42703382/13095028
# Use URL and filesystem safe version since we're using this as a folder name
run_id = st.session_state["run_id"] = base64.urlsafe_b64encode(
os.urandom(6)
).decode("ascii")
run_start_dt = datetime.datetime.now()
### Model init -------------------------------------------------------------
if continue_prev_run is True:
run.model_init(init_image=st.session_state["prev_im"])
elif init_image is not None:
run.model_init(init_image=init_image)
else:
run.model_init()
### Iterate ----------------------------------------------------------------
step_counter = 0
frames = []
try:
# Try block catches st.script_runner.StopExecution, no need of a dedicated stop button
# Reason is st.form is meant to be self-contained either within sidebar, or in main body
# The way the form is implemented in this app splits the form across both regions
# This is intended to prevent the model settings from crowding the main body
# However, touching any button resets the app state, making it impossible to
# implement a stop button that can still dump output
# Thankfully there's a built-in stop button :)
while True:
# While loop to accomodate running predetermined steps or running indefinitely
status_text.text(f"Running step {step_counter}")
_, im = run.iterate()
if num_steps > 0: # skip when num_steps = -1
step_progress_bar.progress((step_counter + 1) / num_steps)
else:
step_progress_bar.progress(100)
# At every step, display and save image
im_display_slot.image(im, caption="Output image", output_format="PNG")
st.session_state["prev_im"] = im
# ref: https://stackoverflow.com/a/33117447/13095028
# im_byte_arr = io.BytesIO()
# im.save(im_byte_arr, format="JPEG")
# frames.append(im_byte_arr.getvalue()) # read()
frames.append(np.asarray(im))
step_counter += 1
if (step_counter == num_steps) and num_steps > 0:
break
# Stitch into video using imageio
writer = imageio.get_writer("temp.mp4", fps=24)
for frame in frames:
writer.append_data(frame)
writer.close()
# Save to output folder if run completed
runoutputdir = outputdir / (
run_start_dt.strftime("%Y%m%dT%H%M%S") + "-" + run_id
)
runoutputdir.mkdir()
# Save final image
im.save(runoutputdir / "output.PNG", format="PNG")
# Save init image
if init_image is not None:
init_image.save(runoutputdir / "init-image.JPEG", format="JPEG")
# Save image prompts
for count, image_prompt in enumerate(image_prompts):
image_prompt.save(
runoutputdir / f"image-prompt-{count}.JPEG", format="JPEG"
)
# Save animation
shutil.copy("temp.mp4", runoutputdir / "anim.mp4")
# Save metadata
details = {
"run_id": run_id,
"num_steps": step_counter,
"planned_num_steps": num_steps,
"text_input": text_input,
"init_image": False if init_image is None else True,
"image_prompts": False if len(image_prompts) == 0 else True,
"continue_prev_run": continue_prev_run,
"prev_run_id": prev_run_id,
"seed": run.seed,
"Xdim": image_x,
"ydim": image_y,
"vqgan_ckpt": vqgan_ckpt,
"start_time": run_start_dt.strftime("%Y%m%dT%H%M%S"),
"end_time": datetime.datetime.now().strftime("%Y%m%dT%H%M%S"),
"mse_weight": mse_weight,
"mse_weight_decay": mse_weight_decay,
"mse_weight_decay_steps": mse_weight_decay_steps,
"tv_loss_weight": tv_loss_weight,
}
if use_scrolling_zooming:
details.update(
{
"translation_x": translation_x,
"translation_y": translation_y,
"rotation_angle": rotation_angle,
"zoom_factor": zoom_factor,
"transform_interval": transform_interval,
}
)
if use_cutout_augmentations:
details["use_cutout_augmentations"] = True
if "git" in sys.modules:
try:
repo = git.Repo(search_parent_directories=True)
commit_sha = repo.head.object.hexsha
details["commit_sha"] = commit_sha[:6]
except Exception as e:
print("GitPython detected but not able to write commit SHA to file")
print(f"raised Exception {e}")
with open(runoutputdir / "details.json", "w") as f:
json.dump(details, f, indent=4)
status_text.text("Done!") # End of run
except st.script_runner.StopException as e:
# Dump output to dashboard
print(f"Received Streamlit StopException")
status_text.text("Execution interruped, dumping outputs ...")
writer = imageio.get_writer("temp.mp4", fps=24)
for frame in frames:
writer.append_data(frame)
writer.close()
# TODO: Make the following DRY
# Save to output folder if run completed
runoutputdir = outputdir / (
run_start_dt.strftime("%Y%m%dT%H%M%S") + "-" + run_id
)
runoutputdir.mkdir()
# Save final image
im.save(runoutputdir / "output.PNG", format="PNG")
# Save init image
if init_image is not None:
init_image.save(runoutputdir / "init-image.JPEG", format="JPEG")
# Save image prompts
for count, image_prompt in enumerate(image_prompts):
image_prompt.save(
runoutputdir / f"image-prompt-{count}.JPEG", format="JPEG"
)
# Save animation
shutil.copy("temp.mp4", runoutputdir / "anim.mp4")
# Save metadata
details = {
"run_id": run_id,
"num_steps": step_counter,
"planned_num_steps": num_steps,
"text_input": text_input,
"init_image": False if init_image is None else True,
"image_prompts": False if len(image_prompts) == 0 else True,
"continue_prev_run": continue_prev_run,
"prev_run_id": prev_run_id,
"seed": run.seed,
"Xdim": image_x,
"ydim": image_y,
"vqgan_ckpt": vqgan_ckpt,
"start_time": run_start_dt.strftime("%Y%m%dT%H%M%S"),
"end_time": datetime.datetime.now().strftime("%Y%m%dT%H%M%S"),
"mse_weight": mse_weight,
"mse_weight_decay": mse_weight_decay,
"mse_weight_decay_steps": mse_weight_decay_steps,
"tv_loss_weight": tv_loss_weight,
}
if use_scrolling_zooming:
details.update(
{
"translation_x": translation_x,
"translation_y": translation_y,
"rotation_angle": rotation_angle,
"zoom_factor": zoom_factor,
"transform_interval": transform_interval,
}
)
if use_cutout_augmentations:
details["use_cutout_augmentations"] = True
if "git" in sys.modules:
try:
repo = git.Repo(search_parent_directories=True)
commit_sha = repo.head.object.hexsha
details["commit_sha"] = commit_sha[:6]
except Exception as e:
print("GitPython detected but not able to write commit SHA to file")
print(f"raised Exception {e}")
with open(runoutputdir / "details.json", "w") as f:
json.dump(details, f, indent=4)
status_text.text("Done!") # End of run
if __name__ == "__main__":
# Argparse to capture GPU num
parser = argparse.ArgumentParser()
parser.add_argument(
"--gpu", type=str, default=None, help="Specify GPU number. Defaults to None."
)
args = parser.parse_args()
# Select specific GPU if chosen
if args.gpu is not None:
for i in args.gpu.split(","):
assert (
int(i) < torch.cuda.device_count()
), f"You specified --gpu {args.gpu} but torch.cuda.device_count() returned {torch.cuda.device_count()}"
try:
device = torch.device(f"cuda:{args.gpu}")
except RuntimeError:
print(traceback.format_exc())
else:
device = None
defaults = OmegaConf.load("defaults.yaml")
outputdir = Path("output")
if not outputdir.exists():
outputdir.mkdir()
st.set_page_config(page_title="VQGAN-CLIP playground")
st.title("VQGAN-CLIP playground")
# Determine what weights are available in `assets/`
weights_dir = Path("assets").resolve()
available_weight_ckpts = list(weights_dir.glob("*.ckpt"))
available_weight_configs = list(weights_dir.glob("*.yaml"))
available_weights = [
i.stem
for i in available_weight_ckpts
if i.stem in [j.stem for j in available_weight_configs]
]
# i.e. no weights found, ask user to download weights
if len(available_weights) == 0:
st.warning("No weights found in `assets/`, refer to `download-weights.sh`")
st.stop()
# Set vqgan_imagenet_f16_1024 as default if possible
if "vqgan_imagenet_f16_1024" in available_weights:
default_weight_index = available_weights.index("vqgan_imagenet_f16_1024")
else:
default_weight_index = 0
# Start of input form
with st.form("form-inputs"):
# Only element not in the sidebar, but in the form
text_input = st.text_input(
"Text prompt",
help="VQGAN-CLIP will generate an image that best fits the prompt",
)
radio = st.sidebar.radio(
"Model weights",
available_weights,
index=default_weight_index,
help="Choose which weights to load, trained on different datasets. Make sure the weights and configs are downloaded to `assets/` as per the README!",
)
num_steps = st.sidebar.number_input(
"Num steps",
value=defaults["num_steps"],
min_value=-1,
max_value=None,
step=1,
help="Specify -1 to run indefinitely. Use Streamlit's stop button in the top right corner to terminate execution. The exception is caught so the most recent output will be dumped to dashboard",
)
image_x = st.sidebar.number_input(
"Xdim", value=defaults["Xdim"], help="Width of output image, in pixels"
)
image_y = st.sidebar.number_input(
"ydim", value=defaults["ydim"], help="Height of output image, in pixels"
)
set_seed = st.sidebar.checkbox(
"Set seed",
value=defaults["set_seed"],
help="Check to set random seed for reproducibility. Will add option to specify seed",
)
seed_widget = st.sidebar.empty()
if set_seed is True:
# Use text_input as number_input relies on JS
# which can't natively handle large numbers
# torch.seed() generates int w/ 19 or 20 chars!
seed_str = seed_widget.text_input(
"Seed", value=str(defaults["seed"]), help="Random seed to use"
)
try:
seed = int(seed_str)
except ValueError as e:
st.error("seed input needs to be int")
else:
seed = None
use_custom_starting_image = st.sidebar.checkbox(
"Use starting image",
value=defaults["use_starting_image"],
help="Check to add a starting image to the network",
)
starting_image_widget = st.sidebar.empty()
if use_custom_starting_image is True:
init_image = starting_image_widget.file_uploader(
"Upload starting image",
type=["png", "jpeg", "jpg"],
accept_multiple_files=False,
help="Starting image for the network, will be resized to fit specified dimensions",
)
# Convert from UploadedFile object to PIL Image
if init_image is not None:
init_image: Image.Image = Image.open(init_image).convert(
"RGB"
) # just to be sure
else:
init_image = None
use_image_prompts = st.sidebar.checkbox(
"Add image prompt(s)",
value=defaults["use_image_prompts"],
help="Check to add image prompt(s), conditions the network similar to the text prompt",
)
image_prompts_widget = st.sidebar.empty()
if use_image_prompts is True:
image_prompts = image_prompts_widget.file_uploader(
"Upload image prompts(s)",
type=["png", "jpeg", "jpg"],
accept_multiple_files=True,
help="Image prompt(s) for the network, will be resized to fit specified dimensions",
)
# Convert from UploadedFile object to PIL Image
if len(image_prompts) != 0:
image_prompts = [Image.open(i).convert("RGB") for i in image_prompts]
else:
image_prompts = []
continue_prev_run = st.sidebar.checkbox(
"Continue previous run",
value=defaults["continue_prev_run"],
help="Use existing image and existing weights for the next run. If yes, ignores 'Use starting image'",
)
use_mse_reg = st.sidebar.checkbox(
"Use MSE regularization",
value=defaults["use_mse_regularization"],
help="Check to add MSE regularization",
)
mse_weight_widget = st.sidebar.empty()
mse_weight_decay_widget = st.sidebar.empty()
mse_weight_decay_steps = st.sidebar.empty()
if use_mse_reg is True:
mse_weight = mse_weight_widget.number_input(
"MSE weight",
value=defaults["mse_weight"],
# min_value=0.0, # leave this out to allow creativity
step=0.05,
help="Set weights for MSE regularization",
)
mse_weight_decay = mse_weight_decay_widget.number_input(
"Decay MSE weight by ...",
value=defaults["mse_weight_decay"],
# min_value=0.0, # leave this out to allow creativity
step=0.05,
help="Subtracts MSE weight by this amount at every step change. MSE weight change stops at zero",
)
mse_weight_decay_steps = mse_weight_decay_steps.number_input(
"... every N steps",
value=defaults["mse_weight_decay_steps"],
min_value=0,
step=1,
help="Number of steps to subtract MSE weight. Leave zero for no weight decay",
)
else:
mse_weight = 0
mse_weight_decay = 0
mse_weight_decay_steps = 0
use_tv_loss = st.sidebar.checkbox(
"Use TV loss regularization",
value=defaults["use_tv_loss_regularization"],
help="Check to add MSE regularization",
)
tv_loss_weight_widget = st.sidebar.empty()
if use_tv_loss is True:
tv_loss_weight = tv_loss_weight_widget.number_input(
"TV loss weight",
value=defaults["tv_loss_weight"],
min_value=0.0,
step=1e-4,
help="Set weights for TV loss regularization, which encourages spatial smoothness. Ref: https://github.com/jcjohnson/neural-style/issues/302",
format="%.1e",
)
else:
tv_loss_weight = 0
use_scrolling_zooming = st.sidebar.checkbox(
"Scrolling/zooming transforms",
value=False,
help="At fixed intervals, move the generated image up/down/left/right or zoom in/out",
)
translation_x_widget = st.sidebar.empty()
translation_y_widget = st.sidebar.empty()
rotation_angle_widget = st.sidebar.empty()
zoom_factor_widget = st.sidebar.empty()
transform_interval_widget = st.sidebar.empty()
if use_scrolling_zooming is True:
translation_x = translation_x_widget.number_input(
"Translation in X", value=0, min_value=0, step=1
)
translation_y = translation_y_widget.number_input(
"Translation in y", value=0, min_value=0, step=1
)
rotation_angle = rotation_angle_widget.number_input(
"Rotation angle (degrees)",
value=0.0,
min_value=0.0,
max_value=360.0,
step=0.05,
format="%.2f",
)
zoom_factor = zoom_factor_widget.number_input(
"Zoom factor",
value=1.0,
min_value=0.1,
max_value=10.0,
step=0.02,
format="%.2f",
)
transform_interval = transform_interval_widget.number_input(
"Iterations per frame",
value=10,
min_value=0,
step=1,
help="Note: Will multiply by num steps above!",
)
else:
translation_x = 0
translation_y = 0
rotation_angle = 0
zoom_factor = 1
transform_interval = 1
use_cutout_augmentations = st.sidebar.checkbox(
"Use cutout augmentations",
value=True,
help="Adds cutout augmentatinos in the image generation process. Uses up to additional 4 GiB of GPU memory. Greatly improves image quality. Toggled on by default.",
)
submitted = st.form_submit_button("Run!")
# End of form
status_text = st.empty()
status_text.text("Pending input prompt")
step_progress_bar = st.progress(0)
im_display_slot = st.empty()
vid_display_slot = st.empty()
debug_slot = st.empty()
if "prev_im" in st.session_state:
im_display_slot.image(
st.session_state["prev_im"], caption="Output image", output_format="PNG"
)
with st.expander("Expand for README"):
with open("README.md", "r") as f:
# Preprocess links to redirect to github
# Thank you https://discuss.streamlit.io/u/asehmi, works like a charm!
# ref: https://discuss.streamlit.io/t/image-in-markdown/13274/8
markdown_links = [str(i) for i in Path("docs/").glob("*.md")]
images = [str(i) for i in Path("docs/images/").glob("*")]
readme_lines = f.readlines()
readme_buffer = []
for line in readme_lines:
for md_link in markdown_links:
if md_link in line:
line = line.replace(
md_link,
"https://github.com/tnwei/vqgan-clip-app/tree/main/"
+ md_link,
)
readme_buffer.append(line)
for image in images:
if image in line:
st.markdown(" ".join(readme_buffer[:-1]))
st.image(
f"https://raw.githubusercontent.com/tnwei/vqgan-clip-app/main/{image}"
)
readme_buffer.clear()
st.markdown(" ".join(readme_buffer))
with st.expander("Expand for CHANGELOG"):
with open("CHANGELOG.md", "r") as f:
st.markdown(f.read())
if submitted:
# debug_slot.write(st.session_state) # DEBUG
status_text.text("Loading weights ...")
generate_image(
# Inputs
text_input=text_input,
vqgan_ckpt=radio,
num_steps=num_steps,
image_x=int(image_x),
image_y=int(image_y),
seed=int(seed) if set_seed is True else None,
init_image=init_image,
image_prompts=image_prompts,
continue_prev_run=continue_prev_run,
mse_weight=mse_weight,
mse_weight_decay=mse_weight_decay,
mse_weight_decay_steps=mse_weight_decay_steps,
use_scrolling_zooming=use_scrolling_zooming,
translation_x=translation_x,
translation_y=translation_y,
rotation_angle=rotation_angle,
zoom_factor=zoom_factor,
transform_interval=transform_interval,
use_cutout_augmentations=use_cutout_augmentations,
device=device,
)
vid_display_slot.video("temp.mp4")
# debug_slot.write(st.session_state) # DEBUG