-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTRELLIS_node.py
206 lines (179 loc) · 8.09 KB
/
TRELLIS_node.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
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import numpy as np
import gc
import os
import torch
import uuid
from .app import image_to_3d
from .trellis.pipelines import TrellisImageTo3DPipeline
from .utils import glb2obj_,obj2fbx_,tensor2imglist,pre_img
import folder_paths
MAX_SEED = np.iinfo(np.int32).max
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
current_path = os.path.dirname(os.path.abspath(__file__))
weigths_dinov2_current_path = os.path.join(folder_paths.models_dir, "dinov2")
if not os.path.exists(weigths_dinov2_current_path):
os.makedirs(weigths_dinov2_current_path)
try:
folder_paths.add_model_folder_path("dinov2", weigths_dinov2_current_path, False)
except:
folder_paths.add_model_folder_path("dinov2", weigths_dinov2_current_path)
class Trellis_LoadModel:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"repo": ("STRING", {"default": "JeffreyXiang/TRELLIS-image-large"}),
"dino": (["none"] + folder_paths.get_filename_list("dinov2"),),
"attn_backend":(["xformers","flash-attn"],),
"spconv_algo":(["auto","flash-native"],),
}
}
RETURN_TYPES = ("MODEL_TRELLIS", )
RETURN_NAMES = ("model",)
FUNCTION = "main_loader"
CATEGORY = "Trellis"
def main_loader(self, repo,dino,attn_backend,spconv_algo):
if attn_backend=="xformers":
os.environ['ATTN_BACKEND'] = 'xformers'
else:
os.environ['ATTN_BACKEND'] = 'flash-attn'
if spconv_algo=="auto":
os.environ['SPCONV_ALGO'] = 'auto'
else:
os.environ['SPCONV_ALGO'] = 'native'
if dino=="none":
raise "need choice dinov2 checkpoint"
TrellisImageTo3DPipeline.dino=folder_paths.get_full_path("dinov2", dino)
TrellisImageTo3DPipeline.dino_moudel=os.path.join(current_path,"facebookresearch/dinov2")
if repo:
model=TrellisImageTo3DPipeline.from_pretrained(repo)
else:
raise "need fill repo"
return (model,)
class Trellis_Sampler:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",), # [B,H,W,C], C=3
"model": ("MODEL_TRELLIS",),
"seed": ("INT", {"default": 0, "min": 0, "max": MAX_SEED}),
"cfg": ("FLOAT", {"default": 7.5, "min": 0.0, "max": 10.0, "step": 0.1, "round": 0.01}),
"steps": ("INT", {"default": 12, "min": 1, "max": 50}),
"slat_cfg": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 10.0, "step": 0.1, "round": 0.01}),
"slat_steps": ("INT", {"default": 12, "min": 1, "max": 50}),
"preprocess_image": ("BOOLEAN", {"default": False},),
"texture_size": ("INT", {"default": 512, "min": 512, "max": 2048, "step": 512, "display": "number"}),
"mesh_simplify": ("FLOAT", {"default": 0.95, "min": 0.0, "max": 0.98, "step": 0.01}),
"mode":(["fast","opt"],),
"multi_image": ("BOOLEAN", {"default": False},),
"multiimage_algo":(["multidiffusion", "stochastic"],),
"gaussians2ply": ("BOOLEAN", {"default": False},),
"covert2video": ("BOOLEAN", {"default": False},),
"glb2obj": ("BOOLEAN", {"default": False},),
"glb2fbx": ("BOOLEAN", {"default": False},),
}
}
RETURN_TYPES = ("STRING", )
RETURN_NAMES = ("model_path",)
FUNCTION = "sampler_main"
CATEGORY = "Trellis"
def sampler_main(self, image, model, seed, cfg, steps,slat_cfg, slat_steps,preprocess_image,texture_size,mesh_simplify,mode,multi_image,multiimage_algo,gaussians2ply,covert2video,glb2obj,glb2fbx):
image_list,image_batch=tensor2imglist(image) #pil_list,batch
if multi_image and image_batch % 3 == 0:
print("********infer multi image,like Three views ******")
image_list=[image_list[i:i + 3] for i in range(0, len(image_list), 3)] #三等分列表
is_multiimage=True
else:
is_multiimage = False
trial_id = str(uuid.uuid4())
output_path = []
for i,img in enumerate(image_list):
model.cuda()
glb=image_to_3d(model,img,preprocess_image,covert2video,trial_id,seed,cfg,steps,slat_cfg,slat_steps,mesh_simplify,texture_size,mode,is_multiimage,gaussians2ply,multiimage_algo)
glb_path = f"{folder_paths.get_output_directory()}/{trial_id}_{i}.glb"
glb.export(glb_path)
output_path.append(glb_path)
model.cpu()
gc.collect()
torch.cuda.empty_cache()
print(f"glb save in {glb_path} ")
if glb2obj:
obj_paths=[]
for path in output_path:
obj_path=os.path.join(os.path.split(path)[0],os.path.split(path)[1].replace(".glb",".obj"))
glb2obj_(path, obj_path)
obj_paths.append(obj_path)
if glb2fbx:
fbx_paths=[]
for i in obj_paths:
fbx_path = os.path.join(os.path.split(i)[0], os.path.split(i)[1].replace(".obj", ".fbx"))
obj2fbx_(i, fbx_path)
fbx_paths.append(fbx_path)
output_path=fbx_paths
else:
output_path=obj_paths
else:
if glb2fbx:
obj_paths = []
fbx_paths = []
for path in output_path:
obj_path = os.path.join(os.path.split(path)[0], os.path.split(path)[1].replace(".glb", ".obj"))
glb2obj_(path, obj_path)
obj_paths.append(obj_path)
for i in obj_paths:
fbx_path = os.path.join(os.path.split(i)[0], os.path.split(i)[1].replace(".obj", ".fbx"))
obj2fbx_(i, fbx_path)
fbx_paths.append(fbx_path)
output_path = obj_paths
model_path = '\n'.join(output_path)
return (model_path,)
class Trellis_multiimage_loader:
@classmethod
def INPUT_TYPES(s):
return {"required": {"image_a": ("IMAGE",),
},
"optional": {"image_b": ("IMAGE",),
"image_c": ("IMAGE",),}
}
RETURN_TYPES = ("IMAGE",)
ETURN_NAMES = ("image",)
FUNCTION = "main_batch"
CATEGORY = "Trellis"
def main_batch(self, image_a, **kwargs):
image_b = kwargs.get("image_b")
image_c = kwargs.get("image_c")
_,height_a,_,_ = image_a.shape
if isinstance(image_b, torch.Tensor) and isinstance(image_c, torch.Tensor):
_, height_b, _, _ = image_b.shape
_, height_c, _, _ = image_c.shape
height = max(height_a, height_b, height_c)
img_list=[pre_img(image_a, height),pre_img(image_b, height),pre_img(image_c, height)]
image = torch.cat(img_list, dim=0)
elif isinstance(image_b, torch.Tensor) and not isinstance(image_c, torch.Tensor):
_, height_b, _, _ = image_b.shape
height = max(height_a, height_b,)
img_list = [pre_img(image_a, height), pre_img(image_b, height)]
image = torch.cat(img_list, dim=0)
elif not isinstance(image_b, torch.Tensor) and isinstance(image_c, torch.Tensor):
_, height_c, _, _ = image_c.shape
height = max(height_a, height_c, )
img_list = [pre_img(image_a, height), pre_img(image_b, height)]
image = torch.cat(img_list, dim=0)
else:
image=image_a
return (image,)
NODE_CLASS_MAPPINGS = {
"Trellis_LoadModel": Trellis_LoadModel,
"Trellis_Sampler": Trellis_Sampler,
"Trellis_multiimage_loader":Trellis_multiimage_loader
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Trellis_LoadModel": "Trellis_LoadModel",
"Trellis_Sampler": "Trellis_Sampler",
"Trellis_multiimage_loader":"Trellis_multiimage_loader"
}