Spaces:
Runtime error
Runtime error
Upload hy3dgen/texgen/utils/alignImg4Tex_utils.py with huggingface_hub
Browse files
hy3dgen/texgen/utils/alignImg4Tex_utils.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Open Source Model Licensed under the Apache License Version 2.0
|
2 |
+
# and Other Licenses of the Third-Party Components therein:
|
3 |
+
# The below Model in this distribution may have been modified by THL A29 Limited
|
4 |
+
# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
|
5 |
+
|
6 |
+
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
|
7 |
+
# The below software and/or models in this distribution may have been
|
8 |
+
# modified by THL A29 Limited ("Tencent Modifications").
|
9 |
+
# All Tencent Modifications are Copyright (C) THL A29 Limited.
|
10 |
+
|
11 |
+
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
12 |
+
# except for the third-party components listed below.
|
13 |
+
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
14 |
+
# in the repsective licenses of these third-party components.
|
15 |
+
# Users must comply with all terms and conditions of original licenses of these third-party
|
16 |
+
# components and must ensure that the usage of the third party components adheres to
|
17 |
+
# all relevant laws and regulations.
|
18 |
+
|
19 |
+
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
20 |
+
# their software and algorithms, including trained model weights, parameters (including
|
21 |
+
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
22 |
+
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
23 |
+
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
24 |
+
|
25 |
+
|
26 |
+
import torch
|
27 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
28 |
+
from diffusers import StableDiffusionControlNetPipeline, StableDiffusionXLControlNetImg2ImgPipeline, ControlNetModel, \
|
29 |
+
AutoencoderKL
|
30 |
+
|
31 |
+
|
32 |
+
class Img2img_Control_Ip_adapter:
|
33 |
+
def __init__(self, device):
|
34 |
+
controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11f1p_sd15_depth', torch_dtype=torch.float16,
|
35 |
+
variant="fp16", use_safetensors=True)
|
36 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
37 |
+
'runwayml/stable-diffusion-v1-5', controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
|
38 |
+
)
|
39 |
+
pipe.load_ip_adapter('h94/IP-Adapter', subfolder="models", weight_name="ip-adapter-plus_sd15.safetensors")
|
40 |
+
pipe.set_ip_adapter_scale(0.7)
|
41 |
+
|
42 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
43 |
+
# pipe.enable_model_cpu_offload()
|
44 |
+
self.pipe = pipe.to(device)
|
45 |
+
|
46 |
+
def __call__(
|
47 |
+
self,
|
48 |
+
prompt,
|
49 |
+
control_image,
|
50 |
+
ip_adapter_image,
|
51 |
+
negative_prompt,
|
52 |
+
height=512,
|
53 |
+
width=512,
|
54 |
+
num_inference_steps=20,
|
55 |
+
guidance_scale=8.0,
|
56 |
+
controlnet_conditioning_scale=1.0,
|
57 |
+
output_type="pil",
|
58 |
+
**kwargs,
|
59 |
+
):
|
60 |
+
results = self.pipe(
|
61 |
+
prompt=prompt,
|
62 |
+
negative_prompt=negative_prompt,
|
63 |
+
image=control_image,
|
64 |
+
ip_adapter_image=ip_adapter_image,
|
65 |
+
generator=torch.manual_seed(42),
|
66 |
+
seed=42,
|
67 |
+
num_inference_steps=num_inference_steps,
|
68 |
+
guidance_scale=guidance_scale,
|
69 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
70 |
+
strength=1,
|
71 |
+
# clip_skip=2,
|
72 |
+
height=height,
|
73 |
+
width=width,
|
74 |
+
output_type=output_type,
|
75 |
+
**kwargs,
|
76 |
+
).images[0]
|
77 |
+
return results
|
78 |
+
|
79 |
+
|
80 |
+
################################################################
|
81 |
+
|
82 |
+
class HesModel:
|
83 |
+
def __init__(self, ):
|
84 |
+
controlnet_depth = ControlNetModel.from_pretrained(
|
85 |
+
'diffusers/controlnet-depth-sdxl-1.0',
|
86 |
+
torch_dtype=torch.float16,
|
87 |
+
variant="fp16",
|
88 |
+
use_safetensors=True
|
89 |
+
)
|
90 |
+
self.pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
|
91 |
+
'stabilityai/stable-diffusion-xl-base-1.0',
|
92 |
+
torch_dtype=torch.float16,
|
93 |
+
variant="fp16",
|
94 |
+
controlnet=controlnet_depth,
|
95 |
+
use_safetensors=True,
|
96 |
+
)
|
97 |
+
self.pipe.vae = AutoencoderKL.from_pretrained(
|
98 |
+
'madebyollin/sdxl-vae-fp16-fix',
|
99 |
+
torch_dtype=torch.float16
|
100 |
+
)
|
101 |
+
|
102 |
+
self.pipe.load_ip_adapter('h94/IP-Adapter', subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors")
|
103 |
+
self.pipe.set_ip_adapter_scale(0.7)
|
104 |
+
self.pipe.to("cuda")
|
105 |
+
|
106 |
+
def __call__(self,
|
107 |
+
init_image,
|
108 |
+
control_image,
|
109 |
+
ip_adapter_image=None,
|
110 |
+
prompt='3D image',
|
111 |
+
negative_prompt='2D image',
|
112 |
+
seed=42,
|
113 |
+
strength=0.8,
|
114 |
+
num_inference_steps=40,
|
115 |
+
guidance_scale=7.5,
|
116 |
+
controlnet_conditioning_scale=0.5,
|
117 |
+
**kwargs
|
118 |
+
):
|
119 |
+
image = self.pipe(
|
120 |
+
prompt=prompt,
|
121 |
+
image=init_image,
|
122 |
+
control_image=control_image,
|
123 |
+
ip_adapter_image=ip_adapter_image,
|
124 |
+
negative_prompt=negative_prompt,
|
125 |
+
num_inference_steps=num_inference_steps,
|
126 |
+
guidance_scale=guidance_scale,
|
127 |
+
strength=strength,
|
128 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
129 |
+
seed=seed,
|
130 |
+
**kwargs
|
131 |
+
).images[0]
|
132 |
+
return image
|