fix(texture): reuse pipeline for IP adapter
Browse files- common.py +54 -23
- embodied_gen/scripts/render_mv.py +14 -3
common.py
CHANGED
|
@@ -138,11 +138,7 @@ elif os.getenv("GRADIO_APP").startswith("textto3d"):
|
|
| 138 |
elif os.getenv("GRADIO_APP") == "texture_edit":
|
| 139 |
DELIGHT = DelightingModel()
|
| 140 |
IMAGESR_MODEL = ImageRealESRGAN(outscale=4)
|
| 141 |
-
|
| 142 |
-
base_ckpt_dir="./weights",
|
| 143 |
-
ip_adapt_scale=0.7,
|
| 144 |
-
device="cuda",
|
| 145 |
-
)
|
| 146 |
PIPELINE = build_texture_gen_pipe(
|
| 147 |
base_ckpt_dir="./weights",
|
| 148 |
ip_adapt_scale=0,
|
|
@@ -671,27 +667,62 @@ def generate_texture_mvimages(
|
|
| 671 |
sub_idxs: tuple[tuple[int]] = ((0, 1, 2), (3, 4, 5)),
|
| 672 |
req: gr.Request = None,
|
| 673 |
) -> list[str]:
|
|
|
|
|
|
|
| 674 |
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 675 |
use_ip_adapter = True if ip_img_path and ip_adapt_scale > 0 else False
|
| 676 |
-
PIPELINE_IP.set_ip_adapter_scale([ip_adapt_scale])
|
| 677 |
-
img_save_paths = infer_pipe(
|
| 678 |
-
index_file=f"{output_root}/condition/index.json",
|
| 679 |
-
controlnet_cond_scale=controlnet_cond_scale,
|
| 680 |
-
guidance_scale=guidance_scale,
|
| 681 |
-
strength=strength,
|
| 682 |
-
num_inference_steps=num_inference_steps,
|
| 683 |
-
ip_adapt_scale=ip_adapt_scale,
|
| 684 |
-
ip_img_path=ip_img_path,
|
| 685 |
-
uid=uid,
|
| 686 |
-
prompt=prompt,
|
| 687 |
-
save_dir=f"{output_root}/multi_view",
|
| 688 |
-
sub_idxs=sub_idxs,
|
| 689 |
-
pipeline=PIPELINE_IP if use_ip_adapter else PIPELINE,
|
| 690 |
-
seed=seed,
|
| 691 |
-
)
|
| 692 |
|
| 693 |
-
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
return img_save_paths + img_save_paths
|
| 697 |
|
|
|
|
| 138 |
elif os.getenv("GRADIO_APP") == "texture_edit":
|
| 139 |
DELIGHT = DelightingModel()
|
| 140 |
IMAGESR_MODEL = ImageRealESRGAN(outscale=4)
|
| 141 |
+
PIPELINE_HAS_IP_ADAPTER = False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
PIPELINE = build_texture_gen_pipe(
|
| 143 |
base_ckpt_dir="./weights",
|
| 144 |
ip_adapt_scale=0,
|
|
|
|
| 667 |
sub_idxs: tuple[tuple[int]] = ((0, 1, 2), (3, 4, 5)),
|
| 668 |
req: gr.Request = None,
|
| 669 |
) -> list[str]:
|
| 670 |
+
global PIPELINE, PIPELINE_HAS_IP_ADAPTER
|
| 671 |
+
|
| 672 |
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 673 |
use_ip_adapter = True if ip_img_path and ip_adapt_scale > 0 else False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
+
if PIPELINE is None:
|
| 676 |
+
PIPELINE = build_texture_gen_pipe(
|
| 677 |
+
base_ckpt_dir="./weights",
|
| 678 |
+
ip_adapt_scale=0,
|
| 679 |
+
device="cuda",
|
| 680 |
+
)
|
| 681 |
+
|
| 682 |
+
if use_ip_adapter and not PIPELINE_HAS_IP_ADAPTER:
|
| 683 |
+
logger.info("Load IP adapter into default texture pipeline")
|
| 684 |
+
if hasattr(PIPELINE.unet, "encoder_hid_proj"):
|
| 685 |
+
PIPELINE.unet.text_encoder_hid_proj = (
|
| 686 |
+
PIPELINE.unet.encoder_hid_proj
|
| 687 |
+
)
|
| 688 |
+
PIPELINE.load_ip_adapter(
|
| 689 |
+
"./weights/Kolors-IP-Adapter-Plus",
|
| 690 |
+
subfolder="",
|
| 691 |
+
weight_name=["ip_adapter_plus_general.bin"],
|
| 692 |
+
)
|
| 693 |
+
PIPELINE_HAS_IP_ADAPTER = True
|
| 694 |
+
|
| 695 |
+
if PIPELINE_HAS_IP_ADAPTER:
|
| 696 |
+
PIPELINE.set_ip_adapter_scale(
|
| 697 |
+
[ip_adapt_scale if use_ip_adapter else 0.0]
|
| 698 |
+
)
|
| 699 |
+
|
| 700 |
+
try:
|
| 701 |
+
img_save_paths = infer_pipe(
|
| 702 |
+
index_file=f"{output_root}/condition/index.json",
|
| 703 |
+
controlnet_cond_scale=controlnet_cond_scale,
|
| 704 |
+
guidance_scale=guidance_scale,
|
| 705 |
+
strength=strength,
|
| 706 |
+
num_inference_steps=num_inference_steps,
|
| 707 |
+
ip_adapt_scale=ip_adapt_scale if use_ip_adapter else 0.0,
|
| 708 |
+
ip_img_path=ip_img_path if use_ip_adapter else None,
|
| 709 |
+
uid=uid,
|
| 710 |
+
prompt=prompt,
|
| 711 |
+
save_dir=f"{output_root}/multi_view",
|
| 712 |
+
sub_idxs=sub_idxs,
|
| 713 |
+
pipeline=PIPELINE,
|
| 714 |
+
seed=seed,
|
| 715 |
+
)
|
| 716 |
+
finally:
|
| 717 |
+
if use_ip_adapter and PIPELINE_HAS_IP_ADAPTER:
|
| 718 |
+
logger.info("Unload IP adapter from default texture pipeline")
|
| 719 |
+
if hasattr(PIPELINE, "unload_ip_adapter"):
|
| 720 |
+
PIPELINE.unload_ip_adapter()
|
| 721 |
+
else:
|
| 722 |
+
PIPELINE = None
|
| 723 |
+
PIPELINE_HAS_IP_ADAPTER = False
|
| 724 |
+
gc.collect()
|
| 725 |
+
torch.cuda.empty_cache()
|
| 726 |
|
| 727 |
return img_save_paths + img_save_paths
|
| 728 |
|
embodied_gen/scripts/render_mv.py
CHANGED
|
@@ -130,11 +130,22 @@ def infer_pipe(
|
|
| 130 |
device=device,
|
| 131 |
)
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
ip_image = Image.open(ip_img_path).convert("RGB")
|
| 135 |
ip_image = ip_image.resize(target_hw[::-1])
|
| 136 |
ip_image = [ip_image]
|
| 137 |
-
pipeline.set_ip_adapter_scale([
|
|
|
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
ip_image = None
|
| 140 |
|
|
@@ -182,7 +193,7 @@ def infer_pipe(
|
|
| 182 |
[str(item) for sublist in sub_idxs for item in sublist]
|
| 183 |
)
|
| 184 |
save_path = os.path.join(
|
| 185 |
-
save_dir, f"sample_idx{str(sub_idxs)}_ip{
|
| 186 |
)
|
| 187 |
make_image_grid(grid_image, row_num, col_num).save(save_path)
|
| 188 |
logger.info(f"Visualize in {save_path}")
|
|
|
|
| 130 |
device=device,
|
| 131 |
)
|
| 132 |
|
| 133 |
+
has_ip_adapter = any(
|
| 134 |
+
hasattr(attn_processor, "to_k_ip")
|
| 135 |
+
for attn_processor in pipeline.unet.attn_processors.values()
|
| 136 |
+
)
|
| 137 |
+
use_ip_adapter = (
|
| 138 |
+
ip_adapt_scale > 0 and ip_img_path is not None and len(ip_img_path) > 0
|
| 139 |
+
)
|
| 140 |
+
effective_ip_adapt_scale = ip_adapt_scale if use_ip_adapter else 0.0
|
| 141 |
+
if use_ip_adapter:
|
| 142 |
ip_image = Image.open(ip_img_path).convert("RGB")
|
| 143 |
ip_image = ip_image.resize(target_hw[::-1])
|
| 144 |
ip_image = [ip_image]
|
| 145 |
+
pipeline.set_ip_adapter_scale([effective_ip_adapt_scale])
|
| 146 |
+
elif has_ip_adapter:
|
| 147 |
+
ip_image = [Image.new("RGB", target_hw[::-1], color=(0, 0, 0))]
|
| 148 |
+
pipeline.set_ip_adapter_scale([0.0])
|
| 149 |
else:
|
| 150 |
ip_image = None
|
| 151 |
|
|
|
|
| 193 |
[str(item) for sublist in sub_idxs for item in sublist]
|
| 194 |
)
|
| 195 |
save_path = os.path.join(
|
| 196 |
+
save_dir, f"sample_idx{str(sub_idxs)}_ip{effective_ip_adapt_scale}.jpg"
|
| 197 |
)
|
| 198 |
make_image_grid(grid_image, row_num, col_num).save(save_path)
|
| 199 |
logger.info(f"Visualize in {save_path}")
|