Upload hy3dpaint/utils/multiview_utils.py with huggingface_hub
Browse files
hy3dpaint/utils/multiview_utils.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
| 2 |
+
# except for the third-party components listed below.
|
| 3 |
+
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
| 4 |
+
# in the repsective licenses of these third-party components.
|
| 5 |
+
# Users must comply with all terms and conditions of original licenses of these third-party
|
| 6 |
+
# components and must ensure that the usage of the third party components adheres to
|
| 7 |
+
# all relevant laws and regulations.
|
| 8 |
+
|
| 9 |
+
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
| 10 |
+
# their software and algorithms, including trained model weights, parameters (including
|
| 11 |
+
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
| 12 |
+
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
| 13 |
+
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import torch
|
| 17 |
+
import random
|
| 18 |
+
import numpy as np
|
| 19 |
+
from PIL import Image
|
| 20 |
+
from typing import List
|
| 21 |
+
import huggingface_hub
|
| 22 |
+
from omegaconf import OmegaConf
|
| 23 |
+
from diffusers import DiffusionPipeline
|
| 24 |
+
from diffusers import EulerAncestralDiscreteScheduler, DDIMScheduler, UniPCMultistepScheduler
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class multiviewDiffusionNet:
|
| 28 |
+
def __init__(self, config) -> None:
|
| 29 |
+
self.device = config.device
|
| 30 |
+
|
| 31 |
+
cfg_path = config.multiview_cfg_path
|
| 32 |
+
custom_pipeline = config.custom_pipeline
|
| 33 |
+
cfg = OmegaConf.load(cfg_path)
|
| 34 |
+
self.cfg = cfg
|
| 35 |
+
self.mode = self.cfg.model.params.stable_diffusion_config.custom_pipeline[2:]
|
| 36 |
+
|
| 37 |
+
model_path = huggingface_hub.snapshot_download(
|
| 38 |
+
repo_id=config.multiview_pretrained_path,
|
| 39 |
+
allow_patterns=["hunyuan3d-paintpbr-v2-1/*"],
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
model_path = os.path.join(model_path, "hunyuan3d-paintpbr-v2-1")
|
| 43 |
+
pipeline = DiffusionPipeline.from_pretrained(
|
| 44 |
+
model_path,
|
| 45 |
+
custom_pipeline=custom_pipeline,
|
| 46 |
+
torch_dtype=torch.float16
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config, timestep_spacing="trailing")
|
| 50 |
+
pipeline.set_progress_bar_config(disable=True)
|
| 51 |
+
pipeline.eval()
|
| 52 |
+
setattr(pipeline, "view_size", cfg.model.params.get("view_size", 320))
|
| 53 |
+
self.pipeline = pipeline.to(self.device)
|
| 54 |
+
|
| 55 |
+
if hasattr(self.pipeline.unet, "use_dino") and self.pipeline.unet.use_dino:
|
| 56 |
+
from hunyuanpaintpbr.unet.modules import Dino_v2
|
| 57 |
+
self.dino_v2 = Dino_v2(config.dino_ckpt_path).to(torch.float16)
|
| 58 |
+
self.dino_v2 = self.dino_v2.to(self.device)
|
| 59 |
+
|
| 60 |
+
def seed_everything(self, seed):
|
| 61 |
+
random.seed(seed)
|
| 62 |
+
np.random.seed(seed)
|
| 63 |
+
torch.manual_seed(seed)
|
| 64 |
+
os.environ["PL_GLOBAL_SEED"] = str(seed)
|
| 65 |
+
|
| 66 |
+
@torch.no_grad()
|
| 67 |
+
def __call__(self, images, conditions, prompt=None, custom_view_size=None, resize_input=False):
|
| 68 |
+
pils = self.forward_one(
|
| 69 |
+
images, conditions, prompt=prompt, custom_view_size=custom_view_size, resize_input=resize_input
|
| 70 |
+
)
|
| 71 |
+
return pils
|
| 72 |
+
|
| 73 |
+
def forward_one(self, input_images, control_images, prompt=None, custom_view_size=None, resize_input=False):
|
| 74 |
+
self.seed_everything(0)
|
| 75 |
+
custom_view_size = custom_view_size if custom_view_size is not None else self.pipeline.view_size
|
| 76 |
+
if not isinstance(input_images, List):
|
| 77 |
+
input_images = [input_images]
|
| 78 |
+
if not resize_input:
|
| 79 |
+
input_images = [
|
| 80 |
+
input_image.resize((self.pipeline.view_size, self.pipeline.view_size)) for input_image in input_images
|
| 81 |
+
]
|
| 82 |
+
else:
|
| 83 |
+
input_images = [input_image.resize((custom_view_size, custom_view_size)) for input_image in input_images]
|
| 84 |
+
for i in range(len(control_images)):
|
| 85 |
+
control_images[i] = control_images[i].resize((custom_view_size, custom_view_size))
|
| 86 |
+
if control_images[i].mode == "L":
|
| 87 |
+
control_images[i] = control_images[i].point(lambda x: 255 if x > 1 else 0, mode="1")
|
| 88 |
+
kwargs = dict(generator=torch.Generator(device=self.pipeline.device).manual_seed(0))
|
| 89 |
+
|
| 90 |
+
num_view = len(control_images) // 2
|
| 91 |
+
normal_image = [[control_images[i] for i in range(num_view)]]
|
| 92 |
+
position_image = [[control_images[i + num_view] for i in range(num_view)]]
|
| 93 |
+
|
| 94 |
+
kwargs["width"] = custom_view_size
|
| 95 |
+
kwargs["height"] = custom_view_size
|
| 96 |
+
kwargs["num_in_batch"] = num_view
|
| 97 |
+
kwargs["images_normal"] = normal_image
|
| 98 |
+
kwargs["images_position"] = position_image
|
| 99 |
+
|
| 100 |
+
if hasattr(self.pipeline.unet, "use_dino") and self.pipeline.unet.use_dino:
|
| 101 |
+
dino_hidden_states = self.dino_v2(input_images[0])
|
| 102 |
+
kwargs["dino_hidden_states"] = dino_hidden_states
|
| 103 |
+
|
| 104 |
+
sync_condition = None
|
| 105 |
+
|
| 106 |
+
infer_steps_dict = {
|
| 107 |
+
"EulerAncestralDiscreteScheduler": 30,
|
| 108 |
+
"UniPCMultistepScheduler": 15,
|
| 109 |
+
"DDIMScheduler": 50,
|
| 110 |
+
"ShiftSNRScheduler": 15,
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
mvd_image = self.pipeline(
|
| 114 |
+
input_images[0:1],
|
| 115 |
+
num_inference_steps=infer_steps_dict[self.pipeline.scheduler.__class__.__name__],
|
| 116 |
+
prompt=prompt,
|
| 117 |
+
sync_condition=sync_condition,
|
| 118 |
+
guidance_scale=3.0,
|
| 119 |
+
**kwargs,
|
| 120 |
+
).images
|
| 121 |
+
|
| 122 |
+
if "pbr" in self.mode:
|
| 123 |
+
mvd_image = {"albedo": mvd_image[:num_view], "mr": mvd_image[num_view:]}
|
| 124 |
+
# mvd_image = {'albedo':mvd_image[:num_view]}
|
| 125 |
+
else:
|
| 126 |
+
mvd_image = {"hdr": mvd_image}
|
| 127 |
+
|
| 128 |
+
return mvd_image
|