Spaces:
Running
on
Zero
Running
on
Zero
Update injection_main.py
Browse files- injection_main.py +10 -110
injection_main.py
CHANGED
@@ -473,10 +473,7 @@ def style_image_with_inversion(
|
|
473 |
|
474 |
if __name__ == "__main__":
|
475 |
|
476 |
-
|
477 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
478 |
-
"stabilityai/stable-diffusion-2-1-base"
|
479 |
-
).to(device)
|
480 |
|
481 |
# pipe = DiffusionPipeline.from_pretrained(
|
482 |
# # "playgroundai/playground-v2-1024px-aesthetic",
|
@@ -487,9 +484,6 @@ if __name__ == "__main__":
|
|
487 |
# )
|
488 |
# pipe.to("cuda")
|
489 |
|
490 |
-
# Set up a DDIM scheduler
|
491 |
-
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
492 |
-
|
493 |
parser = argparse.ArgumentParser(description="Stable Diffusion with OmegaConf")
|
494 |
parser.add_argument(
|
495 |
"--config", type=str, default="config.yaml", help="Path to the config file"
|
@@ -517,113 +511,19 @@ if __name__ == "__main__":
|
|
517 |
# mode = "dataset"
|
518 |
out_name = ["content_delegation", "style_delegation", "style_out"]
|
519 |
|
520 |
-
if mode == "
|
521 |
-
cfg = OmegaConf.load(config_dir)
|
522 |
-
|
523 |
-
base_output_path = cfg.out_path
|
524 |
-
if not os.path.exists(cfg.out_path):
|
525 |
-
os.makedirs(cfg.out_path)
|
526 |
-
base_output_path = os.path.join(base_output_path, cfg.exp_name)
|
527 |
-
|
528 |
-
experiment_output_path = utils.exp_utils.make_unique_experiment_path(
|
529 |
-
base_output_path
|
530 |
-
)
|
531 |
-
|
532 |
-
# Save the experiment configuration
|
533 |
-
config_file_path = os.path.join(experiment_output_path, "config.yaml")
|
534 |
-
omegaconf.OmegaConf.save(cfg, config_file_path)
|
535 |
-
|
536 |
-
# Seed all
|
537 |
-
|
538 |
-
annotation = json.load(open(cfg.annotation))
|
539 |
-
with open(os.path.join(experiment_output_path, "annotation.json"), "w") as f:
|
540 |
-
json.dump(annotation, f)
|
541 |
-
for i, entry in enumerate(annotation):
|
542 |
-
utils.exp_utils.seed_all(cfg.seed)
|
543 |
-
image_path = entry["image_path"]
|
544 |
-
src_prompt = entry["source_prompt"]
|
545 |
-
tgt_prompt = entry["target_prompt"]
|
546 |
-
resolution = 512 if isinstance(pipe, StableDiffusionXLPipeline) else 512
|
547 |
-
input_image = utils.exp_utils.get_processed_image(
|
548 |
-
image_path, device, resolution
|
549 |
-
)
|
550 |
-
|
551 |
-
prompt_in = [
|
552 |
-
src_prompt, # reconstruction
|
553 |
-
tgt_prompt, # uncontrolled style
|
554 |
-
"", # controlled style
|
555 |
-
]
|
556 |
-
|
557 |
-
imgs = style_image_with_inversion(
|
558 |
-
pipe,
|
559 |
-
input_image,
|
560 |
-
src_prompt,
|
561 |
-
style_prompt=prompt_in,
|
562 |
-
num_steps=cfg.num_steps,
|
563 |
-
start_step=cfg.start_step,
|
564 |
-
guidance_scale=cfg.style_cfg_scale,
|
565 |
-
disentangle=cfg.disentangle,
|
566 |
-
resnet_mode=cfg.resnet_mode,
|
567 |
-
share_attn=cfg.share_attn,
|
568 |
-
share_cross_attn=cfg.share_cross_attn,
|
569 |
-
share_resnet_layers=cfg.share_resnet_layers,
|
570 |
-
share_attn_layers=cfg.share_attn_layers,
|
571 |
-
share_key=cfg.share_key,
|
572 |
-
share_query=cfg.share_query,
|
573 |
-
share_value=cfg.share_value,
|
574 |
-
use_content_anchor=cfg.use_content_anchor,
|
575 |
-
use_adain=cfg.use_adain,
|
576 |
-
output_dir=experiment_output_path,
|
577 |
-
)
|
578 |
-
|
579 |
-
for j, img in enumerate(imgs):
|
580 |
-
img.save(f"{experiment_output_path}/out_{i}_{out_name[j]}.png")
|
581 |
-
print(
|
582 |
-
f"Image saved as {experiment_output_path}/out_{i}_{out_name[j]}.png"
|
583 |
-
)
|
584 |
-
elif mode == "cli":
|
585 |
-
cfg = OmegaConf.load(config_dir)
|
586 |
-
utils.exp_utils.seed_all(cfg.seed)
|
587 |
-
image = utils.exp_utils.get_processed_image(args.image_dir, device, 512)
|
588 |
-
tgt_prompt = args.prompt
|
589 |
-
src_prompt = ""
|
590 |
-
prompt_in = [
|
591 |
-
"", # reconstruction
|
592 |
-
tgt_prompt, # uncontrolled style
|
593 |
-
"", # controlled style
|
594 |
-
]
|
595 |
-
out_dir = "./out"
|
596 |
-
os.makedirs(out_dir, exist_ok=True)
|
597 |
-
imgs = style_image_with_inversion(
|
598 |
-
pipe,
|
599 |
-
image,
|
600 |
-
src_prompt,
|
601 |
-
style_prompt=prompt_in,
|
602 |
-
num_steps=cfg.num_steps,
|
603 |
-
start_step=cfg.start_step,
|
604 |
-
guidance_scale=cfg.style_cfg_scale,
|
605 |
-
disentangle=cfg.disentangle,
|
606 |
-
resnet_mode=cfg.resnet_mode,
|
607 |
-
share_attn=cfg.share_attn,
|
608 |
-
share_cross_attn=cfg.share_cross_attn,
|
609 |
-
share_resnet_layers=cfg.share_resnet_layers,
|
610 |
-
share_attn_layers=cfg.share_attn_layers,
|
611 |
-
share_key=cfg.share_key,
|
612 |
-
share_query=cfg.share_query,
|
613 |
-
share_value=cfg.share_value,
|
614 |
-
use_content_anchor=cfg.use_content_anchor,
|
615 |
-
use_adain=cfg.use_adain,
|
616 |
-
output_dir=out_dir,
|
617 |
-
)
|
618 |
-
image_base_name = os.path.basename(args.image_dir).split(".")[0]
|
619 |
-
for j, img in enumerate(imgs):
|
620 |
-
img.save(f"{out_dir}/{image_base_name}_out_{out_name[j]}.png")
|
621 |
-
print(f"Image saved as {out_dir}/{image_base_name}_out_{out_name[j]}.png")
|
622 |
-
elif mode == "app":
|
623 |
# gradio
|
624 |
import gradio as gr
|
625 |
import spaces
|
626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
627 |
@spaces.GPU()
|
628 |
def style_transfer_app(
|
629 |
prompt,
|
|
|
473 |
|
474 |
if __name__ == "__main__":
|
475 |
|
476 |
+
|
|
|
|
|
|
|
477 |
|
478 |
# pipe = DiffusionPipeline.from_pretrained(
|
479 |
# # "playgroundai/playground-v2-1024px-aesthetic",
|
|
|
484 |
# )
|
485 |
# pipe.to("cuda")
|
486 |
|
|
|
|
|
|
|
487 |
parser = argparse.ArgumentParser(description="Stable Diffusion with OmegaConf")
|
488 |
parser.add_argument(
|
489 |
"--config", type=str, default="config.yaml", help="Path to the config file"
|
|
|
511 |
# mode = "dataset"
|
512 |
out_name = ["content_delegation", "style_delegation", "style_out"]
|
513 |
|
514 |
+
if mode == "app":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
# gradio
|
516 |
import gradio as gr
|
517 |
import spaces
|
518 |
|
519 |
+
# Load a pipeline
|
520 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
521 |
+
"stabilityai/stable-diffusion-2-1-base"
|
522 |
+
).to(device)
|
523 |
+
|
524 |
+
# Set up a DDIM scheduler
|
525 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
526 |
+
|
527 |
@spaces.GPU()
|
528 |
def style_transfer_app(
|
529 |
prompt,
|