Spaces:
Running
on
Zero
Running
on
Zero
Update trellis/pipelines/trellis_text_to_3d.py
Browse files- trellis/pipelines/trellis_text_to_3d.py +228 -278
trellis/pipelines/trellis_text_to_3d.py
CHANGED
@@ -1,278 +1,228 @@
|
|
1 |
-
from typing import *
|
2 |
-
import torch
|
3 |
-
import torch.nn as nn
|
4 |
-
import
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
-
from . import
|
9 |
-
from
|
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 |
-
self.
|
37 |
-
self.
|
38 |
-
self.
|
39 |
-
self.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
new_pipeline.
|
58 |
-
|
59 |
-
|
60 |
-
new_pipeline.
|
61 |
-
|
62 |
-
new_pipeline.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
"""
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
model.
|
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 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
def voxelize(self, mesh: o3d.geometry.TriangleMesh) -> torch.Tensor:
|
230 |
-
"""
|
231 |
-
Voxelize a mesh.
|
232 |
-
|
233 |
-
Args:
|
234 |
-
mesh (o3d.geometry.TriangleMesh): The mesh to voxelize.
|
235 |
-
sha256 (str): The SHA256 hash of the mesh.
|
236 |
-
output_dir (str): The output directory.
|
237 |
-
"""
|
238 |
-
vertices = np.asarray(mesh.vertices)
|
239 |
-
aabb = np.stack([vertices.min(0), vertices.max(0)])
|
240 |
-
center = (aabb[0] + aabb[1]) / 2
|
241 |
-
scale = (aabb[1] - aabb[0]).max()
|
242 |
-
vertices = (vertices - center) / scale
|
243 |
-
vertices = np.clip(vertices, -0.5 + 1e-6, 0.5 - 1e-6)
|
244 |
-
mesh.vertices = o3d.utility.Vector3dVector(vertices)
|
245 |
-
voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh_within_bounds(mesh, voxel_size=1/64, min_bound=(-0.5, -0.5, -0.5), max_bound=(0.5, 0.5, 0.5))
|
246 |
-
vertices = np.array([voxel.grid_index for voxel in voxel_grid.get_voxels()])
|
247 |
-
return torch.tensor(vertices).int().cuda()
|
248 |
-
|
249 |
-
@torch.no_grad()
|
250 |
-
def run_variant(
|
251 |
-
self,
|
252 |
-
mesh: o3d.geometry.TriangleMesh,
|
253 |
-
prompt: str,
|
254 |
-
num_samples: int = 1,
|
255 |
-
seed: int = 42,
|
256 |
-
slat_sampler_params: dict = {},
|
257 |
-
formats: List[str] = ['mesh', 'gaussian', 'radiance_field'],
|
258 |
-
) -> dict:
|
259 |
-
"""
|
260 |
-
Run the pipeline for making variants of an asset.
|
261 |
-
|
262 |
-
Args:
|
263 |
-
mesh (o3d.geometry.TriangleMesh): The base mesh.
|
264 |
-
prompt (str): The text prompt.
|
265 |
-
num_samples (int): The number of samples to generate.
|
266 |
-
seed (int): The random seed
|
267 |
-
slat_sampler_params (dict): Additional parameters for the structured latent sampler.
|
268 |
-
formats (List[str]): The formats to decode the structured latent to.
|
269 |
-
"""
|
270 |
-
cond = self.get_cond([prompt])
|
271 |
-
coords = self.voxelize(mesh)
|
272 |
-
coords = torch.cat([
|
273 |
-
torch.arange(num_samples).repeat_interleave(coords.shape[0], 0)[:, None].int().cuda(),
|
274 |
-
coords.repeat(num_samples, 1)
|
275 |
-
], 1)
|
276 |
-
torch.manual_seed(seed)
|
277 |
-
slat = self.sample_slat(cond, coords, slat_sampler_params)
|
278 |
-
return self.decode_slat(slat, formats)
|
|
|
1 |
+
from typing import *
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
import numpy as np
|
6 |
+
from transformers import CLIPTextModel, AutoTokenizer
|
7 |
+
import open3d as o3d
|
8 |
+
from .base import Pipeline
|
9 |
+
from . import samplers
|
10 |
+
from ..modules import sparse as sp
|
11 |
+
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
|
14 |
+
class TrellisTextTo3DPipeline(Pipeline):
|
15 |
+
"""
|
16 |
+
Pipeline for inferring Trellis text-to-3D models.
|
17 |
+
|
18 |
+
Args:
|
19 |
+
models (dict[str, nn.Module]): The models to use in the pipeline.
|
20 |
+
sparse_structure_sampler (samplers.Sampler): The sampler for the sparse structure.
|
21 |
+
slat_sampler (samplers.Sampler): The sampler for the structured latent.
|
22 |
+
slat_normalization (dict): The normalization parameters for the structured latent.
|
23 |
+
text_cond_model (str): The name of the text conditioning model.
|
24 |
+
"""
|
25 |
+
def __init__(
|
26 |
+
self,
|
27 |
+
models: dict[str, nn.Module] = None,
|
28 |
+
sparse_structure_sampler: samplers.Sampler = None,
|
29 |
+
slat_sampler: samplers.Sampler = None,
|
30 |
+
slat_normalization: dict = None,
|
31 |
+
text_cond_model: str = None,
|
32 |
+
):
|
33 |
+
if models is None:
|
34 |
+
return
|
35 |
+
super().__init__(models)
|
36 |
+
self.sparse_structure_sampler = sparse_structure_sampler
|
37 |
+
self.slat_sampler = slat_sampler
|
38 |
+
self.sparse_structure_sampler_params = {}
|
39 |
+
self.slat_sampler_params = {}
|
40 |
+
self.slat_normalization = slat_normalization
|
41 |
+
self._init_text_cond_model(text_cond_model)
|
42 |
+
|
43 |
+
@staticmethod
|
44 |
+
def from_pretrained(path: str) -> "TrellisTextTo3DPipeline":
|
45 |
+
"""
|
46 |
+
Load a pretrained model.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
path (str): The path to the model. Can be either local path or a Hugging Face repository.
|
50 |
+
"""
|
51 |
+
pipeline = super(TrellisTextTo3DPipeline, TrellisTextTo3DPipeline).from_pretrained(path)
|
52 |
+
new_pipeline = TrellisTextTo3DPipeline()
|
53 |
+
new_pipeline.__dict__ = pipeline.__dict__
|
54 |
+
args = pipeline._pretrained_args
|
55 |
+
|
56 |
+
new_pipeline.sparse_structure_sampler = getattr(samplers, args['sparse_structure_sampler']['name'])(**args['sparse_structure_sampler']['args'])
|
57 |
+
new_pipeline.sparse_structure_sampler_params = args['sparse_structure_sampler']['params']
|
58 |
+
|
59 |
+
new_pipeline.slat_sampler = getattr(samplers, args['slat_sampler']['name'])(**args['slat_sampler']['args'])
|
60 |
+
new_pipeline.slat_sampler_params = args['slat_sampler']['params']
|
61 |
+
|
62 |
+
new_pipeline.slat_normalization = args['slat_normalization']
|
63 |
+
|
64 |
+
new_pipeline._init_text_cond_model(args['text_cond_model'])
|
65 |
+
|
66 |
+
return new_pipeline
|
67 |
+
|
68 |
+
def _init_text_cond_model(self, name: str):
|
69 |
+
"""
|
70 |
+
Initialize the text conditioning model.
|
71 |
+
"""
|
72 |
+
# load model
|
73 |
+
model = CLIPTextModel.from_pretrained(name).to(self.device)
|
74 |
+
tokenizer = AutoTokenizer.from_pretrained(name).to(self.device)
|
75 |
+
model.eval()
|
76 |
+
self.text_cond_model = {
|
77 |
+
'model': model,
|
78 |
+
'tokenizer': tokenizer,
|
79 |
+
}
|
80 |
+
self.text_cond_model['null_cond'] = self.encode_text([''])
|
81 |
+
|
82 |
+
@torch.no_grad()
|
83 |
+
def encode_text(self, text: List[str]) -> torch.Tensor:
|
84 |
+
"""
|
85 |
+
Encode the text.
|
86 |
+
"""
|
87 |
+
assert isinstance(text, list) and all(isinstance(t, str) for t in text), "text must be a list of strings"
|
88 |
+
encoding = self.text_cond_model['tokenizer'](text, max_length=77, padding='max_length', truncation=True, return_tensors='pt')
|
89 |
+
tokens = encoding['input_ids'].to(self.device)
|
90 |
+
embeddings = self.text_cond_model['model'](input_ids=tokens).last_hidden_state
|
91 |
+
|
92 |
+
return embeddings
|
93 |
+
|
94 |
+
def get_cond(self, prompt: List[str]) -> dict:
|
95 |
+
"""
|
96 |
+
Get the conditioning information for the model.
|
97 |
+
|
98 |
+
Args:
|
99 |
+
prompt (List[str]): The text prompt.
|
100 |
+
|
101 |
+
Returns:
|
102 |
+
dict: The conditioning information
|
103 |
+
"""
|
104 |
+
cond = self.encode_text(prompt)
|
105 |
+
neg_cond = self.text_cond_model['null_cond']
|
106 |
+
return {
|
107 |
+
'cond': cond,
|
108 |
+
'neg_cond': neg_cond,
|
109 |
+
}
|
110 |
+
|
111 |
+
def sample_sparse_structure(
|
112 |
+
self,
|
113 |
+
cond: dict,
|
114 |
+
num_samples: int = 1,
|
115 |
+
sampler_params: dict = {},
|
116 |
+
) -> torch.Tensor:
|
117 |
+
"""
|
118 |
+
Sample sparse structures with the given conditioning.
|
119 |
+
|
120 |
+
Args:
|
121 |
+
cond (dict): The conditioning information.
|
122 |
+
num_samples (int): The number of samples to generate.
|
123 |
+
sampler_params (dict): Additional parameters for the sampler.
|
124 |
+
"""
|
125 |
+
# Sample occupancy latent
|
126 |
+
flow_model = self.models['sparse_structure_flow_model']
|
127 |
+
reso = flow_model.resolution
|
128 |
+
noise = torch.randn(num_samples, flow_model.in_channels, reso, reso, reso).to(self.device)
|
129 |
+
sampler_params = {**self.sparse_structure_sampler_params, **sampler_params}
|
130 |
+
z_s = self.sparse_structure_sampler.sample(
|
131 |
+
flow_model,
|
132 |
+
noise,
|
133 |
+
**cond,
|
134 |
+
**sampler_params,
|
135 |
+
verbose=True
|
136 |
+
).samples
|
137 |
+
|
138 |
+
# Decode occupancy latent
|
139 |
+
decoder = self.models['sparse_structure_decoder']
|
140 |
+
coords = torch.argwhere(decoder(z_s)>0)[:, [0, 2, 3, 4]].int()
|
141 |
+
|
142 |
+
return coords
|
143 |
+
|
144 |
+
def decode_slat(
|
145 |
+
self,
|
146 |
+
slat: sp.SparseTensor,
|
147 |
+
formats: List[str] = ['mesh', 'gaussian', 'radiance_field'],
|
148 |
+
) -> dict:
|
149 |
+
"""
|
150 |
+
Decode the structured latent.
|
151 |
+
|
152 |
+
Args:
|
153 |
+
slat (sp.SparseTensor): The structured latent.
|
154 |
+
formats (List[str]): The formats to decode the structured latent to.
|
155 |
+
|
156 |
+
Returns:
|
157 |
+
dict: The decoded structured latent.
|
158 |
+
"""
|
159 |
+
ret = {}
|
160 |
+
if 'mesh' in formats:
|
161 |
+
ret['mesh'] = self.models['slat_decoder_mesh'](slat)
|
162 |
+
if 'gaussian' in formats:
|
163 |
+
ret['gaussian'] = self.models['slat_decoder_gs'](slat)
|
164 |
+
if 'radiance_field' in formats:
|
165 |
+
ret['radiance_field'] = self.models['slat_decoder_rf'](slat)
|
166 |
+
return ret
|
167 |
+
|
168 |
+
def sample_slat(
|
169 |
+
self,
|
170 |
+
cond: dict,
|
171 |
+
coords: torch.Tensor,
|
172 |
+
sampler_params: dict = {},
|
173 |
+
) -> sp.SparseTensor:
|
174 |
+
"""
|
175 |
+
Sample structured latent with the given conditioning.
|
176 |
+
|
177 |
+
Args:
|
178 |
+
cond (dict): The conditioning information.
|
179 |
+
coords (torch.Tensor): The coordinates of the sparse structure.
|
180 |
+
sampler_params (dict): Additional parameters for the sampler.
|
181 |
+
"""
|
182 |
+
# Sample structured latent
|
183 |
+
flow_model = self.models['slat_flow_model']
|
184 |
+
noise = sp.SparseTensor(
|
185 |
+
feats=torch.randn(coords.shape[0], flow_model.in_channels).to(self.device),
|
186 |
+
coords=coords,
|
187 |
+
)
|
188 |
+
sampler_params = {**self.slat_sampler_params, **sampler_params}
|
189 |
+
slat = self.slat_sampler.sample(
|
190 |
+
flow_model,
|
191 |
+
noise,
|
192 |
+
**cond,
|
193 |
+
**sampler_params,
|
194 |
+
verbose=True
|
195 |
+
).samples
|
196 |
+
|
197 |
+
std = torch.tensor(self.slat_normalization['std'])[None].to(slat.device)
|
198 |
+
mean = torch.tensor(self.slat_normalization['mean'])[None].to(slat.device)
|
199 |
+
slat = slat * std + mean
|
200 |
+
|
201 |
+
return slat
|
202 |
+
|
203 |
+
@torch.no_grad()
|
204 |
+
def run(
|
205 |
+
self,
|
206 |
+
prompt: str,
|
207 |
+
num_samples: int = 1,
|
208 |
+
seed: int = 42,
|
209 |
+
sparse_structure_sampler_params: dict = {},
|
210 |
+
slat_sampler_params: dict = {},
|
211 |
+
formats: List[str] = ['mesh', 'gaussian', 'radiance_field'],
|
212 |
+
) -> dict:
|
213 |
+
"""
|
214 |
+
Run the pipeline.
|
215 |
+
|
216 |
+
Args:
|
217 |
+
prompt (str): The text prompt.
|
218 |
+
num_samples (int): The number of samples to generate.
|
219 |
+
seed (int): The random seed.
|
220 |
+
sparse_structure_sampler_params (dict): Additional parameters for the sparse structure sampler.
|
221 |
+
slat_sampler_params (dict): Additional parameters for the structured latent sampler.
|
222 |
+
formats (List[str]): The formats to decode the structured latent to.
|
223 |
+
"""
|
224 |
+
cond = self.get_cond([prompt])
|
225 |
+
torch.manual_seed(seed)
|
226 |
+
coords = self.sample_sparse_structure(cond, num_samples, sparse_structure_sampler_params)
|
227 |
+
slat = self.sample_slat(cond, coords, slat_sampler_params)
|
228 |
+
return self.decode_slat(slat, formats)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|