Spaces:
Running
on
Zero
Running
on
Zero
xinjie.wang
commited on
Commit
·
f4cccb0
1
Parent(s):
08b5423
update
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- Makefile +20 -0
- app.py +480 -0
- common.py +887 -0
- embodied_gen/data/backproject.py +518 -0
- embodied_gen/data/backproject_v2.py +687 -0
- embodied_gen/data/datasets.py +256 -0
- embodied_gen/data/differentiable_render.py +526 -0
- embodied_gen/data/mesh_operator.py +452 -0
- embodied_gen/data/utils.py +996 -0
- embodied_gen/models/delight_model.py +200 -0
- embodied_gen/models/gs_model.py +526 -0
- embodied_gen/models/segment_model.py +379 -0
- embodied_gen/models/sr_model.py +174 -0
- embodied_gen/models/text_model.py +160 -0
- embodied_gen/models/texture_model.py +108 -0
- embodied_gen/scripts/imageto3d.py +306 -0
- embodied_gen/scripts/render_gs.py +175 -0
- embodied_gen/scripts/render_mv.py +198 -0
- embodied_gen/scripts/text2image.py +162 -0
- embodied_gen/scripts/textto3d.sh +56 -0
- embodied_gen/scripts/texture_gen.sh +69 -0
- embodied_gen/utils/gpt_clients.py +211 -0
- embodied_gen/utils/gpt_config.yaml +14 -0
- embodied_gen/utils/process_media.py +328 -0
- embodied_gen/utils/tags.py +1 -0
- embodied_gen/validators/aesthetic_predictor.py +149 -0
- embodied_gen/validators/quality_checkers.py +242 -0
- embodied_gen/validators/urdf_convertor.py +419 -0
- requirements.txt +42 -0
- thirdparty/TRELLIS/trellis/__init__.py +6 -0
- thirdparty/TRELLIS/trellis/models/__init__.py +70 -0
- thirdparty/TRELLIS/trellis/models/sparse_structure_flow.py +200 -0
- thirdparty/TRELLIS/trellis/models/sparse_structure_vae.py +306 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_flow.py +262 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/__init__.py +4 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/base.py +117 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_gs.py +122 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_mesh.py +167 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_rf.py +104 -0
- thirdparty/TRELLIS/trellis/models/structured_latent_vae/encoder.py +72 -0
- thirdparty/TRELLIS/trellis/modules/attention/__init__.py +36 -0
- thirdparty/TRELLIS/trellis/modules/attention/full_attn.py +140 -0
- thirdparty/TRELLIS/trellis/modules/attention/modules.py +146 -0
- thirdparty/TRELLIS/trellis/modules/norm.py +25 -0
- thirdparty/TRELLIS/trellis/modules/sparse/__init__.py +102 -0
- thirdparty/TRELLIS/trellis/modules/sparse/attention/__init__.py +4 -0
- thirdparty/TRELLIS/trellis/modules/sparse/attention/full_attn.py +215 -0
- thirdparty/TRELLIS/trellis/modules/sparse/attention/modules.py +139 -0
- thirdparty/TRELLIS/trellis/modules/sparse/attention/serialized_attn.py +193 -0
- thirdparty/TRELLIS/trellis/modules/sparse/attention/windowed_attn.py +135 -0
Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
raw_root=/home/users/xinjie.wang/xinjie/asset3d-gen/
|
| 2 |
+
dirpath=/home/users/xinjie.wang/xinjie/hf_spaces/EmbodiedGen-Image-to-3D
|
| 3 |
+
cp-space-img:
|
| 4 |
+
mkdir -p ${dirpath}
|
| 5 |
+
cp -rf ${raw_root}/embodied_gen ${dirpath}/
|
| 6 |
+
mkdir -p ${dirpath}/thirdparty/TRELLIS
|
| 7 |
+
cp -rf ${raw_root}/thirdparty/TRELLIS/trellis ${dirpath}/thirdparty/TRELLIS
|
| 8 |
+
mkdir -p ${dirpath}/assets/example_image
|
| 9 |
+
cp ${raw_root}/apps/image_to_3d.py ${dirpath}/app.py
|
| 10 |
+
cp ${raw_root}/apps/common.py ${dirpath}/
|
| 11 |
+
|
| 12 |
+
cp ${raw_root}/requirements.txt ${dirpath}/
|
| 13 |
+
rm -rf ${dirpath}/thirdparty/TRELLIS/trellis/representations/mesh/flexicubes/images
|
| 14 |
+
find ${dirpath} -type d -name '__pycache__' -exec rm -r {} +
|
| 15 |
+
sed -i 's/demo.launch(server_name="10.34.8.82", server_port=8081)/demo.launch()/g' ${dirpath}/app.py
|
| 16 |
+
sed -i 's|apps/assets/example_image|assets/example_image|g' ${dirpath}/app.py
|
| 17 |
+
sed -i 's/+cu118//g' ${dirpath}/requirements.txt
|
| 18 |
+
rm -rf ${dirpath}/embodied_gen/data/backup
|
| 19 |
+
|
| 20 |
+
# disable enable_model_cpu_offload, enable_xformers_memory_efficient_attention
|
app.py
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
os.environ["GRADIO_APP"] = "imageto3d"
|
| 21 |
+
from glob import glob
|
| 22 |
+
|
| 23 |
+
import gradio as gr
|
| 24 |
+
from common import (
|
| 25 |
+
MAX_SEED,
|
| 26 |
+
VERSION,
|
| 27 |
+
active_btn_by_content,
|
| 28 |
+
end_session,
|
| 29 |
+
extract_3d_representations_v2,
|
| 30 |
+
extract_urdf,
|
| 31 |
+
get_seed,
|
| 32 |
+
image_css,
|
| 33 |
+
image_to_3d,
|
| 34 |
+
lighting_css,
|
| 35 |
+
preprocess_image_fn,
|
| 36 |
+
preprocess_sam_image_fn,
|
| 37 |
+
select_point,
|
| 38 |
+
start_session,
|
| 39 |
+
)
|
| 40 |
+
from gradio.themes import Default
|
| 41 |
+
from gradio.themes.utils.colors import slate
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(
|
| 44 |
+
delete_cache=(43200, 43200), theme=Default(primary_hue=slate)
|
| 45 |
+
) as demo:
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
f"""
|
| 48 |
+
## ***EmbodiedGen***: Image-to-3D Asset \n
|
| 49 |
+
version: {VERSION}
|
| 50 |
+
"""
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
gr.HTML(image_css)
|
| 54 |
+
gr.HTML(lighting_css)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=2):
|
| 57 |
+
with gr.Tabs() as input_tabs:
|
| 58 |
+
with gr.Tab(
|
| 59 |
+
label="Image(auto seg)", id=0
|
| 60 |
+
) as single_image_input_tab:
|
| 61 |
+
raw_image_cache = gr.Image(
|
| 62 |
+
format="png",
|
| 63 |
+
image_mode="RGB",
|
| 64 |
+
type="pil",
|
| 65 |
+
visible=False,
|
| 66 |
+
)
|
| 67 |
+
image_prompt = gr.Image(
|
| 68 |
+
label="Input Image",
|
| 69 |
+
format="png",
|
| 70 |
+
image_mode="RGBA",
|
| 71 |
+
type="pil",
|
| 72 |
+
height=400,
|
| 73 |
+
elem_classes=["image_fit"],
|
| 74 |
+
)
|
| 75 |
+
gr.Markdown(
|
| 76 |
+
"""
|
| 77 |
+
If you are not satisfied with the auto segmentation
|
| 78 |
+
result, please switch to the `Image(SAM seg)` tab."""
|
| 79 |
+
)
|
| 80 |
+
with gr.Tab(
|
| 81 |
+
label="Image(SAM seg)", id=1
|
| 82 |
+
) as samimage_input_tab:
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
image_prompt_sam = gr.Image(
|
| 86 |
+
label="Input Image",
|
| 87 |
+
type="numpy",
|
| 88 |
+
height=400,
|
| 89 |
+
elem_classes=["image_fit"],
|
| 90 |
+
)
|
| 91 |
+
image_seg_sam = gr.Image(
|
| 92 |
+
label="SAM Seg Image",
|
| 93 |
+
image_mode="RGBA",
|
| 94 |
+
type="pil",
|
| 95 |
+
height=400,
|
| 96 |
+
visible=False,
|
| 97 |
+
)
|
| 98 |
+
with gr.Column(scale=1):
|
| 99 |
+
image_mask_sam = gr.AnnotatedImage(
|
| 100 |
+
elem_classes=["image_fit"]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
fg_bg_radio = gr.Radio(
|
| 104 |
+
["foreground_point", "background_point"],
|
| 105 |
+
label="Select foreground(green) or background(red) points, by default foreground", # noqa
|
| 106 |
+
value="foreground_point",
|
| 107 |
+
)
|
| 108 |
+
gr.Markdown(
|
| 109 |
+
""" Click the `Input Image` to select SAM points,
|
| 110 |
+
after get the satisified segmentation, click `Generate`
|
| 111 |
+
button to generate the 3D asset. \n
|
| 112 |
+
Note: If the segmented foreground is too small relative
|
| 113 |
+
to the entire image area, the generation will fail.
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Accordion(label="Generation Settings", open=False):
|
| 118 |
+
with gr.Row():
|
| 119 |
+
seed = gr.Slider(
|
| 120 |
+
0, MAX_SEED, label="Seed", value=0, step=1
|
| 121 |
+
)
|
| 122 |
+
texture_size = gr.Slider(
|
| 123 |
+
1024,
|
| 124 |
+
4096,
|
| 125 |
+
label="UV texture size",
|
| 126 |
+
value=2048,
|
| 127 |
+
step=256,
|
| 128 |
+
)
|
| 129 |
+
rmbg_tag = gr.Radio(
|
| 130 |
+
choices=["rembg", "rmbg14"],
|
| 131 |
+
value="rembg",
|
| 132 |
+
label="Background Removal Model",
|
| 133 |
+
)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
randomize_seed = gr.Checkbox(
|
| 136 |
+
label="Randomize Seed", value=False
|
| 137 |
+
)
|
| 138 |
+
project_delight = gr.Checkbox(
|
| 139 |
+
label="Backproject delighting",
|
| 140 |
+
value=False,
|
| 141 |
+
)
|
| 142 |
+
gr.Markdown("Geo Structure Generation")
|
| 143 |
+
with gr.Row():
|
| 144 |
+
ss_guidance_strength = gr.Slider(
|
| 145 |
+
0.0,
|
| 146 |
+
10.0,
|
| 147 |
+
label="Guidance Strength",
|
| 148 |
+
value=7.5,
|
| 149 |
+
step=0.1,
|
| 150 |
+
)
|
| 151 |
+
ss_sampling_steps = gr.Slider(
|
| 152 |
+
1, 50, label="Sampling Steps", value=12, step=1
|
| 153 |
+
)
|
| 154 |
+
gr.Markdown("Visual Appearance Generation")
|
| 155 |
+
with gr.Row():
|
| 156 |
+
slat_guidance_strength = gr.Slider(
|
| 157 |
+
0.0,
|
| 158 |
+
10.0,
|
| 159 |
+
label="Guidance Strength",
|
| 160 |
+
value=3.0,
|
| 161 |
+
step=0.1,
|
| 162 |
+
)
|
| 163 |
+
slat_sampling_steps = gr.Slider(
|
| 164 |
+
1, 50, label="Sampling Steps", value=12, step=1
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
generate_btn = gr.Button(
|
| 168 |
+
"Generate(~0.5 mins)", variant="primary", interactive=False
|
| 169 |
+
)
|
| 170 |
+
model_output_obj = gr.Textbox(label="raw mesh .obj", visible=False)
|
| 171 |
+
with gr.Row():
|
| 172 |
+
extract_rep3d_btn = gr.Button(
|
| 173 |
+
"Extract 3D Representation(~2 mins)",
|
| 174 |
+
variant="primary",
|
| 175 |
+
interactive=False,
|
| 176 |
+
)
|
| 177 |
+
with gr.Accordion(
|
| 178 |
+
label="Enter Asset Attributes(optional)", open=False
|
| 179 |
+
):
|
| 180 |
+
asset_cat_text = gr.Textbox(
|
| 181 |
+
label="Enter Asset Category (e.g., chair)"
|
| 182 |
+
)
|
| 183 |
+
height_range_text = gr.Textbox(
|
| 184 |
+
label="Enter **Height Range** in meter (e.g., 0.5-0.6)"
|
| 185 |
+
)
|
| 186 |
+
mass_range_text = gr.Textbox(
|
| 187 |
+
label="Enter **Mass Range** in kg (e.g., 1.1-1.2)"
|
| 188 |
+
)
|
| 189 |
+
asset_version_text = gr.Textbox(
|
| 190 |
+
label=f"Enter version (e.g., {VERSION})"
|
| 191 |
+
)
|
| 192 |
+
with gr.Row():
|
| 193 |
+
extract_urdf_btn = gr.Button(
|
| 194 |
+
"Extract URDF with physics(~1 mins)",
|
| 195 |
+
variant="primary",
|
| 196 |
+
interactive=False,
|
| 197 |
+
)
|
| 198 |
+
with gr.Row():
|
| 199 |
+
gr.Markdown(
|
| 200 |
+
"#### Estimated Asset 3D Attributes(No input required)"
|
| 201 |
+
)
|
| 202 |
+
with gr.Row():
|
| 203 |
+
est_type_text = gr.Textbox(
|
| 204 |
+
label="Asset category", interactive=False
|
| 205 |
+
)
|
| 206 |
+
est_height_text = gr.Textbox(
|
| 207 |
+
label="Real height(.m)", interactive=False
|
| 208 |
+
)
|
| 209 |
+
est_mass_text = gr.Textbox(
|
| 210 |
+
label="Mass(.kg)", interactive=False
|
| 211 |
+
)
|
| 212 |
+
est_mu_text = gr.Textbox(
|
| 213 |
+
label="Friction coefficient", interactive=False
|
| 214 |
+
)
|
| 215 |
+
with gr.Row():
|
| 216 |
+
download_urdf = gr.DownloadButton(
|
| 217 |
+
label="Download URDF", variant="primary", interactive=False
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
gr.Markdown(
|
| 221 |
+
""" NOTE: If `Asset Attributes` are provided, the provided
|
| 222 |
+
properties will be used; otherwise, the GPT-preset properties
|
| 223 |
+
will be applied. \n
|
| 224 |
+
The `Download URDF` file is restored to the real scale and
|
| 225 |
+
has quality inspection, open with an editor to view details.
|
| 226 |
+
"""
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
with gr.Row() as single_image_example:
|
| 230 |
+
examples = gr.Examples(
|
| 231 |
+
label="Image Gallery",
|
| 232 |
+
examples=[
|
| 233 |
+
[image_path]
|
| 234 |
+
for image_path in sorted(
|
| 235 |
+
glob("assets/example_image/*")
|
| 236 |
+
)
|
| 237 |
+
],
|
| 238 |
+
inputs=[image_prompt, rmbg_tag],
|
| 239 |
+
fn=preprocess_image_fn,
|
| 240 |
+
outputs=[image_prompt, raw_image_cache],
|
| 241 |
+
run_on_click=True,
|
| 242 |
+
examples_per_page=10,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
with gr.Row(visible=False) as single_sam_image_example:
|
| 246 |
+
examples = gr.Examples(
|
| 247 |
+
label="Image Gallery",
|
| 248 |
+
examples=[
|
| 249 |
+
[image_path]
|
| 250 |
+
for image_path in sorted(
|
| 251 |
+
glob("assets/example_image/*")
|
| 252 |
+
)
|
| 253 |
+
],
|
| 254 |
+
inputs=[image_prompt_sam],
|
| 255 |
+
fn=preprocess_sam_image_fn,
|
| 256 |
+
outputs=[image_prompt_sam, raw_image_cache],
|
| 257 |
+
run_on_click=True,
|
| 258 |
+
examples_per_page=10,
|
| 259 |
+
)
|
| 260 |
+
with gr.Column(scale=1):
|
| 261 |
+
video_output = gr.Video(
|
| 262 |
+
label="Generated 3D Asset",
|
| 263 |
+
autoplay=True,
|
| 264 |
+
loop=True,
|
| 265 |
+
height=300,
|
| 266 |
+
)
|
| 267 |
+
model_output_gs = gr.Model3D(
|
| 268 |
+
label="Gaussian Representation", height=300, interactive=False
|
| 269 |
+
)
|
| 270 |
+
aligned_gs = gr.Textbox(visible=False)
|
| 271 |
+
gr.Markdown(
|
| 272 |
+
""" The rendering of `Gaussian Representation` takes additional 10s. """ # noqa
|
| 273 |
+
)
|
| 274 |
+
with gr.Row():
|
| 275 |
+
model_output_mesh = gr.Model3D(
|
| 276 |
+
label="Mesh Representation",
|
| 277 |
+
height=300,
|
| 278 |
+
interactive=False,
|
| 279 |
+
clear_color=[0.8, 0.8, 0.8, 1],
|
| 280 |
+
elem_id="lighter_mesh",
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
is_samimage = gr.State(False)
|
| 284 |
+
output_buf = gr.State()
|
| 285 |
+
selected_points = gr.State(value=[])
|
| 286 |
+
|
| 287 |
+
demo.load(start_session)
|
| 288 |
+
demo.unload(end_session)
|
| 289 |
+
|
| 290 |
+
single_image_input_tab.select(
|
| 291 |
+
lambda: tuple(
|
| 292 |
+
[False, gr.Row.update(visible=True), gr.Row.update(visible=False)]
|
| 293 |
+
),
|
| 294 |
+
outputs=[is_samimage, single_image_example, single_sam_image_example],
|
| 295 |
+
)
|
| 296 |
+
samimage_input_tab.select(
|
| 297 |
+
lambda: tuple(
|
| 298 |
+
[True, gr.Row.update(visible=True), gr.Row.update(visible=False)]
|
| 299 |
+
),
|
| 300 |
+
outputs=[is_samimage, single_sam_image_example, single_image_example],
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
image_prompt.upload(
|
| 304 |
+
preprocess_image_fn,
|
| 305 |
+
inputs=[image_prompt, rmbg_tag],
|
| 306 |
+
outputs=[image_prompt, raw_image_cache],
|
| 307 |
+
)
|
| 308 |
+
image_prompt.change(
|
| 309 |
+
lambda: tuple(
|
| 310 |
+
[
|
| 311 |
+
gr.Button(interactive=False),
|
| 312 |
+
gr.Button(interactive=False),
|
| 313 |
+
gr.Button(interactive=False),
|
| 314 |
+
None,
|
| 315 |
+
"",
|
| 316 |
+
None,
|
| 317 |
+
None,
|
| 318 |
+
"",
|
| 319 |
+
"",
|
| 320 |
+
"",
|
| 321 |
+
"",
|
| 322 |
+
"",
|
| 323 |
+
"",
|
| 324 |
+
"",
|
| 325 |
+
"",
|
| 326 |
+
]
|
| 327 |
+
),
|
| 328 |
+
outputs=[
|
| 329 |
+
extract_rep3d_btn,
|
| 330 |
+
extract_urdf_btn,
|
| 331 |
+
download_urdf,
|
| 332 |
+
model_output_gs,
|
| 333 |
+
aligned_gs,
|
| 334 |
+
model_output_mesh,
|
| 335 |
+
video_output,
|
| 336 |
+
asset_cat_text,
|
| 337 |
+
height_range_text,
|
| 338 |
+
mass_range_text,
|
| 339 |
+
asset_version_text,
|
| 340 |
+
est_type_text,
|
| 341 |
+
est_height_text,
|
| 342 |
+
est_mass_text,
|
| 343 |
+
est_mu_text,
|
| 344 |
+
],
|
| 345 |
+
)
|
| 346 |
+
image_prompt.change(
|
| 347 |
+
active_btn_by_content,
|
| 348 |
+
inputs=image_prompt,
|
| 349 |
+
outputs=generate_btn,
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
image_prompt_sam.upload(
|
| 353 |
+
preprocess_sam_image_fn,
|
| 354 |
+
inputs=[image_prompt_sam],
|
| 355 |
+
outputs=[image_prompt_sam, raw_image_cache],
|
| 356 |
+
)
|
| 357 |
+
image_prompt_sam.change(
|
| 358 |
+
lambda: tuple(
|
| 359 |
+
[
|
| 360 |
+
gr.Button(interactive=False),
|
| 361 |
+
gr.Button(interactive=False),
|
| 362 |
+
gr.Button(interactive=False),
|
| 363 |
+
None,
|
| 364 |
+
None,
|
| 365 |
+
None,
|
| 366 |
+
"",
|
| 367 |
+
"",
|
| 368 |
+
"",
|
| 369 |
+
"",
|
| 370 |
+
"",
|
| 371 |
+
"",
|
| 372 |
+
"",
|
| 373 |
+
"",
|
| 374 |
+
None,
|
| 375 |
+
[],
|
| 376 |
+
]
|
| 377 |
+
),
|
| 378 |
+
outputs=[
|
| 379 |
+
extract_rep3d_btn,
|
| 380 |
+
extract_urdf_btn,
|
| 381 |
+
download_urdf,
|
| 382 |
+
model_output_gs,
|
| 383 |
+
model_output_mesh,
|
| 384 |
+
video_output,
|
| 385 |
+
asset_cat_text,
|
| 386 |
+
height_range_text,
|
| 387 |
+
mass_range_text,
|
| 388 |
+
asset_version_text,
|
| 389 |
+
est_type_text,
|
| 390 |
+
est_height_text,
|
| 391 |
+
est_mass_text,
|
| 392 |
+
est_mu_text,
|
| 393 |
+
image_mask_sam,
|
| 394 |
+
selected_points,
|
| 395 |
+
],
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
image_prompt_sam.select(
|
| 399 |
+
select_point,
|
| 400 |
+
[
|
| 401 |
+
image_prompt_sam,
|
| 402 |
+
selected_points,
|
| 403 |
+
fg_bg_radio,
|
| 404 |
+
],
|
| 405 |
+
[image_mask_sam, image_seg_sam],
|
| 406 |
+
)
|
| 407 |
+
image_seg_sam.change(
|
| 408 |
+
active_btn_by_content,
|
| 409 |
+
inputs=image_seg_sam,
|
| 410 |
+
outputs=generate_btn,
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
generate_btn.click(
|
| 414 |
+
get_seed,
|
| 415 |
+
inputs=[randomize_seed, seed],
|
| 416 |
+
outputs=[seed],
|
| 417 |
+
).success(
|
| 418 |
+
image_to_3d,
|
| 419 |
+
inputs=[
|
| 420 |
+
image_prompt,
|
| 421 |
+
seed,
|
| 422 |
+
ss_guidance_strength,
|
| 423 |
+
ss_sampling_steps,
|
| 424 |
+
slat_guidance_strength,
|
| 425 |
+
slat_sampling_steps,
|
| 426 |
+
raw_image_cache,
|
| 427 |
+
image_seg_sam,
|
| 428 |
+
is_samimage,
|
| 429 |
+
],
|
| 430 |
+
outputs=[output_buf, video_output],
|
| 431 |
+
).success(
|
| 432 |
+
lambda: gr.Button(interactive=True),
|
| 433 |
+
outputs=[extract_rep3d_btn],
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
extract_rep3d_btn.click(
|
| 437 |
+
extract_3d_representations_v2,
|
| 438 |
+
inputs=[
|
| 439 |
+
output_buf,
|
| 440 |
+
project_delight,
|
| 441 |
+
texture_size,
|
| 442 |
+
],
|
| 443 |
+
outputs=[
|
| 444 |
+
model_output_mesh,
|
| 445 |
+
model_output_gs,
|
| 446 |
+
model_output_obj,
|
| 447 |
+
aligned_gs,
|
| 448 |
+
],
|
| 449 |
+
).success(
|
| 450 |
+
lambda: gr.Button(interactive=True),
|
| 451 |
+
outputs=[extract_urdf_btn],
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
extract_urdf_btn.click(
|
| 455 |
+
extract_urdf,
|
| 456 |
+
inputs=[
|
| 457 |
+
aligned_gs,
|
| 458 |
+
model_output_obj,
|
| 459 |
+
asset_cat_text,
|
| 460 |
+
height_range_text,
|
| 461 |
+
mass_range_text,
|
| 462 |
+
asset_version_text,
|
| 463 |
+
],
|
| 464 |
+
outputs=[
|
| 465 |
+
download_urdf,
|
| 466 |
+
est_type_text,
|
| 467 |
+
est_height_text,
|
| 468 |
+
est_mass_text,
|
| 469 |
+
est_mu_text,
|
| 470 |
+
],
|
| 471 |
+
queue=True,
|
| 472 |
+
show_progress="full",
|
| 473 |
+
).success(
|
| 474 |
+
lambda: gr.Button(interactive=True),
|
| 475 |
+
outputs=[download_urdf],
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
if __name__ == "__main__":
|
| 480 |
+
demo.launch(server_name="10.34.8.82", server_port=8085)
|
common.py
ADDED
|
@@ -0,0 +1,887 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
import gc
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import shutil
|
| 21 |
+
import subprocess
|
| 22 |
+
import sys
|
| 23 |
+
from glob import glob
|
| 24 |
+
|
| 25 |
+
import cv2
|
| 26 |
+
import gradio as gr
|
| 27 |
+
import numpy as np
|
| 28 |
+
import spaces
|
| 29 |
+
import torch
|
| 30 |
+
import torch.nn.functional as F
|
| 31 |
+
import trimesh
|
| 32 |
+
from easydict import EasyDict as edict
|
| 33 |
+
from PIL import Image
|
| 34 |
+
from embodied_gen.data.backproject_v2 import entrypoint as backproject_api
|
| 35 |
+
from embodied_gen.data.differentiable_render import entrypoint as render_api
|
| 36 |
+
from embodied_gen.data.utils import trellis_preprocess
|
| 37 |
+
from embodied_gen.models.delight_model import DelightingModel
|
| 38 |
+
from embodied_gen.models.gs_model import GaussianOperator
|
| 39 |
+
from embodied_gen.models.segment_model import (
|
| 40 |
+
BMGG14Remover,
|
| 41 |
+
RembgRemover,
|
| 42 |
+
SAMPredictor,
|
| 43 |
+
)
|
| 44 |
+
from embodied_gen.models.sr_model import ImageRealESRGAN, ImageStableSR
|
| 45 |
+
from embodied_gen.scripts.render_gs import entrypoint as render_gs_api
|
| 46 |
+
from embodied_gen.scripts.render_mv import build_texture_gen_pipe, infer_pipe
|
| 47 |
+
from embodied_gen.scripts.text2image import (
|
| 48 |
+
build_text2img_ip_pipeline,
|
| 49 |
+
build_text2img_pipeline,
|
| 50 |
+
text2img_gen,
|
| 51 |
+
)
|
| 52 |
+
from embodied_gen.utils.gpt_clients import GPT_CLIENT
|
| 53 |
+
from embodied_gen.utils.process_media import (
|
| 54 |
+
filter_image_small_connected_components,
|
| 55 |
+
merge_images_video,
|
| 56 |
+
render_video,
|
| 57 |
+
)
|
| 58 |
+
from embodied_gen.utils.tags import VERSION
|
| 59 |
+
from embodied_gen.validators.quality_checkers import (
|
| 60 |
+
BaseChecker,
|
| 61 |
+
ImageAestheticChecker,
|
| 62 |
+
ImageSegChecker,
|
| 63 |
+
MeshGeoChecker,
|
| 64 |
+
)
|
| 65 |
+
from embodied_gen.validators.urdf_convertor import URDFGenerator, zip_files
|
| 66 |
+
|
| 67 |
+
current_file_path = os.path.abspath(__file__)
|
| 68 |
+
current_dir = os.path.dirname(current_file_path)
|
| 69 |
+
sys.path.append(os.path.join(current_dir, ".."))
|
| 70 |
+
from thirdparty.TRELLIS.trellis.pipelines import TrellisImageTo3DPipeline
|
| 71 |
+
from thirdparty.TRELLIS.trellis.representations import (
|
| 72 |
+
Gaussian,
|
| 73 |
+
MeshExtractResult,
|
| 74 |
+
)
|
| 75 |
+
from thirdparty.TRELLIS.trellis.representations.gaussian.general_utils import (
|
| 76 |
+
build_scaling_rotation,
|
| 77 |
+
inverse_sigmoid,
|
| 78 |
+
strip_symmetric,
|
| 79 |
+
)
|
| 80 |
+
from thirdparty.TRELLIS.trellis.utils import postprocessing_utils
|
| 81 |
+
|
| 82 |
+
logging.basicConfig(
|
| 83 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 84 |
+
)
|
| 85 |
+
logger = logging.getLogger(__name__)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
os.environ["TORCH_EXTENSIONS_DIR"] = os.path.expanduser(
|
| 89 |
+
"~/.cache/torch_extensions"
|
| 90 |
+
)
|
| 91 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "false"
|
| 92 |
+
os.environ["SPCONV_ALGO"] = "native"
|
| 93 |
+
|
| 94 |
+
MAX_SEED = 100000
|
| 95 |
+
DELIGHT = DelightingModel()
|
| 96 |
+
IMAGESR_MODEL = ImageRealESRGAN(outscale=4)
|
| 97 |
+
# IMAGESR_MODEL = ImageStableSR()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def patched_setup_functions(self):
|
| 101 |
+
def inverse_softplus(x):
|
| 102 |
+
return x + torch.log(-torch.expm1(-x))
|
| 103 |
+
|
| 104 |
+
def build_covariance_from_scaling_rotation(
|
| 105 |
+
scaling, scaling_modifier, rotation
|
| 106 |
+
):
|
| 107 |
+
L = build_scaling_rotation(scaling_modifier * scaling, rotation)
|
| 108 |
+
actual_covariance = L @ L.transpose(1, 2)
|
| 109 |
+
symm = strip_symmetric(actual_covariance)
|
| 110 |
+
return symm
|
| 111 |
+
|
| 112 |
+
if self.scaling_activation_type == "exp":
|
| 113 |
+
self.scaling_activation = torch.exp
|
| 114 |
+
self.inverse_scaling_activation = torch.log
|
| 115 |
+
elif self.scaling_activation_type == "softplus":
|
| 116 |
+
self.scaling_activation = F.softplus
|
| 117 |
+
self.inverse_scaling_activation = inverse_softplus
|
| 118 |
+
|
| 119 |
+
self.covariance_activation = build_covariance_from_scaling_rotation
|
| 120 |
+
self.opacity_activation = torch.sigmoid
|
| 121 |
+
self.inverse_opacity_activation = inverse_sigmoid
|
| 122 |
+
self.rotation_activation = F.normalize
|
| 123 |
+
|
| 124 |
+
self.scale_bias = self.inverse_scaling_activation(
|
| 125 |
+
torch.tensor(self.scaling_bias)
|
| 126 |
+
).to(self.device)
|
| 127 |
+
self.rots_bias = torch.zeros((4)).to(self.device)
|
| 128 |
+
self.rots_bias[0] = 1
|
| 129 |
+
self.opacity_bias = self.inverse_opacity_activation(
|
| 130 |
+
torch.tensor(self.opacity_bias)
|
| 131 |
+
).to(self.device)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
Gaussian.setup_functions = patched_setup_functions
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def download_kolors_weights() -> None:
|
| 138 |
+
logger.info(f"Download kolors weights from huggingface...")
|
| 139 |
+
subprocess.run(
|
| 140 |
+
[
|
| 141 |
+
"huggingface-cli",
|
| 142 |
+
"download",
|
| 143 |
+
"--resume-download",
|
| 144 |
+
"Kwai-Kolors/Kolors",
|
| 145 |
+
"--local-dir",
|
| 146 |
+
"weights/Kolors",
|
| 147 |
+
],
|
| 148 |
+
check=True,
|
| 149 |
+
)
|
| 150 |
+
subprocess.run(
|
| 151 |
+
[
|
| 152 |
+
"huggingface-cli",
|
| 153 |
+
"download",
|
| 154 |
+
"--resume-download",
|
| 155 |
+
"Kwai-Kolors/Kolors-IP-Adapter-Plus",
|
| 156 |
+
"--local-dir",
|
| 157 |
+
"weights/Kolors-IP-Adapter-Plus",
|
| 158 |
+
],
|
| 159 |
+
check=True,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
if os.getenv("GRADIO_APP") == "imageto3d":
|
| 164 |
+
RBG_REMOVER = RembgRemover()
|
| 165 |
+
RBG14_REMOVER = BMGG14Remover()
|
| 166 |
+
SAM_PREDICTOR = SAMPredictor(model_type="vit_h", device="cpu")
|
| 167 |
+
PIPELINE = TrellisImageTo3DPipeline.from_pretrained(
|
| 168 |
+
"jetx/trellis-image-large"
|
| 169 |
+
)
|
| 170 |
+
# PIPELINE.cuda()
|
| 171 |
+
SEG_CHECKER = ImageSegChecker(GPT_CLIENT)
|
| 172 |
+
GEO_CHECKER = MeshGeoChecker(GPT_CLIENT)
|
| 173 |
+
AESTHETIC_CHECKER = ImageAestheticChecker()
|
| 174 |
+
CHECKERS = [GEO_CHECKER, SEG_CHECKER, AESTHETIC_CHECKER]
|
| 175 |
+
TMP_DIR = os.path.join(
|
| 176 |
+
os.path.dirname(os.path.abspath(__file__)), "sessions/imageto3d"
|
| 177 |
+
)
|
| 178 |
+
elif os.getenv("GRADIO_APP") == "textto3d":
|
| 179 |
+
RBG_REMOVER = RembgRemover()
|
| 180 |
+
RBG14_REMOVER = BMGG14Remover()
|
| 181 |
+
PIPELINE = TrellisImageTo3DPipeline.from_pretrained(
|
| 182 |
+
"jetx/trellis-image-large"
|
| 183 |
+
)
|
| 184 |
+
# PIPELINE.cuda()
|
| 185 |
+
text_model_dir = "weights/Kolors"
|
| 186 |
+
if not os.path.exists(text_model_dir):
|
| 187 |
+
download_kolors_weights()
|
| 188 |
+
|
| 189 |
+
PIPELINE_IMG_IP = build_text2img_ip_pipeline(text_model_dir, ref_scale=0.3)
|
| 190 |
+
PIPELINE_IMG = build_text2img_pipeline(text_model_dir)
|
| 191 |
+
SEG_CHECKER = ImageSegChecker(GPT_CLIENT)
|
| 192 |
+
GEO_CHECKER = MeshGeoChecker(GPT_CLIENT)
|
| 193 |
+
AESTHETIC_CHECKER = ImageAestheticChecker()
|
| 194 |
+
CHECKERS = [GEO_CHECKER, SEG_CHECKER, AESTHETIC_CHECKER]
|
| 195 |
+
TMP_DIR = os.path.join(
|
| 196 |
+
os.path.dirname(os.path.abspath(__file__)), "sessions/textto3d"
|
| 197 |
+
)
|
| 198 |
+
elif os.getenv("GRADIO_APP") == "texture_edit":
|
| 199 |
+
if not os.path.exists("weights/Kolors"):
|
| 200 |
+
download_kolors_weights()
|
| 201 |
+
|
| 202 |
+
PIPELINE_IP = build_texture_gen_pipe(
|
| 203 |
+
base_ckpt_dir="./weights",
|
| 204 |
+
ip_adapt_scale=0.7,
|
| 205 |
+
device="cuda",
|
| 206 |
+
)
|
| 207 |
+
PIPELINE = build_texture_gen_pipe(
|
| 208 |
+
base_ckpt_dir="./weights",
|
| 209 |
+
ip_adapt_scale=0,
|
| 210 |
+
device="cuda",
|
| 211 |
+
)
|
| 212 |
+
TMP_DIR = os.path.join(
|
| 213 |
+
os.path.dirname(os.path.abspath(__file__)), "sessions/texture_edit"
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
lighting_css = """
|
| 220 |
+
<style>
|
| 221 |
+
#lighter_mesh canvas {
|
| 222 |
+
filter: brightness(1.8) !important;
|
| 223 |
+
}
|
| 224 |
+
</style>
|
| 225 |
+
"""
|
| 226 |
+
|
| 227 |
+
image_css = """
|
| 228 |
+
<style>
|
| 229 |
+
.image_fit .image-frame {
|
| 230 |
+
object-fit: contain !important;
|
| 231 |
+
height: 100% !important;
|
| 232 |
+
}
|
| 233 |
+
</style>
|
| 234 |
+
"""
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def start_session(req: gr.Request) -> None:
|
| 238 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 239 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def end_session(req: gr.Request) -> None:
|
| 243 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 244 |
+
if os.path.exists(user_dir):
|
| 245 |
+
shutil.rmtree(user_dir)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
@spaces.GPU
|
| 249 |
+
def preprocess_image_fn(
|
| 250 |
+
image: str | np.ndarray | Image.Image, rmbg_tag: str = "rembg"
|
| 251 |
+
) -> tuple[Image.Image, Image.Image]:
|
| 252 |
+
if isinstance(image, str):
|
| 253 |
+
image = Image.open(image)
|
| 254 |
+
elif isinstance(image, np.ndarray):
|
| 255 |
+
image = Image.fromarray(image)
|
| 256 |
+
|
| 257 |
+
image_cache = image.copy().resize((512, 512))
|
| 258 |
+
|
| 259 |
+
bg_remover = RBG_REMOVER if rmbg_tag == "rembg" else RBG14_REMOVER
|
| 260 |
+
image = bg_remover(image)
|
| 261 |
+
image = trellis_preprocess(image)
|
| 262 |
+
|
| 263 |
+
return image, image_cache
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def preprocess_sam_image_fn(
|
| 267 |
+
image: Image.Image,
|
| 268 |
+
) -> tuple[Image.Image, Image.Image]:
|
| 269 |
+
if isinstance(image, np.ndarray):
|
| 270 |
+
image = Image.fromarray(image)
|
| 271 |
+
|
| 272 |
+
sam_image = SAM_PREDICTOR.preprocess_image(image)
|
| 273 |
+
image_cache = Image.fromarray(sam_image).resize((512, 512))
|
| 274 |
+
SAM_PREDICTOR.predictor.set_image(sam_image)
|
| 275 |
+
|
| 276 |
+
return sam_image, image_cache
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def active_btn_by_content(content: gr.Image) -> gr.Button:
|
| 280 |
+
interactive = True if content is not None else False
|
| 281 |
+
|
| 282 |
+
return gr.Button(interactive=interactive)
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
def active_btn_by_text_content(content: gr.Textbox) -> gr.Button:
|
| 286 |
+
if content is not None and len(content) > 0:
|
| 287 |
+
interactive = True
|
| 288 |
+
else:
|
| 289 |
+
interactive = False
|
| 290 |
+
|
| 291 |
+
return gr.Button(interactive=interactive)
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def get_selected_image(
|
| 295 |
+
choice: str, sample1: str, sample2: str, sample3: str
|
| 296 |
+
) -> str:
|
| 297 |
+
if choice == "sample1":
|
| 298 |
+
return sample1
|
| 299 |
+
elif choice == "sample2":
|
| 300 |
+
return sample2
|
| 301 |
+
elif choice == "sample3":
|
| 302 |
+
return sample3
|
| 303 |
+
else:
|
| 304 |
+
raise ValueError(f"Invalid choice: {choice}")
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def get_cached_image(image_path: str) -> Image.Image:
|
| 308 |
+
if isinstance(image_path, Image.Image):
|
| 309 |
+
return image_path
|
| 310 |
+
return Image.open(image_path).resize((512, 512))
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
@spaces.GPU
|
| 314 |
+
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
| 315 |
+
return {
|
| 316 |
+
"gaussian": {
|
| 317 |
+
**gs.init_params,
|
| 318 |
+
"_xyz": gs._xyz.cpu().numpy(),
|
| 319 |
+
"_features_dc": gs._features_dc.cpu().numpy(),
|
| 320 |
+
"_scaling": gs._scaling.cpu().numpy(),
|
| 321 |
+
"_rotation": gs._rotation.cpu().numpy(),
|
| 322 |
+
"_opacity": gs._opacity.cpu().numpy(),
|
| 323 |
+
},
|
| 324 |
+
"mesh": {
|
| 325 |
+
"vertices": mesh.vertices.cpu().numpy(),
|
| 326 |
+
"faces": mesh.faces.cpu().numpy(),
|
| 327 |
+
},
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def unpack_state(state: dict, device: str = "cpu") -> tuple[Gaussian, dict]:
|
| 332 |
+
gs = Gaussian(
|
| 333 |
+
aabb=state["gaussian"]["aabb"],
|
| 334 |
+
sh_degree=state["gaussian"]["sh_degree"],
|
| 335 |
+
mininum_kernel_size=state["gaussian"]["mininum_kernel_size"],
|
| 336 |
+
scaling_bias=state["gaussian"]["scaling_bias"],
|
| 337 |
+
opacity_bias=state["gaussian"]["opacity_bias"],
|
| 338 |
+
scaling_activation=state["gaussian"]["scaling_activation"],
|
| 339 |
+
device=device,
|
| 340 |
+
)
|
| 341 |
+
gs._xyz = torch.tensor(state["gaussian"]["_xyz"], device=device)
|
| 342 |
+
gs._features_dc = torch.tensor(
|
| 343 |
+
state["gaussian"]["_features_dc"], device=device
|
| 344 |
+
)
|
| 345 |
+
gs._scaling = torch.tensor(state["gaussian"]["_scaling"], device=device)
|
| 346 |
+
gs._rotation = torch.tensor(state["gaussian"]["_rotation"], device=device)
|
| 347 |
+
gs._opacity = torch.tensor(state["gaussian"]["_opacity"], device=device)
|
| 348 |
+
|
| 349 |
+
mesh = edict(
|
| 350 |
+
vertices=torch.tensor(state["mesh"]["vertices"], device=device),
|
| 351 |
+
faces=torch.tensor(state["mesh"]["faces"], device=device),
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
return gs, mesh
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
def get_seed(randomize_seed: bool, seed: int, max_seed: int = MAX_SEED) -> int:
|
| 358 |
+
return np.random.randint(0, max_seed) if randomize_seed else seed
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def select_point(
|
| 362 |
+
image: np.ndarray,
|
| 363 |
+
sel_pix: list,
|
| 364 |
+
point_type: str,
|
| 365 |
+
evt: gr.SelectData,
|
| 366 |
+
):
|
| 367 |
+
if point_type == "foreground_point":
|
| 368 |
+
sel_pix.append((evt.index, 1)) # append the foreground_point
|
| 369 |
+
elif point_type == "background_point":
|
| 370 |
+
sel_pix.append((evt.index, 0)) # append the background_point
|
| 371 |
+
else:
|
| 372 |
+
sel_pix.append((evt.index, 1)) # default foreground_point
|
| 373 |
+
|
| 374 |
+
masks = SAM_PREDICTOR.generate_masks(image, sel_pix)
|
| 375 |
+
seg_image = SAM_PREDICTOR.get_segmented_image(image, masks)
|
| 376 |
+
|
| 377 |
+
for point, label in sel_pix:
|
| 378 |
+
color = (255, 0, 0) if label == 0 else (0, 255, 0)
|
| 379 |
+
marker_type = 1 if label == 0 else 5
|
| 380 |
+
cv2.drawMarker(
|
| 381 |
+
image,
|
| 382 |
+
point,
|
| 383 |
+
color,
|
| 384 |
+
markerType=marker_type,
|
| 385 |
+
markerSize=15,
|
| 386 |
+
thickness=10,
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
torch.cuda.empty_cache()
|
| 390 |
+
|
| 391 |
+
return (image, masks), seg_image
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
@spaces.GPU
|
| 395 |
+
def image_to_3d(
|
| 396 |
+
image: Image.Image,
|
| 397 |
+
seed: int,
|
| 398 |
+
ss_guidance_strength: float,
|
| 399 |
+
ss_sampling_steps: int,
|
| 400 |
+
slat_guidance_strength: float,
|
| 401 |
+
slat_sampling_steps: int,
|
| 402 |
+
raw_image_cache: Image.Image,
|
| 403 |
+
sam_image: Image.Image = None,
|
| 404 |
+
is_sam_image: bool = False,
|
| 405 |
+
req: gr.Request = None,
|
| 406 |
+
) -> tuple[dict, str]:
|
| 407 |
+
if is_sam_image:
|
| 408 |
+
seg_image = filter_image_small_connected_components(sam_image)
|
| 409 |
+
seg_image = Image.fromarray(seg_image, mode="RGBA")
|
| 410 |
+
seg_image = trellis_preprocess(seg_image)
|
| 411 |
+
else:
|
| 412 |
+
seg_image = image
|
| 413 |
+
|
| 414 |
+
if isinstance(seg_image, np.ndarray):
|
| 415 |
+
seg_image = Image.fromarray(seg_image)
|
| 416 |
+
|
| 417 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 418 |
+
os.makedirs(output_root, exist_ok=True)
|
| 419 |
+
seg_image.save(f"{output_root}/seg_image.png")
|
| 420 |
+
raw_image_cache.save(f"{output_root}/raw_image.png")
|
| 421 |
+
PIPELINE.cuda()
|
| 422 |
+
outputs = PIPELINE.run(
|
| 423 |
+
seg_image,
|
| 424 |
+
seed=seed,
|
| 425 |
+
formats=["gaussian", "mesh"],
|
| 426 |
+
preprocess_image=False,
|
| 427 |
+
sparse_structure_sampler_params={
|
| 428 |
+
"steps": ss_sampling_steps,
|
| 429 |
+
"cfg_strength": ss_guidance_strength,
|
| 430 |
+
},
|
| 431 |
+
slat_sampler_params={
|
| 432 |
+
"steps": slat_sampling_steps,
|
| 433 |
+
"cfg_strength": slat_guidance_strength,
|
| 434 |
+
},
|
| 435 |
+
)
|
| 436 |
+
# Set to cpu for memory saving.
|
| 437 |
+
PIPELINE.cpu()
|
| 438 |
+
|
| 439 |
+
gs_model = outputs["gaussian"][0]
|
| 440 |
+
mesh_model = outputs["mesh"][0]
|
| 441 |
+
color_images = render_video(gs_model)["color"]
|
| 442 |
+
normal_images = render_video(mesh_model)["normal"]
|
| 443 |
+
|
| 444 |
+
video_path = os.path.join(output_root, "gs_mesh.mp4")
|
| 445 |
+
merge_images_video(color_images, normal_images, video_path)
|
| 446 |
+
state = pack_state(gs_model, mesh_model)
|
| 447 |
+
|
| 448 |
+
gc.collect()
|
| 449 |
+
torch.cuda.empty_cache()
|
| 450 |
+
|
| 451 |
+
return state, video_path
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
@spaces.GPU
|
| 455 |
+
def extract_3d_representations(
|
| 456 |
+
state: dict, enable_delight: bool, texture_size: int, req: gr.Request
|
| 457 |
+
):
|
| 458 |
+
output_root = TMP_DIR
|
| 459 |
+
output_root = os.path.join(output_root, str(req.session_hash))
|
| 460 |
+
gs_model, mesh_model = unpack_state(state, device="cuda")
|
| 461 |
+
|
| 462 |
+
mesh = postprocessing_utils.to_glb(
|
| 463 |
+
gs_model,
|
| 464 |
+
mesh_model,
|
| 465 |
+
simplify=0.9,
|
| 466 |
+
texture_size=1024,
|
| 467 |
+
verbose=True,
|
| 468 |
+
)
|
| 469 |
+
filename = "sample"
|
| 470 |
+
gs_path = os.path.join(output_root, f"{filename}_gs.ply")
|
| 471 |
+
gs_model.save_ply(gs_path)
|
| 472 |
+
|
| 473 |
+
# Rotate mesh and GS by 90 degrees around Z-axis.
|
| 474 |
+
rot_matrix = [[0, 0, -1], [0, 1, 0], [1, 0, 0]]
|
| 475 |
+
# Addtional rotation for GS to align mesh.
|
| 476 |
+
gs_rot = np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]]) @ np.array(
|
| 477 |
+
rot_matrix
|
| 478 |
+
)
|
| 479 |
+
pose = GaussianOperator.trans_to_quatpose(gs_rot)
|
| 480 |
+
aligned_gs_path = gs_path.replace(".ply", "_aligned.ply")
|
| 481 |
+
GaussianOperator.resave_ply(
|
| 482 |
+
in_ply=gs_path,
|
| 483 |
+
out_ply=aligned_gs_path,
|
| 484 |
+
instance_pose=pose,
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
mesh.vertices = mesh.vertices @ np.array(rot_matrix)
|
| 488 |
+
mesh_obj_path = os.path.join(output_root, f"{filename}.obj")
|
| 489 |
+
mesh.export(mesh_obj_path)
|
| 490 |
+
mesh_glb_path = os.path.join(output_root, f"{filename}.glb")
|
| 491 |
+
mesh.export(mesh_glb_path)
|
| 492 |
+
|
| 493 |
+
torch.cuda.empty_cache()
|
| 494 |
+
|
| 495 |
+
return mesh_glb_path, gs_path, mesh_obj_path, aligned_gs_path
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
def extract_3d_representations_v2(
|
| 499 |
+
state: dict,
|
| 500 |
+
enable_delight: bool,
|
| 501 |
+
texture_size: int,
|
| 502 |
+
req: gr.Request,
|
| 503 |
+
):
|
| 504 |
+
output_root = TMP_DIR
|
| 505 |
+
user_dir = os.path.join(output_root, str(req.session_hash))
|
| 506 |
+
gs_model, mesh_model = unpack_state(state, device="cpu")
|
| 507 |
+
|
| 508 |
+
filename = "sample"
|
| 509 |
+
gs_path = os.path.join(user_dir, f"{filename}_gs.ply")
|
| 510 |
+
gs_model.save_ply(gs_path)
|
| 511 |
+
|
| 512 |
+
# Rotate mesh and GS by 90 degrees around Z-axis.
|
| 513 |
+
rot_matrix = [[0, 0, -1], [0, 1, 0], [1, 0, 0]]
|
| 514 |
+
gs_add_rot = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
|
| 515 |
+
mesh_add_rot = [[1, 0, 0], [0, 0, -1], [0, 1, 0]]
|
| 516 |
+
|
| 517 |
+
# Addtional rotation for GS to align mesh.
|
| 518 |
+
gs_rot = np.array(gs_add_rot) @ np.array(rot_matrix)
|
| 519 |
+
pose = GaussianOperator.trans_to_quatpose(gs_rot)
|
| 520 |
+
aligned_gs_path = gs_path.replace(".ply", "_aligned.ply")
|
| 521 |
+
GaussianOperator.resave_ply(
|
| 522 |
+
in_ply=gs_path,
|
| 523 |
+
out_ply=aligned_gs_path,
|
| 524 |
+
instance_pose=pose,
|
| 525 |
+
device="cpu",
|
| 526 |
+
)
|
| 527 |
+
color_path = os.path.join(user_dir, "color.png")
|
| 528 |
+
render_gs_api(aligned_gs_path, color_path)
|
| 529 |
+
|
| 530 |
+
mesh = trimesh.Trimesh(
|
| 531 |
+
vertices=mesh_model.vertices.cpu().numpy(),
|
| 532 |
+
faces=mesh_model.faces.cpu().numpy(),
|
| 533 |
+
)
|
| 534 |
+
mesh.vertices = mesh.vertices @ np.array(mesh_add_rot)
|
| 535 |
+
mesh.vertices = mesh.vertices @ np.array(rot_matrix)
|
| 536 |
+
|
| 537 |
+
mesh_obj_path = os.path.join(user_dir, f"{filename}.obj")
|
| 538 |
+
mesh.export(mesh_obj_path)
|
| 539 |
+
|
| 540 |
+
mesh = backproject_api(
|
| 541 |
+
delight_model=DELIGHT,
|
| 542 |
+
imagesr_model=IMAGESR_MODEL,
|
| 543 |
+
color_path=color_path,
|
| 544 |
+
mesh_path=mesh_obj_path,
|
| 545 |
+
output_path=mesh_obj_path,
|
| 546 |
+
skip_fix_mesh=False,
|
| 547 |
+
delight=enable_delight,
|
| 548 |
+
texture_wh=[texture_size, texture_size],
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
mesh_glb_path = os.path.join(user_dir, f"{filename}.glb")
|
| 552 |
+
mesh.export(mesh_glb_path)
|
| 553 |
+
|
| 554 |
+
return mesh_glb_path, gs_path, mesh_obj_path, aligned_gs_path
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
def extract_urdf(
|
| 558 |
+
gs_path: str,
|
| 559 |
+
mesh_obj_path: str,
|
| 560 |
+
asset_cat_text: str,
|
| 561 |
+
height_range_text: str,
|
| 562 |
+
mass_range_text: str,
|
| 563 |
+
asset_version_text: str,
|
| 564 |
+
req: gr.Request = None,
|
| 565 |
+
):
|
| 566 |
+
output_root = TMP_DIR
|
| 567 |
+
if req is not None:
|
| 568 |
+
output_root = os.path.join(output_root, str(req.session_hash))
|
| 569 |
+
|
| 570 |
+
# Convert to URDF and recover attrs by GPT.
|
| 571 |
+
filename = "sample"
|
| 572 |
+
urdf_convertor = URDFGenerator(GPT_CLIENT, render_view_num=4)
|
| 573 |
+
asset_attrs = {
|
| 574 |
+
"version": VERSION,
|
| 575 |
+
"gs_model": f"{urdf_convertor.output_mesh_dir}/{filename}_gs.ply",
|
| 576 |
+
}
|
| 577 |
+
if asset_version_text:
|
| 578 |
+
asset_attrs["version"] = asset_version_text
|
| 579 |
+
if asset_cat_text:
|
| 580 |
+
asset_attrs["category"] = asset_cat_text.lower()
|
| 581 |
+
if height_range_text:
|
| 582 |
+
try:
|
| 583 |
+
min_height, max_height = map(float, height_range_text.split("-"))
|
| 584 |
+
asset_attrs["min_height"] = min_height
|
| 585 |
+
asset_attrs["max_height"] = max_height
|
| 586 |
+
except ValueError:
|
| 587 |
+
return "Invalid height input format. Use the format: min-max."
|
| 588 |
+
if mass_range_text:
|
| 589 |
+
try:
|
| 590 |
+
min_mass, max_mass = map(float, mass_range_text.split("-"))
|
| 591 |
+
asset_attrs["min_mass"] = min_mass
|
| 592 |
+
asset_attrs["max_mass"] = max_mass
|
| 593 |
+
except ValueError:
|
| 594 |
+
return "Invalid mass input format. Use the format: min-max."
|
| 595 |
+
|
| 596 |
+
urdf_path = urdf_convertor(
|
| 597 |
+
mesh_path=mesh_obj_path,
|
| 598 |
+
output_root=f"{output_root}/URDF_{filename}",
|
| 599 |
+
**asset_attrs,
|
| 600 |
+
)
|
| 601 |
+
|
| 602 |
+
# Rescale GS and save to URDF/mesh folder.
|
| 603 |
+
real_height = urdf_convertor.get_attr_from_urdf(
|
| 604 |
+
urdf_path, attr_name="real_height"
|
| 605 |
+
)
|
| 606 |
+
out_gs = f"{output_root}/URDF_{filename}/{urdf_convertor.output_mesh_dir}/{filename}_gs.ply" # noqa
|
| 607 |
+
GaussianOperator.resave_ply(
|
| 608 |
+
in_ply=gs_path,
|
| 609 |
+
out_ply=out_gs,
|
| 610 |
+
real_height=real_height,
|
| 611 |
+
device="cpu",
|
| 612 |
+
)
|
| 613 |
+
|
| 614 |
+
# Quality check and update .urdf file.
|
| 615 |
+
mesh_out = f"{output_root}/URDF_{filename}/{urdf_convertor.output_mesh_dir}/{filename}.obj" # noqa
|
| 616 |
+
trimesh.load(mesh_out).export(mesh_out.replace(".obj", ".glb"))
|
| 617 |
+
# image_paths = render_asset3d(
|
| 618 |
+
# mesh_path=mesh_out,
|
| 619 |
+
# output_root=f"{output_root}/URDF_{filename}",
|
| 620 |
+
# output_subdir="qa_renders",
|
| 621 |
+
# num_images=8,
|
| 622 |
+
# elevation=(30, -30),
|
| 623 |
+
# distance=5.5,
|
| 624 |
+
# )
|
| 625 |
+
|
| 626 |
+
image_dir = f"{output_root}/URDF_{filename}/{urdf_convertor.output_render_dir}/image_color" # noqa
|
| 627 |
+
image_paths = glob(f"{image_dir}/*.png")
|
| 628 |
+
images_list = []
|
| 629 |
+
for checker in CHECKERS:
|
| 630 |
+
images = image_paths
|
| 631 |
+
if isinstance(checker, ImageSegChecker):
|
| 632 |
+
images = [
|
| 633 |
+
f"{TMP_DIR}/{req.session_hash}/raw_image.png",
|
| 634 |
+
f"{TMP_DIR}/{req.session_hash}/seg_image.png",
|
| 635 |
+
]
|
| 636 |
+
images_list.append(images)
|
| 637 |
+
|
| 638 |
+
results = BaseChecker.validate(CHECKERS, images_list)
|
| 639 |
+
urdf_convertor.add_quality_tag(urdf_path, results)
|
| 640 |
+
|
| 641 |
+
# Zip urdf files
|
| 642 |
+
urdf_zip = zip_files(
|
| 643 |
+
input_paths=[
|
| 644 |
+
f"{output_root}/URDF_{filename}/{urdf_convertor.output_mesh_dir}",
|
| 645 |
+
f"{output_root}/URDF_{filename}/{filename}.urdf",
|
| 646 |
+
],
|
| 647 |
+
output_zip=f"{output_root}/urdf_{filename}.zip",
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
estimated_type = urdf_convertor.estimated_attrs["category"]
|
| 651 |
+
estimated_height = urdf_convertor.estimated_attrs["height"]
|
| 652 |
+
estimated_mass = urdf_convertor.estimated_attrs["mass"]
|
| 653 |
+
estimated_mu = urdf_convertor.estimated_attrs["mu"]
|
| 654 |
+
|
| 655 |
+
return (
|
| 656 |
+
urdf_zip,
|
| 657 |
+
estimated_type,
|
| 658 |
+
estimated_height,
|
| 659 |
+
estimated_mass,
|
| 660 |
+
estimated_mu,
|
| 661 |
+
)
|
| 662 |
+
|
| 663 |
+
|
| 664 |
+
@spaces.GPU
|
| 665 |
+
def text2image_fn(
|
| 666 |
+
prompt: str,
|
| 667 |
+
guidance_scale: float,
|
| 668 |
+
infer_step: int = 50,
|
| 669 |
+
ip_image: Image.Image | str = None,
|
| 670 |
+
ip_adapt_scale: float = 0.3,
|
| 671 |
+
image_wh: int | tuple[int, int] = [1024, 1024],
|
| 672 |
+
rmbg_tag: str = "rembg",
|
| 673 |
+
n_sample: int = 3,
|
| 674 |
+
req: gr.Request = None,
|
| 675 |
+
):
|
| 676 |
+
if isinstance(image_wh, int):
|
| 677 |
+
image_wh = (image_wh, image_wh)
|
| 678 |
+
output_root = TMP_DIR
|
| 679 |
+
if req is not None:
|
| 680 |
+
output_root = os.path.join(output_root, str(req.session_hash))
|
| 681 |
+
os.makedirs(output_root, exist_ok=True)
|
| 682 |
+
|
| 683 |
+
pipeline = PIPELINE_IMG if ip_image is None else PIPELINE_IMG_IP
|
| 684 |
+
if ip_image is not None:
|
| 685 |
+
pipeline.set_ip_adapter_scale([ip_adapt_scale])
|
| 686 |
+
|
| 687 |
+
images = text2img_gen(
|
| 688 |
+
prompt=prompt,
|
| 689 |
+
n_sample=n_sample,
|
| 690 |
+
guidance_scale=guidance_scale,
|
| 691 |
+
pipeline=pipeline,
|
| 692 |
+
ip_image=ip_image,
|
| 693 |
+
image_wh=image_wh,
|
| 694 |
+
infer_step=infer_step,
|
| 695 |
+
)
|
| 696 |
+
|
| 697 |
+
for idx in range(len(images)):
|
| 698 |
+
image = images[idx]
|
| 699 |
+
images[idx], _ = preprocess_image_fn(image, rmbg_tag)
|
| 700 |
+
|
| 701 |
+
save_paths = []
|
| 702 |
+
for idx, image in enumerate(images):
|
| 703 |
+
save_path = f"{output_root}/sample_{idx}.png"
|
| 704 |
+
image.save(save_path)
|
| 705 |
+
save_paths.append(save_path)
|
| 706 |
+
|
| 707 |
+
logger.info(f"Images saved to {output_root}")
|
| 708 |
+
|
| 709 |
+
gc.collect()
|
| 710 |
+
torch.cuda.empty_cache()
|
| 711 |
+
|
| 712 |
+
return save_paths + save_paths
|
| 713 |
+
|
| 714 |
+
|
| 715 |
+
@spaces.GPU
|
| 716 |
+
def generate_condition(mesh_path: str, req: gr.Request, uuid: str = "sample"):
|
| 717 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 718 |
+
|
| 719 |
+
_ = render_api(
|
| 720 |
+
mesh_path=mesh_path,
|
| 721 |
+
output_root=f"{output_root}/condition",
|
| 722 |
+
uuid=str(uuid),
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
gc.collect()
|
| 726 |
+
torch.cuda.empty_cache()
|
| 727 |
+
|
| 728 |
+
return None, None, None
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
@spaces.GPU
|
| 732 |
+
def generate_texture_mvimages(
|
| 733 |
+
prompt: str,
|
| 734 |
+
controlnet_cond_scale: float = 0.55,
|
| 735 |
+
guidance_scale: float = 9,
|
| 736 |
+
strength: float = 0.9,
|
| 737 |
+
num_inference_steps: int = 50,
|
| 738 |
+
seed: int = 0,
|
| 739 |
+
ip_adapt_scale: float = 0,
|
| 740 |
+
ip_img_path: str = None,
|
| 741 |
+
uid: str = "sample",
|
| 742 |
+
sub_idxs: tuple[tuple[int]] = ((0, 1, 2), (3, 4, 5)),
|
| 743 |
+
req: gr.Request = None,
|
| 744 |
+
) -> list[str]:
|
| 745 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 746 |
+
use_ip_adapter = True if ip_img_path and ip_adapt_scale > 0 else False
|
| 747 |
+
PIPELINE_IP.set_ip_adapter_scale([ip_adapt_scale])
|
| 748 |
+
img_save_paths = infer_pipe(
|
| 749 |
+
index_file=f"{output_root}/condition/index.json",
|
| 750 |
+
controlnet_cond_scale=controlnet_cond_scale,
|
| 751 |
+
guidance_scale=guidance_scale,
|
| 752 |
+
strength=strength,
|
| 753 |
+
num_inference_steps=num_inference_steps,
|
| 754 |
+
ip_adapt_scale=ip_adapt_scale,
|
| 755 |
+
ip_img_path=ip_img_path,
|
| 756 |
+
uid=uid,
|
| 757 |
+
prompt=prompt,
|
| 758 |
+
save_dir=f"{output_root}/multi_view",
|
| 759 |
+
sub_idxs=sub_idxs,
|
| 760 |
+
pipeline=PIPELINE_IP if use_ip_adapter else PIPELINE,
|
| 761 |
+
seed=seed,
|
| 762 |
+
)
|
| 763 |
+
|
| 764 |
+
gc.collect()
|
| 765 |
+
torch.cuda.empty_cache()
|
| 766 |
+
|
| 767 |
+
return img_save_paths + img_save_paths
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
def backproject_texture(
|
| 771 |
+
mesh_path: str,
|
| 772 |
+
input_image: str,
|
| 773 |
+
texture_size: int,
|
| 774 |
+
uuid: str = "sample",
|
| 775 |
+
req: gr.Request = None,
|
| 776 |
+
) -> str:
|
| 777 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 778 |
+
output_dir = os.path.join(output_root, "texture_mesh")
|
| 779 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 780 |
+
command = [
|
| 781 |
+
"backproject-cli",
|
| 782 |
+
"--mesh_path",
|
| 783 |
+
mesh_path,
|
| 784 |
+
"--input_image",
|
| 785 |
+
input_image,
|
| 786 |
+
"--output_root",
|
| 787 |
+
output_dir,
|
| 788 |
+
"--uuid",
|
| 789 |
+
f"{uuid}",
|
| 790 |
+
"--texture_size",
|
| 791 |
+
str(texture_size),
|
| 792 |
+
"--skip_fix_mesh",
|
| 793 |
+
]
|
| 794 |
+
|
| 795 |
+
_ = subprocess.run(
|
| 796 |
+
command, capture_output=True, text=True, encoding="utf-8"
|
| 797 |
+
)
|
| 798 |
+
output_obj_mesh = os.path.join(output_dir, f"{uuid}.obj")
|
| 799 |
+
output_glb_mesh = os.path.join(output_dir, f"{uuid}.glb")
|
| 800 |
+
_ = trimesh.load(output_obj_mesh).export(output_glb_mesh)
|
| 801 |
+
|
| 802 |
+
zip_file = zip_files(
|
| 803 |
+
input_paths=[
|
| 804 |
+
output_glb_mesh,
|
| 805 |
+
output_obj_mesh,
|
| 806 |
+
os.path.join(output_dir, "material.mtl"),
|
| 807 |
+
os.path.join(output_dir, "material_0.png"),
|
| 808 |
+
],
|
| 809 |
+
output_zip=os.path.join(output_dir, f"{uuid}.zip"),
|
| 810 |
+
)
|
| 811 |
+
|
| 812 |
+
gc.collect()
|
| 813 |
+
torch.cuda.empty_cache()
|
| 814 |
+
|
| 815 |
+
return output_glb_mesh, output_obj_mesh, zip_file
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
@spaces.GPU
|
| 819 |
+
def backproject_texture_v2(
|
| 820 |
+
mesh_path: str,
|
| 821 |
+
input_image: str,
|
| 822 |
+
texture_size: int,
|
| 823 |
+
enable_delight: bool = True,
|
| 824 |
+
fix_mesh: bool = False,
|
| 825 |
+
uuid: str = "sample",
|
| 826 |
+
req: gr.Request = None,
|
| 827 |
+
) -> str:
|
| 828 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 829 |
+
output_dir = os.path.join(output_root, "texture_mesh")
|
| 830 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 831 |
+
|
| 832 |
+
textured_mesh = backproject_api(
|
| 833 |
+
delight_model=DELIGHT,
|
| 834 |
+
imagesr_model=IMAGESR_MODEL,
|
| 835 |
+
color_path=input_image,
|
| 836 |
+
mesh_path=mesh_path,
|
| 837 |
+
output_path=f"{output_dir}/{uuid}.obj",
|
| 838 |
+
skip_fix_mesh=not fix_mesh,
|
| 839 |
+
delight=enable_delight,
|
| 840 |
+
texture_wh=[texture_size, texture_size],
|
| 841 |
+
)
|
| 842 |
+
|
| 843 |
+
output_obj_mesh = os.path.join(output_dir, f"{uuid}.obj")
|
| 844 |
+
output_glb_mesh = os.path.join(output_dir, f"{uuid}.glb")
|
| 845 |
+
_ = textured_mesh.export(output_glb_mesh)
|
| 846 |
+
|
| 847 |
+
zip_file = zip_files(
|
| 848 |
+
input_paths=[
|
| 849 |
+
output_glb_mesh,
|
| 850 |
+
output_obj_mesh,
|
| 851 |
+
os.path.join(output_dir, "material.mtl"),
|
| 852 |
+
os.path.join(output_dir, "material_0.png"),
|
| 853 |
+
],
|
| 854 |
+
output_zip=os.path.join(output_dir, f"{uuid}.zip"),
|
| 855 |
+
)
|
| 856 |
+
|
| 857 |
+
gc.collect()
|
| 858 |
+
torch.cuda.empty_cache()
|
| 859 |
+
|
| 860 |
+
return output_glb_mesh, output_obj_mesh, zip_file
|
| 861 |
+
|
| 862 |
+
|
| 863 |
+
@spaces.GPU
|
| 864 |
+
def render_result_video(
|
| 865 |
+
mesh_path: str, video_size: int, req: gr.Request, uuid: str = ""
|
| 866 |
+
) -> str:
|
| 867 |
+
output_root = os.path.join(TMP_DIR, str(req.session_hash))
|
| 868 |
+
output_dir = os.path.join(output_root, "texture_mesh")
|
| 869 |
+
|
| 870 |
+
_ = render_api(
|
| 871 |
+
mesh_path=mesh_path,
|
| 872 |
+
output_root=output_dir,
|
| 873 |
+
num_images=90,
|
| 874 |
+
elevation=[20],
|
| 875 |
+
with_mtl=True,
|
| 876 |
+
pbr_light_factor=1,
|
| 877 |
+
uuid=str(uuid),
|
| 878 |
+
gen_color_mp4=True,
|
| 879 |
+
gen_glonormal_mp4=True,
|
| 880 |
+
distance=5.5,
|
| 881 |
+
resolution_hw=(video_size, video_size),
|
| 882 |
+
)
|
| 883 |
+
|
| 884 |
+
gc.collect()
|
| 885 |
+
torch.cuda.empty_cache()
|
| 886 |
+
|
| 887 |
+
return f"{output_dir}/color.mp4"
|
embodied_gen/data/backproject.py
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import logging
|
| 20 |
+
import math
|
| 21 |
+
import os
|
| 22 |
+
from typing import List, Literal, Union
|
| 23 |
+
|
| 24 |
+
import cv2
|
| 25 |
+
import numpy as np
|
| 26 |
+
import nvdiffrast.torch as dr
|
| 27 |
+
import torch
|
| 28 |
+
import trimesh
|
| 29 |
+
import utils3d
|
| 30 |
+
import xatlas
|
| 31 |
+
from tqdm import tqdm
|
| 32 |
+
from embodied_gen.data.mesh_operator import MeshFixer
|
| 33 |
+
from embodied_gen.data.utils import (
|
| 34 |
+
CameraSetting,
|
| 35 |
+
get_images_from_grid,
|
| 36 |
+
init_kal_camera,
|
| 37 |
+
normalize_vertices_array,
|
| 38 |
+
post_process_texture,
|
| 39 |
+
save_mesh_with_mtl,
|
| 40 |
+
)
|
| 41 |
+
from embodied_gen.models.delight_model import DelightingModel
|
| 42 |
+
|
| 43 |
+
logging.basicConfig(
|
| 44 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 45 |
+
)
|
| 46 |
+
logger = logging.getLogger(__name__)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class TextureBaker(object):
|
| 50 |
+
"""Baking textures onto a mesh from multiple observations.
|
| 51 |
+
|
| 52 |
+
This class take 3D mesh data, camera settings and texture baking parameters
|
| 53 |
+
to generate texture map by projecting images to the mesh from diff views.
|
| 54 |
+
It supports both a fast texture baking approach and a more optimized method
|
| 55 |
+
with total variation regularization.
|
| 56 |
+
|
| 57 |
+
Attributes:
|
| 58 |
+
vertices (torch.Tensor): The vertices of the mesh.
|
| 59 |
+
faces (torch.Tensor): The faces of the mesh, defined by vertex indices.
|
| 60 |
+
uvs (torch.Tensor): The UV coordinates of the mesh.
|
| 61 |
+
camera_params (CameraSetting): Camera setting (intrinsics, extrinsics).
|
| 62 |
+
device (str): The device to run computations on ("cpu" or "cuda").
|
| 63 |
+
w2cs (torch.Tensor): World-to-camera transformation matrices.
|
| 64 |
+
projections (torch.Tensor): Camera projection matrices.
|
| 65 |
+
|
| 66 |
+
Example:
|
| 67 |
+
>>> vertices, faces, uvs = TextureBaker.parametrize_mesh(vertices, faces) # noqa
|
| 68 |
+
>>> texture_backer = TextureBaker(vertices, faces, uvs, camera_params)
|
| 69 |
+
>>> images = get_images_from_grid(args.color_path, image_size)
|
| 70 |
+
>>> texture = texture_backer.bake_texture(
|
| 71 |
+
... images, texture_size=args.texture_size, mode=args.baker_mode
|
| 72 |
+
... )
|
| 73 |
+
>>> texture = post_process_texture(texture)
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
def __init__(
|
| 77 |
+
self,
|
| 78 |
+
vertices: np.ndarray,
|
| 79 |
+
faces: np.ndarray,
|
| 80 |
+
uvs: np.ndarray,
|
| 81 |
+
camera_params: CameraSetting,
|
| 82 |
+
device: str = "cuda",
|
| 83 |
+
) -> None:
|
| 84 |
+
self.vertices = (
|
| 85 |
+
torch.tensor(vertices, device=device)
|
| 86 |
+
if isinstance(vertices, np.ndarray)
|
| 87 |
+
else vertices.to(device)
|
| 88 |
+
)
|
| 89 |
+
self.faces = (
|
| 90 |
+
torch.tensor(faces.astype(np.int32), device=device)
|
| 91 |
+
if isinstance(faces, np.ndarray)
|
| 92 |
+
else faces.to(device)
|
| 93 |
+
)
|
| 94 |
+
self.uvs = (
|
| 95 |
+
torch.tensor(uvs, device=device)
|
| 96 |
+
if isinstance(uvs, np.ndarray)
|
| 97 |
+
else uvs.to(device)
|
| 98 |
+
)
|
| 99 |
+
self.camera_params = camera_params
|
| 100 |
+
self.device = device
|
| 101 |
+
|
| 102 |
+
camera = init_kal_camera(camera_params)
|
| 103 |
+
matrix_mv = camera.view_matrix() # (n_cam 4 4) world2cam
|
| 104 |
+
matrix_mv = kaolin_to_opencv_view(matrix_mv)
|
| 105 |
+
matrix_p = (
|
| 106 |
+
camera.intrinsics.projection_matrix()
|
| 107 |
+
) # (n_cam 4 4) cam2pixel
|
| 108 |
+
self.w2cs = matrix_mv.to(self.device)
|
| 109 |
+
self.projections = matrix_p.to(self.device)
|
| 110 |
+
|
| 111 |
+
@staticmethod
|
| 112 |
+
def parametrize_mesh(
|
| 113 |
+
vertices: np.array, faces: np.array
|
| 114 |
+
) -> Union[np.array, np.array, np.array]:
|
| 115 |
+
vmapping, indices, uvs = xatlas.parametrize(vertices, faces)
|
| 116 |
+
|
| 117 |
+
vertices = vertices[vmapping]
|
| 118 |
+
faces = indices
|
| 119 |
+
|
| 120 |
+
return vertices, faces, uvs
|
| 121 |
+
|
| 122 |
+
def _bake_fast(self, observations, w2cs, projections, texture_size, masks):
|
| 123 |
+
texture = torch.zeros(
|
| 124 |
+
(texture_size * texture_size, 3), dtype=torch.float32
|
| 125 |
+
).cuda()
|
| 126 |
+
texture_weights = torch.zeros(
|
| 127 |
+
(texture_size * texture_size), dtype=torch.float32
|
| 128 |
+
).cuda()
|
| 129 |
+
rastctx = utils3d.torch.RastContext(backend="cuda")
|
| 130 |
+
for observation, w2c, projection in tqdm(
|
| 131 |
+
zip(observations, w2cs, projections),
|
| 132 |
+
total=len(observations),
|
| 133 |
+
desc="Texture baking (fast)",
|
| 134 |
+
):
|
| 135 |
+
with torch.no_grad():
|
| 136 |
+
rast = utils3d.torch.rasterize_triangle_faces(
|
| 137 |
+
rastctx,
|
| 138 |
+
self.vertices[None],
|
| 139 |
+
self.faces,
|
| 140 |
+
observation.shape[1],
|
| 141 |
+
observation.shape[0],
|
| 142 |
+
uv=self.uvs[None],
|
| 143 |
+
view=w2c,
|
| 144 |
+
projection=projection,
|
| 145 |
+
)
|
| 146 |
+
uv_map = rast["uv"][0].detach().flip(0)
|
| 147 |
+
mask = rast["mask"][0].detach().bool() & masks[0]
|
| 148 |
+
|
| 149 |
+
# nearest neighbor interpolation
|
| 150 |
+
uv_map = (uv_map * texture_size).floor().long()
|
| 151 |
+
obs = observation[mask]
|
| 152 |
+
uv_map = uv_map[mask]
|
| 153 |
+
idx = (
|
| 154 |
+
uv_map[:, 0] + (texture_size - uv_map[:, 1] - 1) * texture_size
|
| 155 |
+
)
|
| 156 |
+
texture = texture.scatter_add(
|
| 157 |
+
0, idx.view(-1, 1).expand(-1, 3), obs
|
| 158 |
+
)
|
| 159 |
+
texture_weights = texture_weights.scatter_add(
|
| 160 |
+
0,
|
| 161 |
+
idx,
|
| 162 |
+
torch.ones(
|
| 163 |
+
(obs.shape[0]), dtype=torch.float32, device=texture.device
|
| 164 |
+
),
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
mask = texture_weights > 0
|
| 168 |
+
texture[mask] /= texture_weights[mask][:, None]
|
| 169 |
+
texture = np.clip(
|
| 170 |
+
texture.reshape(texture_size, texture_size, 3).cpu().numpy() * 255,
|
| 171 |
+
0,
|
| 172 |
+
255,
|
| 173 |
+
).astype(np.uint8)
|
| 174 |
+
|
| 175 |
+
# inpaint
|
| 176 |
+
mask = (
|
| 177 |
+
(texture_weights == 0)
|
| 178 |
+
.cpu()
|
| 179 |
+
.numpy()
|
| 180 |
+
.astype(np.uint8)
|
| 181 |
+
.reshape(texture_size, texture_size)
|
| 182 |
+
)
|
| 183 |
+
texture = cv2.inpaint(texture, mask, 3, cv2.INPAINT_TELEA)
|
| 184 |
+
|
| 185 |
+
return texture
|
| 186 |
+
|
| 187 |
+
def _bake_opt(
|
| 188 |
+
self,
|
| 189 |
+
observations,
|
| 190 |
+
w2cs,
|
| 191 |
+
projections,
|
| 192 |
+
texture_size,
|
| 193 |
+
lambda_tv,
|
| 194 |
+
masks,
|
| 195 |
+
total_steps,
|
| 196 |
+
):
|
| 197 |
+
rastctx = utils3d.torch.RastContext(backend="cuda")
|
| 198 |
+
observations = [observations.flip(0) for observations in observations]
|
| 199 |
+
masks = [m.flip(0) for m in masks]
|
| 200 |
+
_uv = []
|
| 201 |
+
_uv_dr = []
|
| 202 |
+
for observation, w2c, projection in tqdm(
|
| 203 |
+
zip(observations, w2cs, projections),
|
| 204 |
+
total=len(w2cs),
|
| 205 |
+
):
|
| 206 |
+
with torch.no_grad():
|
| 207 |
+
rast = utils3d.torch.rasterize_triangle_faces(
|
| 208 |
+
rastctx,
|
| 209 |
+
self.vertices[None],
|
| 210 |
+
self.faces,
|
| 211 |
+
observation.shape[1],
|
| 212 |
+
observation.shape[0],
|
| 213 |
+
uv=self.uvs[None],
|
| 214 |
+
view=w2c,
|
| 215 |
+
projection=projection,
|
| 216 |
+
)
|
| 217 |
+
_uv.append(rast["uv"].detach())
|
| 218 |
+
_uv_dr.append(rast["uv_dr"].detach())
|
| 219 |
+
|
| 220 |
+
texture = torch.nn.Parameter(
|
| 221 |
+
torch.zeros(
|
| 222 |
+
(1, texture_size, texture_size, 3), dtype=torch.float32
|
| 223 |
+
).cuda()
|
| 224 |
+
)
|
| 225 |
+
optimizer = torch.optim.Adam([texture], betas=(0.5, 0.9), lr=1e-2)
|
| 226 |
+
|
| 227 |
+
def cosine_anealing(step, total_steps, start_lr, end_lr):
|
| 228 |
+
return end_lr + 0.5 * (start_lr - end_lr) * (
|
| 229 |
+
1 + np.cos(np.pi * step / total_steps)
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
def tv_loss(texture):
|
| 233 |
+
return torch.nn.functional.l1_loss(
|
| 234 |
+
texture[:, :-1, :, :], texture[:, 1:, :, :]
|
| 235 |
+
) + torch.nn.functional.l1_loss(
|
| 236 |
+
texture[:, :, :-1, :], texture[:, :, 1:, :]
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
with tqdm(total=total_steps, desc="Texture baking") as pbar:
|
| 240 |
+
for step in range(total_steps):
|
| 241 |
+
optimizer.zero_grad()
|
| 242 |
+
selected = np.random.randint(0, len(w2cs))
|
| 243 |
+
uv, uv_dr, observation, mask = (
|
| 244 |
+
_uv[selected],
|
| 245 |
+
_uv_dr[selected],
|
| 246 |
+
observations[selected],
|
| 247 |
+
masks[selected],
|
| 248 |
+
)
|
| 249 |
+
render = dr.texture(texture, uv, uv_dr)[0]
|
| 250 |
+
loss = torch.nn.functional.l1_loss(
|
| 251 |
+
render[mask], observation[mask]
|
| 252 |
+
)
|
| 253 |
+
if lambda_tv > 0:
|
| 254 |
+
loss += lambda_tv * tv_loss(texture)
|
| 255 |
+
loss.backward()
|
| 256 |
+
optimizer.step()
|
| 257 |
+
|
| 258 |
+
optimizer.param_groups[0]["lr"] = cosine_anealing(
|
| 259 |
+
step, total_steps, 1e-2, 1e-5
|
| 260 |
+
)
|
| 261 |
+
pbar.set_postfix({"loss": loss.item()})
|
| 262 |
+
pbar.update()
|
| 263 |
+
texture = np.clip(
|
| 264 |
+
texture[0].flip(0).detach().cpu().numpy() * 255, 0, 255
|
| 265 |
+
).astype(np.uint8)
|
| 266 |
+
mask = 1 - utils3d.torch.rasterize_triangle_faces(
|
| 267 |
+
rastctx,
|
| 268 |
+
(self.uvs * 2 - 1)[None],
|
| 269 |
+
self.faces,
|
| 270 |
+
texture_size,
|
| 271 |
+
texture_size,
|
| 272 |
+
)["mask"][0].detach().cpu().numpy().astype(np.uint8)
|
| 273 |
+
texture = cv2.inpaint(texture, mask, 3, cv2.INPAINT_TELEA)
|
| 274 |
+
|
| 275 |
+
return texture
|
| 276 |
+
|
| 277 |
+
def bake_texture(
|
| 278 |
+
self,
|
| 279 |
+
images: List[np.array],
|
| 280 |
+
texture_size: int = 1024,
|
| 281 |
+
mode: Literal["fast", "opt"] = "opt",
|
| 282 |
+
lambda_tv: float = 1e-2,
|
| 283 |
+
opt_step: int = 2000,
|
| 284 |
+
):
|
| 285 |
+
masks = [np.any(img > 0, axis=-1) for img in images]
|
| 286 |
+
masks = [torch.tensor(m > 0).bool().to(self.device) for m in masks]
|
| 287 |
+
images = [
|
| 288 |
+
torch.tensor(obs / 255.0).float().to(self.device) for obs in images
|
| 289 |
+
]
|
| 290 |
+
|
| 291 |
+
if mode == "fast":
|
| 292 |
+
return self._bake_fast(
|
| 293 |
+
images, self.w2cs, self.projections, texture_size, masks
|
| 294 |
+
)
|
| 295 |
+
elif mode == "opt":
|
| 296 |
+
return self._bake_opt(
|
| 297 |
+
images,
|
| 298 |
+
self.w2cs,
|
| 299 |
+
self.projections,
|
| 300 |
+
texture_size,
|
| 301 |
+
lambda_tv,
|
| 302 |
+
masks,
|
| 303 |
+
opt_step,
|
| 304 |
+
)
|
| 305 |
+
else:
|
| 306 |
+
raise ValueError(f"Unknown mode: {mode}")
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def kaolin_to_opencv_view(raw_matrix):
|
| 310 |
+
R_orig = raw_matrix[:, :3, :3]
|
| 311 |
+
t_orig = raw_matrix[:, :3, 3]
|
| 312 |
+
|
| 313 |
+
R_target = torch.zeros_like(R_orig)
|
| 314 |
+
R_target[:, :, 0] = R_orig[:, :, 2]
|
| 315 |
+
R_target[:, :, 1] = R_orig[:, :, 0]
|
| 316 |
+
R_target[:, :, 2] = R_orig[:, :, 1]
|
| 317 |
+
|
| 318 |
+
t_target = t_orig
|
| 319 |
+
|
| 320 |
+
target_matrix = (
|
| 321 |
+
torch.eye(4, device=raw_matrix.device)
|
| 322 |
+
.unsqueeze(0)
|
| 323 |
+
.repeat(raw_matrix.size(0), 1, 1)
|
| 324 |
+
)
|
| 325 |
+
target_matrix[:, :3, :3] = R_target
|
| 326 |
+
target_matrix[:, :3, 3] = t_target
|
| 327 |
+
|
| 328 |
+
return target_matrix
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def parse_args():
|
| 332 |
+
parser = argparse.ArgumentParser(description="Render settings")
|
| 333 |
+
|
| 334 |
+
parser.add_argument(
|
| 335 |
+
"--mesh_path",
|
| 336 |
+
type=str,
|
| 337 |
+
nargs="+",
|
| 338 |
+
required=True,
|
| 339 |
+
help="Paths to the mesh files for rendering.",
|
| 340 |
+
)
|
| 341 |
+
parser.add_argument(
|
| 342 |
+
"--color_path",
|
| 343 |
+
type=str,
|
| 344 |
+
nargs="+",
|
| 345 |
+
required=True,
|
| 346 |
+
help="Paths to the mesh files for rendering.",
|
| 347 |
+
)
|
| 348 |
+
parser.add_argument(
|
| 349 |
+
"--output_root",
|
| 350 |
+
type=str,
|
| 351 |
+
default="./outputs",
|
| 352 |
+
help="Root directory for output",
|
| 353 |
+
)
|
| 354 |
+
parser.add_argument(
|
| 355 |
+
"--uuid",
|
| 356 |
+
type=str,
|
| 357 |
+
nargs="+",
|
| 358 |
+
default=None,
|
| 359 |
+
help="uuid for rendering saving.",
|
| 360 |
+
)
|
| 361 |
+
parser.add_argument(
|
| 362 |
+
"--num_images", type=int, default=6, help="Number of images to render."
|
| 363 |
+
)
|
| 364 |
+
parser.add_argument(
|
| 365 |
+
"--elevation",
|
| 366 |
+
type=float,
|
| 367 |
+
nargs="+",
|
| 368 |
+
default=[20.0, -10.0],
|
| 369 |
+
help="Elevation angles for the camera (default: [20.0, -10.0])",
|
| 370 |
+
)
|
| 371 |
+
parser.add_argument(
|
| 372 |
+
"--distance",
|
| 373 |
+
type=float,
|
| 374 |
+
default=5,
|
| 375 |
+
help="Camera distance (default: 5)",
|
| 376 |
+
)
|
| 377 |
+
parser.add_argument(
|
| 378 |
+
"--resolution_hw",
|
| 379 |
+
type=int,
|
| 380 |
+
nargs=2,
|
| 381 |
+
default=(512, 512),
|
| 382 |
+
help="Resolution of the output images (default: (512, 512))",
|
| 383 |
+
)
|
| 384 |
+
parser.add_argument(
|
| 385 |
+
"--fov",
|
| 386 |
+
type=float,
|
| 387 |
+
default=30,
|
| 388 |
+
help="Field of view in degrees (default: 30)",
|
| 389 |
+
)
|
| 390 |
+
parser.add_argument(
|
| 391 |
+
"--device",
|
| 392 |
+
type=str,
|
| 393 |
+
choices=["cpu", "cuda"],
|
| 394 |
+
default="cuda",
|
| 395 |
+
help="Device to run on (default: `cuda`)",
|
| 396 |
+
)
|
| 397 |
+
parser.add_argument(
|
| 398 |
+
"--texture_size",
|
| 399 |
+
type=int,
|
| 400 |
+
default=1024,
|
| 401 |
+
help="Texture size for texture baking (default: 1024)",
|
| 402 |
+
)
|
| 403 |
+
parser.add_argument(
|
| 404 |
+
"--baker_mode",
|
| 405 |
+
type=str,
|
| 406 |
+
default="opt",
|
| 407 |
+
help="Texture baking mode, `fast` or `opt` (default: opt)",
|
| 408 |
+
)
|
| 409 |
+
parser.add_argument(
|
| 410 |
+
"--opt_step",
|
| 411 |
+
type=int,
|
| 412 |
+
default=2500,
|
| 413 |
+
help="Optimization steps for texture baking (default: 2500)",
|
| 414 |
+
)
|
| 415 |
+
parser.add_argument(
|
| 416 |
+
"--mesh_sipmlify_ratio",
|
| 417 |
+
type=float,
|
| 418 |
+
default=0.9,
|
| 419 |
+
help="Mesh simplification ratio (default: 0.9)",
|
| 420 |
+
)
|
| 421 |
+
parser.add_argument(
|
| 422 |
+
"--no_coor_trans",
|
| 423 |
+
action="store_true",
|
| 424 |
+
help="Do not transform the asset coordinate system.",
|
| 425 |
+
)
|
| 426 |
+
parser.add_argument(
|
| 427 |
+
"--delight", action="store_true", help="Use delighting model."
|
| 428 |
+
)
|
| 429 |
+
parser.add_argument(
|
| 430 |
+
"--skip_fix_mesh", action="store_true", help="Fix mesh geometry."
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
args = parser.parse_args()
|
| 434 |
+
|
| 435 |
+
if args.uuid is None:
|
| 436 |
+
args.uuid = []
|
| 437 |
+
for path in args.mesh_path:
|
| 438 |
+
uuid = os.path.basename(path).split(".")[0]
|
| 439 |
+
args.uuid.append(uuid)
|
| 440 |
+
|
| 441 |
+
return args
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def entrypoint() -> None:
|
| 445 |
+
args = parse_args()
|
| 446 |
+
camera_params = CameraSetting(
|
| 447 |
+
num_images=args.num_images,
|
| 448 |
+
elevation=args.elevation,
|
| 449 |
+
distance=args.distance,
|
| 450 |
+
resolution_hw=args.resolution_hw,
|
| 451 |
+
fov=math.radians(args.fov),
|
| 452 |
+
device=args.device,
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
for mesh_path, uuid, img_path in zip(
|
| 456 |
+
args.mesh_path, args.uuid, args.color_path
|
| 457 |
+
):
|
| 458 |
+
mesh = trimesh.load(mesh_path)
|
| 459 |
+
if isinstance(mesh, trimesh.Scene):
|
| 460 |
+
mesh = mesh.dump(concatenate=True)
|
| 461 |
+
vertices, scale, center = normalize_vertices_array(mesh.vertices)
|
| 462 |
+
|
| 463 |
+
if not args.no_coor_trans:
|
| 464 |
+
x_rot = np.array([[1, 0, 0], [0, 0, 1], [0, -1, 0]])
|
| 465 |
+
z_rot = np.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
|
| 466 |
+
vertices = vertices @ x_rot
|
| 467 |
+
vertices = vertices @ z_rot
|
| 468 |
+
|
| 469 |
+
faces = mesh.faces.astype(np.int32)
|
| 470 |
+
vertices = vertices.astype(np.float32)
|
| 471 |
+
|
| 472 |
+
if not args.skip_fix_mesh:
|
| 473 |
+
mesh_fixer = MeshFixer(vertices, faces, args.device)
|
| 474 |
+
vertices, faces = mesh_fixer(
|
| 475 |
+
filter_ratio=args.mesh_sipmlify_ratio,
|
| 476 |
+
max_hole_size=0.04,
|
| 477 |
+
resolution=1024,
|
| 478 |
+
num_views=1000,
|
| 479 |
+
norm_mesh_ratio=0.5,
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
vertices, faces, uvs = TextureBaker.parametrize_mesh(vertices, faces)
|
| 483 |
+
texture_backer = TextureBaker(
|
| 484 |
+
vertices,
|
| 485 |
+
faces,
|
| 486 |
+
uvs,
|
| 487 |
+
camera_params,
|
| 488 |
+
)
|
| 489 |
+
images = get_images_from_grid(
|
| 490 |
+
img_path, img_size=camera_params.resolution_hw[0]
|
| 491 |
+
)
|
| 492 |
+
if args.delight:
|
| 493 |
+
delight_model = DelightingModel()
|
| 494 |
+
images = [delight_model(img) for img in images]
|
| 495 |
+
|
| 496 |
+
images = [np.array(img) for img in images]
|
| 497 |
+
texture = texture_backer.bake_texture(
|
| 498 |
+
images=[img[..., :3] for img in images],
|
| 499 |
+
texture_size=args.texture_size,
|
| 500 |
+
mode=args.baker_mode,
|
| 501 |
+
opt_step=args.opt_step,
|
| 502 |
+
)
|
| 503 |
+
texture = post_process_texture(texture)
|
| 504 |
+
|
| 505 |
+
if not args.no_coor_trans:
|
| 506 |
+
vertices = vertices @ np.linalg.inv(z_rot)
|
| 507 |
+
vertices = vertices @ np.linalg.inv(x_rot)
|
| 508 |
+
vertices = vertices / scale
|
| 509 |
+
vertices = vertices + center
|
| 510 |
+
|
| 511 |
+
output_path = os.path.join(args.output_root, f"{uuid}.obj")
|
| 512 |
+
mesh = save_mesh_with_mtl(vertices, faces, uvs, texture, output_path)
|
| 513 |
+
|
| 514 |
+
return
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
if __name__ == "__main__":
|
| 518 |
+
entrypoint()
|
embodied_gen/data/backproject_v2.py
ADDED
|
@@ -0,0 +1,687 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import logging
|
| 20 |
+
import math
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
import cv2
|
| 24 |
+
import numpy as np
|
| 25 |
+
import nvdiffrast.torch as dr
|
| 26 |
+
import spaces
|
| 27 |
+
import torch
|
| 28 |
+
import torch.nn.functional as F
|
| 29 |
+
import trimesh
|
| 30 |
+
import xatlas
|
| 31 |
+
from PIL import Image
|
| 32 |
+
from embodied_gen.data.mesh_operator import MeshFixer
|
| 33 |
+
from embodied_gen.data.utils import (
|
| 34 |
+
CameraSetting,
|
| 35 |
+
DiffrastRender,
|
| 36 |
+
get_images_from_grid,
|
| 37 |
+
init_kal_camera,
|
| 38 |
+
normalize_vertices_array,
|
| 39 |
+
post_process_texture,
|
| 40 |
+
save_mesh_with_mtl,
|
| 41 |
+
)
|
| 42 |
+
from embodied_gen.models.delight_model import DelightingModel
|
| 43 |
+
from embodied_gen.models.sr_model import ImageRealESRGAN
|
| 44 |
+
|
| 45 |
+
logging.basicConfig(
|
| 46 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 47 |
+
)
|
| 48 |
+
logger = logging.getLogger(__name__)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
__all__ = [
|
| 52 |
+
"TextureBacker",
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _transform_vertices(
|
| 57 |
+
mtx: torch.Tensor, pos: torch.Tensor, keepdim: bool = False
|
| 58 |
+
) -> torch.Tensor:
|
| 59 |
+
"""Transform 3D vertices using a projection matrix."""
|
| 60 |
+
t_mtx = torch.as_tensor(mtx, device=pos.device, dtype=pos.dtype)
|
| 61 |
+
if pos.size(-1) == 3:
|
| 62 |
+
pos = torch.cat([pos, torch.ones_like(pos[..., :1])], dim=-1)
|
| 63 |
+
|
| 64 |
+
result = pos @ t_mtx.T
|
| 65 |
+
|
| 66 |
+
return result if keepdim else result.unsqueeze(0)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _bilinear_interpolation_scattering(
|
| 70 |
+
image_h: int, image_w: int, coords: torch.Tensor, values: torch.Tensor
|
| 71 |
+
) -> torch.Tensor:
|
| 72 |
+
"""Bilinear interpolation scattering for grid-based value accumulation."""
|
| 73 |
+
device = values.device
|
| 74 |
+
dtype = values.dtype
|
| 75 |
+
C = values.shape[-1]
|
| 76 |
+
|
| 77 |
+
indices = coords * torch.tensor(
|
| 78 |
+
[image_h - 1, image_w - 1], dtype=dtype, device=device
|
| 79 |
+
)
|
| 80 |
+
i, j = indices.unbind(-1)
|
| 81 |
+
|
| 82 |
+
i0, j0 = (
|
| 83 |
+
indices.floor()
|
| 84 |
+
.long()
|
| 85 |
+
.clamp(0, image_h - 2)
|
| 86 |
+
.clamp(0, image_w - 2)
|
| 87 |
+
.unbind(-1)
|
| 88 |
+
)
|
| 89 |
+
i1, j1 = i0 + 1, j0 + 1
|
| 90 |
+
|
| 91 |
+
w_i = i - i0.float()
|
| 92 |
+
w_j = j - j0.float()
|
| 93 |
+
weights = torch.stack(
|
| 94 |
+
[(1 - w_i) * (1 - w_j), (1 - w_i) * w_j, w_i * (1 - w_j), w_i * w_j],
|
| 95 |
+
dim=1,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
indices_comb = torch.stack(
|
| 99 |
+
[
|
| 100 |
+
torch.stack([i0, j0], dim=1),
|
| 101 |
+
torch.stack([i0, j1], dim=1),
|
| 102 |
+
torch.stack([i1, j0], dim=1),
|
| 103 |
+
torch.stack([i1, j1], dim=1),
|
| 104 |
+
],
|
| 105 |
+
dim=1,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
grid = torch.zeros(image_h, image_w, C, device=device, dtype=dtype)
|
| 109 |
+
cnt = torch.zeros(image_h, image_w, 1, device=device, dtype=dtype)
|
| 110 |
+
|
| 111 |
+
for k in range(4):
|
| 112 |
+
idx = indices_comb[:, k]
|
| 113 |
+
w = weights[:, k].unsqueeze(-1)
|
| 114 |
+
|
| 115 |
+
stride = torch.tensor([image_w, 1], device=device, dtype=torch.long)
|
| 116 |
+
flat_idx = (idx * stride).sum(-1)
|
| 117 |
+
|
| 118 |
+
grid.view(-1, C).scatter_add_(
|
| 119 |
+
0, flat_idx.unsqueeze(-1).expand(-1, C), values * w
|
| 120 |
+
)
|
| 121 |
+
cnt.view(-1, 1).scatter_add_(0, flat_idx.unsqueeze(-1), w)
|
| 122 |
+
|
| 123 |
+
mask = cnt.squeeze(-1) > 0
|
| 124 |
+
grid[mask] = grid[mask] / cnt[mask].repeat(1, C)
|
| 125 |
+
|
| 126 |
+
return grid
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def _texture_inpaint_smooth(
|
| 130 |
+
texture: np.ndarray,
|
| 131 |
+
mask: np.ndarray,
|
| 132 |
+
vertices: np.ndarray,
|
| 133 |
+
faces: np.ndarray,
|
| 134 |
+
uv_map: np.ndarray,
|
| 135 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
| 136 |
+
"""Perform texture inpainting using vertex-based color propagation."""
|
| 137 |
+
image_h, image_w, C = texture.shape
|
| 138 |
+
N = vertices.shape[0]
|
| 139 |
+
|
| 140 |
+
# Initialize vertex data structures
|
| 141 |
+
vtx_mask = np.zeros(N, dtype=np.float32)
|
| 142 |
+
vtx_colors = np.zeros((N, C), dtype=np.float32)
|
| 143 |
+
unprocessed = []
|
| 144 |
+
adjacency = [[] for _ in range(N)]
|
| 145 |
+
|
| 146 |
+
# Build adjacency graph and initial color assignment
|
| 147 |
+
for face_idx in range(faces.shape[0]):
|
| 148 |
+
for k in range(3):
|
| 149 |
+
uv_idx_k = faces[face_idx, k]
|
| 150 |
+
v_idx = faces[face_idx, k]
|
| 151 |
+
|
| 152 |
+
# Convert UV to pixel coordinates with boundary clamping
|
| 153 |
+
u = np.clip(
|
| 154 |
+
int(round(uv_map[uv_idx_k, 0] * (image_w - 1))), 0, image_w - 1
|
| 155 |
+
)
|
| 156 |
+
v = np.clip(
|
| 157 |
+
int(round((1.0 - uv_map[uv_idx_k, 1]) * (image_h - 1))),
|
| 158 |
+
0,
|
| 159 |
+
image_h - 1,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
if mask[v, u]:
|
| 163 |
+
vtx_mask[v_idx] = 1.0
|
| 164 |
+
vtx_colors[v_idx] = texture[v, u]
|
| 165 |
+
elif v_idx not in unprocessed:
|
| 166 |
+
unprocessed.append(v_idx)
|
| 167 |
+
|
| 168 |
+
# Build undirected adjacency graph
|
| 169 |
+
neighbor = faces[face_idx, (k + 1) % 3]
|
| 170 |
+
if neighbor not in adjacency[v_idx]:
|
| 171 |
+
adjacency[v_idx].append(neighbor)
|
| 172 |
+
if v_idx not in adjacency[neighbor]:
|
| 173 |
+
adjacency[neighbor].append(v_idx)
|
| 174 |
+
|
| 175 |
+
# Color propagation with dynamic stopping
|
| 176 |
+
remaining_iters, prev_count = 2, 0
|
| 177 |
+
while remaining_iters > 0:
|
| 178 |
+
current_unprocessed = []
|
| 179 |
+
|
| 180 |
+
for v_idx in unprocessed:
|
| 181 |
+
valid_neighbors = [n for n in adjacency[v_idx] if vtx_mask[n] > 0]
|
| 182 |
+
if not valid_neighbors:
|
| 183 |
+
current_unprocessed.append(v_idx)
|
| 184 |
+
continue
|
| 185 |
+
|
| 186 |
+
# Calculate inverse square distance weights
|
| 187 |
+
neighbors_pos = vertices[valid_neighbors]
|
| 188 |
+
dist_sq = np.sum((vertices[v_idx] - neighbors_pos) ** 2, axis=1)
|
| 189 |
+
weights = 1 / np.maximum(dist_sq, 1e-8)
|
| 190 |
+
|
| 191 |
+
vtx_colors[v_idx] = np.average(
|
| 192 |
+
vtx_colors[valid_neighbors], weights=weights, axis=0
|
| 193 |
+
)
|
| 194 |
+
vtx_mask[v_idx] = 1.0
|
| 195 |
+
|
| 196 |
+
# Update iteration control
|
| 197 |
+
if len(current_unprocessed) == prev_count:
|
| 198 |
+
remaining_iters -= 1
|
| 199 |
+
else:
|
| 200 |
+
remaining_iters = min(remaining_iters + 1, 2)
|
| 201 |
+
prev_count = len(current_unprocessed)
|
| 202 |
+
unprocessed = current_unprocessed
|
| 203 |
+
|
| 204 |
+
# Generate output texture
|
| 205 |
+
inpainted_texture, updated_mask = texture.copy(), mask.copy()
|
| 206 |
+
for face_idx in range(faces.shape[0]):
|
| 207 |
+
for k in range(3):
|
| 208 |
+
v_idx = faces[face_idx, k]
|
| 209 |
+
if not vtx_mask[v_idx]:
|
| 210 |
+
continue
|
| 211 |
+
|
| 212 |
+
# UV coordinate conversion
|
| 213 |
+
uv_idx_k = faces[face_idx, k]
|
| 214 |
+
u = np.clip(
|
| 215 |
+
int(round(uv_map[uv_idx_k, 0] * (image_w - 1))), 0, image_w - 1
|
| 216 |
+
)
|
| 217 |
+
v = np.clip(
|
| 218 |
+
int(round((1.0 - uv_map[uv_idx_k, 1]) * (image_h - 1))),
|
| 219 |
+
0,
|
| 220 |
+
image_h - 1,
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
inpainted_texture[v, u] = vtx_colors[v_idx]
|
| 224 |
+
updated_mask[v, u] = 255
|
| 225 |
+
|
| 226 |
+
return inpainted_texture, updated_mask
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
class TextureBacker:
|
| 230 |
+
"""Texture baking pipeline for multi-view projection and fusion.
|
| 231 |
+
|
| 232 |
+
This class performs UV-based texture generation for a 3D mesh using
|
| 233 |
+
multi-view color images, depth, and normal information. The pipeline
|
| 234 |
+
includes mesh normalization and UV unwrapping, visibility-aware
|
| 235 |
+
back-projection, confidence-weighted texture fusion, and inpainting
|
| 236 |
+
of missing texture regions.
|
| 237 |
+
|
| 238 |
+
Args:
|
| 239 |
+
camera_params (CameraSetting): Camera intrinsics and extrinsics used
|
| 240 |
+
for rendering each view.
|
| 241 |
+
view_weights (list[float]): A list of weights for each view, used
|
| 242 |
+
to blend confidence maps during texture fusion.
|
| 243 |
+
render_wh (tuple[int, int], optional): Resolution (width, height) for
|
| 244 |
+
intermediate rendering passes. Defaults to (2048, 2048).
|
| 245 |
+
texture_wh (tuple[int, int], optional): Output texture resolution
|
| 246 |
+
(width, height). Defaults to (2048, 2048).
|
| 247 |
+
bake_angle_thresh (int, optional): Maximum angle (in degrees) between
|
| 248 |
+
view direction and surface normal for projection to be considered valid.
|
| 249 |
+
Defaults to 75.
|
| 250 |
+
mask_thresh (float, optional): Threshold applied to visibility masks
|
| 251 |
+
during rendering. Defaults to 0.5.
|
| 252 |
+
smooth_texture (bool, optional): If True, apply post-processing (e.g.,
|
| 253 |
+
blurring) to the final texture. Defaults to True.
|
| 254 |
+
"""
|
| 255 |
+
|
| 256 |
+
def __init__(
|
| 257 |
+
self,
|
| 258 |
+
camera_params: CameraSetting,
|
| 259 |
+
view_weights: list[float],
|
| 260 |
+
render_wh: tuple[int, int] = (2048, 2048),
|
| 261 |
+
texture_wh: tuple[int, int] = (2048, 2048),
|
| 262 |
+
bake_angle_thresh: int = 75,
|
| 263 |
+
mask_thresh: float = 0.5,
|
| 264 |
+
smooth_texture: bool = True,
|
| 265 |
+
) -> None:
|
| 266 |
+
self.camera_params = camera_params
|
| 267 |
+
self.renderer = None
|
| 268 |
+
self.view_weights = view_weights
|
| 269 |
+
self.device = camera_params.device
|
| 270 |
+
self.render_wh = render_wh
|
| 271 |
+
self.texture_wh = texture_wh
|
| 272 |
+
self.mask_thresh = mask_thresh
|
| 273 |
+
self.smooth_texture = smooth_texture
|
| 274 |
+
|
| 275 |
+
self.bake_angle_thresh = bake_angle_thresh
|
| 276 |
+
self.bake_unreliable_kernel_size = int(
|
| 277 |
+
(2 / 512) * max(self.render_wh[0], self.render_wh[1])
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
def _lazy_init_render(self, camera_params, mask_thresh):
|
| 281 |
+
if self.renderer is None:
|
| 282 |
+
camera = init_kal_camera(camera_params)
|
| 283 |
+
mv = camera.view_matrix() # (n 4 4) world2cam
|
| 284 |
+
p = camera.intrinsics.projection_matrix()
|
| 285 |
+
# NOTE: add a negative sign at P[0, 2] as the y axis is flipped in `nvdiffrast` output. # noqa
|
| 286 |
+
p[:, 1, 1] = -p[:, 1, 1]
|
| 287 |
+
self.renderer = DiffrastRender(
|
| 288 |
+
p_matrix=p,
|
| 289 |
+
mv_matrix=mv,
|
| 290 |
+
resolution_hw=camera_params.resolution_hw,
|
| 291 |
+
context=dr.RasterizeCudaContext(),
|
| 292 |
+
mask_thresh=mask_thresh,
|
| 293 |
+
grad_db=False,
|
| 294 |
+
device=self.device,
|
| 295 |
+
antialias_mask=True,
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
def load_mesh(self, mesh: trimesh.Trimesh) -> trimesh.Trimesh:
|
| 299 |
+
mesh.vertices, scale, center = normalize_vertices_array(mesh.vertices)
|
| 300 |
+
self.scale, self.center = scale, center
|
| 301 |
+
|
| 302 |
+
vmapping, indices, uvs = xatlas.parametrize(mesh.vertices, mesh.faces)
|
| 303 |
+
uvs[:, 1] = 1 - uvs[:, 1]
|
| 304 |
+
mesh.vertices = mesh.vertices[vmapping]
|
| 305 |
+
mesh.faces = indices
|
| 306 |
+
mesh.visual.uv = uvs
|
| 307 |
+
|
| 308 |
+
return mesh
|
| 309 |
+
|
| 310 |
+
def get_mesh_np_attrs(
|
| 311 |
+
self,
|
| 312 |
+
mesh: trimesh.Trimesh,
|
| 313 |
+
scale: float = None,
|
| 314 |
+
center: np.ndarray = None,
|
| 315 |
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
| 316 |
+
vertices = mesh.vertices.copy()
|
| 317 |
+
faces = mesh.faces.copy()
|
| 318 |
+
uv_map = mesh.visual.uv.copy()
|
| 319 |
+
uv_map[:, 1] = 1.0 - uv_map[:, 1]
|
| 320 |
+
|
| 321 |
+
if scale is not None:
|
| 322 |
+
vertices = vertices / scale
|
| 323 |
+
if center is not None:
|
| 324 |
+
vertices = vertices + center
|
| 325 |
+
|
| 326 |
+
return vertices, faces, uv_map
|
| 327 |
+
|
| 328 |
+
def _render_depth_edges(self, depth_image: torch.Tensor) -> torch.Tensor:
|
| 329 |
+
depth_image_np = depth_image.cpu().numpy()
|
| 330 |
+
depth_image_np = (depth_image_np * 255).astype(np.uint8)
|
| 331 |
+
depth_edges = cv2.Canny(depth_image_np, 30, 80)
|
| 332 |
+
sketch_image = (
|
| 333 |
+
torch.from_numpy(depth_edges).to(depth_image.device).float() / 255
|
| 334 |
+
)
|
| 335 |
+
sketch_image = sketch_image.unsqueeze(-1)
|
| 336 |
+
|
| 337 |
+
return sketch_image
|
| 338 |
+
|
| 339 |
+
def compute_enhanced_viewnormal(
|
| 340 |
+
self, mv_mtx: torch.Tensor, vertices: torch.Tensor, faces: torch.Tensor
|
| 341 |
+
) -> torch.Tensor:
|
| 342 |
+
rast, _ = self.renderer.compute_dr_raster(vertices, faces)
|
| 343 |
+
rendered_view_normals = []
|
| 344 |
+
for idx in range(len(mv_mtx)):
|
| 345 |
+
pos_cam = _transform_vertices(mv_mtx[idx], vertices, keepdim=True)
|
| 346 |
+
pos_cam = pos_cam[:, :3] / pos_cam[:, 3:]
|
| 347 |
+
v0, v1, v2 = (pos_cam[faces[:, i]] for i in range(3))
|
| 348 |
+
face_norm = F.normalize(
|
| 349 |
+
torch.cross(v1 - v0, v2 - v0, dim=-1), dim=-1
|
| 350 |
+
)
|
| 351 |
+
vertex_norm = (
|
| 352 |
+
torch.from_numpy(
|
| 353 |
+
trimesh.geometry.mean_vertex_normals(
|
| 354 |
+
len(pos_cam), faces.cpu(), face_norm.cpu()
|
| 355 |
+
)
|
| 356 |
+
)
|
| 357 |
+
.to(vertices.device)
|
| 358 |
+
.contiguous()
|
| 359 |
+
)
|
| 360 |
+
im_base_normals, _ = dr.interpolate(
|
| 361 |
+
vertex_norm[None, ...].float(),
|
| 362 |
+
rast[idx : idx + 1],
|
| 363 |
+
faces.to(torch.int32),
|
| 364 |
+
)
|
| 365 |
+
rendered_view_normals.append(im_base_normals)
|
| 366 |
+
|
| 367 |
+
rendered_view_normals = torch.cat(rendered_view_normals, dim=0)
|
| 368 |
+
|
| 369 |
+
return rendered_view_normals
|
| 370 |
+
|
| 371 |
+
def back_project(
|
| 372 |
+
self, image, vis_mask, depth, normal, uv
|
| 373 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 374 |
+
image = np.array(image)
|
| 375 |
+
image = torch.as_tensor(image, device=self.device, dtype=torch.float32)
|
| 376 |
+
if image.ndim == 2:
|
| 377 |
+
image = image.unsqueeze(-1)
|
| 378 |
+
image = image / 255
|
| 379 |
+
|
| 380 |
+
depth_inv = (1.0 - depth) * vis_mask
|
| 381 |
+
sketch_image = self._render_depth_edges(depth_inv)
|
| 382 |
+
|
| 383 |
+
cos = F.cosine_similarity(
|
| 384 |
+
torch.tensor([[0, 0, 1]], device=self.device),
|
| 385 |
+
normal.view(-1, 3),
|
| 386 |
+
).view_as(normal[..., :1])
|
| 387 |
+
cos[cos < np.cos(np.radians(self.bake_angle_thresh))] = 0
|
| 388 |
+
|
| 389 |
+
k = self.bake_unreliable_kernel_size * 2 + 1
|
| 390 |
+
kernel = torch.ones((1, 1, k, k), device=self.device)
|
| 391 |
+
|
| 392 |
+
vis_mask = vis_mask.permute(2, 0, 1).unsqueeze(0).float()
|
| 393 |
+
vis_mask = F.conv2d(
|
| 394 |
+
1.0 - vis_mask,
|
| 395 |
+
kernel,
|
| 396 |
+
padding=k // 2,
|
| 397 |
+
)
|
| 398 |
+
vis_mask = 1.0 - (vis_mask > 0).float()
|
| 399 |
+
vis_mask = vis_mask.squeeze(0).permute(1, 2, 0)
|
| 400 |
+
|
| 401 |
+
sketch_image = sketch_image.permute(2, 0, 1).unsqueeze(0)
|
| 402 |
+
sketch_image = F.conv2d(sketch_image, kernel, padding=k // 2)
|
| 403 |
+
sketch_image = (sketch_image > 0).float()
|
| 404 |
+
sketch_image = sketch_image.squeeze(0).permute(1, 2, 0)
|
| 405 |
+
vis_mask = vis_mask * (sketch_image < 0.5)
|
| 406 |
+
|
| 407 |
+
cos[vis_mask == 0] = 0
|
| 408 |
+
valid_pixels = (vis_mask != 0).view(-1)
|
| 409 |
+
|
| 410 |
+
return (
|
| 411 |
+
self._scatter_texture(uv, image, valid_pixels),
|
| 412 |
+
self._scatter_texture(uv, cos, valid_pixels),
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
def _scatter_texture(self, uv, data, mask):
|
| 416 |
+
def __filter_data(data, mask):
|
| 417 |
+
return data.view(-1, data.shape[-1])[mask]
|
| 418 |
+
|
| 419 |
+
return _bilinear_interpolation_scattering(
|
| 420 |
+
self.texture_wh[1],
|
| 421 |
+
self.texture_wh[0],
|
| 422 |
+
__filter_data(uv, mask)[..., [1, 0]],
|
| 423 |
+
__filter_data(data, mask),
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
@torch.no_grad()
|
| 427 |
+
def fast_bake_texture(
|
| 428 |
+
self, textures: list[torch.Tensor], confidence_maps: list[torch.Tensor]
|
| 429 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 430 |
+
channel = textures[0].shape[-1]
|
| 431 |
+
texture_merge = torch.zeros(self.texture_wh + [channel]).to(
|
| 432 |
+
self.device
|
| 433 |
+
)
|
| 434 |
+
trust_map_merge = torch.zeros(self.texture_wh + [1]).to(self.device)
|
| 435 |
+
for texture, cos_map in zip(textures, confidence_maps):
|
| 436 |
+
view_sum = (cos_map > 0).sum()
|
| 437 |
+
painted_sum = ((cos_map > 0) * (trust_map_merge > 0)).sum()
|
| 438 |
+
if painted_sum / view_sum > 0.99:
|
| 439 |
+
continue
|
| 440 |
+
texture_merge += texture * cos_map
|
| 441 |
+
trust_map_merge += cos_map
|
| 442 |
+
texture_merge = texture_merge / torch.clamp(trust_map_merge, min=1e-8)
|
| 443 |
+
|
| 444 |
+
return texture_merge, trust_map_merge > 1e-8
|
| 445 |
+
|
| 446 |
+
def uv_inpaint(
|
| 447 |
+
self, mesh: trimesh.Trimesh, texture: np.ndarray, mask: np.ndarray
|
| 448 |
+
) -> np.ndarray:
|
| 449 |
+
vertices, faces, uv_map = self.get_mesh_np_attrs(mesh)
|
| 450 |
+
|
| 451 |
+
texture, mask = _texture_inpaint_smooth(
|
| 452 |
+
texture, mask, vertices, faces, uv_map
|
| 453 |
+
)
|
| 454 |
+
texture = texture.clip(0, 1)
|
| 455 |
+
texture = cv2.inpaint(
|
| 456 |
+
(texture * 255).astype(np.uint8),
|
| 457 |
+
255 - mask,
|
| 458 |
+
3,
|
| 459 |
+
cv2.INPAINT_NS,
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
return texture
|
| 463 |
+
|
| 464 |
+
@spaces.GPU
|
| 465 |
+
def compute_texture(
|
| 466 |
+
self,
|
| 467 |
+
colors: list[Image.Image],
|
| 468 |
+
mesh: trimesh.Trimesh,
|
| 469 |
+
) -> trimesh.Trimesh:
|
| 470 |
+
self._lazy_init_render(self.camera_params, self.mask_thresh)
|
| 471 |
+
|
| 472 |
+
vertices = torch.from_numpy(mesh.vertices).to(self.device).float()
|
| 473 |
+
faces = torch.from_numpy(mesh.faces).to(self.device).to(torch.int)
|
| 474 |
+
uv_map = torch.from_numpy(mesh.visual.uv).to(self.device).float()
|
| 475 |
+
|
| 476 |
+
rendered_depth, masks = self.renderer.render_depth(vertices, faces)
|
| 477 |
+
norm_deps = self.renderer.normalize_map_by_mask(rendered_depth, masks)
|
| 478 |
+
render_uvs, _ = self.renderer.render_uv(vertices, faces, uv_map)
|
| 479 |
+
view_normals = self.compute_enhanced_viewnormal(
|
| 480 |
+
self.renderer.mv_mtx, vertices, faces
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
textures, weighted_cos_maps = [], []
|
| 484 |
+
for color, mask, dep, normal, uv, weight in zip(
|
| 485 |
+
colors,
|
| 486 |
+
masks,
|
| 487 |
+
norm_deps,
|
| 488 |
+
view_normals,
|
| 489 |
+
render_uvs,
|
| 490 |
+
self.view_weights,
|
| 491 |
+
):
|
| 492 |
+
texture, cos_map = self.back_project(color, mask, dep, normal, uv)
|
| 493 |
+
textures.append(texture)
|
| 494 |
+
weighted_cos_maps.append(weight * (cos_map**4))
|
| 495 |
+
|
| 496 |
+
texture, mask = self.fast_bake_texture(textures, weighted_cos_maps)
|
| 497 |
+
|
| 498 |
+
texture_np = texture.cpu().numpy()
|
| 499 |
+
mask_np = (mask.squeeze(-1).cpu().numpy() * 255).astype(np.uint8)
|
| 500 |
+
|
| 501 |
+
return texture_np, mask_np
|
| 502 |
+
|
| 503 |
+
def __call__(
|
| 504 |
+
self,
|
| 505 |
+
colors: list[Image.Image],
|
| 506 |
+
mesh: trimesh.Trimesh,
|
| 507 |
+
output_path: str,
|
| 508 |
+
) -> trimesh.Trimesh:
|
| 509 |
+
"""Runs the texture baking and exports the textured mesh.
|
| 510 |
+
|
| 511 |
+
Args:
|
| 512 |
+
colors (list[Image.Image]): List of input view images.
|
| 513 |
+
mesh (trimesh.Trimesh): Input mesh to be textured.
|
| 514 |
+
output_path (str): Path to save the output textured mesh (.obj or .glb).
|
| 515 |
+
|
| 516 |
+
Returns:
|
| 517 |
+
trimesh.Trimesh: The textured mesh with UV and texture image.
|
| 518 |
+
"""
|
| 519 |
+
mesh = self.load_mesh(mesh)
|
| 520 |
+
texture_np, mask_np = self.compute_texture(colors, mesh)
|
| 521 |
+
|
| 522 |
+
texture_np = self.uv_inpaint(mesh, texture_np, mask_np)
|
| 523 |
+
if self.smooth_texture:
|
| 524 |
+
texture_np = post_process_texture(texture_np)
|
| 525 |
+
|
| 526 |
+
vertices, faces, uv_map = self.get_mesh_np_attrs(
|
| 527 |
+
mesh, self.scale, self.center
|
| 528 |
+
)
|
| 529 |
+
textured_mesh = save_mesh_with_mtl(
|
| 530 |
+
vertices, faces, uv_map, texture_np, output_path
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
return textured_mesh
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
def parse_args():
|
| 537 |
+
parser = argparse.ArgumentParser(description="Backproject texture")
|
| 538 |
+
parser.add_argument(
|
| 539 |
+
"--color_path",
|
| 540 |
+
type=str,
|
| 541 |
+
help="Multiview color image in 6x512x512 file path",
|
| 542 |
+
)
|
| 543 |
+
parser.add_argument(
|
| 544 |
+
"--mesh_path",
|
| 545 |
+
type=str,
|
| 546 |
+
help="Mesh path, .obj, .glb or .ply",
|
| 547 |
+
)
|
| 548 |
+
parser.add_argument(
|
| 549 |
+
"--output_path",
|
| 550 |
+
type=str,
|
| 551 |
+
help="Output mesh path with suffix",
|
| 552 |
+
)
|
| 553 |
+
parser.add_argument(
|
| 554 |
+
"--num_images", type=int, default=6, help="Number of images to render."
|
| 555 |
+
)
|
| 556 |
+
parser.add_argument(
|
| 557 |
+
"--elevation",
|
| 558 |
+
nargs=2,
|
| 559 |
+
type=float,
|
| 560 |
+
default=[20.0, -10.0],
|
| 561 |
+
help="Elevation angles for the camera (default: [20.0, -10.0])",
|
| 562 |
+
)
|
| 563 |
+
parser.add_argument(
|
| 564 |
+
"--distance",
|
| 565 |
+
type=float,
|
| 566 |
+
default=5,
|
| 567 |
+
help="Camera distance (default: 5)",
|
| 568 |
+
)
|
| 569 |
+
parser.add_argument(
|
| 570 |
+
"--resolution_hw",
|
| 571 |
+
type=int,
|
| 572 |
+
nargs=2,
|
| 573 |
+
default=(2048, 2048),
|
| 574 |
+
help="Resolution of the output images (default: (2048, 2048))",
|
| 575 |
+
)
|
| 576 |
+
parser.add_argument(
|
| 577 |
+
"--fov",
|
| 578 |
+
type=float,
|
| 579 |
+
default=30,
|
| 580 |
+
help="Field of view in degrees (default: 30)",
|
| 581 |
+
)
|
| 582 |
+
parser.add_argument(
|
| 583 |
+
"--device",
|
| 584 |
+
type=str,
|
| 585 |
+
choices=["cpu", "cuda"],
|
| 586 |
+
default="cuda",
|
| 587 |
+
help="Device to run on (default: `cuda`)",
|
| 588 |
+
)
|
| 589 |
+
parser.add_argument(
|
| 590 |
+
"--skip_fix_mesh", action="store_true", help="Fix mesh geometry."
|
| 591 |
+
)
|
| 592 |
+
parser.add_argument(
|
| 593 |
+
"--texture_wh",
|
| 594 |
+
nargs=2,
|
| 595 |
+
type=int,
|
| 596 |
+
default=[2048, 2048],
|
| 597 |
+
help="Texture resolution width and height",
|
| 598 |
+
)
|
| 599 |
+
parser.add_argument(
|
| 600 |
+
"--mesh_sipmlify_ratio",
|
| 601 |
+
type=float,
|
| 602 |
+
default=0.9,
|
| 603 |
+
help="Mesh simplification ratio (default: 0.9)",
|
| 604 |
+
)
|
| 605 |
+
parser.add_argument(
|
| 606 |
+
"--delight", action="store_true", help="Use delighting model."
|
| 607 |
+
)
|
| 608 |
+
parser.add_argument(
|
| 609 |
+
"--smooth_texture", type=bool, default=True, help="Smooth the texture."
|
| 610 |
+
)
|
| 611 |
+
|
| 612 |
+
args, unknown = parser.parse_known_args()
|
| 613 |
+
|
| 614 |
+
return args
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
def entrypoint(
|
| 618 |
+
delight_model: DelightingModel = None,
|
| 619 |
+
imagesr_model: ImageRealESRGAN = None,
|
| 620 |
+
**kwargs,
|
| 621 |
+
) -> trimesh.Trimesh:
|
| 622 |
+
args = parse_args()
|
| 623 |
+
for k, v in kwargs.items():
|
| 624 |
+
if hasattr(args, k) and v is not None:
|
| 625 |
+
setattr(args, k, v)
|
| 626 |
+
|
| 627 |
+
# Setup camera parameters.
|
| 628 |
+
camera_params = CameraSetting(
|
| 629 |
+
num_images=args.num_images,
|
| 630 |
+
elevation=args.elevation,
|
| 631 |
+
distance=args.distance,
|
| 632 |
+
resolution_hw=args.resolution_hw,
|
| 633 |
+
fov=math.radians(args.fov),
|
| 634 |
+
device=args.device,
|
| 635 |
+
)
|
| 636 |
+
view_weights = [1, 0.1, 0.02, 0.1, 1, 0.02]
|
| 637 |
+
|
| 638 |
+
color_grid = Image.open(args.color_path)
|
| 639 |
+
if args.delight:
|
| 640 |
+
if delight_model is None:
|
| 641 |
+
delight_model = DelightingModel()
|
| 642 |
+
save_dir = os.path.dirname(args.output_path)
|
| 643 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 644 |
+
color_grid = delight_model(color_grid)
|
| 645 |
+
color_grid.save(f"{save_dir}/color_grid_delight.png")
|
| 646 |
+
|
| 647 |
+
multiviews = get_images_from_grid(color_grid, img_size=512)
|
| 648 |
+
|
| 649 |
+
# Use RealESRGAN_x4plus for x4 (512->2048) image super resolution.
|
| 650 |
+
if imagesr_model is None:
|
| 651 |
+
imagesr_model = ImageRealESRGAN(outscale=4)
|
| 652 |
+
multiviews = [imagesr_model(img) for img in multiviews]
|
| 653 |
+
multiviews = [img.convert("RGB") for img in multiviews]
|
| 654 |
+
mesh = trimesh.load(args.mesh_path)
|
| 655 |
+
if isinstance(mesh, trimesh.Scene):
|
| 656 |
+
mesh = mesh.dump(concatenate=True)
|
| 657 |
+
|
| 658 |
+
if not args.skip_fix_mesh:
|
| 659 |
+
mesh.vertices, scale, center = normalize_vertices_array(mesh.vertices)
|
| 660 |
+
mesh_fixer = MeshFixer(mesh.vertices, mesh.faces, args.device)
|
| 661 |
+
mesh.vertices, mesh.faces = mesh_fixer(
|
| 662 |
+
filter_ratio=args.mesh_sipmlify_ratio,
|
| 663 |
+
max_hole_size=0.04,
|
| 664 |
+
resolution=1024,
|
| 665 |
+
num_views=1000,
|
| 666 |
+
norm_mesh_ratio=0.5,
|
| 667 |
+
)
|
| 668 |
+
# Restore scale.
|
| 669 |
+
mesh.vertices = mesh.vertices / scale
|
| 670 |
+
mesh.vertices = mesh.vertices + center
|
| 671 |
+
|
| 672 |
+
# Baking texture to mesh.
|
| 673 |
+
texture_backer = TextureBacker(
|
| 674 |
+
camera_params=camera_params,
|
| 675 |
+
view_weights=view_weights,
|
| 676 |
+
render_wh=camera_params.resolution_hw,
|
| 677 |
+
texture_wh=args.texture_wh,
|
| 678 |
+
smooth_texture=args.smooth_texture,
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
textured_mesh = texture_backer(multiviews, mesh, args.output_path)
|
| 682 |
+
|
| 683 |
+
return textured_mesh
|
| 684 |
+
|
| 685 |
+
|
| 686 |
+
if __name__ == "__main__":
|
| 687 |
+
entrypoint()
|
embodied_gen/data/datasets.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
import random
|
| 22 |
+
from typing import Any, Callable, Dict, List, Tuple
|
| 23 |
+
|
| 24 |
+
import torch
|
| 25 |
+
import torch.utils.checkpoint
|
| 26 |
+
from PIL import Image
|
| 27 |
+
from torch import nn
|
| 28 |
+
from torch.utils.data import Dataset
|
| 29 |
+
from torchvision import transforms
|
| 30 |
+
|
| 31 |
+
logging.basicConfig(
|
| 32 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 33 |
+
)
|
| 34 |
+
logger = logging.getLogger(__name__)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
__all__ = [
|
| 38 |
+
"Asset3dGenDataset",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class Asset3dGenDataset(Dataset):
|
| 43 |
+
def __init__(
|
| 44 |
+
self,
|
| 45 |
+
index_file: str,
|
| 46 |
+
target_hw: Tuple[int, int],
|
| 47 |
+
transform: Callable = None,
|
| 48 |
+
control_transform: Callable = None,
|
| 49 |
+
max_train_samples: int = None,
|
| 50 |
+
sub_idxs: List[List[int]] = None,
|
| 51 |
+
seed: int = 79,
|
| 52 |
+
) -> None:
|
| 53 |
+
if not os.path.exists(index_file):
|
| 54 |
+
raise FileNotFoundError(f"{index_file} index_file not found.")
|
| 55 |
+
|
| 56 |
+
self.index_file = index_file
|
| 57 |
+
self.target_hw = target_hw
|
| 58 |
+
self.transform = transform
|
| 59 |
+
self.control_transform = control_transform
|
| 60 |
+
self.max_train_samples = max_train_samples
|
| 61 |
+
self.meta_info = self.prepare_data_index(index_file)
|
| 62 |
+
self.data_list = sorted(self.meta_info.keys())
|
| 63 |
+
self.sub_idxs = sub_idxs # sub_idxs [[0,1,2], [3,4,5], [...], ...]
|
| 64 |
+
self.image_num = 6 # hardcode temp.
|
| 65 |
+
random.seed(seed)
|
| 66 |
+
logger.info(f"Trainset: {len(self)} asset3d instances.")
|
| 67 |
+
|
| 68 |
+
def __len__(self) -> int:
|
| 69 |
+
return len(self.meta_info)
|
| 70 |
+
|
| 71 |
+
def prepare_data_index(self, index_file: str) -> Dict[str, Any]:
|
| 72 |
+
with open(index_file, "r") as fin:
|
| 73 |
+
meta_info = json.load(fin)
|
| 74 |
+
|
| 75 |
+
meta_info_filtered = dict()
|
| 76 |
+
for idx, uid in enumerate(meta_info):
|
| 77 |
+
if "status" not in meta_info[uid]:
|
| 78 |
+
continue
|
| 79 |
+
if meta_info[uid]["status"] != "success":
|
| 80 |
+
continue
|
| 81 |
+
if self.max_train_samples and idx >= self.max_train_samples:
|
| 82 |
+
break
|
| 83 |
+
|
| 84 |
+
meta_info_filtered[uid] = meta_info[uid]
|
| 85 |
+
|
| 86 |
+
logger.info(
|
| 87 |
+
f"Load {len(meta_info)} assets, keep {len(meta_info_filtered)} valids." # noqa
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
return meta_info_filtered
|
| 91 |
+
|
| 92 |
+
def fetch_sample_images(
|
| 93 |
+
self,
|
| 94 |
+
uid: str,
|
| 95 |
+
attrs: List[str],
|
| 96 |
+
sub_index: int = None,
|
| 97 |
+
transform: Callable = None,
|
| 98 |
+
) -> torch.Tensor:
|
| 99 |
+
sample = self.meta_info[uid]
|
| 100 |
+
images = []
|
| 101 |
+
for attr in attrs:
|
| 102 |
+
item = sample[attr]
|
| 103 |
+
if sub_index is not None:
|
| 104 |
+
item = item[sub_index]
|
| 105 |
+
mode = "L" if attr == "image_mask" else "RGB"
|
| 106 |
+
image = Image.open(item).convert(mode)
|
| 107 |
+
if transform is not None:
|
| 108 |
+
image = transform(image)
|
| 109 |
+
if len(image.shape) == 2:
|
| 110 |
+
image = image[..., None]
|
| 111 |
+
images.append(image)
|
| 112 |
+
|
| 113 |
+
images = torch.cat(images, dim=0)
|
| 114 |
+
|
| 115 |
+
return images
|
| 116 |
+
|
| 117 |
+
def fetch_sample_grid_images(
|
| 118 |
+
self,
|
| 119 |
+
uid: str,
|
| 120 |
+
attrs: List[str],
|
| 121 |
+
sub_idxs: List[List[int]],
|
| 122 |
+
transform: Callable = None,
|
| 123 |
+
) -> torch.Tensor:
|
| 124 |
+
assert transform is not None
|
| 125 |
+
|
| 126 |
+
grid_image = []
|
| 127 |
+
for row_idxs in sub_idxs:
|
| 128 |
+
row_image = []
|
| 129 |
+
for row_idx in row_idxs:
|
| 130 |
+
image = self.fetch_sample_images(
|
| 131 |
+
uid, attrs, row_idx, transform
|
| 132 |
+
)
|
| 133 |
+
row_image.append(image)
|
| 134 |
+
row_image = torch.cat(row_image, dim=2) # (c h w)
|
| 135 |
+
grid_image.append(row_image)
|
| 136 |
+
|
| 137 |
+
grid_image = torch.cat(grid_image, dim=1)
|
| 138 |
+
|
| 139 |
+
return grid_image
|
| 140 |
+
|
| 141 |
+
def compute_text_embeddings(
|
| 142 |
+
self, embed_path: str, original_size: Tuple[int, int]
|
| 143 |
+
) -> Dict[str, nn.Module]:
|
| 144 |
+
data_dict = torch.load(embed_path)
|
| 145 |
+
prompt_embeds = data_dict["prompt_embeds"][0]
|
| 146 |
+
add_text_embeds = data_dict["pooled_prompt_embeds"][0]
|
| 147 |
+
|
| 148 |
+
# Need changed if random crop, set as crop_top_left [y1, x1], center crop as [0, 0]. # noqa
|
| 149 |
+
crops_coords_top_left = (0, 0)
|
| 150 |
+
add_time_ids = list(
|
| 151 |
+
original_size + crops_coords_top_left + self.target_hw
|
| 152 |
+
)
|
| 153 |
+
add_time_ids = torch.tensor([add_time_ids])
|
| 154 |
+
# add_time_ids = add_time_ids.repeat((len(add_text_embeds), 1))
|
| 155 |
+
|
| 156 |
+
unet_added_cond_kwargs = {
|
| 157 |
+
"text_embeds": add_text_embeds,
|
| 158 |
+
"time_ids": add_time_ids,
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
return {"prompt_embeds": prompt_embeds, **unet_added_cond_kwargs}
|
| 162 |
+
|
| 163 |
+
def visualize_item(
|
| 164 |
+
self,
|
| 165 |
+
control: torch.Tensor,
|
| 166 |
+
color: torch.Tensor,
|
| 167 |
+
save_dir: str = None,
|
| 168 |
+
) -> List[Image.Image]:
|
| 169 |
+
to_pil = transforms.ToPILImage()
|
| 170 |
+
|
| 171 |
+
color = (color + 1) / 2
|
| 172 |
+
color_pil = to_pil(color)
|
| 173 |
+
normal_pil = to_pil(control[0:3])
|
| 174 |
+
position_pil = to_pil(control[3:6])
|
| 175 |
+
mask_pil = to_pil(control[6:])
|
| 176 |
+
|
| 177 |
+
if save_dir is not None:
|
| 178 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 179 |
+
color_pil.save(f"{save_dir}/rgb.jpg")
|
| 180 |
+
normal_pil.save(f"{save_dir}/normal.jpg")
|
| 181 |
+
position_pil.save(f"{save_dir}/position.jpg")
|
| 182 |
+
mask_pil.save(f"{save_dir}/mask.jpg")
|
| 183 |
+
logger.info(f"Visualization in {save_dir}")
|
| 184 |
+
|
| 185 |
+
return normal_pil, position_pil, mask_pil, color_pil
|
| 186 |
+
|
| 187 |
+
def __getitem__(self, index: int) -> Dict[str, torch.Tensor]:
|
| 188 |
+
uid = self.data_list[index]
|
| 189 |
+
|
| 190 |
+
sub_idxs = self.sub_idxs
|
| 191 |
+
if sub_idxs is None:
|
| 192 |
+
sub_idxs = [[random.randint(0, self.image_num - 1)]]
|
| 193 |
+
|
| 194 |
+
input_image = self.fetch_sample_grid_images(
|
| 195 |
+
uid,
|
| 196 |
+
attrs=["image_view_normal", "image_position", "image_mask"],
|
| 197 |
+
sub_idxs=sub_idxs,
|
| 198 |
+
transform=self.control_transform,
|
| 199 |
+
)
|
| 200 |
+
assert input_image.shape[1:] == self.target_hw
|
| 201 |
+
|
| 202 |
+
output_image = self.fetch_sample_grid_images(
|
| 203 |
+
uid,
|
| 204 |
+
attrs=["image_color"],
|
| 205 |
+
sub_idxs=sub_idxs,
|
| 206 |
+
transform=self.transform,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
sample = self.meta_info[uid]
|
| 210 |
+
text_feats = self.compute_text_embeddings(
|
| 211 |
+
sample["text_feat"], tuple(sample["image_hw"])
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
data = dict(
|
| 215 |
+
pixel_values=output_image,
|
| 216 |
+
conditioning_pixel_values=input_image,
|
| 217 |
+
prompt_embeds=text_feats["prompt_embeds"],
|
| 218 |
+
text_embeds=text_feats["text_embeds"],
|
| 219 |
+
time_ids=text_feats["time_ids"],
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
return data
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
if __name__ == "__main__":
|
| 226 |
+
index_file = "datasets/objaverse/v1.0/statistics_1.0_gobjaverse_filter/view6s_v4/meta_ac2e0ddea8909db26d102c8465b5bcb2.json" # noqa
|
| 227 |
+
target_hw = (512, 512)
|
| 228 |
+
transform_list = [
|
| 229 |
+
transforms.Resize(
|
| 230 |
+
target_hw, interpolation=transforms.InterpolationMode.BILINEAR
|
| 231 |
+
),
|
| 232 |
+
transforms.CenterCrop(target_hw),
|
| 233 |
+
transforms.ToTensor(),
|
| 234 |
+
transforms.Normalize([0.5], [0.5]),
|
| 235 |
+
]
|
| 236 |
+
image_transform = transforms.Compose(transform_list)
|
| 237 |
+
control_transform = transforms.Compose(transform_list[:-1])
|
| 238 |
+
|
| 239 |
+
sub_idxs = [[0, 1, 2], [3, 4, 5]] # None
|
| 240 |
+
if sub_idxs is not None:
|
| 241 |
+
target_hw = (
|
| 242 |
+
target_hw[0] * len(sub_idxs),
|
| 243 |
+
target_hw[1] * len(sub_idxs[0]),
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
dataset = Asset3dGenDataset(
|
| 247 |
+
index_file,
|
| 248 |
+
target_hw,
|
| 249 |
+
image_transform,
|
| 250 |
+
control_transform,
|
| 251 |
+
sub_idxs=sub_idxs,
|
| 252 |
+
)
|
| 253 |
+
data = dataset[0]
|
| 254 |
+
dataset.visualize_item(
|
| 255 |
+
data["conditioning_pixel_values"], data["pixel_values"], save_dir="./"
|
| 256 |
+
)
|
embodied_gen/data/differentiable_render.py
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import json
|
| 20 |
+
import logging
|
| 21 |
+
import math
|
| 22 |
+
import os
|
| 23 |
+
from collections import defaultdict
|
| 24 |
+
from typing import List, Union
|
| 25 |
+
|
| 26 |
+
import cv2
|
| 27 |
+
import nvdiffrast.torch as dr
|
| 28 |
+
import torch
|
| 29 |
+
from tqdm import tqdm
|
| 30 |
+
from embodied_gen.data.utils import (
|
| 31 |
+
CameraSetting,
|
| 32 |
+
DiffrastRender,
|
| 33 |
+
RenderItems,
|
| 34 |
+
as_list,
|
| 35 |
+
calc_vertex_normals,
|
| 36 |
+
import_kaolin_mesh,
|
| 37 |
+
init_kal_camera,
|
| 38 |
+
normalize_vertices_array,
|
| 39 |
+
render_pbr,
|
| 40 |
+
save_images,
|
| 41 |
+
)
|
| 42 |
+
from embodied_gen.utils.process_media import (
|
| 43 |
+
create_gif_from_images,
|
| 44 |
+
create_mp4_from_images,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
|
| 48 |
+
os.environ["TORCH_EXTENSIONS_DIR"] = os.path.expanduser(
|
| 49 |
+
"~/.cache/torch_extensions"
|
| 50 |
+
)
|
| 51 |
+
logging.basicConfig(
|
| 52 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 53 |
+
)
|
| 54 |
+
logger = logging.getLogger(__name__)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
__all__ = ["ImageRender"]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class ImageRender(object):
|
| 61 |
+
"""A differentiable mesh renderer supporting multi-view rendering.
|
| 62 |
+
|
| 63 |
+
This class wraps a differentiable rasterization using `nvdiffrast` to
|
| 64 |
+
render mesh geometry to various maps (normal, depth, alpha, albedo, etc.).
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
render_items (list[RenderItems]): A list of rendering targets to
|
| 68 |
+
generate (e.g., IMAGE, DEPTH, NORMAL, etc.).
|
| 69 |
+
camera_params (CameraSetting): The camera parameters for rendering,
|
| 70 |
+
including intrinsic and extrinsic matrices.
|
| 71 |
+
recompute_vtx_normal (bool, optional): If True, recomputes
|
| 72 |
+
vertex normals from the mesh geometry. Defaults to True.
|
| 73 |
+
with_mtl (bool, optional): Whether to load `.mtl` material files
|
| 74 |
+
for meshes. Defaults to False.
|
| 75 |
+
gen_color_gif (bool, optional): Generate a GIF of rendered
|
| 76 |
+
color images. Defaults to False.
|
| 77 |
+
gen_color_mp4 (bool, optional): Generate an MP4 video of rendered
|
| 78 |
+
color images. Defaults to False.
|
| 79 |
+
gen_viewnormal_mp4 (bool, optional): Generate an MP4 video of
|
| 80 |
+
view-space normals. Defaults to False.
|
| 81 |
+
gen_glonormal_mp4 (bool, optional): Generate an MP4 video of
|
| 82 |
+
global-space normals. Defaults to False.
|
| 83 |
+
no_index_file (bool, optional): If True, skip saving the `index.json`
|
| 84 |
+
summary file. Defaults to False.
|
| 85 |
+
light_factor (float, optional): A scalar multiplier for
|
| 86 |
+
PBR light intensity. Defaults to 1.0.
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
def __init__(
|
| 90 |
+
self,
|
| 91 |
+
render_items: list[RenderItems],
|
| 92 |
+
camera_params: CameraSetting,
|
| 93 |
+
recompute_vtx_normal: bool = True,
|
| 94 |
+
with_mtl: bool = False,
|
| 95 |
+
gen_color_gif: bool = False,
|
| 96 |
+
gen_color_mp4: bool = False,
|
| 97 |
+
gen_viewnormal_mp4: bool = False,
|
| 98 |
+
gen_glonormal_mp4: bool = False,
|
| 99 |
+
no_index_file: bool = False,
|
| 100 |
+
light_factor: float = 1.0,
|
| 101 |
+
) -> None:
|
| 102 |
+
camera = init_kal_camera(camera_params)
|
| 103 |
+
self.camera = camera
|
| 104 |
+
|
| 105 |
+
# Setup MVP matrix and renderer.
|
| 106 |
+
mv = camera.view_matrix() # (n 4 4) world2cam
|
| 107 |
+
p = camera.intrinsics.projection_matrix()
|
| 108 |
+
# NOTE: add a negative sign at P[0, 2] as the y axis is flipped in `nvdiffrast` output. # noqa
|
| 109 |
+
p[:, 1, 1] = -p[:, 1, 1]
|
| 110 |
+
# mvp = torch.bmm(p, mv) # camera.view_projection_matrix()
|
| 111 |
+
self.mv = mv
|
| 112 |
+
self.p = p
|
| 113 |
+
|
| 114 |
+
renderer = DiffrastRender(
|
| 115 |
+
p_matrix=p,
|
| 116 |
+
mv_matrix=mv,
|
| 117 |
+
resolution_hw=camera_params.resolution_hw,
|
| 118 |
+
context=dr.RasterizeCudaContext(),
|
| 119 |
+
mask_thresh=0.5,
|
| 120 |
+
grad_db=False,
|
| 121 |
+
device=camera_params.device,
|
| 122 |
+
antialias_mask=True,
|
| 123 |
+
)
|
| 124 |
+
self.renderer = renderer
|
| 125 |
+
self.recompute_vtx_normal = recompute_vtx_normal
|
| 126 |
+
self.render_items = render_items
|
| 127 |
+
self.device = camera_params.device
|
| 128 |
+
self.with_mtl = with_mtl
|
| 129 |
+
self.gen_color_gif = gen_color_gif
|
| 130 |
+
self.gen_color_mp4 = gen_color_mp4
|
| 131 |
+
self.gen_viewnormal_mp4 = gen_viewnormal_mp4
|
| 132 |
+
self.gen_glonormal_mp4 = gen_glonormal_mp4
|
| 133 |
+
self.light_factor = light_factor
|
| 134 |
+
self.no_index_file = no_index_file
|
| 135 |
+
|
| 136 |
+
def render_mesh(
|
| 137 |
+
self,
|
| 138 |
+
mesh_path: Union[str, List[str]],
|
| 139 |
+
output_root: str,
|
| 140 |
+
uuid: Union[str, List[str]] = None,
|
| 141 |
+
prompts: List[str] = None,
|
| 142 |
+
) -> None:
|
| 143 |
+
mesh_path = as_list(mesh_path)
|
| 144 |
+
if uuid is None:
|
| 145 |
+
uuid = [os.path.basename(p).split(".")[0] for p in mesh_path]
|
| 146 |
+
uuid = as_list(uuid)
|
| 147 |
+
assert len(mesh_path) == len(uuid)
|
| 148 |
+
os.makedirs(output_root, exist_ok=True)
|
| 149 |
+
|
| 150 |
+
meta_info = dict()
|
| 151 |
+
for idx, (path, uid) in tqdm(
|
| 152 |
+
enumerate(zip(mesh_path, uuid)), total=len(mesh_path)
|
| 153 |
+
):
|
| 154 |
+
output_dir = os.path.join(output_root, uid)
|
| 155 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 156 |
+
prompt = prompts[idx] if prompts else None
|
| 157 |
+
data_dict = self(path, output_dir, prompt)
|
| 158 |
+
meta_info[uid] = data_dict
|
| 159 |
+
|
| 160 |
+
if self.no_index_file:
|
| 161 |
+
return
|
| 162 |
+
|
| 163 |
+
index_file = os.path.join(output_root, "index.json")
|
| 164 |
+
with open(index_file, "w") as fout:
|
| 165 |
+
json.dump(meta_info, fout)
|
| 166 |
+
|
| 167 |
+
logger.info(f"Rendering meta info logged in {index_file}")
|
| 168 |
+
|
| 169 |
+
def __call__(
|
| 170 |
+
self, mesh_path: str, output_dir: str, prompt: str = None
|
| 171 |
+
) -> dict[str, str]:
|
| 172 |
+
"""Render a single mesh and return paths to the rendered outputs.
|
| 173 |
+
|
| 174 |
+
Processes the input mesh, renders multiple modalities (e.g., normals,
|
| 175 |
+
depth, albedo), and optionally saves video or image sequences.
|
| 176 |
+
|
| 177 |
+
Args:
|
| 178 |
+
mesh_path (str): Path to the mesh file (.obj/.glb).
|
| 179 |
+
output_dir (str): Directory to save rendered outputs.
|
| 180 |
+
prompt (str, optional): Optional caption prompt for MP4 metadata.
|
| 181 |
+
|
| 182 |
+
Returns:
|
| 183 |
+
dict[str, str]: A mapping render types to the saved image paths.
|
| 184 |
+
"""
|
| 185 |
+
try:
|
| 186 |
+
mesh = import_kaolin_mesh(mesh_path, self.with_mtl)
|
| 187 |
+
except Exception as e:
|
| 188 |
+
logger.error(f"[ERROR MESH LOAD]: {e}, skip {mesh_path}")
|
| 189 |
+
return
|
| 190 |
+
|
| 191 |
+
mesh.vertices, scale, center = normalize_vertices_array(mesh.vertices)
|
| 192 |
+
if self.recompute_vtx_normal:
|
| 193 |
+
mesh.vertex_normals = calc_vertex_normals(
|
| 194 |
+
mesh.vertices, mesh.faces
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
mesh = mesh.to(self.device)
|
| 198 |
+
vertices, faces, vertex_normals = (
|
| 199 |
+
mesh.vertices,
|
| 200 |
+
mesh.faces,
|
| 201 |
+
mesh.vertex_normals,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Perform rendering.
|
| 205 |
+
data_dict = defaultdict(list)
|
| 206 |
+
if RenderItems.ALPHA.value in self.render_items:
|
| 207 |
+
masks, _ = self.renderer.render_rast_alpha(vertices, faces)
|
| 208 |
+
render_paths = save_images(
|
| 209 |
+
masks, f"{output_dir}/{RenderItems.ALPHA}"
|
| 210 |
+
)
|
| 211 |
+
data_dict[RenderItems.ALPHA.value] = render_paths
|
| 212 |
+
|
| 213 |
+
if RenderItems.GLOBAL_NORMAL.value in self.render_items:
|
| 214 |
+
rendered_normals, masks = self.renderer.render_global_normal(
|
| 215 |
+
vertices, faces, vertex_normals
|
| 216 |
+
)
|
| 217 |
+
if self.gen_glonormal_mp4:
|
| 218 |
+
if isinstance(rendered_normals, torch.Tensor):
|
| 219 |
+
rendered_normals = rendered_normals.detach().cpu().numpy()
|
| 220 |
+
create_mp4_from_images(
|
| 221 |
+
rendered_normals,
|
| 222 |
+
output_path=f"{output_dir}/normal.mp4",
|
| 223 |
+
fps=15,
|
| 224 |
+
prompt=prompt,
|
| 225 |
+
)
|
| 226 |
+
else:
|
| 227 |
+
render_paths = save_images(
|
| 228 |
+
rendered_normals,
|
| 229 |
+
f"{output_dir}/{RenderItems.GLOBAL_NORMAL}",
|
| 230 |
+
cvt_color=cv2.COLOR_BGR2RGB,
|
| 231 |
+
)
|
| 232 |
+
data_dict[RenderItems.GLOBAL_NORMAL.value] = render_paths
|
| 233 |
+
|
| 234 |
+
if RenderItems.VIEW_NORMAL.value in self.render_items:
|
| 235 |
+
assert (
|
| 236 |
+
RenderItems.GLOBAL_NORMAL in self.render_items
|
| 237 |
+
), f"Must render global normal firstly, got render_items: {self.render_items}." # noqa
|
| 238 |
+
rendered_view_normals = self.renderer.transform_normal(
|
| 239 |
+
rendered_normals, self.mv, masks, to_view=True
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
if self.gen_viewnormal_mp4:
|
| 243 |
+
create_mp4_from_images(
|
| 244 |
+
rendered_view_normals,
|
| 245 |
+
output_path=f"{output_dir}/view_normal.mp4",
|
| 246 |
+
fps=15,
|
| 247 |
+
prompt=prompt,
|
| 248 |
+
)
|
| 249 |
+
else:
|
| 250 |
+
render_paths = save_images(
|
| 251 |
+
rendered_view_normals,
|
| 252 |
+
f"{output_dir}/{RenderItems.VIEW_NORMAL}",
|
| 253 |
+
cvt_color=cv2.COLOR_BGR2RGB,
|
| 254 |
+
)
|
| 255 |
+
data_dict[RenderItems.VIEW_NORMAL.value] = render_paths
|
| 256 |
+
|
| 257 |
+
if RenderItems.POSITION_MAP.value in self.render_items:
|
| 258 |
+
rendered_position, masks = self.renderer.render_position(
|
| 259 |
+
vertices, faces
|
| 260 |
+
)
|
| 261 |
+
norm_position = self.renderer.normalize_map_by_mask(
|
| 262 |
+
rendered_position, masks
|
| 263 |
+
)
|
| 264 |
+
render_paths = save_images(
|
| 265 |
+
norm_position,
|
| 266 |
+
f"{output_dir}/{RenderItems.POSITION_MAP}",
|
| 267 |
+
cvt_color=cv2.COLOR_BGR2RGB,
|
| 268 |
+
)
|
| 269 |
+
data_dict[RenderItems.POSITION_MAP.value] = render_paths
|
| 270 |
+
|
| 271 |
+
if RenderItems.DEPTH.value in self.render_items:
|
| 272 |
+
rendered_depth, masks = self.renderer.render_depth(vertices, faces)
|
| 273 |
+
norm_depth = self.renderer.normalize_map_by_mask(
|
| 274 |
+
rendered_depth, masks
|
| 275 |
+
)
|
| 276 |
+
render_paths = save_images(
|
| 277 |
+
norm_depth,
|
| 278 |
+
f"{output_dir}/{RenderItems.DEPTH}",
|
| 279 |
+
)
|
| 280 |
+
data_dict[RenderItems.DEPTH.value] = render_paths
|
| 281 |
+
|
| 282 |
+
render_paths = save_images(
|
| 283 |
+
rendered_depth,
|
| 284 |
+
f"{output_dir}/{RenderItems.DEPTH}_exr",
|
| 285 |
+
to_uint8=False,
|
| 286 |
+
format=".exr",
|
| 287 |
+
)
|
| 288 |
+
data_dict[f"{RenderItems.DEPTH.value}_exr"] = render_paths
|
| 289 |
+
|
| 290 |
+
if RenderItems.IMAGE.value in self.render_items:
|
| 291 |
+
images = []
|
| 292 |
+
albedos = []
|
| 293 |
+
diffuses = []
|
| 294 |
+
masks, _ = self.renderer.render_rast_alpha(vertices, faces)
|
| 295 |
+
try:
|
| 296 |
+
for idx, cam in enumerate(self.camera):
|
| 297 |
+
image, albedo, diffuse, _ = render_pbr(
|
| 298 |
+
mesh, cam, light_factor=self.light_factor
|
| 299 |
+
)
|
| 300 |
+
image = torch.cat([image[0], masks[idx]], axis=-1)
|
| 301 |
+
images.append(image.detach().cpu().numpy())
|
| 302 |
+
|
| 303 |
+
if RenderItems.ALBEDO.value in self.render_items:
|
| 304 |
+
albedo = torch.cat([albedo[0], masks[idx]], axis=-1)
|
| 305 |
+
albedos.append(albedo.detach().cpu().numpy())
|
| 306 |
+
|
| 307 |
+
if RenderItems.DIFFUSE.value in self.render_items:
|
| 308 |
+
diffuse = torch.cat([diffuse[0], masks[idx]], axis=-1)
|
| 309 |
+
diffuses.append(diffuse.detach().cpu().numpy())
|
| 310 |
+
|
| 311 |
+
except Exception as e:
|
| 312 |
+
logger.error(f"[ERROR pbr render]: {e}, skip {mesh_path}")
|
| 313 |
+
return
|
| 314 |
+
|
| 315 |
+
if self.gen_color_gif:
|
| 316 |
+
create_gif_from_images(
|
| 317 |
+
images,
|
| 318 |
+
output_path=f"{output_dir}/color.gif",
|
| 319 |
+
fps=15,
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
if self.gen_color_mp4:
|
| 323 |
+
create_mp4_from_images(
|
| 324 |
+
images,
|
| 325 |
+
output_path=f"{output_dir}/color.mp4",
|
| 326 |
+
fps=15,
|
| 327 |
+
prompt=prompt,
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
if self.gen_color_mp4 or self.gen_color_gif:
|
| 331 |
+
return data_dict
|
| 332 |
+
|
| 333 |
+
render_paths = save_images(
|
| 334 |
+
images,
|
| 335 |
+
f"{output_dir}/{RenderItems.IMAGE}",
|
| 336 |
+
cvt_color=cv2.COLOR_BGRA2RGBA,
|
| 337 |
+
)
|
| 338 |
+
data_dict[RenderItems.IMAGE.value] = render_paths
|
| 339 |
+
|
| 340 |
+
render_paths = save_images(
|
| 341 |
+
albedos,
|
| 342 |
+
f"{output_dir}/{RenderItems.ALBEDO}",
|
| 343 |
+
cvt_color=cv2.COLOR_BGRA2RGBA,
|
| 344 |
+
)
|
| 345 |
+
data_dict[RenderItems.ALBEDO.value] = render_paths
|
| 346 |
+
|
| 347 |
+
render_paths = save_images(
|
| 348 |
+
diffuses,
|
| 349 |
+
f"{output_dir}/{RenderItems.DIFFUSE}",
|
| 350 |
+
cvt_color=cv2.COLOR_BGRA2RGBA,
|
| 351 |
+
)
|
| 352 |
+
data_dict[RenderItems.DIFFUSE.value] = render_paths
|
| 353 |
+
|
| 354 |
+
data_dict["status"] = "success"
|
| 355 |
+
|
| 356 |
+
logger.info(f"Finish rendering in {output_dir}")
|
| 357 |
+
|
| 358 |
+
return data_dict
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def parse_args():
|
| 362 |
+
parser = argparse.ArgumentParser(description="Render settings")
|
| 363 |
+
|
| 364 |
+
parser.add_argument(
|
| 365 |
+
"--mesh_path",
|
| 366 |
+
type=str,
|
| 367 |
+
nargs="+",
|
| 368 |
+
help="Paths to the mesh files for rendering.",
|
| 369 |
+
)
|
| 370 |
+
parser.add_argument(
|
| 371 |
+
"--output_root",
|
| 372 |
+
type=str,
|
| 373 |
+
help="Root directory for output",
|
| 374 |
+
)
|
| 375 |
+
parser.add_argument(
|
| 376 |
+
"--uuid",
|
| 377 |
+
type=str,
|
| 378 |
+
nargs="+",
|
| 379 |
+
default=None,
|
| 380 |
+
help="uuid for rendering saving.",
|
| 381 |
+
)
|
| 382 |
+
parser.add_argument(
|
| 383 |
+
"--num_images", type=int, default=6, help="Number of images to render."
|
| 384 |
+
)
|
| 385 |
+
parser.add_argument(
|
| 386 |
+
"--elevation",
|
| 387 |
+
type=float,
|
| 388 |
+
nargs="+",
|
| 389 |
+
default=[20.0, -10.0],
|
| 390 |
+
help="Elevation angles for the camera (default: [20.0, -10.0])",
|
| 391 |
+
)
|
| 392 |
+
parser.add_argument(
|
| 393 |
+
"--distance",
|
| 394 |
+
type=float,
|
| 395 |
+
default=5,
|
| 396 |
+
help="Camera distance (default: 5)",
|
| 397 |
+
)
|
| 398 |
+
parser.add_argument(
|
| 399 |
+
"--resolution_hw",
|
| 400 |
+
type=int,
|
| 401 |
+
nargs=2,
|
| 402 |
+
default=(512, 512),
|
| 403 |
+
help="Resolution of the output images (default: (512, 512))",
|
| 404 |
+
)
|
| 405 |
+
parser.add_argument(
|
| 406 |
+
"--fov",
|
| 407 |
+
type=float,
|
| 408 |
+
default=30,
|
| 409 |
+
help="Field of view in degrees (default: 30)",
|
| 410 |
+
)
|
| 411 |
+
parser.add_argument(
|
| 412 |
+
"--pbr_light_factor",
|
| 413 |
+
type=float,
|
| 414 |
+
default=1.0,
|
| 415 |
+
help="Light factor for mesh PBR rendering (default: 2.)",
|
| 416 |
+
)
|
| 417 |
+
parser.add_argument(
|
| 418 |
+
"--with_mtl",
|
| 419 |
+
action="store_true",
|
| 420 |
+
help="Whether to render with mesh material.",
|
| 421 |
+
)
|
| 422 |
+
parser.add_argument(
|
| 423 |
+
"--gen_color_gif",
|
| 424 |
+
action="store_true",
|
| 425 |
+
help="Whether to generate color .gif rendering file.",
|
| 426 |
+
)
|
| 427 |
+
parser.add_argument(
|
| 428 |
+
"--gen_color_mp4",
|
| 429 |
+
action="store_true",
|
| 430 |
+
help="Whether to generate color .mp4 rendering file.",
|
| 431 |
+
)
|
| 432 |
+
parser.add_argument(
|
| 433 |
+
"--gen_viewnormal_mp4",
|
| 434 |
+
action="store_true",
|
| 435 |
+
help="Whether to generate view normal .mp4 rendering file.",
|
| 436 |
+
)
|
| 437 |
+
parser.add_argument(
|
| 438 |
+
"--gen_glonormal_mp4",
|
| 439 |
+
action="store_true",
|
| 440 |
+
help="Whether to generate global normal .mp4 rendering file.",
|
| 441 |
+
)
|
| 442 |
+
parser.add_argument(
|
| 443 |
+
"--prompts",
|
| 444 |
+
type=str,
|
| 445 |
+
nargs="+",
|
| 446 |
+
default=None,
|
| 447 |
+
help="Text prompts for the rendering.",
|
| 448 |
+
)
|
| 449 |
+
|
| 450 |
+
args = parser.parse_args()
|
| 451 |
+
|
| 452 |
+
if args.uuid is None and args.mesh_path is not None:
|
| 453 |
+
args.uuid = []
|
| 454 |
+
for path in args.mesh_path:
|
| 455 |
+
uuid = os.path.basename(path).split(".")[0]
|
| 456 |
+
args.uuid.append(uuid)
|
| 457 |
+
|
| 458 |
+
return args
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def entrypoint(**kwargs) -> None:
|
| 462 |
+
args = parse_args()
|
| 463 |
+
for k, v in kwargs.items():
|
| 464 |
+
if hasattr(args, k) and v is not None:
|
| 465 |
+
setattr(args, k, v)
|
| 466 |
+
|
| 467 |
+
camera_settings = CameraSetting(
|
| 468 |
+
num_images=args.num_images,
|
| 469 |
+
elevation=args.elevation,
|
| 470 |
+
distance=args.distance,
|
| 471 |
+
resolution_hw=args.resolution_hw,
|
| 472 |
+
fov=math.radians(args.fov),
|
| 473 |
+
device="cuda",
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
render_items = [
|
| 477 |
+
RenderItems.ALPHA.value,
|
| 478 |
+
RenderItems.GLOBAL_NORMAL.value,
|
| 479 |
+
RenderItems.VIEW_NORMAL.value,
|
| 480 |
+
RenderItems.POSITION_MAP.value,
|
| 481 |
+
RenderItems.IMAGE.value,
|
| 482 |
+
RenderItems.DEPTH.value,
|
| 483 |
+
# RenderItems.ALBEDO.value,
|
| 484 |
+
# RenderItems.DIFFUSE.value,
|
| 485 |
+
]
|
| 486 |
+
|
| 487 |
+
gen_video = (
|
| 488 |
+
args.gen_color_gif
|
| 489 |
+
or args.gen_color_mp4
|
| 490 |
+
or args.gen_viewnormal_mp4
|
| 491 |
+
or args.gen_glonormal_mp4
|
| 492 |
+
)
|
| 493 |
+
if gen_video:
|
| 494 |
+
render_items = []
|
| 495 |
+
if args.gen_color_gif or args.gen_color_mp4:
|
| 496 |
+
render_items.append(RenderItems.IMAGE.value)
|
| 497 |
+
if args.gen_glonormal_mp4:
|
| 498 |
+
render_items.append(RenderItems.GLOBAL_NORMAL.value)
|
| 499 |
+
if args.gen_viewnormal_mp4:
|
| 500 |
+
render_items.append(RenderItems.VIEW_NORMAL.value)
|
| 501 |
+
if RenderItems.GLOBAL_NORMAL.value not in render_items:
|
| 502 |
+
render_items.append(RenderItems.GLOBAL_NORMAL.value)
|
| 503 |
+
|
| 504 |
+
image_render = ImageRender(
|
| 505 |
+
render_items=render_items,
|
| 506 |
+
camera_params=camera_settings,
|
| 507 |
+
with_mtl=args.with_mtl,
|
| 508 |
+
gen_color_gif=args.gen_color_gif,
|
| 509 |
+
gen_color_mp4=args.gen_color_mp4,
|
| 510 |
+
gen_viewnormal_mp4=args.gen_viewnormal_mp4,
|
| 511 |
+
gen_glonormal_mp4=args.gen_glonormal_mp4,
|
| 512 |
+
light_factor=args.pbr_light_factor,
|
| 513 |
+
no_index_file=gen_video,
|
| 514 |
+
)
|
| 515 |
+
image_render.render_mesh(
|
| 516 |
+
mesh_path=args.mesh_path,
|
| 517 |
+
output_root=args.output_root,
|
| 518 |
+
uuid=args.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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
from typing import Tuple, Union
|
| 20 |
+
|
| 21 |
+
import igraph
|
| 22 |
+
import numpy as np
|
| 23 |
+
import pyvista as pv
|
| 24 |
+
import spaces
|
| 25 |
+
import torch
|
| 26 |
+
import utils3d
|
| 27 |
+
from pymeshfix import _meshfix
|
| 28 |
+
from tqdm import tqdm
|
| 29 |
+
|
| 30 |
+
logging.basicConfig(
|
| 31 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 32 |
+
)
|
| 33 |
+
logger = logging.getLogger(__name__)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
__all__ = ["MeshFixer"]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _radical_inverse(base, n):
|
| 40 |
+
val = 0
|
| 41 |
+
inv_base = 1.0 / base
|
| 42 |
+
inv_base_n = inv_base
|
| 43 |
+
while n > 0:
|
| 44 |
+
digit = n % base
|
| 45 |
+
val += digit * inv_base_n
|
| 46 |
+
n //= base
|
| 47 |
+
inv_base_n *= inv_base
|
| 48 |
+
return val
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _halton_sequence(dim, n):
|
| 52 |
+
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
|
| 53 |
+
return [_radical_inverse(PRIMES[dim], n) for dim in range(dim)]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _hammersley_sequence(dim, n, num_samples):
|
| 57 |
+
return [n / num_samples] + _halton_sequence(dim - 1, n)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _sphere_hammersley_seq(n, num_samples, offset=(0, 0), remap=False):
|
| 61 |
+
"""Generate a point on a unit sphere using the Hammersley sequence.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
n (int): The index of the sample.
|
| 65 |
+
num_samples (int): The total number of samples.
|
| 66 |
+
offset (tuple, optional): Offset for the u and v coordinates.
|
| 67 |
+
remap (bool, optional): Whether to remap the u coordinate.
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
list: A list containing the spherical coordinates [phi, theta].
|
| 71 |
+
"""
|
| 72 |
+
u, v = _hammersley_sequence(2, n, num_samples)
|
| 73 |
+
u += offset[0] / num_samples
|
| 74 |
+
v += offset[1]
|
| 75 |
+
|
| 76 |
+
if remap:
|
| 77 |
+
u = 2 * u if u < 0.25 else 2 / 3 * u + 1 / 3
|
| 78 |
+
|
| 79 |
+
theta = np.arccos(1 - 2 * u) - np.pi / 2
|
| 80 |
+
phi = v * 2 * np.pi
|
| 81 |
+
return [phi, theta]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class MeshFixer(object):
|
| 85 |
+
"""MeshFixer simplifies and repairs 3D triangle meshes by TSDF.
|
| 86 |
+
|
| 87 |
+
Attributes:
|
| 88 |
+
vertices (torch.Tensor): A tensor of shape (V, 3) representing vertex positions.
|
| 89 |
+
faces (torch.Tensor): A tensor of shape (F, 3) representing face indices.
|
| 90 |
+
device (str): Device to run computations on, typically "cuda" or "cpu".
|
| 91 |
+
|
| 92 |
+
Main logic reference: https://github.com/microsoft/TRELLIS/blob/main/trellis/utils/postprocessing_utils.py#L22
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
def __init__(
|
| 96 |
+
self,
|
| 97 |
+
vertices: Union[torch.Tensor, np.ndarray],
|
| 98 |
+
faces: Union[torch.Tensor, np.ndarray],
|
| 99 |
+
device: str = "cuda",
|
| 100 |
+
) -> None:
|
| 101 |
+
self.device = device
|
| 102 |
+
if isinstance(vertices, np.ndarray):
|
| 103 |
+
vertices = torch.tensor(vertices)
|
| 104 |
+
self.vertices = vertices
|
| 105 |
+
|
| 106 |
+
if isinstance(faces, np.ndarray):
|
| 107 |
+
faces = torch.tensor(faces)
|
| 108 |
+
self.faces = faces
|
| 109 |
+
|
| 110 |
+
@staticmethod
|
| 111 |
+
def log_mesh_changes(method):
|
| 112 |
+
def wrapper(self, *args, **kwargs):
|
| 113 |
+
logger.info(
|
| 114 |
+
f"Before {method.__name__}: {self.vertices.shape[0]} vertices, {self.faces.shape[0]} faces" # noqa
|
| 115 |
+
)
|
| 116 |
+
result = method(self, *args, **kwargs)
|
| 117 |
+
logger.info(
|
| 118 |
+
f"After {method.__name__}: {self.vertices.shape[0]} vertices, {self.faces.shape[0]} faces" # noqa
|
| 119 |
+
)
|
| 120 |
+
return result
|
| 121 |
+
|
| 122 |
+
return wrapper
|
| 123 |
+
|
| 124 |
+
@log_mesh_changes
|
| 125 |
+
def fill_holes(
|
| 126 |
+
self,
|
| 127 |
+
max_hole_size: float,
|
| 128 |
+
max_hole_nbe: int,
|
| 129 |
+
resolution: int,
|
| 130 |
+
num_views: int,
|
| 131 |
+
norm_mesh_ratio: float = 1.0,
|
| 132 |
+
) -> None:
|
| 133 |
+
self.vertices = self.vertices * norm_mesh_ratio
|
| 134 |
+
vertices, self.faces = self._fill_holes(
|
| 135 |
+
self.vertices,
|
| 136 |
+
self.faces,
|
| 137 |
+
max_hole_size,
|
| 138 |
+
max_hole_nbe,
|
| 139 |
+
resolution,
|
| 140 |
+
num_views,
|
| 141 |
+
)
|
| 142 |
+
self.vertices = vertices / norm_mesh_ratio
|
| 143 |
+
|
| 144 |
+
@staticmethod
|
| 145 |
+
@torch.no_grad()
|
| 146 |
+
def _fill_holes(
|
| 147 |
+
vertices: torch.Tensor,
|
| 148 |
+
faces: torch.Tensor,
|
| 149 |
+
max_hole_size: float,
|
| 150 |
+
max_hole_nbe: int,
|
| 151 |
+
resolution: int,
|
| 152 |
+
num_views: int,
|
| 153 |
+
) -> Union[torch.Tensor, torch.Tensor]:
|
| 154 |
+
yaws, pitchs = [], []
|
| 155 |
+
for i in range(num_views):
|
| 156 |
+
y, p = _sphere_hammersley_seq(i, num_views)
|
| 157 |
+
yaws.append(y)
|
| 158 |
+
pitchs.append(p)
|
| 159 |
+
|
| 160 |
+
yaws, pitchs = torch.tensor(yaws).to(vertices), torch.tensor(
|
| 161 |
+
pitchs
|
| 162 |
+
).to(vertices)
|
| 163 |
+
radius, fov = 2.0, torch.deg2rad(torch.tensor(40)).to(vertices)
|
| 164 |
+
projection = utils3d.torch.perspective_from_fov_xy(fov, fov, 1, 3)
|
| 165 |
+
|
| 166 |
+
views = []
|
| 167 |
+
for yaw, pitch in zip(yaws, pitchs):
|
| 168 |
+
orig = (
|
| 169 |
+
torch.tensor(
|
| 170 |
+
[
|
| 171 |
+
torch.sin(yaw) * torch.cos(pitch),
|
| 172 |
+
torch.cos(yaw) * torch.cos(pitch),
|
| 173 |
+
torch.sin(pitch),
|
| 174 |
+
]
|
| 175 |
+
).to(vertices)
|
| 176 |
+
* radius
|
| 177 |
+
)
|
| 178 |
+
view = utils3d.torch.view_look_at(
|
| 179 |
+
orig,
|
| 180 |
+
torch.tensor([0, 0, 0]).to(vertices),
|
| 181 |
+
torch.tensor([0, 0, 1]).to(vertices),
|
| 182 |
+
)
|
| 183 |
+
views.append(view)
|
| 184 |
+
views = torch.stack(views, dim=0)
|
| 185 |
+
|
| 186 |
+
# Rasterize the mesh
|
| 187 |
+
visibility = torch.zeros(
|
| 188 |
+
faces.shape[0], dtype=torch.int32, device=faces.device
|
| 189 |
+
)
|
| 190 |
+
rastctx = utils3d.torch.RastContext(backend="cuda")
|
| 191 |
+
|
| 192 |
+
for i in tqdm(
|
| 193 |
+
range(views.shape[0]), total=views.shape[0], desc="Rasterizing"
|
| 194 |
+
):
|
| 195 |
+
view = views[i]
|
| 196 |
+
buffers = utils3d.torch.rasterize_triangle_faces(
|
| 197 |
+
rastctx,
|
| 198 |
+
vertices[None],
|
| 199 |
+
faces,
|
| 200 |
+
resolution,
|
| 201 |
+
resolution,
|
| 202 |
+
view=view,
|
| 203 |
+
projection=projection,
|
| 204 |
+
)
|
| 205 |
+
face_id = buffers["face_id"][0][buffers["mask"][0] > 0.95] - 1
|
| 206 |
+
face_id = torch.unique(face_id).long()
|
| 207 |
+
visibility[face_id] += 1
|
| 208 |
+
|
| 209 |
+
# Normalize visibility by the number of views
|
| 210 |
+
visibility = visibility.float() / num_views
|
| 211 |
+
|
| 212 |
+
# Mincut: Identify outer and inner faces
|
| 213 |
+
edges, face2edge, edge_degrees = utils3d.torch.compute_edges(faces)
|
| 214 |
+
boundary_edge_indices = torch.nonzero(edge_degrees == 1).reshape(-1)
|
| 215 |
+
connected_components = utils3d.torch.compute_connected_components(
|
| 216 |
+
faces, edges, face2edge
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
outer_face_indices = torch.zeros(
|
| 220 |
+
faces.shape[0], dtype=torch.bool, device=faces.device
|
| 221 |
+
)
|
| 222 |
+
for i in range(len(connected_components)):
|
| 223 |
+
outer_face_indices[connected_components[i]] = visibility[
|
| 224 |
+
connected_components[i]
|
| 225 |
+
] > min(
|
| 226 |
+
max(
|
| 227 |
+
visibility[connected_components[i]].quantile(0.75).item(),
|
| 228 |
+
0.25,
|
| 229 |
+
),
|
| 230 |
+
0.5,
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
outer_face_indices = outer_face_indices.nonzero().reshape(-1)
|
| 234 |
+
inner_face_indices = torch.nonzero(visibility == 0).reshape(-1)
|
| 235 |
+
|
| 236 |
+
if inner_face_indices.shape[0] == 0:
|
| 237 |
+
return vertices, faces
|
| 238 |
+
|
| 239 |
+
# Construct dual graph (faces as nodes, edges as edges)
|
| 240 |
+
dual_edges, dual_edge2edge = utils3d.torch.compute_dual_graph(
|
| 241 |
+
face2edge
|
| 242 |
+
)
|
| 243 |
+
dual_edge2edge = edges[dual_edge2edge]
|
| 244 |
+
dual_edges_weights = torch.norm(
|
| 245 |
+
vertices[dual_edge2edge[:, 0]] - vertices[dual_edge2edge[:, 1]],
|
| 246 |
+
dim=1,
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Mincut: Construct main graph and solve the mincut problem
|
| 250 |
+
g = igraph.Graph()
|
| 251 |
+
g.add_vertices(faces.shape[0])
|
| 252 |
+
g.add_edges(dual_edges.cpu().numpy())
|
| 253 |
+
g.es["weight"] = dual_edges_weights.cpu().numpy()
|
| 254 |
+
|
| 255 |
+
g.add_vertex("s") # source
|
| 256 |
+
g.add_vertex("t") # target
|
| 257 |
+
|
| 258 |
+
g.add_edges(
|
| 259 |
+
[(f, "s") for f in inner_face_indices],
|
| 260 |
+
attributes={
|
| 261 |
+
"weight": torch.ones(
|
| 262 |
+
inner_face_indices.shape[0], dtype=torch.float32
|
| 263 |
+
)
|
| 264 |
+
.cpu()
|
| 265 |
+
.numpy()
|
| 266 |
+
},
|
| 267 |
+
)
|
| 268 |
+
g.add_edges(
|
| 269 |
+
[(f, "t") for f in outer_face_indices],
|
| 270 |
+
attributes={
|
| 271 |
+
"weight": torch.ones(
|
| 272 |
+
outer_face_indices.shape[0], dtype=torch.float32
|
| 273 |
+
)
|
| 274 |
+
.cpu()
|
| 275 |
+
.numpy()
|
| 276 |
+
},
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
cut = g.mincut("s", "t", (np.array(g.es["weight"]) * 1000).tolist())
|
| 280 |
+
remove_face_indices = torch.tensor(
|
| 281 |
+
[v for v in cut.partition[0] if v < faces.shape[0]],
|
| 282 |
+
dtype=torch.long,
|
| 283 |
+
device=faces.device,
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
# Check if the cut is valid with each connected component
|
| 287 |
+
to_remove_cc = utils3d.torch.compute_connected_components(
|
| 288 |
+
faces[remove_face_indices]
|
| 289 |
+
)
|
| 290 |
+
valid_remove_cc = []
|
| 291 |
+
cutting_edges = []
|
| 292 |
+
for cc in to_remove_cc:
|
| 293 |
+
# Check visibility median for connected component
|
| 294 |
+
visibility_median = visibility[remove_face_indices[cc]].median()
|
| 295 |
+
if visibility_median > 0.25:
|
| 296 |
+
continue
|
| 297 |
+
|
| 298 |
+
# Check if the cutting loop is small enough
|
| 299 |
+
cc_edge_indices, cc_edges_degree = torch.unique(
|
| 300 |
+
face2edge[remove_face_indices[cc]], return_counts=True
|
| 301 |
+
)
|
| 302 |
+
cc_boundary_edge_indices = cc_edge_indices[cc_edges_degree == 1]
|
| 303 |
+
cc_new_boundary_edge_indices = cc_boundary_edge_indices[
|
| 304 |
+
~torch.isin(cc_boundary_edge_indices, boundary_edge_indices)
|
| 305 |
+
]
|
| 306 |
+
if len(cc_new_boundary_edge_indices) > 0:
|
| 307 |
+
cc_new_boundary_edge_cc = (
|
| 308 |
+
utils3d.torch.compute_edge_connected_components(
|
| 309 |
+
edges[cc_new_boundary_edge_indices]
|
| 310 |
+
)
|
| 311 |
+
)
|
| 312 |
+
cc_new_boundary_edges_cc_center = [
|
| 313 |
+
vertices[edges[cc_new_boundary_edge_indices[edge_cc]]]
|
| 314 |
+
.mean(dim=1)
|
| 315 |
+
.mean(dim=0)
|
| 316 |
+
for edge_cc in cc_new_boundary_edge_cc
|
| 317 |
+
]
|
| 318 |
+
cc_new_boundary_edges_cc_area = []
|
| 319 |
+
for i, edge_cc in enumerate(cc_new_boundary_edge_cc):
|
| 320 |
+
_e1 = (
|
| 321 |
+
vertices[
|
| 322 |
+
edges[cc_new_boundary_edge_indices[edge_cc]][:, 0]
|
| 323 |
+
]
|
| 324 |
+
- cc_new_boundary_edges_cc_center[i]
|
| 325 |
+
)
|
| 326 |
+
_e2 = (
|
| 327 |
+
vertices[
|
| 328 |
+
edges[cc_new_boundary_edge_indices[edge_cc]][:, 1]
|
| 329 |
+
]
|
| 330 |
+
- cc_new_boundary_edges_cc_center[i]
|
| 331 |
+
)
|
| 332 |
+
cc_new_boundary_edges_cc_area.append(
|
| 333 |
+
torch.norm(torch.cross(_e1, _e2, dim=-1), dim=1).sum()
|
| 334 |
+
* 0.5
|
| 335 |
+
)
|
| 336 |
+
cutting_edges.append(cc_new_boundary_edge_indices)
|
| 337 |
+
if any(
|
| 338 |
+
[
|
| 339 |
+
_l > max_hole_size
|
| 340 |
+
for _l in cc_new_boundary_edges_cc_area
|
| 341 |
+
]
|
| 342 |
+
):
|
| 343 |
+
continue
|
| 344 |
+
|
| 345 |
+
valid_remove_cc.append(cc)
|
| 346 |
+
|
| 347 |
+
if len(valid_remove_cc) > 0:
|
| 348 |
+
remove_face_indices = remove_face_indices[
|
| 349 |
+
torch.cat(valid_remove_cc)
|
| 350 |
+
]
|
| 351 |
+
mask = torch.ones(
|
| 352 |
+
faces.shape[0], dtype=torch.bool, device=faces.device
|
| 353 |
+
)
|
| 354 |
+
mask[remove_face_indices] = 0
|
| 355 |
+
faces = faces[mask]
|
| 356 |
+
faces, vertices = utils3d.torch.remove_unreferenced_vertices(
|
| 357 |
+
faces, vertices
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
tqdm.write(f"Removed {(~mask).sum()} faces by mincut")
|
| 361 |
+
else:
|
| 362 |
+
tqdm.write(f"Removed 0 faces by mincut")
|
| 363 |
+
|
| 364 |
+
# Fill small boundaries (holes)
|
| 365 |
+
mesh = _meshfix.PyTMesh()
|
| 366 |
+
mesh.load_array(vertices.cpu().numpy(), faces.cpu().numpy())
|
| 367 |
+
mesh.fill_small_boundaries(nbe=max_hole_nbe, refine=True)
|
| 368 |
+
|
| 369 |
+
_vertices, _faces = mesh.return_arrays()
|
| 370 |
+
vertices = torch.tensor(_vertices).to(vertices)
|
| 371 |
+
faces = torch.tensor(_faces).to(faces)
|
| 372 |
+
|
| 373 |
+
return vertices, faces
|
| 374 |
+
|
| 375 |
+
@property
|
| 376 |
+
def vertices_np(self) -> np.ndarray:
|
| 377 |
+
return self.vertices.cpu().numpy()
|
| 378 |
+
|
| 379 |
+
@property
|
| 380 |
+
def faces_np(self) -> np.ndarray:
|
| 381 |
+
return self.faces.cpu().numpy()
|
| 382 |
+
|
| 383 |
+
@log_mesh_changes
|
| 384 |
+
def simplify(self, ratio: float) -> None:
|
| 385 |
+
"""Simplify the mesh using quadric edge collapse decimation.
|
| 386 |
+
|
| 387 |
+
Args:
|
| 388 |
+
ratio (float): Ratio of faces to filter out.
|
| 389 |
+
"""
|
| 390 |
+
if ratio <= 0 or ratio >= 1:
|
| 391 |
+
raise ValueError("Simplify ratio must be between 0 and 1.")
|
| 392 |
+
|
| 393 |
+
# Convert to PyVista format for simplification
|
| 394 |
+
mesh = pv.PolyData(
|
| 395 |
+
self.vertices_np,
|
| 396 |
+
np.hstack([np.full((self.faces.shape[0], 1), 3), self.faces_np]),
|
| 397 |
+
)
|
| 398 |
+
mesh = mesh.decimate(ratio, progress_bar=True)
|
| 399 |
+
|
| 400 |
+
# Update vertices and faces
|
| 401 |
+
self.vertices = torch.tensor(
|
| 402 |
+
mesh.points, device=self.device, dtype=torch.float32
|
| 403 |
+
)
|
| 404 |
+
self.faces = torch.tensor(
|
| 405 |
+
mesh.faces.reshape(-1, 4)[:, 1:],
|
| 406 |
+
device=self.device,
|
| 407 |
+
dtype=torch.int32,
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
@spaces.GPU
|
| 411 |
+
def __call__(
|
| 412 |
+
self,
|
| 413 |
+
filter_ratio: float,
|
| 414 |
+
max_hole_size: float,
|
| 415 |
+
resolution: int,
|
| 416 |
+
num_views: int,
|
| 417 |
+
norm_mesh_ratio: float = 1.0,
|
| 418 |
+
) -> Tuple[np.ndarray, np.ndarray]:
|
| 419 |
+
"""Post-process the mesh by simplifying and filling holes.
|
| 420 |
+
|
| 421 |
+
This method performs a two-step process:
|
| 422 |
+
1. Simplifies mesh by reducing faces using quadric edge decimation.
|
| 423 |
+
2. Fills holes by removing invisible faces, repairing small boundaries.
|
| 424 |
+
|
| 425 |
+
Args:
|
| 426 |
+
filter_ratio (float): Ratio of faces to simplify out.
|
| 427 |
+
Must be in the range (0, 1).
|
| 428 |
+
max_hole_size (float): Maximum area of a hole to fill. Connected
|
| 429 |
+
components of holes larger than this size will not be repaired.
|
| 430 |
+
resolution (int): Resolution of the rasterization buffer.
|
| 431 |
+
num_views (int): Number of viewpoints to sample for rasterization.
|
| 432 |
+
norm_mesh_ratio (float, optional): A scaling factor applied to the
|
| 433 |
+
vertices of the mesh during processing.
|
| 434 |
+
|
| 435 |
+
Returns:
|
| 436 |
+
Tuple[np.ndarray, np.ndarray]:
|
| 437 |
+
- vertices: Simplified and repaired vertex array of (V, 3).
|
| 438 |
+
- faces: Simplified and repaired face array of (F, 3).
|
| 439 |
+
"""
|
| 440 |
+
self.vertices = self.vertices.to(self.device)
|
| 441 |
+
self.faces = self.faces.to(self.device)
|
| 442 |
+
|
| 443 |
+
self.simplify(ratio=filter_ratio)
|
| 444 |
+
self.fill_holes(
|
| 445 |
+
max_hole_size=max_hole_size,
|
| 446 |
+
max_hole_nbe=int(250 * np.sqrt(1 - filter_ratio)),
|
| 447 |
+
resolution=resolution,
|
| 448 |
+
num_views=num_views,
|
| 449 |
+
norm_mesh_ratio=norm_mesh_ratio,
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
return self.vertices_np, self.faces_np
|
embodied_gen/data/utils.py
ADDED
|
@@ -0,0 +1,996 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import math
|
| 19 |
+
import os
|
| 20 |
+
import random
|
| 21 |
+
import zipfile
|
| 22 |
+
from typing import List, Tuple, Union
|
| 23 |
+
|
| 24 |
+
import cv2
|
| 25 |
+
import kaolin as kal
|
| 26 |
+
import numpy as np
|
| 27 |
+
import nvdiffrast.torch as dr
|
| 28 |
+
import torch
|
| 29 |
+
import torch.nn.functional as F
|
| 30 |
+
from PIL import Image
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
from kolors.models.modeling_chatglm import ChatGLMModel
|
| 34 |
+
from kolors.models.tokenization_chatglm import ChatGLMTokenizer
|
| 35 |
+
except ImportError:
|
| 36 |
+
ChatGLMTokenizer = None
|
| 37 |
+
ChatGLMModel = None
|
| 38 |
+
import logging
|
| 39 |
+
from dataclasses import dataclass, field
|
| 40 |
+
from enum import Enum
|
| 41 |
+
|
| 42 |
+
import trimesh
|
| 43 |
+
from kaolin.render.camera import Camera
|
| 44 |
+
from torch import nn
|
| 45 |
+
|
| 46 |
+
logger = logging.getLogger(__name__)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
__all__ = [
|
| 50 |
+
"DiffrastRender",
|
| 51 |
+
"save_images",
|
| 52 |
+
"render_pbr",
|
| 53 |
+
"prelabel_text_feature",
|
| 54 |
+
"calc_vertex_normals",
|
| 55 |
+
"normalize_vertices_array",
|
| 56 |
+
"load_mesh_to_unit_cube",
|
| 57 |
+
"as_list",
|
| 58 |
+
"CameraSetting",
|
| 59 |
+
"RenderItems",
|
| 60 |
+
"import_kaolin_mesh",
|
| 61 |
+
"save_mesh_with_mtl",
|
| 62 |
+
"get_images_from_grid",
|
| 63 |
+
"post_process_texture",
|
| 64 |
+
"quat_mult",
|
| 65 |
+
"quat_to_rotmat",
|
| 66 |
+
"gamma_shs",
|
| 67 |
+
"resize_pil",
|
| 68 |
+
"trellis_preprocess",
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class DiffrastRender(object):
|
| 73 |
+
"""A class to handle differentiable rendering using nvdiffrast.
|
| 74 |
+
|
| 75 |
+
This class provides methods to render position, depth, and normal maps
|
| 76 |
+
with optional anti-aliasing and gradient disabling for rasterization.
|
| 77 |
+
|
| 78 |
+
Attributes:
|
| 79 |
+
p_mtx (torch.Tensor): Projection matrix.
|
| 80 |
+
mv_mtx (torch.Tensor): Model-view matrix.
|
| 81 |
+
mvp_mtx (torch.Tensor): Model-view-projection matrix, calculated as
|
| 82 |
+
p_mtx @ mv_mtx if not provided.
|
| 83 |
+
resolution_hw (Tuple[int, int]): Height and width of the rendering resolution. # noqa
|
| 84 |
+
_ctx (Union[dr.RasterizeCudaContext, dr.RasterizeGLContext]): Rasterization context. # noqa
|
| 85 |
+
mask_thresh (float): Threshold for mask creation.
|
| 86 |
+
grad_db (bool): Whether to disable gradients during rasterization.
|
| 87 |
+
antialias_mask (bool): Whether to apply anti-aliasing to the mask.
|
| 88 |
+
device (str): Device used for rendering ('cuda' or 'cpu').
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
def __init__(
|
| 92 |
+
self,
|
| 93 |
+
p_matrix: torch.Tensor,
|
| 94 |
+
mv_matrix: torch.Tensor,
|
| 95 |
+
resolution_hw: Tuple[int, int],
|
| 96 |
+
context: Union[dr.RasterizeCudaContext, dr.RasterizeGLContext] = None,
|
| 97 |
+
mvp_matrix: torch.Tensor = None,
|
| 98 |
+
mask_thresh: float = 0.5,
|
| 99 |
+
grad_db: bool = False,
|
| 100 |
+
antialias_mask: bool = True,
|
| 101 |
+
align_coordinate: bool = True,
|
| 102 |
+
device: str = "cuda",
|
| 103 |
+
) -> None:
|
| 104 |
+
self.p_mtx = p_matrix
|
| 105 |
+
self.mv_mtx = mv_matrix
|
| 106 |
+
if mvp_matrix is None:
|
| 107 |
+
self.mvp_mtx = torch.bmm(p_matrix, mv_matrix)
|
| 108 |
+
|
| 109 |
+
self.resolution_hw = resolution_hw
|
| 110 |
+
if context is None:
|
| 111 |
+
context = dr.RasterizeCudaContext(device=device)
|
| 112 |
+
self._ctx = context
|
| 113 |
+
self.mask_thresh = mask_thresh
|
| 114 |
+
self.grad_db = grad_db
|
| 115 |
+
self.antialias_mask = antialias_mask
|
| 116 |
+
self.align_coordinate = align_coordinate
|
| 117 |
+
self.device = device
|
| 118 |
+
|
| 119 |
+
def compute_dr_raster(
|
| 120 |
+
self,
|
| 121 |
+
vertices: torch.Tensor,
|
| 122 |
+
faces: torch.Tensor,
|
| 123 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 124 |
+
vertices_clip = self.transform_vertices(vertices, matrix=self.mvp_mtx)
|
| 125 |
+
rast, _ = dr.rasterize(
|
| 126 |
+
self._ctx,
|
| 127 |
+
vertices_clip,
|
| 128 |
+
faces.int(),
|
| 129 |
+
resolution=self.resolution_hw,
|
| 130 |
+
grad_db=self.grad_db,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
return rast, vertices_clip
|
| 134 |
+
|
| 135 |
+
def transform_vertices(
|
| 136 |
+
self,
|
| 137 |
+
vertices: torch.Tensor,
|
| 138 |
+
matrix: torch.Tensor,
|
| 139 |
+
) -> torch.Tensor:
|
| 140 |
+
verts_ones = torch.ones((len(vertices), 1)).to(vertices)
|
| 141 |
+
verts_homo = torch.cat([vertices, verts_ones], dim=-1)
|
| 142 |
+
trans_vertices = torch.matmul(verts_homo, matrix.permute(0, 2, 1))
|
| 143 |
+
|
| 144 |
+
return trans_vertices
|
| 145 |
+
|
| 146 |
+
def normalize_map_by_mask_separately(
|
| 147 |
+
self, map: torch.Tensor, mask: torch.Tensor
|
| 148 |
+
) -> torch.Tensor:
|
| 149 |
+
# Normalize each map separately by mask, normalized map in [0, 1].
|
| 150 |
+
normalized_maps = []
|
| 151 |
+
for map_item, mask_item in zip(map, mask):
|
| 152 |
+
normalized_map = self.normalize_map_by_mask(map_item, mask_item)
|
| 153 |
+
normalized_maps.append(normalized_map)
|
| 154 |
+
|
| 155 |
+
normalized_maps = torch.stack(normalized_maps, dim=0)
|
| 156 |
+
|
| 157 |
+
return normalized_maps
|
| 158 |
+
|
| 159 |
+
def normalize_map_by_mask(
|
| 160 |
+
self, map: torch.Tensor, mask: torch.Tensor
|
| 161 |
+
) -> torch.Tensor:
|
| 162 |
+
# Normalize all maps in total by mask, normalized map in [0, 1].
|
| 163 |
+
foreground = (mask == 1).squeeze(dim=-1)
|
| 164 |
+
foreground_elements = map[foreground]
|
| 165 |
+
if len(foreground_elements) == 0:
|
| 166 |
+
return map
|
| 167 |
+
|
| 168 |
+
min_val, _ = foreground_elements.min(dim=0)
|
| 169 |
+
max_val, _ = foreground_elements.max(dim=0)
|
| 170 |
+
val_range = (max_val - min_val).clip(min=1e-6)
|
| 171 |
+
|
| 172 |
+
normalized_map = (map - min_val) / val_range
|
| 173 |
+
normalized_map = torch.lerp(
|
| 174 |
+
torch.zeros_like(normalized_map), normalized_map, mask
|
| 175 |
+
)
|
| 176 |
+
normalized_map[normalized_map < 0] = 0
|
| 177 |
+
|
| 178 |
+
return normalized_map
|
| 179 |
+
|
| 180 |
+
def _compute_mask(
|
| 181 |
+
self,
|
| 182 |
+
rast: torch.Tensor,
|
| 183 |
+
vertices_clip: torch.Tensor,
|
| 184 |
+
faces: torch.Tensor,
|
| 185 |
+
) -> torch.Tensor:
|
| 186 |
+
mask = (rast[..., 3:] > 0).float()
|
| 187 |
+
mask = mask.clip(min=0, max=1)
|
| 188 |
+
|
| 189 |
+
if self.antialias_mask is True:
|
| 190 |
+
mask = dr.antialias(mask, rast, vertices_clip, faces)
|
| 191 |
+
else:
|
| 192 |
+
foreground = mask > self.mask_thresh
|
| 193 |
+
mask[foreground] = 1
|
| 194 |
+
mask[~foreground] = 0
|
| 195 |
+
|
| 196 |
+
return mask
|
| 197 |
+
|
| 198 |
+
def render_rast_alpha(
|
| 199 |
+
self,
|
| 200 |
+
vertices: torch.Tensor,
|
| 201 |
+
faces: torch.Tensor,
|
| 202 |
+
):
|
| 203 |
+
faces = faces.to(torch.int32)
|
| 204 |
+
rast, vertices_clip = self.compute_dr_raster(vertices, faces)
|
| 205 |
+
mask = self._compute_mask(rast, vertices_clip, faces)
|
| 206 |
+
|
| 207 |
+
return mask, rast
|
| 208 |
+
|
| 209 |
+
def render_position(
|
| 210 |
+
self,
|
| 211 |
+
vertices: torch.Tensor,
|
| 212 |
+
faces: torch.Tensor,
|
| 213 |
+
) -> Union[torch.Tensor, torch.Tensor]:
|
| 214 |
+
# Vertices in model coordinate system, real position coordinate number.
|
| 215 |
+
faces = faces.to(torch.int32)
|
| 216 |
+
mask, rast = self.render_rast_alpha(vertices, faces)
|
| 217 |
+
|
| 218 |
+
vertices_model = vertices[None, ...].contiguous().float()
|
| 219 |
+
position_map, _ = dr.interpolate(vertices_model, rast, faces)
|
| 220 |
+
# Align with blender.
|
| 221 |
+
if self.align_coordinate:
|
| 222 |
+
position_map = position_map[..., [0, 2, 1]]
|
| 223 |
+
position_map[..., 1] = -position_map[..., 1]
|
| 224 |
+
|
| 225 |
+
position_map = torch.lerp(
|
| 226 |
+
torch.zeros_like(position_map), position_map, mask
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
return position_map, mask
|
| 230 |
+
|
| 231 |
+
def render_uv(
|
| 232 |
+
self,
|
| 233 |
+
vertices: torch.Tensor,
|
| 234 |
+
faces: torch.Tensor,
|
| 235 |
+
vtx_uv: torch.Tensor,
|
| 236 |
+
) -> Union[torch.Tensor, torch.Tensor]:
|
| 237 |
+
faces = faces.to(torch.int32)
|
| 238 |
+
mask, rast = self.render_rast_alpha(vertices, faces)
|
| 239 |
+
uv_map, _ = dr.interpolate(vtx_uv, rast, faces)
|
| 240 |
+
uv_map = torch.lerp(torch.zeros_like(uv_map), uv_map, mask)
|
| 241 |
+
|
| 242 |
+
return uv_map, mask
|
| 243 |
+
|
| 244 |
+
def render_depth(
|
| 245 |
+
self,
|
| 246 |
+
vertices: torch.Tensor,
|
| 247 |
+
faces: torch.Tensor,
|
| 248 |
+
) -> Union[torch.Tensor, torch.Tensor]:
|
| 249 |
+
# Vertices in model coordinate system, real depth coordinate number.
|
| 250 |
+
faces = faces.to(torch.int32)
|
| 251 |
+
mask, rast = self.render_rast_alpha(vertices, faces)
|
| 252 |
+
|
| 253 |
+
vertices_camera = self.transform_vertices(vertices, matrix=self.mv_mtx)
|
| 254 |
+
vertices_camera = vertices_camera[..., 2:3].contiguous().float()
|
| 255 |
+
depth_map, _ = dr.interpolate(vertices_camera, rast, faces)
|
| 256 |
+
# Change camera depth minus to positive.
|
| 257 |
+
if self.align_coordinate:
|
| 258 |
+
depth_map = -depth_map
|
| 259 |
+
depth_map = torch.lerp(torch.zeros_like(depth_map), depth_map, mask)
|
| 260 |
+
|
| 261 |
+
return depth_map, mask
|
| 262 |
+
|
| 263 |
+
def render_global_normal(
|
| 264 |
+
self,
|
| 265 |
+
vertices: torch.Tensor,
|
| 266 |
+
faces: torch.Tensor,
|
| 267 |
+
vertice_normals: torch.Tensor,
|
| 268 |
+
) -> Union[torch.Tensor, torch.Tensor]:
|
| 269 |
+
# NOTE: vertice_normals in [-1, 1], return normal in [0, 1].
|
| 270 |
+
# vertices / vertice_normals in model coordinate system.
|
| 271 |
+
faces = faces.to(torch.int32)
|
| 272 |
+
mask, rast = self.render_rast_alpha(vertices, faces)
|
| 273 |
+
im_base_normals, _ = dr.interpolate(
|
| 274 |
+
vertice_normals[None, ...].float(), rast, faces
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
if im_base_normals is not None:
|
| 278 |
+
faces = faces.to(torch.int64)
|
| 279 |
+
vertices_cam = self.transform_vertices(
|
| 280 |
+
vertices, matrix=self.mv_mtx
|
| 281 |
+
)
|
| 282 |
+
face_vertices_ndc = kal.ops.mesh.index_vertices_by_faces(
|
| 283 |
+
vertices_cam[..., :3], faces
|
| 284 |
+
)
|
| 285 |
+
face_normal_sign = kal.ops.mesh.face_normals(face_vertices_ndc)[
|
| 286 |
+
..., 2
|
| 287 |
+
]
|
| 288 |
+
for idx in range(len(im_base_normals)):
|
| 289 |
+
face_idx = (rast[idx, ..., -1].long() - 1).contiguous()
|
| 290 |
+
im_normal_sign = torch.sign(face_normal_sign[idx, face_idx])
|
| 291 |
+
im_normal_sign[face_idx == -1] = 0
|
| 292 |
+
im_base_normals[idx] *= im_normal_sign.unsqueeze(-1)
|
| 293 |
+
|
| 294 |
+
normal = (im_base_normals + 1) / 2
|
| 295 |
+
normal = normal.clip(min=0, max=1)
|
| 296 |
+
normal = torch.lerp(torch.zeros_like(normal), normal, mask)
|
| 297 |
+
|
| 298 |
+
return normal, mask
|
| 299 |
+
|
| 300 |
+
def transform_normal(
|
| 301 |
+
self,
|
| 302 |
+
normals: torch.Tensor,
|
| 303 |
+
trans_matrix: torch.Tensor,
|
| 304 |
+
masks: torch.Tensor,
|
| 305 |
+
to_view: bool,
|
| 306 |
+
) -> torch.Tensor:
|
| 307 |
+
# NOTE: input normals in [0, 1], output normals in [0, 1].
|
| 308 |
+
normals = normals.clone()
|
| 309 |
+
assert len(normals) == len(trans_matrix)
|
| 310 |
+
|
| 311 |
+
if not to_view:
|
| 312 |
+
# Flip the sign on the x-axis to match inv bae system for global transformation. # noqa
|
| 313 |
+
normals[..., 0] = 1 - normals[..., 0]
|
| 314 |
+
|
| 315 |
+
normals = 2 * normals - 1
|
| 316 |
+
b, h, w, c = normals.shape
|
| 317 |
+
|
| 318 |
+
transformed_normals = []
|
| 319 |
+
for normal, matrix in zip(normals, trans_matrix):
|
| 320 |
+
# Transform normals using the transformation matrix (4x4).
|
| 321 |
+
reshaped_normals = normal.view(-1, c) # (h w 3) -> (hw 3)
|
| 322 |
+
padded_vectors = torch.nn.functional.pad(
|
| 323 |
+
reshaped_normals, pad=(0, 1), mode="constant", value=0.0
|
| 324 |
+
)
|
| 325 |
+
transformed_normal = torch.matmul(
|
| 326 |
+
padded_vectors, matrix.transpose(0, 1)
|
| 327 |
+
)[..., :3]
|
| 328 |
+
|
| 329 |
+
# Normalize and clip the normals to [0, 1] range.
|
| 330 |
+
transformed_normal = F.normalize(transformed_normal, p=2, dim=-1)
|
| 331 |
+
transformed_normal = (transformed_normal + 1) / 2
|
| 332 |
+
|
| 333 |
+
if to_view:
|
| 334 |
+
# Flip the sign on the x-axis to match bae system for view transformation. # noqa
|
| 335 |
+
transformed_normal[..., 0] = 1 - transformed_normal[..., 0]
|
| 336 |
+
|
| 337 |
+
transformed_normals.append(transformed_normal.view(h, w, c))
|
| 338 |
+
|
| 339 |
+
transformed_normals = torch.stack(transformed_normals, dim=0)
|
| 340 |
+
|
| 341 |
+
if masks is not None:
|
| 342 |
+
transformed_normals = torch.lerp(
|
| 343 |
+
torch.zeros_like(transformed_normals),
|
| 344 |
+
transformed_normals,
|
| 345 |
+
masks,
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
return transformed_normals
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def _az_el_to_points(
|
| 352 |
+
azimuths: np.ndarray, elevations: np.ndarray
|
| 353 |
+
) -> np.ndarray:
|
| 354 |
+
x = np.cos(azimuths) * np.cos(elevations)
|
| 355 |
+
y = np.sin(azimuths) * np.cos(elevations)
|
| 356 |
+
z = np.sin(elevations)
|
| 357 |
+
|
| 358 |
+
return np.stack([x, y, z], axis=-1)
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def _compute_az_el_by_views(
|
| 362 |
+
num_view: int, el: float
|
| 363 |
+
) -> Tuple[np.ndarray, np.ndarray]:
|
| 364 |
+
azimuths = np.arange(num_view) / num_view * np.pi * 2
|
| 365 |
+
elevations = np.deg2rad(np.array([el] * num_view))
|
| 366 |
+
|
| 367 |
+
return azimuths, elevations
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
def _compute_cam_pts_by_az_el(
|
| 371 |
+
azs: np.ndarray,
|
| 372 |
+
els: np.ndarray,
|
| 373 |
+
distance: float,
|
| 374 |
+
extra_pts: np.ndarray = None,
|
| 375 |
+
) -> np.ndarray:
|
| 376 |
+
distances = np.array([distance for _ in range(len(azs))])
|
| 377 |
+
cam_pts = _az_el_to_points(azs, els) * distances[:, None]
|
| 378 |
+
|
| 379 |
+
if extra_pts is not None:
|
| 380 |
+
cam_pts = np.concatenate([cam_pts, extra_pts], axis=0)
|
| 381 |
+
|
| 382 |
+
# Align coordinate system.
|
| 383 |
+
cam_pts = cam_pts[:, [0, 2, 1]] # xyz -> xzy
|
| 384 |
+
cam_pts[..., 2] = -cam_pts[..., 2]
|
| 385 |
+
|
| 386 |
+
return cam_pts
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def compute_cam_pts_by_views(
|
| 390 |
+
num_view: int, el: float, distance: float, extra_pts: np.ndarray = None
|
| 391 |
+
) -> torch.Tensor:
|
| 392 |
+
"""Computes object-center camera points for a given number of views.
|
| 393 |
+
|
| 394 |
+
Args:
|
| 395 |
+
num_view (int): The number of views (camera positions) to compute.
|
| 396 |
+
el (float): The elevation angle in degrees.
|
| 397 |
+
distance (float): The distance from the origin to the camera.
|
| 398 |
+
extra_pts (np.ndarray): Extra camera points postion.
|
| 399 |
+
|
| 400 |
+
Returns:
|
| 401 |
+
torch.Tensor: A tensor containing the camera points for each view, with shape `(num_view, 3)`. # noqa
|
| 402 |
+
"""
|
| 403 |
+
azimuths, elevations = _compute_az_el_by_views(num_view, el)
|
| 404 |
+
cam_pts = _compute_cam_pts_by_az_el(
|
| 405 |
+
azimuths, elevations, distance, extra_pts
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
return cam_pts
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
def save_images(
|
| 412 |
+
images: Union[list[np.ndarray], list[torch.Tensor]],
|
| 413 |
+
output_dir: str,
|
| 414 |
+
cvt_color: str = None,
|
| 415 |
+
format: str = ".png",
|
| 416 |
+
to_uint8: bool = True,
|
| 417 |
+
verbose: bool = False,
|
| 418 |
+
) -> List[str]:
|
| 419 |
+
# NOTE: images in [0, 1]
|
| 420 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 421 |
+
save_paths = []
|
| 422 |
+
for idx, image in enumerate(images):
|
| 423 |
+
if isinstance(image, torch.Tensor):
|
| 424 |
+
image = image.detach().cpu().numpy()
|
| 425 |
+
if to_uint8:
|
| 426 |
+
image = image.clip(min=0, max=1)
|
| 427 |
+
image = (255.0 * image).astype(np.uint8)
|
| 428 |
+
if cvt_color is not None:
|
| 429 |
+
image = cv2.cvtColor(image, cvt_color)
|
| 430 |
+
save_path = os.path.join(output_dir, f"{idx:04d}{format}")
|
| 431 |
+
save_paths.append(save_path)
|
| 432 |
+
|
| 433 |
+
cv2.imwrite(save_path, image)
|
| 434 |
+
|
| 435 |
+
if verbose:
|
| 436 |
+
logger.info(f"Images saved in {output_dir}")
|
| 437 |
+
|
| 438 |
+
return save_paths
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def _current_lighting(
|
| 442 |
+
azimuths: List[float],
|
| 443 |
+
elevations: List[float],
|
| 444 |
+
light_factor: float = 1.0,
|
| 445 |
+
device: str = "cuda",
|
| 446 |
+
):
|
| 447 |
+
# azimuths, elevations in degress.
|
| 448 |
+
directions = []
|
| 449 |
+
for az, el in zip(azimuths, elevations):
|
| 450 |
+
az, el = math.radians(az), math.radians(el)
|
| 451 |
+
direction = kal.render.lighting.sg_direction_from_azimuth_elevation(
|
| 452 |
+
az, el
|
| 453 |
+
)
|
| 454 |
+
directions.append(direction)
|
| 455 |
+
directions = torch.cat(directions, dim=0)
|
| 456 |
+
|
| 457 |
+
amplitude = torch.ones_like(directions) * light_factor
|
| 458 |
+
light_condition = kal.render.lighting.SgLightingParameters(
|
| 459 |
+
amplitude=amplitude,
|
| 460 |
+
direction=directions,
|
| 461 |
+
sharpness=3,
|
| 462 |
+
).to(device)
|
| 463 |
+
|
| 464 |
+
# light_condition = kal.render.lighting.SgLightingParameters.from_sun(
|
| 465 |
+
# directions, strength=1, angle=90, color=None
|
| 466 |
+
# ).to(device)
|
| 467 |
+
|
| 468 |
+
return light_condition
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
def render_pbr(
|
| 472 |
+
mesh,
|
| 473 |
+
camera,
|
| 474 |
+
device="cuda",
|
| 475 |
+
cxt=None,
|
| 476 |
+
custom_materials=None,
|
| 477 |
+
light_factor=1.0,
|
| 478 |
+
):
|
| 479 |
+
if cxt is None:
|
| 480 |
+
cxt = dr.RasterizeCudaContext()
|
| 481 |
+
|
| 482 |
+
light_condition = _current_lighting(
|
| 483 |
+
azimuths=[0, 90, 180, 270],
|
| 484 |
+
elevations=[90, 60, 30, 20],
|
| 485 |
+
light_factor=light_factor,
|
| 486 |
+
device=device,
|
| 487 |
+
)
|
| 488 |
+
render_res = kal.render.easy_render.render_mesh(
|
| 489 |
+
camera,
|
| 490 |
+
mesh,
|
| 491 |
+
lighting=light_condition,
|
| 492 |
+
nvdiffrast_context=cxt,
|
| 493 |
+
custom_materials=custom_materials,
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
+
image = render_res[kal.render.easy_render.RenderPass.render]
|
| 497 |
+
image = image.clip(0, 1)
|
| 498 |
+
|
| 499 |
+
albedo = render_res[kal.render.easy_render.RenderPass.albedo]
|
| 500 |
+
albedo = albedo.clip(0, 1)
|
| 501 |
+
|
| 502 |
+
diffuse = render_res[kal.render.easy_render.RenderPass.diffuse]
|
| 503 |
+
diffuse = diffuse.clip(0, 1)
|
| 504 |
+
|
| 505 |
+
normal = render_res[kal.render.easy_render.RenderPass.normals]
|
| 506 |
+
normal = normal.clip(-1, 1)
|
| 507 |
+
|
| 508 |
+
return image, albedo, diffuse, normal
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def _move_to_target_device(data, device: str):
|
| 512 |
+
if isinstance(data, dict):
|
| 513 |
+
for key, value in data.items():
|
| 514 |
+
data[key] = _move_to_target_device(value, device)
|
| 515 |
+
elif isinstance(data, torch.Tensor):
|
| 516 |
+
return data.to(device)
|
| 517 |
+
|
| 518 |
+
return data
|
| 519 |
+
|
| 520 |
+
|
| 521 |
+
def _encode_prompt(
|
| 522 |
+
prompt_batch,
|
| 523 |
+
text_encoders,
|
| 524 |
+
tokenizers,
|
| 525 |
+
proportion_empty_prompts=0,
|
| 526 |
+
is_train=True,
|
| 527 |
+
):
|
| 528 |
+
prompt_embeds_list = []
|
| 529 |
+
|
| 530 |
+
captions = []
|
| 531 |
+
for caption in prompt_batch:
|
| 532 |
+
if random.random() < proportion_empty_prompts:
|
| 533 |
+
captions.append("")
|
| 534 |
+
elif isinstance(caption, str):
|
| 535 |
+
captions.append(caption)
|
| 536 |
+
elif isinstance(caption, (list, np.ndarray)):
|
| 537 |
+
captions.append(random.choice(caption) if is_train else caption[0])
|
| 538 |
+
|
| 539 |
+
with torch.no_grad():
|
| 540 |
+
for tokenizer, text_encoder in zip(tokenizers, text_encoders):
|
| 541 |
+
text_inputs = tokenizer(
|
| 542 |
+
captions,
|
| 543 |
+
padding="max_length",
|
| 544 |
+
max_length=256,
|
| 545 |
+
truncation=True,
|
| 546 |
+
return_tensors="pt",
|
| 547 |
+
).to(text_encoder.device)
|
| 548 |
+
|
| 549 |
+
output = text_encoder(
|
| 550 |
+
input_ids=text_inputs.input_ids,
|
| 551 |
+
attention_mask=text_inputs.attention_mask,
|
| 552 |
+
position_ids=text_inputs.position_ids,
|
| 553 |
+
output_hidden_states=True,
|
| 554 |
+
)
|
| 555 |
+
|
| 556 |
+
# We are only interested in the pooled output of the text encoder.
|
| 557 |
+
prompt_embeds = output.hidden_states[-2].permute(1, 0, 2).clone()
|
| 558 |
+
pooled_prompt_embeds = output.hidden_states[-1][-1, :, :].clone()
|
| 559 |
+
bs_embed, seq_len, _ = prompt_embeds.shape
|
| 560 |
+
prompt_embeds = prompt_embeds.view(bs_embed, seq_len, -1)
|
| 561 |
+
prompt_embeds_list.append(prompt_embeds)
|
| 562 |
+
|
| 563 |
+
prompt_embeds = torch.concat(prompt_embeds_list, dim=-1)
|
| 564 |
+
pooled_prompt_embeds = pooled_prompt_embeds.view(bs_embed, -1)
|
| 565 |
+
|
| 566 |
+
return prompt_embeds, pooled_prompt_embeds
|
| 567 |
+
|
| 568 |
+
|
| 569 |
+
def load_llm_models(pretrained_model_name_or_path: str, device: str):
|
| 570 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(
|
| 571 |
+
pretrained_model_name_or_path,
|
| 572 |
+
subfolder="text_encoder",
|
| 573 |
+
)
|
| 574 |
+
text_encoder = ChatGLMModel.from_pretrained(
|
| 575 |
+
pretrained_model_name_or_path,
|
| 576 |
+
subfolder="text_encoder",
|
| 577 |
+
).to(device)
|
| 578 |
+
|
| 579 |
+
text_encoders = [
|
| 580 |
+
text_encoder,
|
| 581 |
+
]
|
| 582 |
+
tokenizers = [
|
| 583 |
+
tokenizer,
|
| 584 |
+
]
|
| 585 |
+
|
| 586 |
+
logger.info(f"Load model from {pretrained_model_name_or_path} done.")
|
| 587 |
+
|
| 588 |
+
return tokenizers, text_encoders
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
def prelabel_text_feature(
|
| 592 |
+
prompt_batch: List[str],
|
| 593 |
+
output_dir: str,
|
| 594 |
+
tokenizers: nn.Module,
|
| 595 |
+
text_encoders: nn.Module,
|
| 596 |
+
) -> List[str]:
|
| 597 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 598 |
+
|
| 599 |
+
# prompt_batch ["text..."]
|
| 600 |
+
prompt_embeds, pooled_prompt_embeds = _encode_prompt(
|
| 601 |
+
prompt_batch, text_encoders, tokenizers
|
| 602 |
+
)
|
| 603 |
+
|
| 604 |
+
prompt_embeds = _move_to_target_device(prompt_embeds, device="cpu")
|
| 605 |
+
pooled_prompt_embeds = _move_to_target_device(
|
| 606 |
+
pooled_prompt_embeds, device="cpu"
|
| 607 |
+
)
|
| 608 |
+
|
| 609 |
+
data_dict = dict(
|
| 610 |
+
prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
save_path = os.path.join(output_dir, "text_feat.pth")
|
| 614 |
+
torch.save(data_dict, save_path)
|
| 615 |
+
|
| 616 |
+
return save_path
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def _calc_face_normals(
|
| 620 |
+
vertices: torch.Tensor, # V,3 first vertex may be unreferenced
|
| 621 |
+
faces: torch.Tensor, # F,3 long, first face may be all zero
|
| 622 |
+
normalize: bool = False,
|
| 623 |
+
) -> torch.Tensor: # F,3
|
| 624 |
+
full_vertices = vertices[faces] # F,C=3,3
|
| 625 |
+
v0, v1, v2 = full_vertices.unbind(dim=1) # F,3
|
| 626 |
+
face_normals = torch.cross(v1 - v0, v2 - v0, dim=1) # F,3
|
| 627 |
+
if normalize:
|
| 628 |
+
face_normals = F.normalize(
|
| 629 |
+
face_normals, eps=1e-6, dim=1
|
| 630 |
+
) # TODO inplace?
|
| 631 |
+
return face_normals # F,3
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
def calc_vertex_normals(
|
| 635 |
+
vertices: torch.Tensor, # V,3 first vertex may be unreferenced
|
| 636 |
+
faces: torch.Tensor, # F,3 long, first face may be all zero
|
| 637 |
+
face_normals: torch.Tensor = None, # F,3, not normalized
|
| 638 |
+
) -> torch.Tensor: # F,3
|
| 639 |
+
_F = faces.shape[0]
|
| 640 |
+
|
| 641 |
+
if face_normals is None:
|
| 642 |
+
face_normals = _calc_face_normals(vertices, faces)
|
| 643 |
+
|
| 644 |
+
vertex_normals = torch.zeros(
|
| 645 |
+
(vertices.shape[0], 3, 3), dtype=vertices.dtype, device=vertices.device
|
| 646 |
+
) # V,C=3,3
|
| 647 |
+
vertex_normals.scatter_add_(
|
| 648 |
+
dim=0,
|
| 649 |
+
index=faces[:, :, None].expand(_F, 3, 3),
|
| 650 |
+
src=face_normals[:, None, :].expand(_F, 3, 3),
|
| 651 |
+
)
|
| 652 |
+
vertex_normals = vertex_normals.sum(dim=1) # V,3
|
| 653 |
+
return F.normalize(vertex_normals, eps=1e-6, dim=1)
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
def normalize_vertices_array(
|
| 657 |
+
vertices: Union[torch.Tensor, np.ndarray],
|
| 658 |
+
mesh_scale: float = 1.0,
|
| 659 |
+
exec_norm: bool = True,
|
| 660 |
+
):
|
| 661 |
+
if isinstance(vertices, torch.Tensor):
|
| 662 |
+
bbmin, bbmax = vertices.min(0)[0], vertices.max(0)[0]
|
| 663 |
+
else:
|
| 664 |
+
bbmin, bbmax = vertices.min(0), vertices.max(0) # (3,)
|
| 665 |
+
center = (bbmin + bbmax) * 0.5
|
| 666 |
+
bbsize = bbmax - bbmin
|
| 667 |
+
scale = 2 * mesh_scale / bbsize.max()
|
| 668 |
+
if exec_norm:
|
| 669 |
+
vertices = (vertices - center) * scale
|
| 670 |
+
|
| 671 |
+
return vertices, scale, center
|
| 672 |
+
|
| 673 |
+
|
| 674 |
+
def load_mesh_to_unit_cube(
|
| 675 |
+
mesh_file: str,
|
| 676 |
+
mesh_scale: float = 1.0,
|
| 677 |
+
) -> tuple[trimesh.Trimesh, float, list[float]]:
|
| 678 |
+
if not os.path.exists(mesh_file):
|
| 679 |
+
raise FileNotFoundError(f"mesh_file path {mesh_file} not exists.")
|
| 680 |
+
|
| 681 |
+
mesh = trimesh.load(mesh_file)
|
| 682 |
+
if isinstance(mesh, trimesh.Scene):
|
| 683 |
+
mesh = trimesh.utils.concatenate(mesh)
|
| 684 |
+
|
| 685 |
+
vertices, scale, center = normalize_vertices_array(
|
| 686 |
+
mesh.vertices, mesh_scale
|
| 687 |
+
)
|
| 688 |
+
mesh.vertices = vertices
|
| 689 |
+
|
| 690 |
+
return mesh, scale, center
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
def as_list(obj):
|
| 694 |
+
if isinstance(obj, (list, tuple)):
|
| 695 |
+
return obj
|
| 696 |
+
elif isinstance(obj, set):
|
| 697 |
+
return list(obj)
|
| 698 |
+
else:
|
| 699 |
+
return [obj]
|
| 700 |
+
|
| 701 |
+
|
| 702 |
+
@dataclass
|
| 703 |
+
class CameraSetting:
|
| 704 |
+
"""Camera settings for images rendering."""
|
| 705 |
+
|
| 706 |
+
num_images: int
|
| 707 |
+
elevation: list[float]
|
| 708 |
+
distance: float
|
| 709 |
+
resolution_hw: tuple[int, int]
|
| 710 |
+
fov: float
|
| 711 |
+
at: tuple[float, float, float] = field(
|
| 712 |
+
default_factory=lambda: (0.0, 0.0, 0.0)
|
| 713 |
+
)
|
| 714 |
+
up: tuple[float, float, float] = field(
|
| 715 |
+
default_factory=lambda: (0.0, 1.0, 0.0)
|
| 716 |
+
)
|
| 717 |
+
device: str = "cuda"
|
| 718 |
+
near: float = 1e-2
|
| 719 |
+
far: float = 1e2
|
| 720 |
+
|
| 721 |
+
def __post_init__(
|
| 722 |
+
self,
|
| 723 |
+
):
|
| 724 |
+
h = self.resolution_hw[0]
|
| 725 |
+
f = (h / 2) / math.tan(self.fov / 2)
|
| 726 |
+
cx = self.resolution_hw[1] / 2
|
| 727 |
+
cy = self.resolution_hw[0] / 2
|
| 728 |
+
Ks = [
|
| 729 |
+
[f, 0, cx],
|
| 730 |
+
[0, f, cy],
|
| 731 |
+
[0, 0, 1],
|
| 732 |
+
]
|
| 733 |
+
|
| 734 |
+
self.Ks = Ks
|
| 735 |
+
|
| 736 |
+
|
| 737 |
+
@dataclass
|
| 738 |
+
class RenderItems(str, Enum):
|
| 739 |
+
IMAGE = "image_color"
|
| 740 |
+
ALPHA = "image_mask"
|
| 741 |
+
VIEW_NORMAL = "image_view_normal"
|
| 742 |
+
GLOBAL_NORMAL = "image_global_normal"
|
| 743 |
+
POSITION_MAP = "image_position"
|
| 744 |
+
DEPTH = "image_depth"
|
| 745 |
+
ALBEDO = "image_albedo"
|
| 746 |
+
DIFFUSE = "image_diffuse"
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
def _compute_az_el_by_camera_params(
|
| 750 |
+
camera_params: CameraSetting, flip_az: bool = False
|
| 751 |
+
):
|
| 752 |
+
num_view = camera_params.num_images // len(camera_params.elevation)
|
| 753 |
+
view_interval = 2 * np.pi / num_view / 2
|
| 754 |
+
azimuths = []
|
| 755 |
+
elevations = []
|
| 756 |
+
for idx, el in enumerate(camera_params.elevation):
|
| 757 |
+
azs = np.arange(num_view) / num_view * np.pi * 2 + idx * view_interval
|
| 758 |
+
if flip_az:
|
| 759 |
+
azs *= -1
|
| 760 |
+
els = np.deg2rad(np.array([el] * num_view))
|
| 761 |
+
azimuths.append(azs)
|
| 762 |
+
elevations.append(els)
|
| 763 |
+
|
| 764 |
+
azimuths = np.concatenate(azimuths, axis=0)
|
| 765 |
+
elevations = np.concatenate(elevations, axis=0)
|
| 766 |
+
|
| 767 |
+
return azimuths, elevations
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
def init_kal_camera(camera_params: CameraSetting) -> Camera:
|
| 771 |
+
azimuths, elevations = _compute_az_el_by_camera_params(camera_params)
|
| 772 |
+
cam_pts = _compute_cam_pts_by_az_el(
|
| 773 |
+
azimuths, elevations, camera_params.distance
|
| 774 |
+
)
|
| 775 |
+
|
| 776 |
+
up = torch.cat(
|
| 777 |
+
[
|
| 778 |
+
torch.tensor(camera_params.up).repeat(camera_params.num_images, 1),
|
| 779 |
+
],
|
| 780 |
+
dim=0,
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
camera = Camera.from_args(
|
| 784 |
+
eye=torch.tensor(cam_pts),
|
| 785 |
+
at=torch.tensor(camera_params.at),
|
| 786 |
+
up=up,
|
| 787 |
+
fov=camera_params.fov,
|
| 788 |
+
height=camera_params.resolution_hw[0],
|
| 789 |
+
width=camera_params.resolution_hw[1],
|
| 790 |
+
near=camera_params.near,
|
| 791 |
+
far=camera_params.far,
|
| 792 |
+
device=camera_params.device,
|
| 793 |
+
)
|
| 794 |
+
|
| 795 |
+
return camera
|
| 796 |
+
|
| 797 |
+
|
| 798 |
+
def import_kaolin_mesh(mesh_path: str, with_mtl: bool = False):
|
| 799 |
+
if mesh_path.endswith(".glb"):
|
| 800 |
+
mesh = kal.io.gltf.import_mesh(mesh_path)
|
| 801 |
+
elif mesh_path.endswith(".obj"):
|
| 802 |
+
with_material = True if with_mtl else False
|
| 803 |
+
mesh = kal.io.obj.import_mesh(mesh_path, with_materials=with_material)
|
| 804 |
+
if with_mtl and mesh.materials and len(mesh.materials) > 0:
|
| 805 |
+
material = kal.render.materials.PBRMaterial()
|
| 806 |
+
assert (
|
| 807 |
+
"map_Kd" in mesh.materials[0]
|
| 808 |
+
), "'map_Kd' not found in materials."
|
| 809 |
+
material.diffuse_texture = mesh.materials[0]["map_Kd"] / 255.0
|
| 810 |
+
mesh.materials = [material]
|
| 811 |
+
elif mesh_path.endswith(".ply"):
|
| 812 |
+
mesh = trimesh.load(mesh_path)
|
| 813 |
+
mesh_path = mesh_path.replace(".ply", ".obj")
|
| 814 |
+
mesh.export(mesh_path)
|
| 815 |
+
mesh = kal.io.obj.import_mesh(mesh_path)
|
| 816 |
+
elif mesh_path.endswith(".off"):
|
| 817 |
+
mesh = kal.io.off.import_mesh(mesh_path)
|
| 818 |
+
else:
|
| 819 |
+
raise RuntimeError(
|
| 820 |
+
f"{mesh_path} mesh type not supported, "
|
| 821 |
+
"supported mesh type `.glb`, `.obj`, `.ply`, `.off`."
|
| 822 |
+
)
|
| 823 |
+
|
| 824 |
+
return mesh
|
| 825 |
+
|
| 826 |
+
|
| 827 |
+
def save_mesh_with_mtl(
|
| 828 |
+
vertices: np.ndarray,
|
| 829 |
+
faces: np.ndarray,
|
| 830 |
+
uvs: np.ndarray,
|
| 831 |
+
texture: Union[Image.Image, np.ndarray],
|
| 832 |
+
output_path: str,
|
| 833 |
+
material_base=(250, 250, 250, 255),
|
| 834 |
+
) -> trimesh.Trimesh:
|
| 835 |
+
if isinstance(texture, np.ndarray):
|
| 836 |
+
texture = Image.fromarray(texture)
|
| 837 |
+
|
| 838 |
+
mesh = trimesh.Trimesh(
|
| 839 |
+
vertices,
|
| 840 |
+
faces,
|
| 841 |
+
visual=trimesh.visual.TextureVisuals(uv=uvs, image=texture),
|
| 842 |
+
)
|
| 843 |
+
mesh.visual.material = trimesh.visual.material.SimpleMaterial(
|
| 844 |
+
image=texture,
|
| 845 |
+
diffuse=material_base,
|
| 846 |
+
ambient=material_base,
|
| 847 |
+
specular=material_base,
|
| 848 |
+
)
|
| 849 |
+
|
| 850 |
+
dir_name = os.path.dirname(output_path)
|
| 851 |
+
os.makedirs(dir_name, exist_ok=True)
|
| 852 |
+
|
| 853 |
+
_ = mesh.export(output_path)
|
| 854 |
+
# texture.save(os.path.join(dir_name, f"{file_name}_texture.png"))
|
| 855 |
+
|
| 856 |
+
logger.info(f"Saved mesh with texture to {output_path}")
|
| 857 |
+
|
| 858 |
+
return mesh
|
| 859 |
+
|
| 860 |
+
|
| 861 |
+
def get_images_from_grid(
|
| 862 |
+
image: Union[str, Image.Image], img_size: int
|
| 863 |
+
) -> list[Image.Image]:
|
| 864 |
+
if isinstance(image, str):
|
| 865 |
+
image = Image.open(image)
|
| 866 |
+
|
| 867 |
+
view_images = np.array(image)
|
| 868 |
+
view_images = np.concatenate(
|
| 869 |
+
[view_images[:img_size, ...], view_images[img_size:, ...]], axis=1
|
| 870 |
+
)
|
| 871 |
+
images = np.split(view_images, view_images.shape[1] // img_size, axis=1)
|
| 872 |
+
images = [Image.fromarray(img) for img in images]
|
| 873 |
+
|
| 874 |
+
return images
|
| 875 |
+
|
| 876 |
+
|
| 877 |
+
def post_process_texture(texture: np.ndarray, iter: int = 1) -> np.ndarray:
|
| 878 |
+
for _ in range(iter):
|
| 879 |
+
texture = cv2.fastNlMeansDenoisingColored(texture, None, 2, 2, 7, 15)
|
| 880 |
+
texture = cv2.bilateralFilter(
|
| 881 |
+
texture, d=5, sigmaColor=20, sigmaSpace=20
|
| 882 |
+
)
|
| 883 |
+
|
| 884 |
+
return texture
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
def quat_mult(q1, q2):
|
| 888 |
+
# NOTE:
|
| 889 |
+
# Q1 is the quaternion that rotates the vector from the original position to the final position # noqa
|
| 890 |
+
# Q2 is the quaternion that been rotated
|
| 891 |
+
w1, x1, y1, z1 = q1.T
|
| 892 |
+
w2, x2, y2, z2 = q2.T
|
| 893 |
+
w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
|
| 894 |
+
x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2
|
| 895 |
+
y = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2
|
| 896 |
+
z = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2
|
| 897 |
+
return torch.stack([w, x, y, z]).T
|
| 898 |
+
|
| 899 |
+
|
| 900 |
+
def quat_to_rotmat(quats: torch.Tensor, mode="wxyz") -> torch.Tensor:
|
| 901 |
+
"""Convert quaternion to rotation matrix."""
|
| 902 |
+
quats = F.normalize(quats, p=2, dim=-1)
|
| 903 |
+
|
| 904 |
+
if mode == "xyzw":
|
| 905 |
+
x, y, z, w = torch.unbind(quats, dim=-1)
|
| 906 |
+
elif mode == "wxyz":
|
| 907 |
+
w, x, y, z = torch.unbind(quats, dim=-1)
|
| 908 |
+
else:
|
| 909 |
+
raise ValueError(f"Invalid mode: {mode}.")
|
| 910 |
+
|
| 911 |
+
R = torch.stack(
|
| 912 |
+
[
|
| 913 |
+
1 - 2 * (y**2 + z**2),
|
| 914 |
+
2 * (x * y - w * z),
|
| 915 |
+
2 * (x * z + w * y),
|
| 916 |
+
2 * (x * y + w * z),
|
| 917 |
+
1 - 2 * (x**2 + z**2),
|
| 918 |
+
2 * (y * z - w * x),
|
| 919 |
+
2 * (x * z - w * y),
|
| 920 |
+
2 * (y * z + w * x),
|
| 921 |
+
1 - 2 * (x**2 + y**2),
|
| 922 |
+
],
|
| 923 |
+
dim=-1,
|
| 924 |
+
)
|
| 925 |
+
|
| 926 |
+
return R.reshape(quats.shape[:-1] + (3, 3))
|
| 927 |
+
|
| 928 |
+
|
| 929 |
+
def gamma_shs(shs: torch.Tensor, gamma: float) -> torch.Tensor:
|
| 930 |
+
C0 = 0.28209479177387814 # Constant for normalization in spherical harmonics # noqa
|
| 931 |
+
# Clip to the range [0.0, 1.0], apply gamma correction, and then un-clip back # noqa
|
| 932 |
+
new_shs = torch.clip(shs * C0 + 0.5, 0.0, 1.0)
|
| 933 |
+
new_shs = (torch.pow(new_shs, gamma) - 0.5) / C0
|
| 934 |
+
return new_shs
|
| 935 |
+
|
| 936 |
+
|
| 937 |
+
def resize_pil(image: Image.Image, max_size: int = 1024) -> Image.Image:
|
| 938 |
+
max_size = max(image.size)
|
| 939 |
+
scale = min(1, 1024 / max_size)
|
| 940 |
+
if scale < 1:
|
| 941 |
+
new_size = (int(image.width * scale), int(image.height * scale))
|
| 942 |
+
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
| 943 |
+
|
| 944 |
+
return image
|
| 945 |
+
|
| 946 |
+
|
| 947 |
+
def trellis_preprocess(image: Image.Image) -> Image.Image:
|
| 948 |
+
"""Process the input image as trellis done."""
|
| 949 |
+
image_np = np.array(image)
|
| 950 |
+
alpha = image_np[:, :, 3]
|
| 951 |
+
bbox = np.argwhere(alpha > 0.8 * 255)
|
| 952 |
+
bbox = (
|
| 953 |
+
np.min(bbox[:, 1]),
|
| 954 |
+
np.min(bbox[:, 0]),
|
| 955 |
+
np.max(bbox[:, 1]),
|
| 956 |
+
np.max(bbox[:, 0]),
|
| 957 |
+
)
|
| 958 |
+
center = (bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2
|
| 959 |
+
size = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
|
| 960 |
+
size = int(size * 1.2)
|
| 961 |
+
bbox = (
|
| 962 |
+
center[0] - size // 2,
|
| 963 |
+
center[1] - size // 2,
|
| 964 |
+
center[0] + size // 2,
|
| 965 |
+
center[1] + size // 2,
|
| 966 |
+
)
|
| 967 |
+
image = image.crop(bbox)
|
| 968 |
+
image = image.resize((518, 518), Image.Resampling.LANCZOS)
|
| 969 |
+
image = np.array(image).astype(np.float32) / 255
|
| 970 |
+
image = image[:, :, :3] * image[:, :, 3:4]
|
| 971 |
+
image = Image.fromarray((image * 255).astype(np.uint8))
|
| 972 |
+
|
| 973 |
+
return image
|
| 974 |
+
|
| 975 |
+
|
| 976 |
+
def zip_files(input_paths: list[str], output_zip: str) -> str:
|
| 977 |
+
with zipfile.ZipFile(output_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 978 |
+
for input_path in input_paths:
|
| 979 |
+
if not os.path.exists(input_path):
|
| 980 |
+
raise FileNotFoundError(f"File not found: {input_path}")
|
| 981 |
+
|
| 982 |
+
if os.path.isdir(input_path):
|
| 983 |
+
for root, _, files in os.walk(input_path):
|
| 984 |
+
for file in files:
|
| 985 |
+
file_path = os.path.join(root, file)
|
| 986 |
+
arcname = os.path.relpath(
|
| 987 |
+
file_path, start=os.path.commonpath(input_paths)
|
| 988 |
+
)
|
| 989 |
+
zipf.write(file_path, arcname=arcname)
|
| 990 |
+
else:
|
| 991 |
+
arcname = os.path.relpath(
|
| 992 |
+
input_path, start=os.path.commonpath(input_paths)
|
| 993 |
+
)
|
| 994 |
+
zipf.write(input_path, arcname=arcname)
|
| 995 |
+
|
| 996 |
+
return output_zip
|
embodied_gen/models/delight_model.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
from typing import Union
|
| 20 |
+
|
| 21 |
+
import cv2
|
| 22 |
+
import numpy as np
|
| 23 |
+
import spaces
|
| 24 |
+
import torch
|
| 25 |
+
from diffusers import (
|
| 26 |
+
EulerAncestralDiscreteScheduler,
|
| 27 |
+
StableDiffusionInstructPix2PixPipeline,
|
| 28 |
+
)
|
| 29 |
+
from huggingface_hub import snapshot_download
|
| 30 |
+
from PIL import Image
|
| 31 |
+
from embodied_gen.models.segment_model import RembgRemover
|
| 32 |
+
|
| 33 |
+
__all__ = [
|
| 34 |
+
"DelightingModel",
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class DelightingModel(object):
|
| 39 |
+
"""A model to remove the lighting in image space.
|
| 40 |
+
|
| 41 |
+
This model is encapsulated based on the Hunyuan3D-Delight model
|
| 42 |
+
from https://huggingface.co/tencent/Hunyuan3D-2/tree/main/hunyuan3d-delight-v2-0 # noqa
|
| 43 |
+
|
| 44 |
+
Attributes:
|
| 45 |
+
image_guide_scale (float): Weight of image guidance in diffusion process.
|
| 46 |
+
text_guide_scale (float): Weight of text (prompt) guidance in diffusion process.
|
| 47 |
+
num_infer_step (int): Number of inference steps for diffusion model.
|
| 48 |
+
mask_erosion_size (int): Size of erosion kernel for alpha mask cleanup.
|
| 49 |
+
device (str): Device used for inference, e.g., 'cuda' or 'cpu'.
|
| 50 |
+
seed (int): Random seed for diffusion model reproducibility.
|
| 51 |
+
model_path (str): Filesystem path to pretrained model weights.
|
| 52 |
+
pipeline: Lazy-loaded diffusion pipeline instance.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
def __init__(
|
| 56 |
+
self,
|
| 57 |
+
model_path: str = None,
|
| 58 |
+
num_infer_step: int = 50,
|
| 59 |
+
mask_erosion_size: int = 3,
|
| 60 |
+
image_guide_scale: float = 1.5,
|
| 61 |
+
text_guide_scale: float = 1.0,
|
| 62 |
+
device: str = "cuda",
|
| 63 |
+
seed: int = 0,
|
| 64 |
+
) -> None:
|
| 65 |
+
self.image_guide_scale = image_guide_scale
|
| 66 |
+
self.text_guide_scale = text_guide_scale
|
| 67 |
+
self.num_infer_step = num_infer_step
|
| 68 |
+
self.mask_erosion_size = mask_erosion_size
|
| 69 |
+
self.kernel = np.ones(
|
| 70 |
+
(self.mask_erosion_size, self.mask_erosion_size), np.uint8
|
| 71 |
+
)
|
| 72 |
+
self.seed = seed
|
| 73 |
+
self.device = device
|
| 74 |
+
self.pipeline = None # lazy load model adapt to @spaces.GPU
|
| 75 |
+
|
| 76 |
+
if model_path is None:
|
| 77 |
+
suffix = "hunyuan3d-delight-v2-0"
|
| 78 |
+
model_path = snapshot_download(
|
| 79 |
+
repo_id="tencent/Hunyuan3D-2", allow_patterns=f"{suffix}/*"
|
| 80 |
+
)
|
| 81 |
+
model_path = os.path.join(model_path, suffix)
|
| 82 |
+
|
| 83 |
+
self.model_path = model_path
|
| 84 |
+
|
| 85 |
+
def _lazy_init_pipeline(self):
|
| 86 |
+
if self.pipeline is None:
|
| 87 |
+
pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 88 |
+
self.model_path,
|
| 89 |
+
torch_dtype=torch.float16,
|
| 90 |
+
safety_checker=None,
|
| 91 |
+
)
|
| 92 |
+
pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(
|
| 93 |
+
pipeline.scheduler.config
|
| 94 |
+
)
|
| 95 |
+
pipeline.set_progress_bar_config(disable=True)
|
| 96 |
+
|
| 97 |
+
pipeline.to(self.device, torch.float16)
|
| 98 |
+
self.pipeline = pipeline
|
| 99 |
+
|
| 100 |
+
def recenter_image(
|
| 101 |
+
self, image: Image.Image, border_ratio: float = 0.2
|
| 102 |
+
) -> Image.Image:
|
| 103 |
+
if image.mode == "RGB":
|
| 104 |
+
return image
|
| 105 |
+
elif image.mode == "L":
|
| 106 |
+
image = image.convert("RGB")
|
| 107 |
+
return image
|
| 108 |
+
|
| 109 |
+
alpha_channel = np.array(image)[:, :, 3]
|
| 110 |
+
non_zero_indices = np.argwhere(alpha_channel > 0)
|
| 111 |
+
if non_zero_indices.size == 0:
|
| 112 |
+
raise ValueError("Image is fully transparent")
|
| 113 |
+
|
| 114 |
+
min_row, min_col = non_zero_indices.min(axis=0)
|
| 115 |
+
max_row, max_col = non_zero_indices.max(axis=0)
|
| 116 |
+
|
| 117 |
+
cropped_image = image.crop(
|
| 118 |
+
(min_col, min_row, max_col + 1, max_row + 1)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
width, height = cropped_image.size
|
| 122 |
+
border_width = int(width * border_ratio)
|
| 123 |
+
border_height = int(height * border_ratio)
|
| 124 |
+
|
| 125 |
+
new_width = width + 2 * border_width
|
| 126 |
+
new_height = height + 2 * border_height
|
| 127 |
+
|
| 128 |
+
square_size = max(new_width, new_height)
|
| 129 |
+
|
| 130 |
+
new_image = Image.new(
|
| 131 |
+
"RGBA", (square_size, square_size), (255, 255, 255, 0)
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
paste_x = (square_size - new_width) // 2 + border_width
|
| 135 |
+
paste_y = (square_size - new_height) // 2 + border_height
|
| 136 |
+
|
| 137 |
+
new_image.paste(cropped_image, (paste_x, paste_y))
|
| 138 |
+
|
| 139 |
+
return new_image
|
| 140 |
+
|
| 141 |
+
@spaces.GPU
|
| 142 |
+
@torch.no_grad()
|
| 143 |
+
def __call__(
|
| 144 |
+
self,
|
| 145 |
+
image: Union[str, np.ndarray, Image.Image],
|
| 146 |
+
preprocess: bool = False,
|
| 147 |
+
target_wh: tuple[int, int] = None,
|
| 148 |
+
) -> Image.Image:
|
| 149 |
+
self._lazy_init_pipeline()
|
| 150 |
+
|
| 151 |
+
if isinstance(image, str):
|
| 152 |
+
image = Image.open(image)
|
| 153 |
+
elif isinstance(image, np.ndarray):
|
| 154 |
+
image = Image.fromarray(image)
|
| 155 |
+
|
| 156 |
+
if preprocess:
|
| 157 |
+
bg_remover = RembgRemover()
|
| 158 |
+
image = bg_remover(image)
|
| 159 |
+
image = self.recenter_image(image)
|
| 160 |
+
|
| 161 |
+
if target_wh is not None:
|
| 162 |
+
image = image.resize(target_wh)
|
| 163 |
+
else:
|
| 164 |
+
target_wh = image.size
|
| 165 |
+
|
| 166 |
+
image_array = np.array(image)
|
| 167 |
+
assert image_array.shape[-1] == 4, "Image must have alpha channel"
|
| 168 |
+
|
| 169 |
+
raw_alpha_channel = image_array[:, :, 3]
|
| 170 |
+
alpha_channel = cv2.erode(raw_alpha_channel, self.kernel, iterations=1)
|
| 171 |
+
image_array[alpha_channel == 0, :3] = 255 # must be white background
|
| 172 |
+
image_array[:, :, 3] = alpha_channel
|
| 173 |
+
|
| 174 |
+
image = self.pipeline(
|
| 175 |
+
prompt="",
|
| 176 |
+
image=Image.fromarray(image_array).convert("RGB"),
|
| 177 |
+
generator=torch.manual_seed(self.seed),
|
| 178 |
+
num_inference_steps=self.num_infer_step,
|
| 179 |
+
image_guidance_scale=self.image_guide_scale,
|
| 180 |
+
guidance_scale=self.text_guide_scale,
|
| 181 |
+
).images[0]
|
| 182 |
+
|
| 183 |
+
alpha_channel = Image.fromarray(alpha_channel)
|
| 184 |
+
rgba_image = image.convert("RGBA").resize(target_wh)
|
| 185 |
+
rgba_image.putalpha(alpha_channel)
|
| 186 |
+
|
| 187 |
+
return rgba_image
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
if __name__ == "__main__":
|
| 191 |
+
delighting_model = DelightingModel()
|
| 192 |
+
image_path = "apps/assets/example_image/sample_12.jpg"
|
| 193 |
+
image = delighting_model(
|
| 194 |
+
image_path, preprocess=True, target_wh=(512, 512)
|
| 195 |
+
) # noqa
|
| 196 |
+
image.save("delight.png")
|
| 197 |
+
|
| 198 |
+
# image_path = "embodied_gen/scripts/test_robot.png"
|
| 199 |
+
# image = delighting_model(image_path)
|
| 200 |
+
# image.save("delighting_image_a2.png")
|
embodied_gen/models/gs_model.py
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import struct
|
| 21 |
+
from dataclasses import dataclass
|
| 22 |
+
from typing import Optional
|
| 23 |
+
|
| 24 |
+
import cv2
|
| 25 |
+
import numpy as np
|
| 26 |
+
import torch
|
| 27 |
+
from gsplat.cuda._wrapper import spherical_harmonics
|
| 28 |
+
from gsplat.rendering import rasterization
|
| 29 |
+
from plyfile import PlyData
|
| 30 |
+
from scipy.spatial.transform import Rotation
|
| 31 |
+
from embodied_gen.data.utils import gamma_shs, quat_mult, quat_to_rotmat
|
| 32 |
+
|
| 33 |
+
logging.basicConfig(level=logging.INFO)
|
| 34 |
+
logger = logging.getLogger(__name__)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
__all__ = [
|
| 38 |
+
"RenderResult",
|
| 39 |
+
"GaussianOperator",
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@dataclass
|
| 44 |
+
class RenderResult:
|
| 45 |
+
rgb: np.ndarray
|
| 46 |
+
depth: np.ndarray
|
| 47 |
+
opacity: np.ndarray
|
| 48 |
+
mask_threshold: float = 10
|
| 49 |
+
mask: Optional[np.ndarray] = None
|
| 50 |
+
rgba: Optional[np.ndarray] = None
|
| 51 |
+
|
| 52 |
+
def __post_init__(self):
|
| 53 |
+
if isinstance(self.rgb, torch.Tensor):
|
| 54 |
+
rgb = self.rgb.detach().cpu().numpy()
|
| 55 |
+
rgb = (rgb * 255).astype(np.uint8)
|
| 56 |
+
self.rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)
|
| 57 |
+
if isinstance(self.depth, torch.Tensor):
|
| 58 |
+
self.depth = self.depth.detach().cpu().numpy()
|
| 59 |
+
if isinstance(self.opacity, torch.Tensor):
|
| 60 |
+
opacity = self.opacity.detach().cpu().numpy()
|
| 61 |
+
opacity = (opacity * 255).astype(np.uint8)
|
| 62 |
+
self.opacity = cv2.cvtColor(opacity, cv2.COLOR_GRAY2RGB)
|
| 63 |
+
mask = np.where(self.opacity > self.mask_threshold, 255, 0)
|
| 64 |
+
self.mask = mask[..., 0:1].astype(np.uint8)
|
| 65 |
+
self.rgba = np.concatenate([self.rgb, self.mask], axis=-1)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@dataclass
|
| 69 |
+
class GaussianBase:
|
| 70 |
+
_opacities: torch.Tensor
|
| 71 |
+
_means: torch.Tensor
|
| 72 |
+
_scales: torch.Tensor
|
| 73 |
+
_quats: torch.Tensor
|
| 74 |
+
_rgbs: Optional[torch.Tensor] = None
|
| 75 |
+
_features_dc: Optional[torch.Tensor] = None
|
| 76 |
+
_features_rest: Optional[torch.Tensor] = None
|
| 77 |
+
sh_degree: Optional[int] = 0
|
| 78 |
+
device: str = "cuda"
|
| 79 |
+
|
| 80 |
+
def __post_init__(self):
|
| 81 |
+
self.active_sh_degree: int = self.sh_degree
|
| 82 |
+
self.to(self.device)
|
| 83 |
+
|
| 84 |
+
def to(self, device: str) -> None:
|
| 85 |
+
for k, v in self.__dict__.items():
|
| 86 |
+
if not isinstance(v, torch.Tensor):
|
| 87 |
+
continue
|
| 88 |
+
self.__dict__[k] = v.to(device)
|
| 89 |
+
|
| 90 |
+
def get_numpy_data(self):
|
| 91 |
+
data = {}
|
| 92 |
+
for k, v in self.__dict__.items():
|
| 93 |
+
if not isinstance(v, torch.Tensor):
|
| 94 |
+
continue
|
| 95 |
+
data[k] = v.detach().cpu().numpy()
|
| 96 |
+
|
| 97 |
+
return data
|
| 98 |
+
|
| 99 |
+
def quat_norm(self, x: torch.Tensor) -> torch.Tensor:
|
| 100 |
+
return x / x.norm(dim=-1, keepdim=True)
|
| 101 |
+
|
| 102 |
+
@classmethod
|
| 103 |
+
def load_from_ply(
|
| 104 |
+
cls,
|
| 105 |
+
path: str,
|
| 106 |
+
gamma: float = 1.0,
|
| 107 |
+
device: str = "cuda",
|
| 108 |
+
) -> "GaussianBase":
|
| 109 |
+
plydata = PlyData.read(path)
|
| 110 |
+
xyz = torch.stack(
|
| 111 |
+
(
|
| 112 |
+
torch.tensor(plydata.elements[0]["x"], dtype=torch.float32),
|
| 113 |
+
torch.tensor(plydata.elements[0]["y"], dtype=torch.float32),
|
| 114 |
+
torch.tensor(plydata.elements[0]["z"], dtype=torch.float32),
|
| 115 |
+
),
|
| 116 |
+
dim=1,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
opacities = torch.tensor(
|
| 120 |
+
plydata.elements[0]["opacity"], dtype=torch.float32
|
| 121 |
+
).unsqueeze(-1)
|
| 122 |
+
features_dc = torch.zeros((xyz.shape[0], 3), dtype=torch.float32)
|
| 123 |
+
features_dc[:, 0] = torch.tensor(
|
| 124 |
+
plydata.elements[0]["f_dc_0"], dtype=torch.float32
|
| 125 |
+
)
|
| 126 |
+
features_dc[:, 1] = torch.tensor(
|
| 127 |
+
plydata.elements[0]["f_dc_1"], dtype=torch.float32
|
| 128 |
+
)
|
| 129 |
+
features_dc[:, 2] = torch.tensor(
|
| 130 |
+
plydata.elements[0]["f_dc_2"], dtype=torch.float32
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
scale_names = [
|
| 134 |
+
p.name
|
| 135 |
+
for p in plydata.elements[0].properties
|
| 136 |
+
if p.name.startswith("scale_")
|
| 137 |
+
]
|
| 138 |
+
scale_names = sorted(scale_names, key=lambda x: int(x.split("_")[-1]))
|
| 139 |
+
scales = torch.zeros(
|
| 140 |
+
(xyz.shape[0], len(scale_names)), dtype=torch.float32
|
| 141 |
+
)
|
| 142 |
+
for idx, attr_name in enumerate(scale_names):
|
| 143 |
+
scales[:, idx] = torch.tensor(
|
| 144 |
+
plydata.elements[0][attr_name], dtype=torch.float32
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
rot_names = [
|
| 148 |
+
p.name
|
| 149 |
+
for p in plydata.elements[0].properties
|
| 150 |
+
if p.name.startswith("rot_")
|
| 151 |
+
]
|
| 152 |
+
rot_names = sorted(rot_names, key=lambda x: int(x.split("_")[-1]))
|
| 153 |
+
rots = torch.zeros((xyz.shape[0], len(rot_names)), dtype=torch.float32)
|
| 154 |
+
for idx, attr_name in enumerate(rot_names):
|
| 155 |
+
rots[:, idx] = torch.tensor(
|
| 156 |
+
plydata.elements[0][attr_name], dtype=torch.float32
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
rots = rots / torch.norm(rots, dim=-1, keepdim=True)
|
| 160 |
+
|
| 161 |
+
# extra features
|
| 162 |
+
extra_f_names = [
|
| 163 |
+
p.name
|
| 164 |
+
for p in plydata.elements[0].properties
|
| 165 |
+
if p.name.startswith("f_rest_")
|
| 166 |
+
]
|
| 167 |
+
extra_f_names = sorted(
|
| 168 |
+
extra_f_names, key=lambda x: int(x.split("_")[-1])
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
max_sh_degree = int(np.sqrt((len(extra_f_names) + 3) / 3) - 1)
|
| 172 |
+
if max_sh_degree != 0:
|
| 173 |
+
features_extra = torch.zeros(
|
| 174 |
+
(xyz.shape[0], len(extra_f_names)), dtype=torch.float32
|
| 175 |
+
)
|
| 176 |
+
for idx, attr_name in enumerate(extra_f_names):
|
| 177 |
+
features_extra[:, idx] = torch.tensor(
|
| 178 |
+
plydata.elements[0][attr_name], dtype=torch.float32
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
features_extra = features_extra.view(
|
| 182 |
+
(features_extra.shape[0], 3, (max_sh_degree + 1) ** 2 - 1)
|
| 183 |
+
)
|
| 184 |
+
features_extra = features_extra.permute(0, 2, 1)
|
| 185 |
+
|
| 186 |
+
if abs(gamma - 1.0) > 1e-3:
|
| 187 |
+
features_dc = gamma_shs(features_dc, gamma)
|
| 188 |
+
features_extra[..., :] = 0.0
|
| 189 |
+
opacities *= 0.8
|
| 190 |
+
|
| 191 |
+
shs = torch.cat(
|
| 192 |
+
[
|
| 193 |
+
features_dc.reshape(-1, 3),
|
| 194 |
+
features_extra.reshape(len(features_dc), -1),
|
| 195 |
+
],
|
| 196 |
+
dim=-1,
|
| 197 |
+
)
|
| 198 |
+
else:
|
| 199 |
+
# sh_dim is 0, only dc features
|
| 200 |
+
shs = features_dc
|
| 201 |
+
features_extra = None
|
| 202 |
+
|
| 203 |
+
return cls(
|
| 204 |
+
sh_degree=max_sh_degree,
|
| 205 |
+
_means=xyz,
|
| 206 |
+
_opacities=opacities,
|
| 207 |
+
_rgbs=shs,
|
| 208 |
+
_scales=scales,
|
| 209 |
+
_quats=rots,
|
| 210 |
+
_features_dc=features_dc,
|
| 211 |
+
_features_rest=features_extra,
|
| 212 |
+
device=device,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
def save_to_ply(
|
| 216 |
+
self, path: str, colors: torch.Tensor = None, enable_mask: bool = False
|
| 217 |
+
):
|
| 218 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 219 |
+
numpy_data = self.get_numpy_data()
|
| 220 |
+
means = numpy_data["_means"]
|
| 221 |
+
scales = numpy_data["_scales"]
|
| 222 |
+
quats = numpy_data["_quats"]
|
| 223 |
+
opacities = numpy_data["_opacities"]
|
| 224 |
+
sh0 = numpy_data["_features_dc"]
|
| 225 |
+
shN = numpy_data.get("_features_rest", np.zeros((means.shape[0], 0)))
|
| 226 |
+
shN = shN.reshape(means.shape[0], -1)
|
| 227 |
+
|
| 228 |
+
# Create a mask to identify rows with NaN or Inf in any of the numpy_data arrays # noqa
|
| 229 |
+
if enable_mask:
|
| 230 |
+
invalid_mask = (
|
| 231 |
+
np.isnan(means).any(axis=1)
|
| 232 |
+
| np.isinf(means).any(axis=1)
|
| 233 |
+
| np.isnan(scales).any(axis=1)
|
| 234 |
+
| np.isinf(scales).any(axis=1)
|
| 235 |
+
| np.isnan(quats).any(axis=1)
|
| 236 |
+
| np.isinf(quats).any(axis=1)
|
| 237 |
+
| np.isnan(opacities).any(axis=0)
|
| 238 |
+
| np.isinf(opacities).any(axis=0)
|
| 239 |
+
| np.isnan(sh0).any(axis=1)
|
| 240 |
+
| np.isinf(sh0).any(axis=1)
|
| 241 |
+
| np.isnan(shN).any(axis=1)
|
| 242 |
+
| np.isinf(shN).any(axis=1)
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# Filter out rows with NaNs or Infs from all data arrays
|
| 246 |
+
means = means[~invalid_mask]
|
| 247 |
+
scales = scales[~invalid_mask]
|
| 248 |
+
quats = quats[~invalid_mask]
|
| 249 |
+
opacities = opacities[~invalid_mask]
|
| 250 |
+
sh0 = sh0[~invalid_mask]
|
| 251 |
+
shN = shN[~invalid_mask]
|
| 252 |
+
|
| 253 |
+
num_points = means.shape[0]
|
| 254 |
+
|
| 255 |
+
with open(path, "wb") as f:
|
| 256 |
+
# Write PLY header
|
| 257 |
+
f.write(b"ply\n")
|
| 258 |
+
f.write(b"format binary_little_endian 1.0\n")
|
| 259 |
+
f.write(f"element vertex {num_points}\n".encode())
|
| 260 |
+
f.write(b"property float x\n")
|
| 261 |
+
f.write(b"property float y\n")
|
| 262 |
+
f.write(b"property float z\n")
|
| 263 |
+
f.write(b"property float nx\n")
|
| 264 |
+
f.write(b"property float ny\n")
|
| 265 |
+
f.write(b"property float nz\n")
|
| 266 |
+
|
| 267 |
+
if colors is not None:
|
| 268 |
+
for j in range(colors.shape[1]):
|
| 269 |
+
f.write(f"property float f_dc_{j}\n".encode())
|
| 270 |
+
else:
|
| 271 |
+
for i, data in enumerate([sh0, shN]):
|
| 272 |
+
prefix = "f_dc" if i == 0 else "f_rest"
|
| 273 |
+
for j in range(data.shape[1]):
|
| 274 |
+
f.write(f"property float {prefix}_{j}\n".encode())
|
| 275 |
+
|
| 276 |
+
f.write(b"property float opacity\n")
|
| 277 |
+
|
| 278 |
+
for i in range(scales.shape[1]):
|
| 279 |
+
f.write(f"property float scale_{i}\n".encode())
|
| 280 |
+
for i in range(quats.shape[1]):
|
| 281 |
+
f.write(f"property float rot_{i}\n".encode())
|
| 282 |
+
|
| 283 |
+
f.write(b"end_header\n")
|
| 284 |
+
|
| 285 |
+
# Write vertex data
|
| 286 |
+
for i in range(num_points):
|
| 287 |
+
f.write(struct.pack("<fff", *means[i])) # x, y, z
|
| 288 |
+
f.write(struct.pack("<fff", 0, 0, 0)) # nx, ny, nz (zeros)
|
| 289 |
+
|
| 290 |
+
if colors is not None:
|
| 291 |
+
color = colors.detach().cpu().numpy()
|
| 292 |
+
for j in range(color.shape[1]):
|
| 293 |
+
f_dc = (color[i, j] - 0.5) / 0.2820947917738781
|
| 294 |
+
f.write(struct.pack("<f", f_dc))
|
| 295 |
+
else:
|
| 296 |
+
for data in [sh0, shN]:
|
| 297 |
+
for j in range(data.shape[1]):
|
| 298 |
+
f.write(struct.pack("<f", data[i, j]))
|
| 299 |
+
|
| 300 |
+
f.write(struct.pack("<f", opacities[i])) # opacity
|
| 301 |
+
|
| 302 |
+
for data in [scales, quats]:
|
| 303 |
+
for j in range(data.shape[1]):
|
| 304 |
+
f.write(struct.pack("<f", data[i, j]))
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
@dataclass
|
| 308 |
+
class GaussianOperator(GaussianBase):
|
| 309 |
+
"""Gaussian Splatting operator.
|
| 310 |
+
|
| 311 |
+
Supports transformation, scaling, color computation, and
|
| 312 |
+
rasterization-based rendering.
|
| 313 |
+
|
| 314 |
+
Inherits:
|
| 315 |
+
GaussianBase: Base class with Gaussian params (means, scales, etc.)
|
| 316 |
+
|
| 317 |
+
Functionality includes:
|
| 318 |
+
- Applying instance poses to transform Gaussian means and quaternions.
|
| 319 |
+
- Scaling Gaussians to a real-world size.
|
| 320 |
+
- Computing colors using spherical harmonics.
|
| 321 |
+
- Rendering images via differentiable rasterization.
|
| 322 |
+
- Exporting transformed and rescaled models to .ply format.
|
| 323 |
+
"""
|
| 324 |
+
|
| 325 |
+
def _compute_transform(
|
| 326 |
+
self,
|
| 327 |
+
means: torch.Tensor,
|
| 328 |
+
quats: torch.Tensor,
|
| 329 |
+
instance_pose: torch.Tensor,
|
| 330 |
+
):
|
| 331 |
+
"""Compute the transform of the GS models.
|
| 332 |
+
|
| 333 |
+
Args:
|
| 334 |
+
means: tensor of gs means.
|
| 335 |
+
quats: tensor of gs quaternions.
|
| 336 |
+
instance_pose: instances poses in [x y z qx qy qz qw] format.
|
| 337 |
+
|
| 338 |
+
"""
|
| 339 |
+
# (x y z qx qy qz qw) -> (x y z qw qx qy qz)
|
| 340 |
+
instance_pose = instance_pose[[0, 1, 2, 6, 3, 4, 5]]
|
| 341 |
+
cur_instances_quats = self.quat_norm(instance_pose[3:])
|
| 342 |
+
rot_cur = quat_to_rotmat(cur_instances_quats, mode="wxyz")
|
| 343 |
+
|
| 344 |
+
# update the means
|
| 345 |
+
num_gs = means.shape[0]
|
| 346 |
+
trans_per_pts = torch.stack([instance_pose[:3]] * num_gs, dim=0)
|
| 347 |
+
quat_per_pts = torch.stack([instance_pose[3:]] * num_gs, dim=0)
|
| 348 |
+
rot_per_pts = torch.stack([rot_cur] * num_gs, dim=0) # (num_gs, 3, 3)
|
| 349 |
+
|
| 350 |
+
# update the means
|
| 351 |
+
cur_means = (
|
| 352 |
+
torch.bmm(rot_per_pts, means.unsqueeze(-1)).squeeze(-1)
|
| 353 |
+
+ trans_per_pts
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
# update the quats
|
| 357 |
+
_quats = self.quat_norm(quats)
|
| 358 |
+
cur_quats = quat_mult(quat_per_pts, _quats)
|
| 359 |
+
|
| 360 |
+
return cur_means, cur_quats
|
| 361 |
+
|
| 362 |
+
def get_gaussians(
|
| 363 |
+
self,
|
| 364 |
+
c2w: torch.Tensor = None,
|
| 365 |
+
instance_pose: torch.Tensor = None,
|
| 366 |
+
apply_activate: bool = False,
|
| 367 |
+
) -> "GaussianBase":
|
| 368 |
+
"""Get Gaussian data under the given instance_pose."""
|
| 369 |
+
if c2w is None:
|
| 370 |
+
c2w = torch.eye(4).to(self.device)
|
| 371 |
+
|
| 372 |
+
if instance_pose is not None:
|
| 373 |
+
# compute the transformed gs means and quats
|
| 374 |
+
world_means, world_quats = self._compute_transform(
|
| 375 |
+
self._means, self._quats, instance_pose.float().to(self.device)
|
| 376 |
+
)
|
| 377 |
+
else:
|
| 378 |
+
world_means, world_quats = self._means, self._quats
|
| 379 |
+
|
| 380 |
+
# get colors of gaussians
|
| 381 |
+
if self._features_rest is not None:
|
| 382 |
+
colors = torch.cat(
|
| 383 |
+
(self._features_dc[:, None, :], self._features_rest), dim=1
|
| 384 |
+
)
|
| 385 |
+
else:
|
| 386 |
+
colors = self._features_dc[:, None, :]
|
| 387 |
+
|
| 388 |
+
if self.sh_degree > 0:
|
| 389 |
+
viewdirs = world_means.detach() - c2w[..., :3, 3] # (N, 3)
|
| 390 |
+
viewdirs = viewdirs / viewdirs.norm(dim=-1, keepdim=True)
|
| 391 |
+
rgbs = spherical_harmonics(self.sh_degree, viewdirs, colors)
|
| 392 |
+
rgbs = torch.clamp(rgbs + 0.5, 0.0, 1.0)
|
| 393 |
+
else:
|
| 394 |
+
rgbs = torch.sigmoid(colors[:, 0, :])
|
| 395 |
+
|
| 396 |
+
gs_dict = dict(
|
| 397 |
+
_means=world_means,
|
| 398 |
+
_opacities=(
|
| 399 |
+
torch.sigmoid(self._opacities)
|
| 400 |
+
if apply_activate
|
| 401 |
+
else self._opacities
|
| 402 |
+
),
|
| 403 |
+
_rgbs=rgbs,
|
| 404 |
+
_scales=(
|
| 405 |
+
torch.exp(self._scales) if apply_activate else self._scales
|
| 406 |
+
),
|
| 407 |
+
_quats=self.quat_norm(world_quats),
|
| 408 |
+
_features_dc=self._features_dc,
|
| 409 |
+
_features_rest=self._features_rest,
|
| 410 |
+
sh_degree=self.sh_degree,
|
| 411 |
+
device=self.device,
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
return GaussianOperator(**gs_dict)
|
| 415 |
+
|
| 416 |
+
def rescale(self, scale: float):
|
| 417 |
+
if scale != 1.0:
|
| 418 |
+
self._means *= scale
|
| 419 |
+
self._scales += torch.log(self._scales.new_tensor(scale))
|
| 420 |
+
|
| 421 |
+
def set_scale_by_height(self, real_height: float) -> None:
|
| 422 |
+
def _ptp(tensor, dim):
|
| 423 |
+
val = tensor.max(dim=dim).values - tensor.min(dim=dim).values
|
| 424 |
+
return val.tolist()
|
| 425 |
+
|
| 426 |
+
xyz_scale = max(_ptp(self._means, dim=0))
|
| 427 |
+
self.rescale(1 / (xyz_scale + 1e-6)) # Normalize to [-0.5, 0.5]
|
| 428 |
+
raw_height = _ptp(self._means, dim=0)[1]
|
| 429 |
+
scale = real_height / raw_height
|
| 430 |
+
|
| 431 |
+
self.rescale(scale)
|
| 432 |
+
|
| 433 |
+
return
|
| 434 |
+
|
| 435 |
+
@staticmethod
|
| 436 |
+
def resave_ply(
|
| 437 |
+
in_ply: str,
|
| 438 |
+
out_ply: str,
|
| 439 |
+
real_height: float = None,
|
| 440 |
+
instance_pose: np.ndarray = None,
|
| 441 |
+
device: str = "cuda",
|
| 442 |
+
) -> None:
|
| 443 |
+
gs_model = GaussianOperator.load_from_ply(in_ply, device=device)
|
| 444 |
+
|
| 445 |
+
if instance_pose is not None:
|
| 446 |
+
gs_model = gs_model.get_gaussians(instance_pose=instance_pose)
|
| 447 |
+
|
| 448 |
+
if real_height is not None:
|
| 449 |
+
gs_model.set_scale_by_height(real_height)
|
| 450 |
+
|
| 451 |
+
gs_model.save_to_ply(out_ply)
|
| 452 |
+
|
| 453 |
+
return
|
| 454 |
+
|
| 455 |
+
@staticmethod
|
| 456 |
+
def trans_to_quatpose(
|
| 457 |
+
rot_matrix: list[list[float]],
|
| 458 |
+
trans_matrix: list[float] = [0, 0, 0],
|
| 459 |
+
) -> torch.Tensor:
|
| 460 |
+
if isinstance(rot_matrix, list):
|
| 461 |
+
rot_matrix = np.array(rot_matrix)
|
| 462 |
+
|
| 463 |
+
rot = Rotation.from_matrix(rot_matrix)
|
| 464 |
+
qx, qy, qz, qw = rot.as_quat()
|
| 465 |
+
instance_pose = torch.tensor([*trans_matrix, qx, qy, qz, qw])
|
| 466 |
+
|
| 467 |
+
return instance_pose
|
| 468 |
+
|
| 469 |
+
def render(
|
| 470 |
+
self,
|
| 471 |
+
c2w: torch.Tensor,
|
| 472 |
+
Ks: torch.Tensor,
|
| 473 |
+
image_width: int,
|
| 474 |
+
image_height: int,
|
| 475 |
+
) -> RenderResult:
|
| 476 |
+
gs = self.get_gaussians(c2w, apply_activate=True)
|
| 477 |
+
renders, alphas, _ = rasterization(
|
| 478 |
+
means=gs._means,
|
| 479 |
+
quats=gs._quats,
|
| 480 |
+
scales=gs._scales,
|
| 481 |
+
opacities=gs._opacities.squeeze(),
|
| 482 |
+
colors=gs._rgbs,
|
| 483 |
+
viewmats=torch.linalg.inv(c2w)[None, ...],
|
| 484 |
+
Ks=Ks[None, ...],
|
| 485 |
+
width=image_width,
|
| 486 |
+
height=image_height,
|
| 487 |
+
packed=False,
|
| 488 |
+
absgrad=True,
|
| 489 |
+
sparse_grad=False,
|
| 490 |
+
# rasterize_mode="classic",
|
| 491 |
+
rasterize_mode="antialiased",
|
| 492 |
+
**{
|
| 493 |
+
"near_plane": 0.01,
|
| 494 |
+
"far_plane": 1000000000,
|
| 495 |
+
"radius_clip": 0.0,
|
| 496 |
+
"render_mode": "RGB+ED",
|
| 497 |
+
},
|
| 498 |
+
)
|
| 499 |
+
renders = renders[0]
|
| 500 |
+
alphas = alphas[0].squeeze(-1)
|
| 501 |
+
|
| 502 |
+
assert renders.shape[-1] == 4, f"Must render rgb, depth and alpha"
|
| 503 |
+
rendered_rgb, rendered_depth = torch.split(renders, [3, 1], dim=-1)
|
| 504 |
+
|
| 505 |
+
return RenderResult(
|
| 506 |
+
torch.clamp(rendered_rgb, min=0, max=1),
|
| 507 |
+
rendered_depth,
|
| 508 |
+
alphas[..., None],
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
if __name__ == "__main__":
|
| 513 |
+
input_gs = "outputs/test/debug.ply"
|
| 514 |
+
output_gs = "./debug_v3.ply"
|
| 515 |
+
gs_model: GaussianOperator = GaussianOperator.load_from_ply(input_gs)
|
| 516 |
+
|
| 517 |
+
# 绕 x 轴旋转 180°
|
| 518 |
+
R_x = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
|
| 519 |
+
instance_pose = gs_model.trans_to_quatpose(R_x)
|
| 520 |
+
gs_model = gs_model.get_gaussians(instance_pose=instance_pose)
|
| 521 |
+
|
| 522 |
+
gs_model.rescale(2)
|
| 523 |
+
|
| 524 |
+
gs_model.set_scale_by_height(1.3)
|
| 525 |
+
|
| 526 |
+
gs_model.save_to_ply(output_gs)
|
embodied_gen/models/segment_model.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
from typing import Literal, Union
|
| 21 |
+
|
| 22 |
+
import cv2
|
| 23 |
+
import numpy as np
|
| 24 |
+
import rembg
|
| 25 |
+
import torch
|
| 26 |
+
from huggingface_hub import snapshot_download
|
| 27 |
+
from PIL import Image
|
| 28 |
+
from segment_anything import (
|
| 29 |
+
SamAutomaticMaskGenerator,
|
| 30 |
+
SamPredictor,
|
| 31 |
+
sam_model_registry,
|
| 32 |
+
)
|
| 33 |
+
from transformers import pipeline
|
| 34 |
+
from embodied_gen.data.utils import resize_pil, trellis_preprocess
|
| 35 |
+
from embodied_gen.utils.process_media import filter_small_connected_components
|
| 36 |
+
from embodied_gen.validators.quality_checkers import ImageSegChecker
|
| 37 |
+
|
| 38 |
+
logging.basicConfig(level=logging.INFO)
|
| 39 |
+
logger = logging.getLogger(__name__)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
__all__ = [
|
| 43 |
+
"SAMRemover",
|
| 44 |
+
"SAMPredictor",
|
| 45 |
+
"RembgRemover",
|
| 46 |
+
"get_segmented_image_by_agent",
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class SAMRemover(object):
|
| 51 |
+
"""Loading SAM models and performing background removal on images.
|
| 52 |
+
|
| 53 |
+
Attributes:
|
| 54 |
+
checkpoint (str): Path to the model checkpoint.
|
| 55 |
+
model_type (str): Type of the SAM model to load (default: "vit_h").
|
| 56 |
+
area_ratio (float): Area ratio filtering small connected components.
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
def __init__(
|
| 60 |
+
self,
|
| 61 |
+
checkpoint: str = None,
|
| 62 |
+
model_type: str = "vit_h",
|
| 63 |
+
area_ratio: float = 15,
|
| 64 |
+
):
|
| 65 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 66 |
+
self.model_type = model_type
|
| 67 |
+
self.area_ratio = area_ratio
|
| 68 |
+
|
| 69 |
+
if checkpoint is None:
|
| 70 |
+
suffix = "sam"
|
| 71 |
+
model_path = snapshot_download(
|
| 72 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 73 |
+
)
|
| 74 |
+
checkpoint = os.path.join(
|
| 75 |
+
model_path, suffix, "sam_vit_h_4b8939.pth"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
self.mask_generator = self._load_sam_model(checkpoint)
|
| 79 |
+
|
| 80 |
+
def _load_sam_model(self, checkpoint: str) -> SamAutomaticMaskGenerator:
|
| 81 |
+
sam = sam_model_registry[self.model_type](checkpoint=checkpoint)
|
| 82 |
+
sam.to(device=self.device)
|
| 83 |
+
|
| 84 |
+
return SamAutomaticMaskGenerator(sam)
|
| 85 |
+
|
| 86 |
+
def __call__(
|
| 87 |
+
self, image: Union[str, Image.Image, np.ndarray], save_path: str = None
|
| 88 |
+
) -> Image.Image:
|
| 89 |
+
"""Removes the background from an image using the SAM model.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
image (Union[str, Image.Image, np.ndarray]): Input image,
|
| 93 |
+
can be a file path, PIL Image, or numpy array.
|
| 94 |
+
save_path (str): Path to save the output image (default: None).
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
Image.Image: The image with background removed,
|
| 98 |
+
including an alpha channel.
|
| 99 |
+
"""
|
| 100 |
+
# Convert input to numpy array
|
| 101 |
+
if isinstance(image, str):
|
| 102 |
+
image = Image.open(image)
|
| 103 |
+
elif isinstance(image, np.ndarray):
|
| 104 |
+
image = Image.fromarray(image).convert("RGB")
|
| 105 |
+
image = resize_pil(image)
|
| 106 |
+
image = np.array(image.convert("RGB"))
|
| 107 |
+
|
| 108 |
+
# Generate masks
|
| 109 |
+
masks = self.mask_generator.generate(image)
|
| 110 |
+
masks = sorted(masks, key=lambda x: x["area"], reverse=True)
|
| 111 |
+
|
| 112 |
+
if not masks:
|
| 113 |
+
logger.warning(
|
| 114 |
+
"Segmentation failed: No mask generated, return raw image."
|
| 115 |
+
)
|
| 116 |
+
output_image = Image.fromarray(image, mode="RGB")
|
| 117 |
+
else:
|
| 118 |
+
# Use the largest mask
|
| 119 |
+
best_mask = masks[0]["segmentation"]
|
| 120 |
+
mask = (best_mask * 255).astype(np.uint8)
|
| 121 |
+
mask = filter_small_connected_components(
|
| 122 |
+
mask, area_ratio=self.area_ratio
|
| 123 |
+
)
|
| 124 |
+
# Apply the mask to remove the background
|
| 125 |
+
background_removed = cv2.bitwise_and(image, image, mask=mask)
|
| 126 |
+
output_image = np.dstack((background_removed, mask))
|
| 127 |
+
output_image = Image.fromarray(output_image, mode="RGBA")
|
| 128 |
+
|
| 129 |
+
if save_path is not None:
|
| 130 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
| 131 |
+
output_image.save(save_path)
|
| 132 |
+
|
| 133 |
+
return output_image
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class SAMPredictor(object):
|
| 137 |
+
def __init__(
|
| 138 |
+
self,
|
| 139 |
+
checkpoint: str = None,
|
| 140 |
+
model_type: str = "vit_h",
|
| 141 |
+
binary_thresh: float = 0.1,
|
| 142 |
+
device: str = "cuda",
|
| 143 |
+
):
|
| 144 |
+
self.device = device
|
| 145 |
+
self.model_type = model_type
|
| 146 |
+
|
| 147 |
+
if checkpoint is None:
|
| 148 |
+
suffix = "sam"
|
| 149 |
+
model_path = snapshot_download(
|
| 150 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 151 |
+
)
|
| 152 |
+
checkpoint = os.path.join(
|
| 153 |
+
model_path, suffix, "sam_vit_h_4b8939.pth"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
self.predictor = self._load_sam_model(checkpoint)
|
| 157 |
+
self.binary_thresh = binary_thresh
|
| 158 |
+
|
| 159 |
+
def _load_sam_model(self, checkpoint: str) -> SamPredictor:
|
| 160 |
+
sam = sam_model_registry[self.model_type](checkpoint=checkpoint)
|
| 161 |
+
sam.to(device=self.device)
|
| 162 |
+
|
| 163 |
+
return SamPredictor(sam)
|
| 164 |
+
|
| 165 |
+
def preprocess_image(self, image: Image.Image) -> np.ndarray:
|
| 166 |
+
if isinstance(image, str):
|
| 167 |
+
image = Image.open(image)
|
| 168 |
+
elif isinstance(image, np.ndarray):
|
| 169 |
+
image = Image.fromarray(image).convert("RGB")
|
| 170 |
+
|
| 171 |
+
image = resize_pil(image)
|
| 172 |
+
image = np.array(image.convert("RGB"))
|
| 173 |
+
|
| 174 |
+
return image
|
| 175 |
+
|
| 176 |
+
def generate_masks(
|
| 177 |
+
self,
|
| 178 |
+
image: np.ndarray,
|
| 179 |
+
selected_points: list[list[int]],
|
| 180 |
+
) -> np.ndarray:
|
| 181 |
+
if len(selected_points) == 0:
|
| 182 |
+
return []
|
| 183 |
+
|
| 184 |
+
points = (
|
| 185 |
+
torch.Tensor([p for p, _ in selected_points])
|
| 186 |
+
.to(self.predictor.device)
|
| 187 |
+
.unsqueeze(1)
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
labels = (
|
| 191 |
+
torch.Tensor([int(l) for _, l in selected_points])
|
| 192 |
+
.to(self.predictor.device)
|
| 193 |
+
.unsqueeze(1)
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
transformed_points = self.predictor.transform.apply_coords_torch(
|
| 197 |
+
points, image.shape[:2]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
masks, scores, _ = self.predictor.predict_torch(
|
| 201 |
+
point_coords=transformed_points,
|
| 202 |
+
point_labels=labels,
|
| 203 |
+
multimask_output=True,
|
| 204 |
+
)
|
| 205 |
+
valid_mask = masks[:, torch.argmax(scores, dim=1)]
|
| 206 |
+
masks_pos = valid_mask[labels[:, 0] == 1, 0].cpu().detach().numpy()
|
| 207 |
+
masks_neg = valid_mask[labels[:, 0] == 0, 0].cpu().detach().numpy()
|
| 208 |
+
if len(masks_neg) == 0:
|
| 209 |
+
masks_neg = np.zeros_like(masks_pos)
|
| 210 |
+
if len(masks_pos) == 0:
|
| 211 |
+
masks_pos = np.zeros_like(masks_neg)
|
| 212 |
+
masks_neg = masks_neg.max(axis=0, keepdims=True)
|
| 213 |
+
masks_pos = masks_pos.max(axis=0, keepdims=True)
|
| 214 |
+
valid_mask = (masks_pos.astype(int) - masks_neg.astype(int)).clip(0, 1)
|
| 215 |
+
|
| 216 |
+
binary_mask = (valid_mask > self.binary_thresh).astype(np.int32)
|
| 217 |
+
|
| 218 |
+
return [(mask, f"mask_{i}") for i, mask in enumerate(binary_mask)]
|
| 219 |
+
|
| 220 |
+
def get_segmented_image(
|
| 221 |
+
self, image: np.ndarray, masks: list[tuple[np.ndarray, str]]
|
| 222 |
+
) -> Image.Image:
|
| 223 |
+
seg_image = Image.fromarray(image, mode="RGB")
|
| 224 |
+
alpha_channel = np.zeros(
|
| 225 |
+
(seg_image.height, seg_image.width), dtype=np.uint8
|
| 226 |
+
)
|
| 227 |
+
for mask, _ in masks:
|
| 228 |
+
# Use the maximum to combine multiple masks
|
| 229 |
+
alpha_channel = np.maximum(alpha_channel, mask)
|
| 230 |
+
|
| 231 |
+
alpha_channel = np.clip(alpha_channel, 0, 1)
|
| 232 |
+
alpha_channel = (alpha_channel * 255).astype(np.uint8)
|
| 233 |
+
alpha_image = Image.fromarray(alpha_channel, mode="L")
|
| 234 |
+
r, g, b = seg_image.split()
|
| 235 |
+
seg_image = Image.merge("RGBA", (r, g, b, alpha_image))
|
| 236 |
+
|
| 237 |
+
return seg_image
|
| 238 |
+
|
| 239 |
+
def __call__(
|
| 240 |
+
self,
|
| 241 |
+
image: Union[str, Image.Image, np.ndarray],
|
| 242 |
+
selected_points: list[list[int]],
|
| 243 |
+
) -> Image.Image:
|
| 244 |
+
image = self.preprocess_image(image)
|
| 245 |
+
self.predictor.set_image(image)
|
| 246 |
+
masks = self.generate_masks(image, selected_points)
|
| 247 |
+
|
| 248 |
+
return self.get_segmented_image(image, masks)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
class RembgRemover(object):
|
| 252 |
+
def __init__(self):
|
| 253 |
+
self.rembg_session = rembg.new_session("u2net")
|
| 254 |
+
|
| 255 |
+
def __call__(
|
| 256 |
+
self, image: Union[str, Image.Image, np.ndarray], save_path: str = None
|
| 257 |
+
) -> Image.Image:
|
| 258 |
+
if isinstance(image, str):
|
| 259 |
+
image = Image.open(image)
|
| 260 |
+
elif isinstance(image, np.ndarray):
|
| 261 |
+
image = Image.fromarray(image)
|
| 262 |
+
|
| 263 |
+
image = resize_pil(image)
|
| 264 |
+
output_image = rembg.remove(image, session=self.rembg_session)
|
| 265 |
+
|
| 266 |
+
if save_path is not None:
|
| 267 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
| 268 |
+
output_image.save(save_path)
|
| 269 |
+
|
| 270 |
+
return output_image
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
class BMGG14Remover(object):
|
| 274 |
+
def __init__(self) -> None:
|
| 275 |
+
self.model = pipeline(
|
| 276 |
+
"image-segmentation",
|
| 277 |
+
model="briaai/RMBG-1.4",
|
| 278 |
+
trust_remote_code=True,
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
def __call__(
|
| 282 |
+
self, image: Union[str, Image.Image, np.ndarray], save_path: str = None
|
| 283 |
+
):
|
| 284 |
+
if isinstance(image, str):
|
| 285 |
+
image = Image.open(image)
|
| 286 |
+
elif isinstance(image, np.ndarray):
|
| 287 |
+
image = Image.fromarray(image)
|
| 288 |
+
|
| 289 |
+
image = resize_pil(image)
|
| 290 |
+
output_image = self.model(image)
|
| 291 |
+
|
| 292 |
+
if save_path is not None:
|
| 293 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
| 294 |
+
output_image.save(save_path)
|
| 295 |
+
|
| 296 |
+
return output_image
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def invert_rgba_pil(
|
| 300 |
+
image: Image.Image, mask: Image.Image, save_path: str = None
|
| 301 |
+
) -> Image.Image:
|
| 302 |
+
mask = (255 - np.array(mask))[..., None]
|
| 303 |
+
image_array = np.concatenate([np.array(image), mask], axis=-1)
|
| 304 |
+
inverted_image = Image.fromarray(image_array, "RGBA")
|
| 305 |
+
|
| 306 |
+
if save_path is not None:
|
| 307 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
| 308 |
+
inverted_image.save(save_path)
|
| 309 |
+
|
| 310 |
+
return inverted_image
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def get_segmented_image_by_agent(
|
| 314 |
+
image: Image.Image,
|
| 315 |
+
sam_remover: SAMRemover,
|
| 316 |
+
rbg_remover: RembgRemover,
|
| 317 |
+
seg_checker: ImageSegChecker = None,
|
| 318 |
+
save_path: str = None,
|
| 319 |
+
mode: Literal["loose", "strict"] = "loose",
|
| 320 |
+
) -> Image.Image:
|
| 321 |
+
def _is_valid_seg(raw_img: Image.Image, seg_img: Image.Image) -> bool:
|
| 322 |
+
if seg_checker is None:
|
| 323 |
+
return True
|
| 324 |
+
return raw_img.mode == "RGBA" and seg_checker([raw_img, seg_img])[0]
|
| 325 |
+
|
| 326 |
+
out_sam = f"{save_path}_sam.png" if save_path else None
|
| 327 |
+
out_sam_inv = f"{save_path}_sam_inv.png" if save_path else None
|
| 328 |
+
out_rbg = f"{save_path}_rbg.png" if save_path else None
|
| 329 |
+
|
| 330 |
+
seg_image = sam_remover(image, out_sam)
|
| 331 |
+
seg_image = seg_image.convert("RGBA")
|
| 332 |
+
_, _, _, alpha = seg_image.split()
|
| 333 |
+
seg_image_inv = invert_rgba_pil(image.convert("RGB"), alpha, out_sam_inv)
|
| 334 |
+
seg_image_rbg = rbg_remover(image, out_rbg)
|
| 335 |
+
|
| 336 |
+
final_image = None
|
| 337 |
+
if _is_valid_seg(image, seg_image):
|
| 338 |
+
final_image = seg_image
|
| 339 |
+
elif _is_valid_seg(image, seg_image_inv):
|
| 340 |
+
final_image = seg_image_inv
|
| 341 |
+
elif _is_valid_seg(image, seg_image_rbg):
|
| 342 |
+
logger.warning(f"Failed to segment by `SAM`, retry with `rembg`.")
|
| 343 |
+
final_image = seg_image_rbg
|
| 344 |
+
else:
|
| 345 |
+
if mode == "strict":
|
| 346 |
+
raise RuntimeError(
|
| 347 |
+
f"Failed to segment by `SAM` or `rembg`, abort."
|
| 348 |
+
)
|
| 349 |
+
logger.warning("Failed to segment by SAM or rembg, use raw image.")
|
| 350 |
+
final_image = image.convert("RGBA")
|
| 351 |
+
|
| 352 |
+
if save_path:
|
| 353 |
+
final_image.save(save_path)
|
| 354 |
+
|
| 355 |
+
final_image = trellis_preprocess(final_image)
|
| 356 |
+
|
| 357 |
+
return final_image
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
if __name__ == "__main__":
|
| 361 |
+
input_image = "outputs/text2image/demo_objects/electrical/sample_0.jpg"
|
| 362 |
+
output_image = "sample_0_seg2.png"
|
| 363 |
+
|
| 364 |
+
# input_image = "outputs/text2image/tmp/coffee_machine.jpeg"
|
| 365 |
+
# output_image = "outputs/text2image/tmp/coffee_machine_seg.png"
|
| 366 |
+
|
| 367 |
+
# input_image = "outputs/text2image/tmp/bucket.jpeg"
|
| 368 |
+
# output_image = "outputs/text2image/tmp/bucket_seg.png"
|
| 369 |
+
|
| 370 |
+
remover = SAMRemover(model_type="vit_h")
|
| 371 |
+
remover = RembgRemover()
|
| 372 |
+
clean_image = remover(input_image)
|
| 373 |
+
clean_image.save(output_image)
|
| 374 |
+
get_segmented_image_by_agent(
|
| 375 |
+
Image.open(input_image), remover, remover, None, "./test_seg.png"
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
remover = BMGG14Remover()
|
| 379 |
+
remover("embodied_gen/models/test_seg.jpg", "./seg.png")
|
embodied_gen/models/sr_model.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
from typing import Union
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
import spaces
|
| 24 |
+
import torch
|
| 25 |
+
from huggingface_hub import snapshot_download
|
| 26 |
+
from PIL import Image
|
| 27 |
+
from embodied_gen.data.utils import get_images_from_grid
|
| 28 |
+
|
| 29 |
+
logging.basicConfig(
|
| 30 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 31 |
+
)
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
__all__ = [
|
| 36 |
+
"ImageStableSR",
|
| 37 |
+
"ImageRealESRGAN",
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class ImageStableSR:
|
| 42 |
+
"""Super-resolution image upscaler using Stable Diffusion x4 upscaling model from StabilityAI."""
|
| 43 |
+
|
| 44 |
+
def __init__(
|
| 45 |
+
self,
|
| 46 |
+
model_path: str = "stabilityai/stable-diffusion-x4-upscaler",
|
| 47 |
+
device="cuda",
|
| 48 |
+
) -> None:
|
| 49 |
+
from diffusers import StableDiffusionUpscalePipeline
|
| 50 |
+
|
| 51 |
+
self.up_pipeline_x4 = StableDiffusionUpscalePipeline.from_pretrained(
|
| 52 |
+
model_path,
|
| 53 |
+
torch_dtype=torch.float16,
|
| 54 |
+
).to(device)
|
| 55 |
+
self.up_pipeline_x4.set_progress_bar_config(disable=True)
|
| 56 |
+
# self.up_pipeline_x4.enable_model_cpu_offload()
|
| 57 |
+
|
| 58 |
+
@spaces.GPU
|
| 59 |
+
def __call__(
|
| 60 |
+
self,
|
| 61 |
+
image: Union[Image.Image, np.ndarray],
|
| 62 |
+
prompt: str = "",
|
| 63 |
+
infer_step: int = 20,
|
| 64 |
+
) -> Image.Image:
|
| 65 |
+
if isinstance(image, np.ndarray):
|
| 66 |
+
image = Image.fromarray(image)
|
| 67 |
+
|
| 68 |
+
image = image.convert("RGB")
|
| 69 |
+
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
upscaled_image = self.up_pipeline_x4(
|
| 72 |
+
image=image,
|
| 73 |
+
prompt=[prompt],
|
| 74 |
+
num_inference_steps=infer_step,
|
| 75 |
+
).images[0]
|
| 76 |
+
|
| 77 |
+
return upscaled_image
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class ImageRealESRGAN:
|
| 81 |
+
"""A wrapper for Real-ESRGAN-based image super-resolution.
|
| 82 |
+
|
| 83 |
+
This class uses the RealESRGAN model to perform image upscaling,
|
| 84 |
+
typically by a factor of 4.
|
| 85 |
+
|
| 86 |
+
Attributes:
|
| 87 |
+
outscale (int): The output image scale factor (e.g., 2, 4).
|
| 88 |
+
model_path (str): Path to the pre-trained model weights.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
def __init__(self, outscale: int, model_path: str = None) -> None:
|
| 92 |
+
# monkey patch to support torchvision>=0.16
|
| 93 |
+
import torchvision
|
| 94 |
+
from packaging import version
|
| 95 |
+
|
| 96 |
+
if version.parse(torchvision.__version__) > version.parse("0.16"):
|
| 97 |
+
import sys
|
| 98 |
+
import types
|
| 99 |
+
|
| 100 |
+
import torchvision.transforms.functional as TF
|
| 101 |
+
|
| 102 |
+
functional_tensor = types.ModuleType(
|
| 103 |
+
"torchvision.transforms.functional_tensor"
|
| 104 |
+
)
|
| 105 |
+
functional_tensor.rgb_to_grayscale = TF.rgb_to_grayscale
|
| 106 |
+
sys.modules["torchvision.transforms.functional_tensor"] = (
|
| 107 |
+
functional_tensor
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
self.outscale = outscale
|
| 111 |
+
self.upsampler = None
|
| 112 |
+
|
| 113 |
+
if model_path is None:
|
| 114 |
+
suffix = "super_resolution"
|
| 115 |
+
model_path = snapshot_download(
|
| 116 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 117 |
+
)
|
| 118 |
+
model_path = os.path.join(
|
| 119 |
+
model_path, suffix, "RealESRGAN_x4plus.pth"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
self.model_path = model_path
|
| 123 |
+
|
| 124 |
+
def _lazy_init(self):
|
| 125 |
+
if self.upsampler is None:
|
| 126 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 127 |
+
from realesrgan import RealESRGANer
|
| 128 |
+
|
| 129 |
+
model = RRDBNet(
|
| 130 |
+
num_in_ch=3,
|
| 131 |
+
num_out_ch=3,
|
| 132 |
+
num_feat=64,
|
| 133 |
+
num_block=23,
|
| 134 |
+
num_grow_ch=32,
|
| 135 |
+
scale=4,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
self.upsampler = RealESRGANer(
|
| 139 |
+
scale=4,
|
| 140 |
+
model_path=self.model_path,
|
| 141 |
+
model=model,
|
| 142 |
+
pre_pad=0,
|
| 143 |
+
half=True,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
@spaces.GPU
|
| 147 |
+
def __call__(self, image: Union[Image.Image, np.ndarray]) -> Image.Image:
|
| 148 |
+
self._lazy_init()
|
| 149 |
+
|
| 150 |
+
if isinstance(image, Image.Image):
|
| 151 |
+
image = np.array(image)
|
| 152 |
+
|
| 153 |
+
with torch.no_grad():
|
| 154 |
+
output, _ = self.upsampler.enhance(image, outscale=self.outscale)
|
| 155 |
+
|
| 156 |
+
return Image.fromarray(output)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
color_path = "outputs/texture_mesh_gen/multi_view/color_sample0.png"
|
| 161 |
+
|
| 162 |
+
# Use RealESRGAN_x4plus for x4 (512->2048) image super resolution.
|
| 163 |
+
super_model = ImageRealESRGAN(outscale=4)
|
| 164 |
+
multiviews = get_images_from_grid(color_path, img_size=512)
|
| 165 |
+
multiviews = [super_model(img.convert("RGB")) for img in multiviews]
|
| 166 |
+
for idx, img in enumerate(multiviews):
|
| 167 |
+
img.save(f"sr{idx}.png")
|
| 168 |
+
|
| 169 |
+
# # Use stable diffusion for x4 (512->2048) image super resolution.
|
| 170 |
+
# super_model = ImageStableSR()
|
| 171 |
+
# multiviews = get_images_from_grid(color_path, img_size=512)
|
| 172 |
+
# multiviews = [super_model(img) for img in multiviews]
|
| 173 |
+
# for idx, img in enumerate(multiviews):
|
| 174 |
+
# img.save(f"sr_stable{idx}.png")
|
embodied_gen/models/text_model.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
|
| 20 |
+
import torch
|
| 21 |
+
from diffusers import (
|
| 22 |
+
AutoencoderKL,
|
| 23 |
+
EulerDiscreteScheduler,
|
| 24 |
+
UNet2DConditionModel,
|
| 25 |
+
)
|
| 26 |
+
from kolors.models.modeling_chatglm import ChatGLMModel
|
| 27 |
+
from kolors.models.tokenization_chatglm import ChatGLMTokenizer
|
| 28 |
+
from kolors.models.unet_2d_condition import (
|
| 29 |
+
UNet2DConditionModel as UNet2DConditionModelIP,
|
| 30 |
+
)
|
| 31 |
+
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256 import (
|
| 32 |
+
StableDiffusionXLPipeline,
|
| 33 |
+
)
|
| 34 |
+
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256_ipadapter import ( # noqa
|
| 35 |
+
StableDiffusionXLPipeline as StableDiffusionXLPipelineIP,
|
| 36 |
+
)
|
| 37 |
+
from PIL import Image
|
| 38 |
+
from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection
|
| 39 |
+
|
| 40 |
+
logging.basicConfig(level=logging.INFO)
|
| 41 |
+
logger = logging.getLogger(__name__)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
__all__ = [
|
| 45 |
+
"build_text2img_ip_pipeline",
|
| 46 |
+
"build_text2img_pipeline",
|
| 47 |
+
"text2img_gen",
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def build_text2img_ip_pipeline(
|
| 52 |
+
ckpt_dir: str,
|
| 53 |
+
ref_scale: float,
|
| 54 |
+
device: str = "cuda",
|
| 55 |
+
) -> StableDiffusionXLPipelineIP:
|
| 56 |
+
text_encoder = ChatGLMModel.from_pretrained(
|
| 57 |
+
f"{ckpt_dir}/text_encoder", torch_dtype=torch.float16
|
| 58 |
+
).half()
|
| 59 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(f"{ckpt_dir}/text_encoder")
|
| 60 |
+
vae = AutoencoderKL.from_pretrained(
|
| 61 |
+
f"{ckpt_dir}/vae", revision=None
|
| 62 |
+
).half()
|
| 63 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(f"{ckpt_dir}/scheduler")
|
| 64 |
+
unet = UNet2DConditionModelIP.from_pretrained(
|
| 65 |
+
f"{ckpt_dir}/unet", revision=None
|
| 66 |
+
).half()
|
| 67 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
| 68 |
+
f"{ckpt_dir}/../Kolors-IP-Adapter-Plus/image_encoder",
|
| 69 |
+
ignore_mismatched_sizes=True,
|
| 70 |
+
).to(dtype=torch.float16)
|
| 71 |
+
clip_image_processor = CLIPImageProcessor(size=336, crop_size=336)
|
| 72 |
+
|
| 73 |
+
pipe = StableDiffusionXLPipelineIP(
|
| 74 |
+
vae=vae,
|
| 75 |
+
text_encoder=text_encoder,
|
| 76 |
+
tokenizer=tokenizer,
|
| 77 |
+
unet=unet,
|
| 78 |
+
scheduler=scheduler,
|
| 79 |
+
image_encoder=image_encoder,
|
| 80 |
+
feature_extractor=clip_image_processor,
|
| 81 |
+
force_zeros_for_empty_prompt=False,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if hasattr(pipe.unet, "encoder_hid_proj"):
|
| 85 |
+
pipe.unet.text_encoder_hid_proj = pipe.unet.encoder_hid_proj
|
| 86 |
+
|
| 87 |
+
pipe.load_ip_adapter(
|
| 88 |
+
f"{ckpt_dir}/../Kolors-IP-Adapter-Plus",
|
| 89 |
+
subfolder="",
|
| 90 |
+
weight_name=["ip_adapter_plus_general.bin"],
|
| 91 |
+
)
|
| 92 |
+
pipe.set_ip_adapter_scale([ref_scale])
|
| 93 |
+
|
| 94 |
+
pipe = pipe.to(device)
|
| 95 |
+
# pipe.enable_model_cpu_offload()
|
| 96 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 97 |
+
# pipe.enable_vae_slicing()
|
| 98 |
+
|
| 99 |
+
return pipe
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def build_text2img_pipeline(
|
| 103 |
+
ckpt_dir: str,
|
| 104 |
+
device: str = "cuda",
|
| 105 |
+
) -> StableDiffusionXLPipeline:
|
| 106 |
+
text_encoder = ChatGLMModel.from_pretrained(
|
| 107 |
+
f"{ckpt_dir}/text_encoder", torch_dtype=torch.float16
|
| 108 |
+
).half()
|
| 109 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(f"{ckpt_dir}/text_encoder")
|
| 110 |
+
vae = AutoencoderKL.from_pretrained(
|
| 111 |
+
f"{ckpt_dir}/vae", revision=None
|
| 112 |
+
).half()
|
| 113 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(f"{ckpt_dir}/scheduler")
|
| 114 |
+
unet = UNet2DConditionModel.from_pretrained(
|
| 115 |
+
f"{ckpt_dir}/unet", revision=None
|
| 116 |
+
).half()
|
| 117 |
+
pipe = StableDiffusionXLPipeline(
|
| 118 |
+
vae=vae,
|
| 119 |
+
text_encoder=text_encoder,
|
| 120 |
+
tokenizer=tokenizer,
|
| 121 |
+
unet=unet,
|
| 122 |
+
scheduler=scheduler,
|
| 123 |
+
force_zeros_for_empty_prompt=False,
|
| 124 |
+
)
|
| 125 |
+
pipe = pipe.to(device)
|
| 126 |
+
# pipe.enable_model_cpu_offload()
|
| 127 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 128 |
+
|
| 129 |
+
return pipe
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def text2img_gen(
|
| 133 |
+
prompt: str,
|
| 134 |
+
n_sample: int,
|
| 135 |
+
guidance_scale: float,
|
| 136 |
+
pipeline: StableDiffusionXLPipeline | StableDiffusionXLPipelineIP,
|
| 137 |
+
ip_image: Image.Image | str = None,
|
| 138 |
+
image_wh: tuple[int, int] = [1024, 1024],
|
| 139 |
+
infer_step: int = 50,
|
| 140 |
+
ip_image_size: int = 512,
|
| 141 |
+
) -> list[Image.Image]:
|
| 142 |
+
prompt = "Single " + prompt + ", in the center of the image"
|
| 143 |
+
prompt += ", high quality, high resolution, best quality, white background, 3D style," # noqa
|
| 144 |
+
logger.info(f"Processing prompt: {prompt}")
|
| 145 |
+
|
| 146 |
+
kwargs = dict(
|
| 147 |
+
prompt=prompt,
|
| 148 |
+
height=image_wh[1],
|
| 149 |
+
width=image_wh[0],
|
| 150 |
+
num_inference_steps=infer_step,
|
| 151 |
+
guidance_scale=guidance_scale,
|
| 152 |
+
num_images_per_prompt=n_sample,
|
| 153 |
+
)
|
| 154 |
+
if ip_image is not None:
|
| 155 |
+
if isinstance(ip_image, str):
|
| 156 |
+
ip_image = Image.open(ip_image)
|
| 157 |
+
ip_image = ip_image.resize((ip_image_size, ip_image_size))
|
| 158 |
+
kwargs.update(ip_adapter_image=[ip_image])
|
| 159 |
+
|
| 160 |
+
return pipeline(**kwargs).images
|
embodied_gen/models/texture_model.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
import torch
|
| 21 |
+
from diffusers import AutoencoderKL, DiffusionPipeline, EulerDiscreteScheduler
|
| 22 |
+
from huggingface_hub import snapshot_download
|
| 23 |
+
from kolors.models.controlnet import ControlNetModel
|
| 24 |
+
from kolors.models.modeling_chatglm import ChatGLMModel
|
| 25 |
+
from kolors.models.tokenization_chatglm import ChatGLMTokenizer
|
| 26 |
+
from kolors.models.unet_2d_condition import UNet2DConditionModel
|
| 27 |
+
from kolors.pipelines.pipeline_controlnet_xl_kolors_img2img import (
|
| 28 |
+
StableDiffusionXLControlNetImg2ImgPipeline,
|
| 29 |
+
)
|
| 30 |
+
from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection
|
| 31 |
+
|
| 32 |
+
__all__ = [
|
| 33 |
+
"build_texture_gen_pipe",
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def build_texture_gen_pipe(
|
| 38 |
+
base_ckpt_dir: str,
|
| 39 |
+
controlnet_ckpt: str = None,
|
| 40 |
+
ip_adapt_scale: float = 0,
|
| 41 |
+
device: str = "cuda",
|
| 42 |
+
) -> DiffusionPipeline:
|
| 43 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(
|
| 44 |
+
f"{base_ckpt_dir}/Kolors/text_encoder"
|
| 45 |
+
)
|
| 46 |
+
text_encoder = ChatGLMModel.from_pretrained(
|
| 47 |
+
f"{base_ckpt_dir}/Kolors/text_encoder", torch_dtype=torch.float16
|
| 48 |
+
).half()
|
| 49 |
+
vae = AutoencoderKL.from_pretrained(
|
| 50 |
+
f"{base_ckpt_dir}/Kolors/vae", revision=None
|
| 51 |
+
).half()
|
| 52 |
+
unet = UNet2DConditionModel.from_pretrained(
|
| 53 |
+
f"{base_ckpt_dir}/Kolors/unet", revision=None
|
| 54 |
+
).half()
|
| 55 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(
|
| 56 |
+
f"{base_ckpt_dir}/Kolors/scheduler"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if controlnet_ckpt is None:
|
| 60 |
+
suffix = "geo_cond_mv"
|
| 61 |
+
model_path = snapshot_download(
|
| 62 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 63 |
+
)
|
| 64 |
+
controlnet_ckpt = os.path.join(model_path, suffix)
|
| 65 |
+
|
| 66 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 67 |
+
controlnet_ckpt, use_safetensors=True
|
| 68 |
+
).half()
|
| 69 |
+
|
| 70 |
+
# IP-Adapter model
|
| 71 |
+
image_encoder = None
|
| 72 |
+
clip_image_processor = None
|
| 73 |
+
if ip_adapt_scale > 0:
|
| 74 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
| 75 |
+
f"{base_ckpt_dir}/Kolors-IP-Adapter-Plus/image_encoder",
|
| 76 |
+
# ignore_mismatched_sizes=True,
|
| 77 |
+
).to(dtype=torch.float16)
|
| 78 |
+
ip_img_size = 336
|
| 79 |
+
clip_image_processor = CLIPImageProcessor(
|
| 80 |
+
size=ip_img_size, crop_size=ip_img_size
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
pipe = StableDiffusionXLControlNetImg2ImgPipeline(
|
| 84 |
+
vae=vae,
|
| 85 |
+
controlnet=controlnet,
|
| 86 |
+
text_encoder=text_encoder,
|
| 87 |
+
tokenizer=tokenizer,
|
| 88 |
+
unet=unet,
|
| 89 |
+
scheduler=scheduler,
|
| 90 |
+
image_encoder=image_encoder,
|
| 91 |
+
feature_extractor=clip_image_processor,
|
| 92 |
+
force_zeros_for_empty_prompt=False,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if ip_adapt_scale > 0:
|
| 96 |
+
if hasattr(pipe.unet, "encoder_hid_proj"):
|
| 97 |
+
pipe.unet.text_encoder_hid_proj = pipe.unet.encoder_hid_proj
|
| 98 |
+
pipe.load_ip_adapter(
|
| 99 |
+
f"{base_ckpt_dir}/Kolors-IP-Adapter-Plus",
|
| 100 |
+
subfolder="",
|
| 101 |
+
weight_name=["ip_adapter_plus_general.bin"],
|
| 102 |
+
)
|
| 103 |
+
pipe.set_ip_adapter_scale([ip_adapt_scale])
|
| 104 |
+
|
| 105 |
+
pipe = pipe.to(device)
|
| 106 |
+
# pipe.enable_model_cpu_offload()
|
| 107 |
+
|
| 108 |
+
return pipe
|
embodied_gen/scripts/imageto3d.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
from glob import glob
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 25 |
+
import trimesh
|
| 26 |
+
from PIL import Image
|
| 27 |
+
from embodied_gen.data.backproject_v2 import entrypoint as backproject_api
|
| 28 |
+
from embodied_gen.data.utils import trellis_preprocess
|
| 29 |
+
from embodied_gen.models.delight_model import DelightingModel
|
| 30 |
+
from embodied_gen.models.gs_model import GaussianOperator
|
| 31 |
+
from embodied_gen.models.segment_model import (
|
| 32 |
+
BMGG14Remover,
|
| 33 |
+
RembgRemover,
|
| 34 |
+
SAMPredictor,
|
| 35 |
+
)
|
| 36 |
+
from embodied_gen.models.sr_model import ImageRealESRGAN
|
| 37 |
+
from embodied_gen.scripts.render_gs import entrypoint as render_gs_api
|
| 38 |
+
from embodied_gen.utils.gpt_clients import GPT_CLIENT
|
| 39 |
+
from embodied_gen.utils.process_media import merge_images_video, render_video
|
| 40 |
+
from embodied_gen.utils.tags import VERSION
|
| 41 |
+
from embodied_gen.validators.quality_checkers import (
|
| 42 |
+
BaseChecker,
|
| 43 |
+
ImageAestheticChecker,
|
| 44 |
+
ImageSegChecker,
|
| 45 |
+
MeshGeoChecker,
|
| 46 |
+
)
|
| 47 |
+
from embodied_gen.validators.urdf_convertor import URDFGenerator
|
| 48 |
+
|
| 49 |
+
current_file_path = os.path.abspath(__file__)
|
| 50 |
+
current_dir = os.path.dirname(current_file_path)
|
| 51 |
+
sys.path.append(os.path.join(current_dir, "../.."))
|
| 52 |
+
from thirdparty.TRELLIS.trellis.pipelines import TrellisImageTo3DPipeline
|
| 53 |
+
|
| 54 |
+
logging.basicConfig(
|
| 55 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 56 |
+
)
|
| 57 |
+
logger = logging.getLogger(__name__)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
os.environ["TORCH_EXTENSIONS_DIR"] = os.path.expanduser(
|
| 61 |
+
"~/.cache/torch_extensions"
|
| 62 |
+
)
|
| 63 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "false"
|
| 64 |
+
os.environ["SPCONV_ALGO"] = "native"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
DELIGHT = DelightingModel()
|
| 68 |
+
IMAGESR_MODEL = ImageRealESRGAN(outscale=4)
|
| 69 |
+
|
| 70 |
+
RBG_REMOVER = RembgRemover()
|
| 71 |
+
RBG14_REMOVER = BMGG14Remover()
|
| 72 |
+
SAM_PREDICTOR = SAMPredictor(model_type="vit_h", device="cpu")
|
| 73 |
+
PIPELINE = TrellisImageTo3DPipeline.from_pretrained("jetx/trellis-image-large")
|
| 74 |
+
PIPELINE.cuda()
|
| 75 |
+
SEG_CHECKER = ImageSegChecker(GPT_CLIENT)
|
| 76 |
+
GEO_CHECKER = MeshGeoChecker(GPT_CLIENT)
|
| 77 |
+
AESTHETIC_CHECKER = ImageAestheticChecker()
|
| 78 |
+
CHECKERS = [GEO_CHECKER, SEG_CHECKER, AESTHETIC_CHECKER]
|
| 79 |
+
TMP_DIR = os.path.join(
|
| 80 |
+
os.path.dirname(os.path.abspath(__file__)), "sessions/imageto3d"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def parse_args():
|
| 85 |
+
parser = argparse.ArgumentParser(description="Image to 3D pipeline args.")
|
| 86 |
+
parser.add_argument(
|
| 87 |
+
"--image_path", type=str, nargs="+", help="Path to the input images."
|
| 88 |
+
)
|
| 89 |
+
parser.add_argument(
|
| 90 |
+
"--image_root", type=str, help="Path to the input images folder."
|
| 91 |
+
)
|
| 92 |
+
parser.add_argument(
|
| 93 |
+
"--output_root",
|
| 94 |
+
type=str,
|
| 95 |
+
required=True,
|
| 96 |
+
help="Root directory for saving outputs.",
|
| 97 |
+
)
|
| 98 |
+
parser.add_argument(
|
| 99 |
+
"--no_mesh", action="store_true", help="Do not output mesh files."
|
| 100 |
+
)
|
| 101 |
+
parser.add_argument(
|
| 102 |
+
"--height_range",
|
| 103 |
+
type=str,
|
| 104 |
+
default=None,
|
| 105 |
+
help="The hight in meter to restore the mesh real size.",
|
| 106 |
+
)
|
| 107 |
+
parser.add_argument(
|
| 108 |
+
"--mass_range",
|
| 109 |
+
type=str,
|
| 110 |
+
default=None,
|
| 111 |
+
help="The mass in kg to restore the mesh real weight.",
|
| 112 |
+
)
|
| 113 |
+
parser.add_argument("--asset_type", type=str, default=None)
|
| 114 |
+
parser.add_argument("--skip_exists", action="store_true")
|
| 115 |
+
parser.add_argument("--strict_seg", action="store_true")
|
| 116 |
+
parser.add_argument("--version", type=str, default=VERSION)
|
| 117 |
+
args = parser.parse_args()
|
| 118 |
+
|
| 119 |
+
assert (
|
| 120 |
+
args.image_path or args.image_root
|
| 121 |
+
), "Please provide either --image_path or --image_root."
|
| 122 |
+
if not args.image_path:
|
| 123 |
+
args.image_path = glob(os.path.join(args.image_root, "*.png"))
|
| 124 |
+
args.image_path += glob(os.path.join(args.image_root, "*.jpg"))
|
| 125 |
+
args.image_path += glob(os.path.join(args.image_root, "*.jpeg"))
|
| 126 |
+
|
| 127 |
+
return args
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
args = parse_args()
|
| 132 |
+
|
| 133 |
+
for image_path in args.image_path:
|
| 134 |
+
try:
|
| 135 |
+
filename = os.path.basename(image_path).split(".")[0]
|
| 136 |
+
output_root = args.output_root
|
| 137 |
+
if args.image_root is not None:
|
| 138 |
+
output_root = os.path.join(output_root, filename)
|
| 139 |
+
os.makedirs(output_root, exist_ok=True)
|
| 140 |
+
|
| 141 |
+
mesh_out = f"{output_root}/{filename}.obj"
|
| 142 |
+
if args.skip_exists and os.path.exists(mesh_out):
|
| 143 |
+
logger.info(
|
| 144 |
+
f"Skip {image_path}, already processed in {mesh_out}"
|
| 145 |
+
)
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
image = Image.open(image_path)
|
| 149 |
+
image.save(f"{output_root}/{filename}_raw.png")
|
| 150 |
+
|
| 151 |
+
# Segmentation: Get segmented image using SAM or Rembg.
|
| 152 |
+
seg_path = f"{output_root}/{filename}_cond.png"
|
| 153 |
+
if image.mode != "RGBA":
|
| 154 |
+
seg_image = RBG_REMOVER(image, save_path=seg_path)
|
| 155 |
+
seg_image = trellis_preprocess(seg_image)
|
| 156 |
+
else:
|
| 157 |
+
seg_image = image
|
| 158 |
+
seg_image.save(seg_path)
|
| 159 |
+
|
| 160 |
+
# Run the pipeline
|
| 161 |
+
try:
|
| 162 |
+
outputs = PIPELINE.run(
|
| 163 |
+
seg_image,
|
| 164 |
+
preprocess_image=False,
|
| 165 |
+
# Optional parameters
|
| 166 |
+
# seed=1,
|
| 167 |
+
# sparse_structure_sampler_params={
|
| 168 |
+
# "steps": 12,
|
| 169 |
+
# "cfg_strength": 7.5,
|
| 170 |
+
# },
|
| 171 |
+
# slat_sampler_params={
|
| 172 |
+
# "steps": 12,
|
| 173 |
+
# "cfg_strength": 3,
|
| 174 |
+
# },
|
| 175 |
+
)
|
| 176 |
+
except Exception as e:
|
| 177 |
+
logger.error(
|
| 178 |
+
f"[Pipeline Failed] process {image_path}: {e}, skip."
|
| 179 |
+
)
|
| 180 |
+
continue
|
| 181 |
+
|
| 182 |
+
# Render and save color and mesh videos
|
| 183 |
+
gs_model = outputs["gaussian"][0]
|
| 184 |
+
mesh_model = outputs["mesh"][0]
|
| 185 |
+
color_images = render_video(gs_model)["color"]
|
| 186 |
+
normal_images = render_video(mesh_model)["normal"]
|
| 187 |
+
video_path = os.path.join(output_root, "gs_mesh.mp4")
|
| 188 |
+
merge_images_video(color_images, normal_images, video_path)
|
| 189 |
+
|
| 190 |
+
if not args.no_mesh:
|
| 191 |
+
# Save the raw Gaussian model
|
| 192 |
+
gs_path = mesh_out.replace(".obj", "_gs.ply")
|
| 193 |
+
gs_model.save_ply(gs_path)
|
| 194 |
+
|
| 195 |
+
# Rotate mesh and GS by 90 degrees around Z-axis.
|
| 196 |
+
rot_matrix = [[0, 0, -1], [0, 1, 0], [1, 0, 0]]
|
| 197 |
+
gs_add_rot = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
|
| 198 |
+
mesh_add_rot = [[1, 0, 0], [0, 0, -1], [0, 1, 0]]
|
| 199 |
+
|
| 200 |
+
# Addtional rotation for GS to align mesh.
|
| 201 |
+
gs_rot = np.array(gs_add_rot) @ np.array(rot_matrix)
|
| 202 |
+
pose = GaussianOperator.trans_to_quatpose(gs_rot)
|
| 203 |
+
aligned_gs_path = gs_path.replace(".ply", "_aligned.ply")
|
| 204 |
+
GaussianOperator.resave_ply(
|
| 205 |
+
in_ply=gs_path,
|
| 206 |
+
out_ply=aligned_gs_path,
|
| 207 |
+
instance_pose=pose,
|
| 208 |
+
device="cpu",
|
| 209 |
+
)
|
| 210 |
+
color_path = os.path.join(output_root, "color.png")
|
| 211 |
+
render_gs_api(aligned_gs_path, color_path)
|
| 212 |
+
|
| 213 |
+
mesh = trimesh.Trimesh(
|
| 214 |
+
vertices=mesh_model.vertices.cpu().numpy(),
|
| 215 |
+
faces=mesh_model.faces.cpu().numpy(),
|
| 216 |
+
)
|
| 217 |
+
mesh.vertices = mesh.vertices @ np.array(mesh_add_rot)
|
| 218 |
+
mesh.vertices = mesh.vertices @ np.array(rot_matrix)
|
| 219 |
+
|
| 220 |
+
mesh_obj_path = os.path.join(output_root, f"{filename}.obj")
|
| 221 |
+
mesh.export(mesh_obj_path)
|
| 222 |
+
|
| 223 |
+
mesh = backproject_api(
|
| 224 |
+
delight_model=DELIGHT,
|
| 225 |
+
imagesr_model=IMAGESR_MODEL,
|
| 226 |
+
color_path=color_path,
|
| 227 |
+
mesh_path=mesh_obj_path,
|
| 228 |
+
output_path=mesh_obj_path,
|
| 229 |
+
skip_fix_mesh=False,
|
| 230 |
+
delight=True,
|
| 231 |
+
texture_wh=[2048, 2048],
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
mesh_glb_path = os.path.join(output_root, f"{filename}.glb")
|
| 235 |
+
mesh.export(mesh_glb_path)
|
| 236 |
+
|
| 237 |
+
urdf_convertor = URDFGenerator(GPT_CLIENT, render_view_num=4)
|
| 238 |
+
asset_attrs = {
|
| 239 |
+
"version": VERSION,
|
| 240 |
+
"gs_model": f"{urdf_convertor.output_mesh_dir}/{filename}_gs.ply",
|
| 241 |
+
}
|
| 242 |
+
if args.height_range:
|
| 243 |
+
min_height, max_height = map(
|
| 244 |
+
float, args.height_range.split("-")
|
| 245 |
+
)
|
| 246 |
+
asset_attrs["min_height"] = min_height
|
| 247 |
+
asset_attrs["max_height"] = max_height
|
| 248 |
+
if args.mass_range:
|
| 249 |
+
min_mass, max_mass = map(float, args.mass_range.split("-"))
|
| 250 |
+
asset_attrs["min_mass"] = min_mass
|
| 251 |
+
asset_attrs["max_mass"] = max_mass
|
| 252 |
+
if args.asset_type:
|
| 253 |
+
asset_attrs["category"] = args.asset_type
|
| 254 |
+
if args.version:
|
| 255 |
+
asset_attrs["version"] = args.version
|
| 256 |
+
|
| 257 |
+
urdf_path = urdf_convertor(
|
| 258 |
+
mesh_path=mesh_obj_path,
|
| 259 |
+
output_root=f"{output_root}/URDF_{filename}",
|
| 260 |
+
**asset_attrs,
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
# Rescale GS and save to URDF/mesh folder.
|
| 264 |
+
real_height = urdf_convertor.get_attr_from_urdf(
|
| 265 |
+
urdf_path, attr_name="real_height"
|
| 266 |
+
)
|
| 267 |
+
out_gs = f"{output_root}/URDF_{filename}/{urdf_convertor.output_mesh_dir}/{filename}_gs.ply" # noqa
|
| 268 |
+
GaussianOperator.resave_ply(
|
| 269 |
+
in_ply=aligned_gs_path,
|
| 270 |
+
out_ply=out_gs,
|
| 271 |
+
real_height=real_height,
|
| 272 |
+
device="cpu",
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
# Quality check and update .urdf file.
|
| 276 |
+
mesh_out = f"{output_root}/URDF_{filename}/{urdf_convertor.output_mesh_dir}/{filename}.obj" # noqa
|
| 277 |
+
trimesh.load(mesh_out).export(mesh_out.replace(".obj", ".glb"))
|
| 278 |
+
# image_paths = render_asset3d(
|
| 279 |
+
# mesh_path=mesh_out,
|
| 280 |
+
# output_root=f"{output_root}/URDF_{filename}",
|
| 281 |
+
# output_subdir="qa_renders",
|
| 282 |
+
# num_images=8,
|
| 283 |
+
# elevation=(30, -30),
|
| 284 |
+
# distance=5.5,
|
| 285 |
+
# )
|
| 286 |
+
|
| 287 |
+
image_dir = f"{output_root}/URDF_{filename}/{urdf_convertor.output_render_dir}/image_color" # noqa
|
| 288 |
+
image_paths = glob(f"{image_dir}/*.png")
|
| 289 |
+
images_list = []
|
| 290 |
+
for checker in CHECKERS:
|
| 291 |
+
images = image_paths
|
| 292 |
+
if isinstance(checker, ImageSegChecker):
|
| 293 |
+
images = [
|
| 294 |
+
f"{output_root}/{filename}_raw.png",
|
| 295 |
+
f"{output_root}/{filename}_cond.png",
|
| 296 |
+
]
|
| 297 |
+
images_list.append(images)
|
| 298 |
+
|
| 299 |
+
results = BaseChecker.validate(CHECKERS, images_list)
|
| 300 |
+
urdf_convertor.add_quality_tag(urdf_path, results)
|
| 301 |
+
|
| 302 |
+
except Exception as e:
|
| 303 |
+
logger.error(f"Failed to process {image_path}: {e}, skip.")
|
| 304 |
+
continue
|
| 305 |
+
|
| 306 |
+
logger.info(f"Processing complete. Outputs saved to {args.output_root}")
|
embodied_gen/scripts/render_gs.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import logging
|
| 20 |
+
import math
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
import cv2
|
| 24 |
+
import numpy as np
|
| 25 |
+
import spaces
|
| 26 |
+
import torch
|
| 27 |
+
from tqdm import tqdm
|
| 28 |
+
from embodied_gen.data.utils import (
|
| 29 |
+
CameraSetting,
|
| 30 |
+
init_kal_camera,
|
| 31 |
+
normalize_vertices_array,
|
| 32 |
+
)
|
| 33 |
+
from embodied_gen.models.gs_model import GaussianOperator
|
| 34 |
+
|
| 35 |
+
logging.basicConfig(
|
| 36 |
+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 37 |
+
)
|
| 38 |
+
logger = logging.getLogger(__name__)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def parse_args():
|
| 42 |
+
parser = argparse.ArgumentParser(description="Render GS color images")
|
| 43 |
+
|
| 44 |
+
parser.add_argument(
|
| 45 |
+
"--input_gs", type=str, help="Input render GS.ply path."
|
| 46 |
+
)
|
| 47 |
+
parser.add_argument(
|
| 48 |
+
"--output_path",
|
| 49 |
+
type=str,
|
| 50 |
+
help="Output grid image path for rendered GS color images.",
|
| 51 |
+
)
|
| 52 |
+
parser.add_argument(
|
| 53 |
+
"--num_images", type=int, default=6, help="Number of images to render."
|
| 54 |
+
)
|
| 55 |
+
parser.add_argument(
|
| 56 |
+
"--elevation",
|
| 57 |
+
type=float,
|
| 58 |
+
nargs="+",
|
| 59 |
+
default=[20.0, -10.0],
|
| 60 |
+
help="Elevation angles for the camera (default: [20.0, -10.0])",
|
| 61 |
+
)
|
| 62 |
+
parser.add_argument(
|
| 63 |
+
"--distance",
|
| 64 |
+
type=float,
|
| 65 |
+
default=5,
|
| 66 |
+
help="Camera distance (default: 5)",
|
| 67 |
+
)
|
| 68 |
+
parser.add_argument(
|
| 69 |
+
"--resolution_hw",
|
| 70 |
+
type=int,
|
| 71 |
+
nargs=2,
|
| 72 |
+
default=(512, 512),
|
| 73 |
+
help="Resolution of the output images (default: (512, 512))",
|
| 74 |
+
)
|
| 75 |
+
parser.add_argument(
|
| 76 |
+
"--fov",
|
| 77 |
+
type=float,
|
| 78 |
+
default=30,
|
| 79 |
+
help="Field of view in degrees (default: 30)",
|
| 80 |
+
)
|
| 81 |
+
parser.add_argument(
|
| 82 |
+
"--device",
|
| 83 |
+
type=str,
|
| 84 |
+
choices=["cpu", "cuda"],
|
| 85 |
+
default="cuda",
|
| 86 |
+
help="Device to run on (default: `cuda`)",
|
| 87 |
+
)
|
| 88 |
+
parser.add_argument(
|
| 89 |
+
"--image_size",
|
| 90 |
+
type=int,
|
| 91 |
+
default=512,
|
| 92 |
+
help="Output image size for single view in color grid (default: 512)",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
args, unknown = parser.parse_known_args()
|
| 96 |
+
|
| 97 |
+
return args
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def load_gs_model(
|
| 101 |
+
input_gs: str, pre_quat: list[float] = [0.0, 0.7071, 0.0, -0.7071]
|
| 102 |
+
) -> GaussianOperator:
|
| 103 |
+
gs_model = GaussianOperator.load_from_ply(input_gs)
|
| 104 |
+
# Normalize vertices to [-1, 1], center to (0, 0, 0).
|
| 105 |
+
_, scale, center = normalize_vertices_array(gs_model._means)
|
| 106 |
+
scale, center = float(scale), center.tolist()
|
| 107 |
+
transpose = [*[-v for v in center], *pre_quat]
|
| 108 |
+
instance_pose = torch.tensor(transpose).to(gs_model.device)
|
| 109 |
+
gs_model = gs_model.get_gaussians(instance_pose=instance_pose)
|
| 110 |
+
gs_model.rescale(scale)
|
| 111 |
+
|
| 112 |
+
return gs_model
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
@spaces.GPU
|
| 116 |
+
def entrypoint(input_gs: str = None, output_path: str = None) -> None:
|
| 117 |
+
args = parse_args()
|
| 118 |
+
if isinstance(input_gs, str):
|
| 119 |
+
args.input_gs = input_gs
|
| 120 |
+
if isinstance(output_path, str):
|
| 121 |
+
args.output_path = output_path
|
| 122 |
+
|
| 123 |
+
# Setup camera parameters
|
| 124 |
+
camera_params = CameraSetting(
|
| 125 |
+
num_images=args.num_images,
|
| 126 |
+
elevation=args.elevation,
|
| 127 |
+
distance=args.distance,
|
| 128 |
+
resolution_hw=args.resolution_hw,
|
| 129 |
+
fov=math.radians(args.fov),
|
| 130 |
+
device=args.device,
|
| 131 |
+
)
|
| 132 |
+
camera = init_kal_camera(camera_params)
|
| 133 |
+
matrix_mv = camera.view_matrix() # (n_cam 4 4) world2cam
|
| 134 |
+
matrix_mv[:, :3, 3] = -matrix_mv[:, :3, 3]
|
| 135 |
+
w2cs = matrix_mv.to(camera_params.device)
|
| 136 |
+
c2ws = [torch.linalg.inv(matrix) for matrix in w2cs]
|
| 137 |
+
Ks = torch.tensor(camera_params.Ks).to(camera_params.device)
|
| 138 |
+
|
| 139 |
+
# Load GS model and normalize.
|
| 140 |
+
gs_model = load_gs_model(args.input_gs, pre_quat=[0.0, 0.0, 1.0, 0.0])
|
| 141 |
+
|
| 142 |
+
# Render GS color images.
|
| 143 |
+
images = []
|
| 144 |
+
for idx in tqdm(range(len(c2ws)), desc="Rendering GS"):
|
| 145 |
+
result = gs_model.render(
|
| 146 |
+
c2ws[idx],
|
| 147 |
+
Ks=Ks,
|
| 148 |
+
image_width=camera_params.resolution_hw[1],
|
| 149 |
+
image_height=camera_params.resolution_hw[0],
|
| 150 |
+
)
|
| 151 |
+
color = cv2.resize(
|
| 152 |
+
result.rgba,
|
| 153 |
+
(args.image_size, args.image_size),
|
| 154 |
+
interpolation=cv2.INTER_AREA,
|
| 155 |
+
)
|
| 156 |
+
images.append(color)
|
| 157 |
+
|
| 158 |
+
# Cat color images into grid image and save.
|
| 159 |
+
select_idxs = [[0, 2, 1], [5, 4, 3]] # fix order for 6 views
|
| 160 |
+
grid_image = []
|
| 161 |
+
for row_idxs in select_idxs:
|
| 162 |
+
row_image = []
|
| 163 |
+
for row_idx in row_idxs:
|
| 164 |
+
row_image.append(images[row_idx])
|
| 165 |
+
row_image = np.concatenate(row_image, axis=1)
|
| 166 |
+
grid_image.append(row_image)
|
| 167 |
+
|
| 168 |
+
grid_image = np.concatenate(grid_image, axis=0)
|
| 169 |
+
os.makedirs(os.path.dirname(args.output_path), exist_ok=True)
|
| 170 |
+
cv2.imwrite(args.output_path, grid_image)
|
| 171 |
+
logger.info(f"Saved grid image to {args.output_path}")
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
if __name__ == "__main__":
|
| 175 |
+
entrypoint()
|
embodied_gen/scripts/render_mv.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import random
|
| 21 |
+
from typing import List, Tuple
|
| 22 |
+
|
| 23 |
+
import fire
|
| 24 |
+
import numpy as np
|
| 25 |
+
import torch
|
| 26 |
+
from diffusers.utils import make_image_grid
|
| 27 |
+
from kolors.pipelines.pipeline_controlnet_xl_kolors_img2img import (
|
| 28 |
+
StableDiffusionXLControlNetImg2ImgPipeline,
|
| 29 |
+
)
|
| 30 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 31 |
+
from torchvision import transforms
|
| 32 |
+
from embodied_gen.data.datasets import Asset3dGenDataset
|
| 33 |
+
from embodied_gen.models.texture_model import build_texture_gen_pipe
|
| 34 |
+
|
| 35 |
+
logging.basicConfig(level=logging.INFO)
|
| 36 |
+
logger = logging.getLogger(__name__)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_init_noise_image(image: Image.Image) -> Image.Image:
|
| 40 |
+
blurred_image = image.convert("L").filter(
|
| 41 |
+
ImageFilter.GaussianBlur(radius=3)
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
enhancer = ImageEnhance.Contrast(blurred_image)
|
| 45 |
+
image_decreased_contrast = enhancer.enhance(factor=0.5)
|
| 46 |
+
|
| 47 |
+
return image_decreased_contrast
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def infer_pipe(
|
| 51 |
+
index_file: str,
|
| 52 |
+
controlnet_ckpt: str = None,
|
| 53 |
+
uid: str = None,
|
| 54 |
+
prompt: str = None,
|
| 55 |
+
controlnet_cond_scale: float = 0.4,
|
| 56 |
+
control_guidance_end: float = 0.9,
|
| 57 |
+
strength: float = 1.0,
|
| 58 |
+
num_inference_steps: int = 50,
|
| 59 |
+
guidance_scale: float = 10,
|
| 60 |
+
ip_adapt_scale: float = 0,
|
| 61 |
+
ip_img_path: str = None,
|
| 62 |
+
sub_idxs: List[List[int]] = None,
|
| 63 |
+
num_images_per_prompt: int = 3, # increase if want similar images.
|
| 64 |
+
device: str = "cuda",
|
| 65 |
+
save_dir: str = "infer_vis",
|
| 66 |
+
seed: int = None,
|
| 67 |
+
target_hw: tuple[int, int] = (512, 512),
|
| 68 |
+
pipeline: StableDiffusionXLControlNetImg2ImgPipeline = None,
|
| 69 |
+
) -> str:
|
| 70 |
+
# sub_idxs = [[0, 1, 2], [3, 4, 5]] # None for single image.
|
| 71 |
+
if sub_idxs is None:
|
| 72 |
+
sub_idxs = [[random.randint(0, 5)]] # 6 views.
|
| 73 |
+
target_hw = [2 * size for size in target_hw]
|
| 74 |
+
|
| 75 |
+
transform_list = [
|
| 76 |
+
transforms.Resize(
|
| 77 |
+
target_hw, interpolation=transforms.InterpolationMode.BILINEAR
|
| 78 |
+
),
|
| 79 |
+
transforms.CenterCrop(target_hw),
|
| 80 |
+
transforms.ToTensor(),
|
| 81 |
+
transforms.Normalize([0.5], [0.5]),
|
| 82 |
+
]
|
| 83 |
+
image_transform = transforms.Compose(transform_list)
|
| 84 |
+
control_transform = transforms.Compose(transform_list[:-1])
|
| 85 |
+
|
| 86 |
+
grid_hw = (target_hw[0] * len(sub_idxs), target_hw[1] * len(sub_idxs[0]))
|
| 87 |
+
dataset = Asset3dGenDataset(
|
| 88 |
+
index_file, target_hw=grid_hw, sub_idxs=sub_idxs
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if uid is None:
|
| 92 |
+
uid = random.choice(list(dataset.meta_info.keys()))
|
| 93 |
+
if prompt is None:
|
| 94 |
+
prompt = dataset.meta_info[uid]["capture"]
|
| 95 |
+
if isinstance(prompt, List) or isinstance(prompt, Tuple):
|
| 96 |
+
prompt = ", ".join(map(str, prompt))
|
| 97 |
+
# prompt += "high quality, ultra-clear, high resolution, best quality, 4k"
|
| 98 |
+
# prompt += "高品质,清晰,细节"
|
| 99 |
+
prompt += ", high quality, high resolution, best quality"
|
| 100 |
+
# prompt += ", with diffuse lighting, showing no reflections."
|
| 101 |
+
logger.info(f"Inference with prompt: {prompt}")
|
| 102 |
+
|
| 103 |
+
negative_prompt = "nsfw,阴影,低分辨率,伪影、模糊,霓虹灯,高光,镜面反射"
|
| 104 |
+
|
| 105 |
+
control_image = dataset.fetch_sample_grid_images(
|
| 106 |
+
uid,
|
| 107 |
+
attrs=["image_view_normal", "image_position", "image_mask"],
|
| 108 |
+
sub_idxs=sub_idxs,
|
| 109 |
+
transform=control_transform,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
color_image = dataset.fetch_sample_grid_images(
|
| 113 |
+
uid,
|
| 114 |
+
attrs=["image_color"],
|
| 115 |
+
sub_idxs=sub_idxs,
|
| 116 |
+
transform=image_transform,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
normal_pil, position_pil, mask_pil, color_pil = dataset.visualize_item(
|
| 120 |
+
control_image,
|
| 121 |
+
color_image,
|
| 122 |
+
save_dir=save_dir,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if pipeline is None:
|
| 126 |
+
pipeline = build_texture_gen_pipe(
|
| 127 |
+
base_ckpt_dir="./weights",
|
| 128 |
+
controlnet_ckpt=controlnet_ckpt,
|
| 129 |
+
ip_adapt_scale=ip_adapt_scale,
|
| 130 |
+
device=device,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
if ip_adapt_scale > 0 and ip_img_path is not None and len(ip_img_path) > 0:
|
| 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([ip_adapt_scale])
|
| 138 |
+
else:
|
| 139 |
+
ip_image = None
|
| 140 |
+
|
| 141 |
+
generator = None
|
| 142 |
+
if seed is not None:
|
| 143 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 144 |
+
torch.manual_seed(seed)
|
| 145 |
+
np.random.seed(seed)
|
| 146 |
+
random.seed(seed)
|
| 147 |
+
|
| 148 |
+
init_image = get_init_noise_image(normal_pil)
|
| 149 |
+
# init_image = get_init_noise_image(color_pil)
|
| 150 |
+
|
| 151 |
+
images = []
|
| 152 |
+
row_num, col_num = 2, 3
|
| 153 |
+
img_save_paths = []
|
| 154 |
+
while len(images) < col_num:
|
| 155 |
+
image = pipeline(
|
| 156 |
+
prompt=prompt,
|
| 157 |
+
image=init_image,
|
| 158 |
+
controlnet_conditioning_scale=controlnet_cond_scale,
|
| 159 |
+
control_guidance_end=control_guidance_end,
|
| 160 |
+
strength=strength,
|
| 161 |
+
control_image=control_image[None, ...],
|
| 162 |
+
negative_prompt=negative_prompt,
|
| 163 |
+
num_inference_steps=num_inference_steps,
|
| 164 |
+
guidance_scale=guidance_scale,
|
| 165 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 166 |
+
ip_adapter_image=ip_image,
|
| 167 |
+
generator=generator,
|
| 168 |
+
).images
|
| 169 |
+
images.extend(image)
|
| 170 |
+
|
| 171 |
+
grid_image = [normal_pil, position_pil, color_pil] + images[:col_num]
|
| 172 |
+
# save_dir = os.path.join(save_dir, uid)
|
| 173 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 174 |
+
|
| 175 |
+
for idx in range(col_num):
|
| 176 |
+
rgba_image = Image.merge("RGBA", (*images[idx].split(), mask_pil))
|
| 177 |
+
img_save_path = os.path.join(save_dir, f"color_sample{idx}.png")
|
| 178 |
+
rgba_image.save(img_save_path)
|
| 179 |
+
img_save_paths.append(img_save_path)
|
| 180 |
+
|
| 181 |
+
sub_idxs = "_".join(
|
| 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{ip_adapt_scale}.jpg"
|
| 186 |
+
)
|
| 187 |
+
make_image_grid(grid_image, row_num, col_num).save(save_path)
|
| 188 |
+
logger.info(f"Visualize in {save_path}")
|
| 189 |
+
|
| 190 |
+
return img_save_paths
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def entrypoint() -> None:
|
| 194 |
+
fire.Fire(infer_pipe)
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
if __name__ == "__main__":
|
| 198 |
+
entrypoint()
|
embodied_gen/scripts/text2image.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256 import (
|
| 23 |
+
StableDiffusionXLPipeline,
|
| 24 |
+
)
|
| 25 |
+
from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256_ipadapter import ( # noqa
|
| 26 |
+
StableDiffusionXLPipeline as StableDiffusionXLPipelineIP,
|
| 27 |
+
)
|
| 28 |
+
from tqdm import tqdm
|
| 29 |
+
from embodied_gen.models.text_model import (
|
| 30 |
+
build_text2img_ip_pipeline,
|
| 31 |
+
build_text2img_pipeline,
|
| 32 |
+
text2img_gen,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
logging.basicConfig(level=logging.INFO)
|
| 36 |
+
logger = logging.getLogger(__name__)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def parse_args():
|
| 40 |
+
parser = argparse.ArgumentParser(description="Text to Image.")
|
| 41 |
+
parser.add_argument(
|
| 42 |
+
"--prompts",
|
| 43 |
+
type=str,
|
| 44 |
+
nargs="+",
|
| 45 |
+
help="List of prompts (space-separated).",
|
| 46 |
+
)
|
| 47 |
+
parser.add_argument(
|
| 48 |
+
"--ref_image",
|
| 49 |
+
type=str,
|
| 50 |
+
nargs="+",
|
| 51 |
+
help="List of ref_image paths (space-separated).",
|
| 52 |
+
)
|
| 53 |
+
parser.add_argument(
|
| 54 |
+
"--output_root",
|
| 55 |
+
type=str,
|
| 56 |
+
help="Root directory for saving outputs.",
|
| 57 |
+
)
|
| 58 |
+
parser.add_argument(
|
| 59 |
+
"--guidance_scale",
|
| 60 |
+
type=float,
|
| 61 |
+
default=12.0,
|
| 62 |
+
help="Guidance scale for the diffusion model.",
|
| 63 |
+
)
|
| 64 |
+
parser.add_argument(
|
| 65 |
+
"--ref_scale",
|
| 66 |
+
type=float,
|
| 67 |
+
default=0.3,
|
| 68 |
+
help="Reference image scale for the IP adapter.",
|
| 69 |
+
)
|
| 70 |
+
parser.add_argument(
|
| 71 |
+
"--n_sample",
|
| 72 |
+
type=int,
|
| 73 |
+
default=1,
|
| 74 |
+
)
|
| 75 |
+
parser.add_argument(
|
| 76 |
+
"--resolution",
|
| 77 |
+
type=int,
|
| 78 |
+
default=1024,
|
| 79 |
+
)
|
| 80 |
+
parser.add_argument(
|
| 81 |
+
"--infer_step",
|
| 82 |
+
type=int,
|
| 83 |
+
default=50,
|
| 84 |
+
)
|
| 85 |
+
args = parser.parse_args()
|
| 86 |
+
|
| 87 |
+
return args
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def entrypoint(
|
| 91 |
+
pipeline: StableDiffusionXLPipeline | StableDiffusionXLPipelineIP = None,
|
| 92 |
+
**kwargs,
|
| 93 |
+
) -> list[str]:
|
| 94 |
+
args = parse_args()
|
| 95 |
+
for k, v in kwargs.items():
|
| 96 |
+
if hasattr(args, k) and v is not None:
|
| 97 |
+
setattr(args, k, v)
|
| 98 |
+
|
| 99 |
+
prompts = args.prompts
|
| 100 |
+
if len(prompts) == 1 and prompts[0].endswith(".txt"):
|
| 101 |
+
with open(prompts[0], "r") as f:
|
| 102 |
+
prompts = f.readlines()
|
| 103 |
+
prompts = [
|
| 104 |
+
prompt.strip() for prompt in prompts if prompt.strip() != ""
|
| 105 |
+
]
|
| 106 |
+
|
| 107 |
+
os.makedirs(args.output_root, exist_ok=True)
|
| 108 |
+
|
| 109 |
+
ip_img_paths = args.ref_image
|
| 110 |
+
if ip_img_paths is None or len(ip_img_paths) == 0:
|
| 111 |
+
args.ref_scale = 0
|
| 112 |
+
ip_img_paths = [None] * len(prompts)
|
| 113 |
+
elif isinstance(ip_img_paths, str):
|
| 114 |
+
ip_img_paths = [ip_img_paths] * len(prompts)
|
| 115 |
+
elif isinstance(ip_img_paths, list):
|
| 116 |
+
if len(ip_img_paths) == 1:
|
| 117 |
+
ip_img_paths = ip_img_paths * len(prompts)
|
| 118 |
+
else:
|
| 119 |
+
raise ValueError("Invalid ref_image paths.")
|
| 120 |
+
assert len(ip_img_paths) == len(
|
| 121 |
+
prompts
|
| 122 |
+
), f"Number of ref images does not match prompts, {len(ip_img_paths)} != {len(prompts)}" # noqa
|
| 123 |
+
|
| 124 |
+
if pipeline is None:
|
| 125 |
+
if args.ref_scale > 0:
|
| 126 |
+
pipeline = build_text2img_ip_pipeline(
|
| 127 |
+
"weights/Kolors",
|
| 128 |
+
ref_scale=args.ref_scale,
|
| 129 |
+
)
|
| 130 |
+
else:
|
| 131 |
+
pipeline = build_text2img_pipeline("weights/Kolors")
|
| 132 |
+
|
| 133 |
+
for idx, (prompt, ip_img_path) in tqdm(
|
| 134 |
+
enumerate(zip(prompts, ip_img_paths)),
|
| 135 |
+
desc="Generating images",
|
| 136 |
+
total=len(prompts),
|
| 137 |
+
):
|
| 138 |
+
images = text2img_gen(
|
| 139 |
+
prompt=prompt,
|
| 140 |
+
n_sample=args.n_sample,
|
| 141 |
+
guidance_scale=args.guidance_scale,
|
| 142 |
+
pipeline=pipeline,
|
| 143 |
+
ip_image=ip_img_path,
|
| 144 |
+
image_wh=[args.resolution, args.resolution],
|
| 145 |
+
infer_step=args.infer_step,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
save_paths = []
|
| 149 |
+
for sub_idx, image in enumerate(images):
|
| 150 |
+
save_path = (
|
| 151 |
+
f"{args.output_root}/sample_{idx*args.n_sample+sub_idx}.png"
|
| 152 |
+
)
|
| 153 |
+
image.save(save_path)
|
| 154 |
+
save_paths.append(save_path)
|
| 155 |
+
|
| 156 |
+
logger.info(f"Images saved to {args.output_root}")
|
| 157 |
+
|
| 158 |
+
return save_paths
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
entrypoint()
|
embodied_gen/scripts/textto3d.sh
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Initialize variables
|
| 4 |
+
prompts=()
|
| 5 |
+
output_root=""
|
| 6 |
+
|
| 7 |
+
# Parse arguments
|
| 8 |
+
while [[ $# -gt 0 ]]; do
|
| 9 |
+
case "$1" in
|
| 10 |
+
--prompts)
|
| 11 |
+
shift
|
| 12 |
+
while [[ $# -gt 0 && ! "$1" =~ ^-- ]]; do
|
| 13 |
+
prompts+=("$1")
|
| 14 |
+
shift
|
| 15 |
+
done
|
| 16 |
+
;;
|
| 17 |
+
--output_root)
|
| 18 |
+
output_root="$2"
|
| 19 |
+
shift 2
|
| 20 |
+
;;
|
| 21 |
+
*)
|
| 22 |
+
echo "Unknown argument: $1"
|
| 23 |
+
exit 1
|
| 24 |
+
;;
|
| 25 |
+
esac
|
| 26 |
+
done
|
| 27 |
+
|
| 28 |
+
# Validate required arguments
|
| 29 |
+
if [[ ${#prompts[@]} -eq 0 || -z "$output_root" ]]; then
|
| 30 |
+
echo "Missing required arguments."
|
| 31 |
+
echo "Usage: bash run_text2asset3d.sh --prompts \"Prompt1\" \"Prompt2\" --output_root <path>"
|
| 32 |
+
exit 1
|
| 33 |
+
fi
|
| 34 |
+
|
| 35 |
+
# Print arguments (for debugging)
|
| 36 |
+
echo "Prompts:"
|
| 37 |
+
for p in "${prompts[@]}"; do
|
| 38 |
+
echo " - $p"
|
| 39 |
+
done
|
| 40 |
+
echo "Output root: ${output_root}"
|
| 41 |
+
|
| 42 |
+
# Concatenate prompts for Python command
|
| 43 |
+
prompt_args=""
|
| 44 |
+
for p in "${prompts[@]}"; do
|
| 45 |
+
prompt_args+="\"$p\" "
|
| 46 |
+
done
|
| 47 |
+
|
| 48 |
+
# Step 1: Text-to-Image
|
| 49 |
+
eval python3 embodied_gen/scripts/text2image.py \
|
| 50 |
+
--prompts ${prompt_args} \
|
| 51 |
+
--output_root "${output_root}/images"
|
| 52 |
+
|
| 53 |
+
# Step 2: Image-to-3D
|
| 54 |
+
python3 embodied_gen/scripts/imageto3d.py \
|
| 55 |
+
--image_root "${output_root}/images" \
|
| 56 |
+
--output_root "${output_root}/asset3d"
|
embodied_gen/scripts/texture_gen.sh
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
while [[ $# -gt 0 ]]; do
|
| 4 |
+
case $1 in
|
| 5 |
+
--mesh_path)
|
| 6 |
+
mesh_path="$2"
|
| 7 |
+
shift 2
|
| 8 |
+
;;
|
| 9 |
+
--prompt)
|
| 10 |
+
prompt="$2"
|
| 11 |
+
shift 2
|
| 12 |
+
;;
|
| 13 |
+
--uuid)
|
| 14 |
+
uuid="$2"
|
| 15 |
+
shift 2
|
| 16 |
+
;;
|
| 17 |
+
--output_root)
|
| 18 |
+
output_root="$2"
|
| 19 |
+
shift 2
|
| 20 |
+
;;
|
| 21 |
+
*)
|
| 22 |
+
echo "unknown: $1"
|
| 23 |
+
exit 1
|
| 24 |
+
;;
|
| 25 |
+
esac
|
| 26 |
+
done
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if [[ -z "$mesh_path" || -z "$prompt" || -z "$uuid" || -z "$output_root" ]]; then
|
| 30 |
+
echo "params missing"
|
| 31 |
+
echo "usage: bash run.sh --mesh_path <path> --prompt <text> --uuid <id> --output_root <path>"
|
| 32 |
+
exit 1
|
| 33 |
+
fi
|
| 34 |
+
|
| 35 |
+
# Step 1: drender-cli for condition rendering
|
| 36 |
+
drender-cli --mesh_path ${mesh_path} \
|
| 37 |
+
--output_root ${output_root}/condition \
|
| 38 |
+
--uuid ${uuid}
|
| 39 |
+
|
| 40 |
+
# Step 2: multi-view rendering
|
| 41 |
+
python embodied_gen/scripts/render_mv.py \
|
| 42 |
+
--index_file "${output_root}/condition/index.json" \
|
| 43 |
+
--controlnet_cond_scale 0.75 \
|
| 44 |
+
--guidance_scale 9 \
|
| 45 |
+
--strength 0.9 \
|
| 46 |
+
--num_inference_steps 40 \
|
| 47 |
+
--ip_adapt_scale 0 \
|
| 48 |
+
--ip_img_path None \
|
| 49 |
+
--uid ${uuid} \
|
| 50 |
+
--prompt "${prompt}" \
|
| 51 |
+
--save_dir "${output_root}/multi_view" \
|
| 52 |
+
--sub_idxs "[[0,1,2],[3,4,5]]" \
|
| 53 |
+
--seed 0
|
| 54 |
+
|
| 55 |
+
# Step 3: backprojection
|
| 56 |
+
backproject-cli --mesh_path ${mesh_path} \
|
| 57 |
+
--color_path ${output_root}/multi_view/color_sample0.png \
|
| 58 |
+
--output_path "${output_root}/texture_mesh/${uuid}.obj" \
|
| 59 |
+
--skip_fix_mesh \
|
| 60 |
+
--delight
|
| 61 |
+
|
| 62 |
+
# Step 4: final rendering of textured mesh
|
| 63 |
+
drender-cli --mesh_path "${output_root}/texture_mesh/${uuid}.obj" \
|
| 64 |
+
--output_root ${output_root}/texture_mesh \
|
| 65 |
+
--num_images 90 \
|
| 66 |
+
--elevation 20 \
|
| 67 |
+
--with_mtl \
|
| 68 |
+
--gen_color_mp4 \
|
| 69 |
+
--pbr_light_factor 1.2
|
embodied_gen/utils/gpt_clients.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import base64
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
from io import BytesIO
|
| 22 |
+
from typing import Optional
|
| 23 |
+
|
| 24 |
+
import yaml
|
| 25 |
+
from openai import AzureOpenAI, OpenAI # pip install openai
|
| 26 |
+
from PIL import Image
|
| 27 |
+
from tenacity import (
|
| 28 |
+
retry,
|
| 29 |
+
stop_after_attempt,
|
| 30 |
+
stop_after_delay,
|
| 31 |
+
wait_random_exponential,
|
| 32 |
+
)
|
| 33 |
+
from embodied_gen.utils.process_media import combine_images_to_base64
|
| 34 |
+
|
| 35 |
+
logging.basicConfig(level=logging.INFO)
|
| 36 |
+
logger = logging.getLogger(__name__)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class GPTclient:
|
| 40 |
+
"""A client to interact with the GPT model via OpenAI or Azure API."""
|
| 41 |
+
|
| 42 |
+
def __init__(
|
| 43 |
+
self,
|
| 44 |
+
endpoint: str,
|
| 45 |
+
api_key: str,
|
| 46 |
+
model_name: str = "yfb-gpt-4o",
|
| 47 |
+
api_version: str = None,
|
| 48 |
+
verbose: bool = False,
|
| 49 |
+
):
|
| 50 |
+
if api_version is not None:
|
| 51 |
+
self.client = AzureOpenAI(
|
| 52 |
+
azure_endpoint=endpoint,
|
| 53 |
+
api_key=api_key,
|
| 54 |
+
api_version=api_version,
|
| 55 |
+
)
|
| 56 |
+
else:
|
| 57 |
+
self.client = OpenAI(
|
| 58 |
+
base_url=endpoint,
|
| 59 |
+
api_key=api_key,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
self.endpoint = endpoint
|
| 63 |
+
self.model_name = model_name
|
| 64 |
+
self.image_formats = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}
|
| 65 |
+
self.verbose = verbose
|
| 66 |
+
logger.info(f"Using GPT model: {self.model_name}.")
|
| 67 |
+
|
| 68 |
+
@retry(
|
| 69 |
+
wait=wait_random_exponential(min=1, max=20),
|
| 70 |
+
stop=(stop_after_attempt(10) | stop_after_delay(30)),
|
| 71 |
+
)
|
| 72 |
+
def completion_with_backoff(self, **kwargs):
|
| 73 |
+
return self.client.chat.completions.create(**kwargs)
|
| 74 |
+
|
| 75 |
+
def query(
|
| 76 |
+
self,
|
| 77 |
+
text_prompt: str,
|
| 78 |
+
image_base64: Optional[list[str | Image.Image]] = None,
|
| 79 |
+
system_role: Optional[str] = None,
|
| 80 |
+
) -> Optional[str]:
|
| 81 |
+
"""Queries the GPT model with a text and optional image prompts.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
text_prompt (str): The main text input that the model responds to.
|
| 85 |
+
image_base64 (Optional[List[str]]): A list of image base64 strings
|
| 86 |
+
or local image paths or PIL.Image to accompany the text prompt.
|
| 87 |
+
system_role (Optional[str]): Optional system-level instructions
|
| 88 |
+
that specify the behavior of the assistant.
|
| 89 |
+
|
| 90 |
+
Returns:
|
| 91 |
+
Optional[str]: The response content generated by the model based on
|
| 92 |
+
the prompt. Returns `None` if an error occurs.
|
| 93 |
+
"""
|
| 94 |
+
if system_role is None:
|
| 95 |
+
system_role = "You are a highly knowledgeable assistant specializing in physics, engineering, and object properties." # noqa
|
| 96 |
+
|
| 97 |
+
content_user = [
|
| 98 |
+
{
|
| 99 |
+
"type": "text",
|
| 100 |
+
"text": text_prompt,
|
| 101 |
+
},
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
# Process images if provided
|
| 105 |
+
if image_base64 is not None:
|
| 106 |
+
image_base64 = (
|
| 107 |
+
image_base64
|
| 108 |
+
if isinstance(image_base64, list)
|
| 109 |
+
else [image_base64]
|
| 110 |
+
)
|
| 111 |
+
for img in image_base64:
|
| 112 |
+
if isinstance(img, Image.Image):
|
| 113 |
+
buffer = BytesIO()
|
| 114 |
+
img.save(buffer, format=img.format or "PNG")
|
| 115 |
+
buffer.seek(0)
|
| 116 |
+
image_binary = buffer.read()
|
| 117 |
+
img = base64.b64encode(image_binary).decode("utf-8")
|
| 118 |
+
elif (
|
| 119 |
+
len(os.path.splitext(img)) > 1
|
| 120 |
+
and os.path.splitext(img)[-1].lower() in self.image_formats
|
| 121 |
+
):
|
| 122 |
+
if not os.path.exists(img):
|
| 123 |
+
raise FileNotFoundError(f"Image file not found: {img}")
|
| 124 |
+
with open(img, "rb") as f:
|
| 125 |
+
img = base64.b64encode(f.read()).decode("utf-8")
|
| 126 |
+
|
| 127 |
+
content_user.append(
|
| 128 |
+
{
|
| 129 |
+
"type": "image_url",
|
| 130 |
+
"image_url": {"url": f"data:image/png;base64,{img}"},
|
| 131 |
+
}
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
payload = {
|
| 135 |
+
"messages": [
|
| 136 |
+
{"role": "system", "content": system_role},
|
| 137 |
+
{"role": "user", "content": content_user},
|
| 138 |
+
],
|
| 139 |
+
"temperature": 0.1,
|
| 140 |
+
"max_tokens": 500,
|
| 141 |
+
"top_p": 0.1,
|
| 142 |
+
"frequency_penalty": 0,
|
| 143 |
+
"presence_penalty": 0,
|
| 144 |
+
"stop": None,
|
| 145 |
+
}
|
| 146 |
+
payload.update({"model": self.model_name})
|
| 147 |
+
|
| 148 |
+
response = None
|
| 149 |
+
try:
|
| 150 |
+
response = self.completion_with_backoff(**payload)
|
| 151 |
+
response = response.choices[0].message.content
|
| 152 |
+
except Exception as e:
|
| 153 |
+
logger.error(f"Error GPTclint {self.endpoint} API call: {e}")
|
| 154 |
+
response = None
|
| 155 |
+
|
| 156 |
+
if self.verbose:
|
| 157 |
+
logger.info(f"Prompt: {text_prompt}")
|
| 158 |
+
logger.info(f"Response: {response}")
|
| 159 |
+
|
| 160 |
+
return response
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
with open("embodied_gen/utils/gpt_config.yaml", "r") as f:
|
| 164 |
+
config = yaml.safe_load(f)
|
| 165 |
+
|
| 166 |
+
agent_type = config["agent_type"]
|
| 167 |
+
agent_config = config.get(agent_type, {})
|
| 168 |
+
|
| 169 |
+
# Prefer environment variables, fallback to YAML config
|
| 170 |
+
endpoint = os.environ.get("ENDPOINT", agent_config.get("endpoint"))
|
| 171 |
+
api_key = os.environ.get("API_KEY", agent_config.get("api_key"))
|
| 172 |
+
api_version = os.environ.get("API_VERSION", agent_config.get("api_version"))
|
| 173 |
+
model_name = os.environ.get("MODEL_NAME", agent_config.get("model_name"))
|
| 174 |
+
|
| 175 |
+
GPT_CLIENT = GPTclient(
|
| 176 |
+
endpoint=endpoint,
|
| 177 |
+
api_key=api_key,
|
| 178 |
+
api_version=api_version,
|
| 179 |
+
model_name=model_name,
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
if "openrouter" in GPT_CLIENT.endpoint:
|
| 184 |
+
response = GPT_CLIENT.query(
|
| 185 |
+
text_prompt="What is the content in each image?",
|
| 186 |
+
image_base64=combine_images_to_base64(
|
| 187 |
+
[
|
| 188 |
+
"outputs/text2image/demo_objects/bed/sample_0.jpg",
|
| 189 |
+
"outputs/imageto3d/v2/cups/sample_69/URDF_sample_69/qa_renders/image_color/003.png", # noqa
|
| 190 |
+
"outputs/text2image/demo_objects/cardboard/sample_1.jpg",
|
| 191 |
+
]
|
| 192 |
+
), # input raw image_path if only one image
|
| 193 |
+
)
|
| 194 |
+
print(response)
|
| 195 |
+
else:
|
| 196 |
+
response = GPT_CLIENT.query(
|
| 197 |
+
text_prompt="What is the content in the images?",
|
| 198 |
+
image_base64=[
|
| 199 |
+
Image.open("outputs/text2image/demo_objects/bed/sample_0.jpg"),
|
| 200 |
+
Image.open(
|
| 201 |
+
"outputs/imageto3d/v2/cups/sample_69/URDF_sample_69/qa_renders/image_color/003.png" # noqa
|
| 202 |
+
),
|
| 203 |
+
],
|
| 204 |
+
)
|
| 205 |
+
print(response)
|
| 206 |
+
|
| 207 |
+
# test2: text prompt
|
| 208 |
+
response = GPT_CLIENT.query(
|
| 209 |
+
text_prompt="What is the capital of China?"
|
| 210 |
+
)
|
| 211 |
+
print(response)
|
embodied_gen/utils/gpt_config.yaml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# config.yaml
|
| 2 |
+
agent_type: "qwen2.5-vl" # gpt-4o or qwen2.5-vl
|
| 3 |
+
|
| 4 |
+
gpt-4o:
|
| 5 |
+
endpoint: https://xxx.openai.azure.com
|
| 6 |
+
api_key: xxx
|
| 7 |
+
api_version: 2025-xx-xx
|
| 8 |
+
model_name: yfb-gpt-4o
|
| 9 |
+
|
| 10 |
+
qwen2.5-vl:
|
| 11 |
+
endpoint: https://openrouter.ai/api/v1
|
| 12 |
+
api_key: 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
|
| 13 |
+
api_version: null
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|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import base64
|
| 19 |
+
import logging
|
| 20 |
+
import math
|
| 21 |
+
import os
|
| 22 |
+
import subprocess
|
| 23 |
+
import sys
|
| 24 |
+
from glob import glob
|
| 25 |
+
from io import BytesIO
|
| 26 |
+
from typing import Union
|
| 27 |
+
|
| 28 |
+
import cv2
|
| 29 |
+
import imageio
|
| 30 |
+
import numpy as np
|
| 31 |
+
import PIL.Image as Image
|
| 32 |
+
import spaces
|
| 33 |
+
import torch
|
| 34 |
+
from moviepy.editor import VideoFileClip, clips_array
|
| 35 |
+
from tqdm import tqdm
|
| 36 |
+
|
| 37 |
+
current_file_path = os.path.abspath(__file__)
|
| 38 |
+
current_dir = os.path.dirname(current_file_path)
|
| 39 |
+
sys.path.append(os.path.join(current_dir, "../.."))
|
| 40 |
+
from thirdparty.TRELLIS.trellis.renderers.mesh_renderer import MeshRenderer
|
| 41 |
+
from thirdparty.TRELLIS.trellis.representations import MeshExtractResult
|
| 42 |
+
from thirdparty.TRELLIS.trellis.utils.render_utils import (
|
| 43 |
+
render_frames,
|
| 44 |
+
yaw_pitch_r_fov_to_extrinsics_intrinsics,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
logging.basicConfig(level=logging.INFO)
|
| 48 |
+
logger = logging.getLogger(__name__)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
__all__ = [
|
| 52 |
+
"render_asset3d",
|
| 53 |
+
"merge_images_video",
|
| 54 |
+
"filter_small_connected_components",
|
| 55 |
+
"filter_image_small_connected_components",
|
| 56 |
+
"combine_images_to_base64",
|
| 57 |
+
"render_mesh",
|
| 58 |
+
"render_video",
|
| 59 |
+
"create_mp4_from_images",
|
| 60 |
+
"create_gif_from_images",
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@spaces.GPU
|
| 65 |
+
def render_asset3d(
|
| 66 |
+
mesh_path: str,
|
| 67 |
+
output_root: str,
|
| 68 |
+
distance: float = 5.0,
|
| 69 |
+
num_images: int = 1,
|
| 70 |
+
elevation: list[float] = (0.0,),
|
| 71 |
+
pbr_light_factor: float = 1.5,
|
| 72 |
+
return_key: str = "image_color/*",
|
| 73 |
+
output_subdir: str = "renders",
|
| 74 |
+
gen_color_mp4: bool = False,
|
| 75 |
+
gen_viewnormal_mp4: bool = False,
|
| 76 |
+
gen_glonormal_mp4: bool = False,
|
| 77 |
+
) -> list[str]:
|
| 78 |
+
command = [
|
| 79 |
+
"python3",
|
| 80 |
+
"embodied_gen/data/differentiable_render.py",
|
| 81 |
+
"--mesh_path",
|
| 82 |
+
mesh_path,
|
| 83 |
+
"--output_root",
|
| 84 |
+
output_root,
|
| 85 |
+
"--uuid",
|
| 86 |
+
output_subdir,
|
| 87 |
+
"--distance",
|
| 88 |
+
str(distance),
|
| 89 |
+
"--num_images",
|
| 90 |
+
str(num_images),
|
| 91 |
+
"--elevation",
|
| 92 |
+
*map(str, elevation),
|
| 93 |
+
"--pbr_light_factor",
|
| 94 |
+
str(pbr_light_factor),
|
| 95 |
+
"--with_mtl",
|
| 96 |
+
]
|
| 97 |
+
if gen_color_mp4:
|
| 98 |
+
command.append("--gen_color_mp4")
|
| 99 |
+
if gen_viewnormal_mp4:
|
| 100 |
+
command.append("--gen_viewnormal_mp4")
|
| 101 |
+
if gen_glonormal_mp4:
|
| 102 |
+
command.append("--gen_glonormal_mp4")
|
| 103 |
+
try:
|
| 104 |
+
subprocess.run(command, check=True)
|
| 105 |
+
except subprocess.CalledProcessError as e:
|
| 106 |
+
logger.error(f"Error occurred during rendering: {e}.")
|
| 107 |
+
|
| 108 |
+
dst_paths = glob(os.path.join(output_root, output_subdir, return_key))
|
| 109 |
+
|
| 110 |
+
return dst_paths
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def merge_images_video(color_images, normal_images, output_path) -> None:
|
| 114 |
+
width = color_images[0].shape[1]
|
| 115 |
+
combined_video = [
|
| 116 |
+
np.hstack([rgb_img[:, : width // 2], normal_img[:, width // 2 :]])
|
| 117 |
+
for rgb_img, normal_img in zip(color_images, normal_images)
|
| 118 |
+
]
|
| 119 |
+
imageio.mimsave(output_path, combined_video, fps=50)
|
| 120 |
+
|
| 121 |
+
return
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def merge_video_video(
|
| 125 |
+
video_path1: str, video_path2: str, output_path: str
|
| 126 |
+
) -> None:
|
| 127 |
+
"""Merge two videos by the left half and the right half of the videos."""
|
| 128 |
+
clip1 = VideoFileClip(video_path1)
|
| 129 |
+
clip2 = VideoFileClip(video_path2)
|
| 130 |
+
|
| 131 |
+
if clip1.size != clip2.size:
|
| 132 |
+
raise ValueError("The resolutions of the two videos do not match.")
|
| 133 |
+
|
| 134 |
+
width, height = clip1.size
|
| 135 |
+
clip1_half = clip1.crop(x1=0, y1=0, x2=width // 2, y2=height)
|
| 136 |
+
clip2_half = clip2.crop(x1=width // 2, y1=0, x2=width, y2=height)
|
| 137 |
+
final_clip = clips_array([[clip1_half, clip2_half]])
|
| 138 |
+
final_clip.write_videofile(output_path, codec="libx264")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def filter_small_connected_components(
|
| 142 |
+
mask: Union[Image.Image, np.ndarray],
|
| 143 |
+
area_ratio: float,
|
| 144 |
+
connectivity: int = 8,
|
| 145 |
+
) -> np.ndarray:
|
| 146 |
+
if isinstance(mask, Image.Image):
|
| 147 |
+
mask = np.array(mask)
|
| 148 |
+
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(
|
| 149 |
+
mask,
|
| 150 |
+
connectivity=connectivity,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
small_components = np.zeros_like(mask, dtype=np.uint8)
|
| 154 |
+
mask_area = (mask != 0).sum()
|
| 155 |
+
min_area = mask_area // area_ratio
|
| 156 |
+
for label in range(1, num_labels):
|
| 157 |
+
area = stats[label, cv2.CC_STAT_AREA]
|
| 158 |
+
if area < min_area:
|
| 159 |
+
small_components[labels == label] = 255
|
| 160 |
+
|
| 161 |
+
mask = cv2.bitwise_and(mask, cv2.bitwise_not(small_components))
|
| 162 |
+
|
| 163 |
+
return mask
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def filter_image_small_connected_components(
|
| 167 |
+
image: Union[Image.Image, np.ndarray],
|
| 168 |
+
area_ratio: float = 10,
|
| 169 |
+
connectivity: int = 8,
|
| 170 |
+
) -> np.ndarray:
|
| 171 |
+
if isinstance(image, Image.Image):
|
| 172 |
+
image = image.convert("RGBA")
|
| 173 |
+
image = np.array(image)
|
| 174 |
+
|
| 175 |
+
mask = image[..., 3]
|
| 176 |
+
mask = filter_small_connected_components(mask, area_ratio, connectivity)
|
| 177 |
+
image[..., 3] = mask
|
| 178 |
+
|
| 179 |
+
return image
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def combine_images_to_base64(
|
| 183 |
+
images: list[str | Image.Image],
|
| 184 |
+
cat_row_col: tuple[int, int] = None,
|
| 185 |
+
target_wh: tuple[int, int] = (512, 512),
|
| 186 |
+
) -> str:
|
| 187 |
+
n_images = len(images)
|
| 188 |
+
if cat_row_col is None:
|
| 189 |
+
n_col = math.ceil(math.sqrt(n_images))
|
| 190 |
+
n_row = math.ceil(n_images / n_col)
|
| 191 |
+
else:
|
| 192 |
+
n_row, n_col = cat_row_col
|
| 193 |
+
|
| 194 |
+
images = [
|
| 195 |
+
Image.open(p).convert("RGB") if isinstance(p, str) else p
|
| 196 |
+
for p in images[: n_row * n_col]
|
| 197 |
+
]
|
| 198 |
+
images = [img.resize(target_wh) for img in images]
|
| 199 |
+
|
| 200 |
+
grid_w, grid_h = n_col * target_wh[0], n_row * target_wh[1]
|
| 201 |
+
grid = Image.new("RGB", (grid_w, grid_h), (255, 255, 255))
|
| 202 |
+
|
| 203 |
+
for idx, img in enumerate(images):
|
| 204 |
+
row, col = divmod(idx, n_col)
|
| 205 |
+
grid.paste(img, (col * target_wh[0], row * target_wh[1]))
|
| 206 |
+
|
| 207 |
+
buffer = BytesIO()
|
| 208 |
+
grid.save(buffer, format="PNG")
|
| 209 |
+
|
| 210 |
+
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@spaces.GPU
|
| 214 |
+
def render_mesh(sample, extrinsics, intrinsics, options={}, **kwargs):
|
| 215 |
+
renderer = MeshRenderer()
|
| 216 |
+
renderer.rendering_options.resolution = options.get("resolution", 512)
|
| 217 |
+
renderer.rendering_options.near = options.get("near", 1)
|
| 218 |
+
renderer.rendering_options.far = options.get("far", 100)
|
| 219 |
+
renderer.rendering_options.ssaa = options.get("ssaa", 4)
|
| 220 |
+
rets = {}
|
| 221 |
+
for extr, intr in tqdm(zip(extrinsics, intrinsics), desc="Rendering"):
|
| 222 |
+
res = renderer.render(sample, extr, intr)
|
| 223 |
+
if "normal" not in rets:
|
| 224 |
+
rets["normal"] = []
|
| 225 |
+
normal = torch.lerp(
|
| 226 |
+
torch.zeros_like(res["normal"]), res["normal"], res["mask"]
|
| 227 |
+
)
|
| 228 |
+
normal = np.clip(
|
| 229 |
+
normal.detach().cpu().numpy().transpose(1, 2, 0) * 255, 0, 255
|
| 230 |
+
).astype(np.uint8)
|
| 231 |
+
rets["normal"].append(normal)
|
| 232 |
+
|
| 233 |
+
return rets
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
@spaces.GPU
|
| 237 |
+
def render_video(
|
| 238 |
+
sample,
|
| 239 |
+
resolution=512,
|
| 240 |
+
bg_color=(0, 0, 0),
|
| 241 |
+
num_frames=300,
|
| 242 |
+
r=2,
|
| 243 |
+
fov=40,
|
| 244 |
+
**kwargs,
|
| 245 |
+
):
|
| 246 |
+
yaws = torch.linspace(0, 2 * 3.1415, num_frames)
|
| 247 |
+
yaws = yaws.tolist()
|
| 248 |
+
pitch = [0.5] * num_frames
|
| 249 |
+
extrinsics, intrinsics = yaw_pitch_r_fov_to_extrinsics_intrinsics(
|
| 250 |
+
yaws, pitch, r, fov
|
| 251 |
+
)
|
| 252 |
+
render_fn = (
|
| 253 |
+
render_mesh if isinstance(sample, MeshExtractResult) else render_frames
|
| 254 |
+
)
|
| 255 |
+
result = render_fn(
|
| 256 |
+
sample,
|
| 257 |
+
extrinsics,
|
| 258 |
+
intrinsics,
|
| 259 |
+
{"resolution": resolution, "bg_color": bg_color},
|
| 260 |
+
**kwargs,
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
return result
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def create_mp4_from_images(images, output_path, fps=10, prompt=None):
|
| 267 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 268 |
+
font_scale = 0.5
|
| 269 |
+
font_thickness = 1
|
| 270 |
+
color = (255, 255, 255)
|
| 271 |
+
position = (20, 25)
|
| 272 |
+
|
| 273 |
+
with imageio.get_writer(output_path, fps=fps) as writer:
|
| 274 |
+
for image in images:
|
| 275 |
+
image = image.clip(min=0, max=1)
|
| 276 |
+
image = (255.0 * image).astype(np.uint8)
|
| 277 |
+
image = image[..., :3]
|
| 278 |
+
if prompt is not None:
|
| 279 |
+
cv2.putText(
|
| 280 |
+
image,
|
| 281 |
+
prompt,
|
| 282 |
+
position,
|
| 283 |
+
font,
|
| 284 |
+
font_scale,
|
| 285 |
+
color,
|
| 286 |
+
font_thickness,
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
writer.append_data(image)
|
| 290 |
+
|
| 291 |
+
logger.info(f"MP4 video saved to {output_path}")
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def create_gif_from_images(images, output_path, fps=10):
|
| 295 |
+
pil_images = []
|
| 296 |
+
for image in images:
|
| 297 |
+
image = image.clip(min=0, max=1)
|
| 298 |
+
image = (255.0 * image).astype(np.uint8)
|
| 299 |
+
image = Image.fromarray(image, mode="RGBA")
|
| 300 |
+
pil_images.append(image.convert("RGB"))
|
| 301 |
+
|
| 302 |
+
duration = 1000 // fps
|
| 303 |
+
pil_images[0].save(
|
| 304 |
+
output_path,
|
| 305 |
+
save_all=True,
|
| 306 |
+
append_images=pil_images[1:],
|
| 307 |
+
duration=duration,
|
| 308 |
+
loop=0,
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
logger.info(f"GIF saved to {output_path}")
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
if __name__ == "__main__":
|
| 315 |
+
# Example usage:
|
| 316 |
+
merge_video_video(
|
| 317 |
+
"outputs/imageto3d/room_bottle7/room_bottle_007/URDF_room_bottle_007/mesh_glo_normal.mp4", # noqa
|
| 318 |
+
"outputs/imageto3d/room_bottle7/room_bottle_007/URDF_room_bottle_007/mesh.mp4", # noqa
|
| 319 |
+
"merge.mp4",
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
image_base64 = combine_images_to_base64(
|
| 323 |
+
[
|
| 324 |
+
"apps/assets/example_image/sample_00.jpg",
|
| 325 |
+
"apps/assets/example_image/sample_01.jpg",
|
| 326 |
+
"apps/assets/example_image/sample_02.jpg",
|
| 327 |
+
]
|
| 328 |
+
)
|
embodied_gen/utils/tags.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
VERSION = "v0.1.0"
|
embodied_gen/validators/aesthetic_predictor.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
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file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
import clip
|
| 21 |
+
import numpy as np
|
| 22 |
+
import pytorch_lightning as pl
|
| 23 |
+
import torch
|
| 24 |
+
import torch.nn as nn
|
| 25 |
+
from huggingface_hub import snapshot_download
|
| 26 |
+
from PIL import Image
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class AestheticPredictor:
|
| 30 |
+
"""Aesthetic Score Predictor.
|
| 31 |
+
|
| 32 |
+
Checkpoints from https://github.com/christophschuhmann/improved-aesthetic-predictor/tree/main
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
clip_model_dir (str): Path to the directory of the CLIP model.
|
| 36 |
+
sac_model_path (str): Path to the pre-trained SAC model.
|
| 37 |
+
device (str): Device to use for computation ("cuda" or "cpu").
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def __init__(self, clip_model_dir=None, sac_model_path=None, device="cpu"):
|
| 41 |
+
|
| 42 |
+
self.device = device
|
| 43 |
+
|
| 44 |
+
if clip_model_dir is None:
|
| 45 |
+
model_path = snapshot_download(
|
| 46 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns="aesthetic/*"
|
| 47 |
+
)
|
| 48 |
+
suffix = "aesthetic"
|
| 49 |
+
model_path = snapshot_download(
|
| 50 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 51 |
+
)
|
| 52 |
+
clip_model_dir = os.path.join(model_path, suffix)
|
| 53 |
+
|
| 54 |
+
if sac_model_path is None:
|
| 55 |
+
model_path = snapshot_download(
|
| 56 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns="aesthetic/*"
|
| 57 |
+
)
|
| 58 |
+
suffix = "aesthetic"
|
| 59 |
+
model_path = snapshot_download(
|
| 60 |
+
repo_id="xinjjj/RoboAssetGen", allow_patterns=f"{suffix}/*"
|
| 61 |
+
)
|
| 62 |
+
sac_model_path = os.path.join(
|
| 63 |
+
model_path, suffix, "sac+logos+ava1-l14-linearMSE.pth"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
self.clip_model, self.preprocess = self._load_clip_model(
|
| 67 |
+
clip_model_dir
|
| 68 |
+
)
|
| 69 |
+
self.sac_model = self._load_sac_model(sac_model_path, input_size=768)
|
| 70 |
+
|
| 71 |
+
class MLP(pl.LightningModule): # noqa
|
| 72 |
+
def __init__(self, input_size):
|
| 73 |
+
super().__init__()
|
| 74 |
+
self.layers = nn.Sequential(
|
| 75 |
+
nn.Linear(input_size, 1024),
|
| 76 |
+
nn.Dropout(0.2),
|
| 77 |
+
nn.Linear(1024, 128),
|
| 78 |
+
nn.Dropout(0.2),
|
| 79 |
+
nn.Linear(128, 64),
|
| 80 |
+
nn.Dropout(0.1),
|
| 81 |
+
nn.Linear(64, 16),
|
| 82 |
+
nn.Linear(16, 1),
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def forward(self, x):
|
| 86 |
+
return self.layers(x)
|
| 87 |
+
|
| 88 |
+
@staticmethod
|
| 89 |
+
def normalized(a, axis=-1, order=2):
|
| 90 |
+
"""Normalize the array to unit norm."""
|
| 91 |
+
l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
|
| 92 |
+
l2[l2 == 0] = 1
|
| 93 |
+
return a / np.expand_dims(l2, axis)
|
| 94 |
+
|
| 95 |
+
def _load_clip_model(self, model_dir: str, model_name: str = "ViT-L/14"):
|
| 96 |
+
"""Load the CLIP model."""
|
| 97 |
+
model, preprocess = clip.load(
|
| 98 |
+
model_name, download_root=model_dir, device=self.device
|
| 99 |
+
)
|
| 100 |
+
return model, preprocess
|
| 101 |
+
|
| 102 |
+
def _load_sac_model(self, model_path, input_size):
|
| 103 |
+
"""Load the SAC model."""
|
| 104 |
+
model = self.MLP(input_size)
|
| 105 |
+
ckpt = torch.load(model_path)
|
| 106 |
+
model.load_state_dict(ckpt)
|
| 107 |
+
model.to(self.device)
|
| 108 |
+
model.eval()
|
| 109 |
+
return model
|
| 110 |
+
|
| 111 |
+
def predict(self, image_path):
|
| 112 |
+
"""Predict the aesthetic score for a given image.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
image_path (str): Path to the image file.
|
| 116 |
+
|
| 117 |
+
Returns:
|
| 118 |
+
float: Predicted aesthetic score.
|
| 119 |
+
"""
|
| 120 |
+
pil_image = Image.open(image_path)
|
| 121 |
+
image = self.preprocess(pil_image).unsqueeze(0).to(self.device)
|
| 122 |
+
|
| 123 |
+
with torch.no_grad():
|
| 124 |
+
# Extract CLIP features
|
| 125 |
+
image_features = self.clip_model.encode_image(image)
|
| 126 |
+
# Normalize features
|
| 127 |
+
normalized_features = self.normalized(
|
| 128 |
+
image_features.cpu().detach().numpy()
|
| 129 |
+
)
|
| 130 |
+
# Predict score
|
| 131 |
+
prediction = self.sac_model(
|
| 132 |
+
torch.from_numpy(normalized_features)
|
| 133 |
+
.type(torch.FloatTensor)
|
| 134 |
+
.to(self.device)
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
return prediction.item()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
# Configuration
|
| 142 |
+
img_path = "apps/assets/example_image/sample_00.jpg"
|
| 143 |
+
|
| 144 |
+
# Initialize the predictor
|
| 145 |
+
predictor = AestheticPredictor()
|
| 146 |
+
|
| 147 |
+
# Predict the aesthetic score
|
| 148 |
+
score = predictor.predict(img_path)
|
| 149 |
+
print("Aesthetic score predicted by the model:", score)
|
embodied_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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
from tqdm import tqdm
|
| 22 |
+
from embodied_gen.utils.gpt_clients import GPT_CLIENT, GPTclient
|
| 23 |
+
from embodied_gen.utils.process_media import render_asset3d
|
| 24 |
+
from embodied_gen.validators.aesthetic_predictor import AestheticPredictor
|
| 25 |
+
|
| 26 |
+
logging.basicConfig(level=logging.INFO)
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class BaseChecker:
|
| 31 |
+
def __init__(self, prompt: str = None, verbose: bool = False) -> None:
|
| 32 |
+
self.prompt = prompt
|
| 33 |
+
self.verbose = verbose
|
| 34 |
+
|
| 35 |
+
def query(self, *args, **kwargs):
|
| 36 |
+
raise NotImplementedError(
|
| 37 |
+
"Subclasses must implement the query method."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def __call__(self, *args, **kwargs) -> bool:
|
| 41 |
+
response = self.query(*args, **kwargs)
|
| 42 |
+
if response is None:
|
| 43 |
+
response = "Error when calling gpt api."
|
| 44 |
+
|
| 45 |
+
if self.verbose and response != "YES":
|
| 46 |
+
logger.info(response)
|
| 47 |
+
|
| 48 |
+
flag = "YES" in response
|
| 49 |
+
response = "YES" if flag else response
|
| 50 |
+
|
| 51 |
+
return flag, response
|
| 52 |
+
|
| 53 |
+
@staticmethod
|
| 54 |
+
def validate(
|
| 55 |
+
checkers: list["BaseChecker"], images_list: list[list[str]]
|
| 56 |
+
) -> list:
|
| 57 |
+
assert len(checkers) == len(images_list)
|
| 58 |
+
results = []
|
| 59 |
+
overall_result = True
|
| 60 |
+
for checker, images in zip(checkers, images_list):
|
| 61 |
+
qa_flag, qa_info = checker(images)
|
| 62 |
+
if isinstance(qa_info, str):
|
| 63 |
+
qa_info = qa_info.replace("\n", ".")
|
| 64 |
+
results.append([checker.__class__.__name__, qa_info])
|
| 65 |
+
if qa_flag is False:
|
| 66 |
+
overall_result = False
|
| 67 |
+
|
| 68 |
+
results.append(["overall", "YES" if overall_result else "NO"])
|
| 69 |
+
|
| 70 |
+
return results
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class MeshGeoChecker(BaseChecker):
|
| 74 |
+
"""A geometry quality checker for 3D mesh assets using GPT-based reasoning.
|
| 75 |
+
|
| 76 |
+
This class leverages a multi-modal GPT client to analyze rendered images
|
| 77 |
+
of a 3D object and determine if its geometry is complete.
|
| 78 |
+
|
| 79 |
+
Attributes:
|
| 80 |
+
gpt_client (GPTclient): The GPT client used for multi-modal querying.
|
| 81 |
+
prompt (str): The prompt sent to the GPT model. If not provided, a default one is used.
|
| 82 |
+
verbose (bool): Whether to print debug information during evaluation.
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
def __init__(
|
| 86 |
+
self,
|
| 87 |
+
gpt_client: GPTclient,
|
| 88 |
+
prompt: str = None,
|
| 89 |
+
verbose: bool = False,
|
| 90 |
+
) -> None:
|
| 91 |
+
super().__init__(prompt, verbose)
|
| 92 |
+
self.gpt_client = gpt_client
|
| 93 |
+
if self.prompt is None:
|
| 94 |
+
self.prompt = """
|
| 95 |
+
Refer to the provided multi-view rendering images to evaluate
|
| 96 |
+
whether the geometry of the 3D object asset is complete and
|
| 97 |
+
whether the asset can be placed stably on the ground.
|
| 98 |
+
Return "YES" only if reach the requirments,
|
| 99 |
+
otherwise "NO" and explain the reason very briefly.
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
def query(self, image_paths: str) -> str:
|
| 103 |
+
# Hardcode tmp because of the openrouter can't input multi images.
|
| 104 |
+
if "openrouter" in self.gpt_client.endpoint:
|
| 105 |
+
from embodied_gen.utils.process_media import (
|
| 106 |
+
combine_images_to_base64,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
image_paths = combine_images_to_base64(image_paths)
|
| 110 |
+
|
| 111 |
+
return self.gpt_client.query(
|
| 112 |
+
text_prompt=self.prompt,
|
| 113 |
+
image_base64=image_paths,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class ImageSegChecker(BaseChecker):
|
| 118 |
+
"""A segmentation quality checker for 3D assets using GPT-based reasoning.
|
| 119 |
+
|
| 120 |
+
This class compares an original image with its segmented version to
|
| 121 |
+
evaluate whether the segmentation successfully isolates the main object
|
| 122 |
+
with minimal truncation and correct foreground extraction.
|
| 123 |
+
|
| 124 |
+
Attributes:
|
| 125 |
+
gpt_client (GPTclient): GPT client used for multi-modal image analysis.
|
| 126 |
+
prompt (str): The prompt used to guide the GPT model for evaluation.
|
| 127 |
+
verbose (bool): Whether to enable verbose logging.
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
def __init__(
|
| 131 |
+
self,
|
| 132 |
+
gpt_client: GPTclient,
|
| 133 |
+
prompt: str = None,
|
| 134 |
+
verbose: bool = False,
|
| 135 |
+
) -> None:
|
| 136 |
+
super().__init__(prompt, verbose)
|
| 137 |
+
self.gpt_client = gpt_client
|
| 138 |
+
if self.prompt is None:
|
| 139 |
+
self.prompt = """
|
| 140 |
+
The first image is the original, and the second image is the
|
| 141 |
+
result after segmenting the main object. Evaluate the segmentation
|
| 142 |
+
quality to ensure the main object is clearly segmented without
|
| 143 |
+
significant truncation. Note that the foreground of the object
|
| 144 |
+
needs to be extracted instead of the background.
|
| 145 |
+
Minor imperfections can be ignored. If segmentation is acceptable,
|
| 146 |
+
return "YES" only; otherwise, return "NO" with
|
| 147 |
+
very brief explanation.
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
def query(self, image_paths: list[str]) -> str:
|
| 151 |
+
if len(image_paths) != 2:
|
| 152 |
+
raise ValueError(
|
| 153 |
+
"ImageSegChecker requires exactly two images: [raw_image, seg_image]." # noqa
|
| 154 |
+
)
|
| 155 |
+
# Hardcode tmp because of the openrouter can't input multi images.
|
| 156 |
+
if "openrouter" in self.gpt_client.endpoint:
|
| 157 |
+
from embodied_gen.utils.process_media import (
|
| 158 |
+
combine_images_to_base64,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
image_paths = combine_images_to_base64(image_paths)
|
| 162 |
+
|
| 163 |
+
return self.gpt_client.query(
|
| 164 |
+
text_prompt=self.prompt,
|
| 165 |
+
image_base64=image_paths,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
class ImageAestheticChecker(BaseChecker):
|
| 170 |
+
"""A class for evaluating the aesthetic quality of images.
|
| 171 |
+
|
| 172 |
+
Attributes:
|
| 173 |
+
clip_model_dir (str): Path to the CLIP model directory.
|
| 174 |
+
sac_model_path (str): Path to the aesthetic predictor model weights.
|
| 175 |
+
thresh (float): Threshold above which images are considered aesthetically acceptable.
|
| 176 |
+
verbose (bool): Whether to print detailed log messages.
|
| 177 |
+
predictor (AestheticPredictor): The model used to predict aesthetic scores.
|
| 178 |
+
"""
|
| 179 |
+
|
| 180 |
+
def __init__(
|
| 181 |
+
self,
|
| 182 |
+
clip_model_dir: str = None,
|
| 183 |
+
sac_model_path: str = None,
|
| 184 |
+
thresh: float = 4.50,
|
| 185 |
+
verbose: bool = False,
|
| 186 |
+
) -> None:
|
| 187 |
+
super().__init__(verbose=verbose)
|
| 188 |
+
self.clip_model_dir = clip_model_dir
|
| 189 |
+
self.sac_model_path = sac_model_path
|
| 190 |
+
self.thresh = thresh
|
| 191 |
+
self.predictor = AestheticPredictor(clip_model_dir, sac_model_path)
|
| 192 |
+
|
| 193 |
+
def query(self, image_paths: list[str]) -> float:
|
| 194 |
+
scores = [self.predictor.predict(img_path) for img_path in image_paths]
|
| 195 |
+
return sum(scores) / len(scores)
|
| 196 |
+
|
| 197 |
+
def __call__(self, image_paths: list[str], **kwargs) -> bool:
|
| 198 |
+
avg_score = self.query(image_paths)
|
| 199 |
+
if self.verbose:
|
| 200 |
+
logger.info(f"Average aesthetic score: {avg_score}")
|
| 201 |
+
return avg_score > self.thresh, avg_score
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
geo_checker = MeshGeoChecker(GPT_CLIENT)
|
| 206 |
+
seg_checker = ImageSegChecker(GPT_CLIENT)
|
| 207 |
+
aesthetic_checker = ImageAestheticChecker()
|
| 208 |
+
|
| 209 |
+
checkers = [geo_checker, seg_checker, aesthetic_checker]
|
| 210 |
+
|
| 211 |
+
output_root = "outputs/test_gpt"
|
| 212 |
+
|
| 213 |
+
fails = []
|
| 214 |
+
for idx in tqdm(range(150)):
|
| 215 |
+
mesh_path = f"outputs/imageto3d/demo_objects/cups/sample_{idx}/sample_{idx}.obj" # noqa
|
| 216 |
+
if not os.path.exists(mesh_path):
|
| 217 |
+
continue
|
| 218 |
+
image_paths = render_asset3d(
|
| 219 |
+
mesh_path,
|
| 220 |
+
f"{output_root}/{idx}",
|
| 221 |
+
num_images=8,
|
| 222 |
+
elevation=(30, -30),
|
| 223 |
+
distance=5.5,
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
for cid, checker in enumerate(checkers):
|
| 227 |
+
if isinstance(checker, ImageSegChecker):
|
| 228 |
+
images = [
|
| 229 |
+
f"outputs/imageto3d/demo_objects/cups/sample_{idx}/sample_{idx}_raw.png", # noqa
|
| 230 |
+
f"outputs/imageto3d/demo_objects/cups/sample_{idx}/sample_{idx}_cond.png", # noqa
|
| 231 |
+
]
|
| 232 |
+
else:
|
| 233 |
+
images = image_paths
|
| 234 |
+
result, info = checker(images)
|
| 235 |
+
logger.info(
|
| 236 |
+
f"Checker {checker.__class__.__name__}: {result}, {info}, mesh {mesh_path}" # noqa
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
if result is False:
|
| 240 |
+
fails.append((idx, cid, info))
|
| 241 |
+
|
| 242 |
+
break
|
embodied_gen/validators/urdf_convertor.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project EmbodiedGen
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2025 Horizon Robotics. All Rights Reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 14 |
+
# implied. See the License for the specific language governing
|
| 15 |
+
# permissions and limitations under the License.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import shutil
|
| 21 |
+
import xml.etree.ElementTree as ET
|
| 22 |
+
from datetime import datetime
|
| 23 |
+
from xml.dom.minidom import parseString
|
| 24 |
+
|
| 25 |
+
import numpy as np
|
| 26 |
+
import trimesh
|
| 27 |
+
from embodied_gen.data.utils import zip_files
|
| 28 |
+
from embodied_gen.utils.gpt_clients import GPT_CLIENT, GPTclient
|
| 29 |
+
from embodied_gen.utils.process_media import render_asset3d
|
| 30 |
+
from embodied_gen.utils.tags import VERSION
|
| 31 |
+
|
| 32 |
+
logging.basicConfig(level=logging.INFO)
|
| 33 |
+
logger = logging.getLogger(__name__)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
__all__ = ["URDFGenerator"]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
URDF_TEMPLATE = """
|
| 40 |
+
<robot name="template_robot">
|
| 41 |
+
<link name="template_link">
|
| 42 |
+
<visual>
|
| 43 |
+
<geometry>
|
| 44 |
+
<mesh filename="mesh.obj" scale="1.0 1.0 1.0"/>
|
| 45 |
+
</geometry>
|
| 46 |
+
</visual>
|
| 47 |
+
<collision>
|
| 48 |
+
<geometry>
|
| 49 |
+
<mesh filename="mesh.obj" scale="1.0 1.0 1.0"/>
|
| 50 |
+
</geometry>
|
| 51 |
+
<gazebo>
|
| 52 |
+
<mu1>0.8</mu1> <!-- Main friction coefficient -->
|
| 53 |
+
<mu2>0.6</mu2> <!-- Secondary friction coefficient -->
|
| 54 |
+
</gazebo>
|
| 55 |
+
</collision>
|
| 56 |
+
<inertial>
|
| 57 |
+
<mass value="1.0"/>
|
| 58 |
+
<origin xyz="0 0 0"/>
|
| 59 |
+
<inertia ixx="1.0" ixy="0.0" ixz="0.0" iyy="1.0" iyz="0.0" izz="1.0"/>
|
| 60 |
+
</inertial>
|
| 61 |
+
<extra_info>
|
| 62 |
+
<scale>1.0</scale>
|
| 63 |
+
<version>"0.0.0"</version>
|
| 64 |
+
<category>"unknown"</category>
|
| 65 |
+
<description>"unknown"</description>
|
| 66 |
+
<min_height>0.0</min_height>
|
| 67 |
+
<max_height>0.0</max_height>
|
| 68 |
+
<real_height>0.0</real_height>
|
| 69 |
+
<min_mass>0.0</min_mass>
|
| 70 |
+
<max_mass>0.0</max_mass>
|
| 71 |
+
<generate_time>"-1"</generate_time>
|
| 72 |
+
<gs_model>""</gs_model>
|
| 73 |
+
</extra_info>
|
| 74 |
+
</link>
|
| 75 |
+
</robot>
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class URDFGenerator(object):
|
| 80 |
+
def __init__(
|
| 81 |
+
self,
|
| 82 |
+
gpt_client: GPTclient,
|
| 83 |
+
mesh_file_list: list[str] = ["material_0.png", "material.mtl"],
|
| 84 |
+
prompt_template: str = None,
|
| 85 |
+
attrs_name: list[str] = None,
|
| 86 |
+
render_dir: str = "urdf_renders",
|
| 87 |
+
render_view_num: int = 4,
|
| 88 |
+
) -> None:
|
| 89 |
+
if mesh_file_list is None:
|
| 90 |
+
mesh_file_list = []
|
| 91 |
+
self.mesh_file_list = mesh_file_list
|
| 92 |
+
self.output_mesh_dir = "mesh"
|
| 93 |
+
self.output_render_dir = render_dir
|
| 94 |
+
self.gpt_client = gpt_client
|
| 95 |
+
self.render_view_num = render_view_num
|
| 96 |
+
if render_view_num == 4:
|
| 97 |
+
view_desc = "This is orthographic projection showing the front, left, right and back views " # noqa
|
| 98 |
+
else:
|
| 99 |
+
view_desc = "This is the rendered views "
|
| 100 |
+
|
| 101 |
+
if prompt_template is None:
|
| 102 |
+
prompt_template = (
|
| 103 |
+
view_desc
|
| 104 |
+
+ """of the 3D object asset,
|
| 105 |
+
category: {category}.
|
| 106 |
+
Give the category of this object asset (within 3 words),
|
| 107 |
+
(if category is already provided, use it directly),
|
| 108 |
+
accurately describe this 3D object asset (within 15 words),
|
| 109 |
+
and give the recommended geometric height range (unit: meter),
|
| 110 |
+
weight range (unit: kilogram), the average static friction
|
| 111 |
+
coefficient of the object relative to rubber and the average
|
| 112 |
+
dynamic friction coefficient of the object relative to rubber.
|
| 113 |
+
Return response format as shown in Example.
|
| 114 |
+
|
| 115 |
+
Example:
|
| 116 |
+
Category: cup
|
| 117 |
+
Description: shiny golden cup with floral design
|
| 118 |
+
Height: 0.1-0.15 m
|
| 119 |
+
Weight: 0.3-0.6 kg
|
| 120 |
+
Static friction coefficient: 1.1
|
| 121 |
+
Dynamic friction coefficient: 0.9
|
| 122 |
+
"""
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
self.prompt_template = prompt_template
|
| 126 |
+
if attrs_name is None:
|
| 127 |
+
attrs_name = [
|
| 128 |
+
"category",
|
| 129 |
+
"description",
|
| 130 |
+
"min_height",
|
| 131 |
+
"max_height",
|
| 132 |
+
"real_height",
|
| 133 |
+
"min_mass",
|
| 134 |
+
"max_mass",
|
| 135 |
+
"version",
|
| 136 |
+
"generate_time",
|
| 137 |
+
"gs_model",
|
| 138 |
+
]
|
| 139 |
+
self.attrs_name = attrs_name
|
| 140 |
+
|
| 141 |
+
def parse_response(self, response: str) -> dict[str, any]:
|
| 142 |
+
lines = response.split("\n")
|
| 143 |
+
lines = [line.strip() for line in lines if line]
|
| 144 |
+
category = lines[0].split(": ")[1]
|
| 145 |
+
description = lines[1].split(": ")[1]
|
| 146 |
+
min_height, max_height = map(
|
| 147 |
+
lambda x: float(x.strip().replace(",", "").split()[0]),
|
| 148 |
+
lines[2].split(": ")[1].split("-"),
|
| 149 |
+
)
|
| 150 |
+
min_mass, max_mass = map(
|
| 151 |
+
lambda x: float(x.strip().replace(",", "").split()[0]),
|
| 152 |
+
lines[3].split(": ")[1].split("-"),
|
| 153 |
+
)
|
| 154 |
+
mu1 = float(lines[4].split(": ")[1].replace(",", ""))
|
| 155 |
+
mu2 = float(lines[5].split(": ")[1].replace(",", ""))
|
| 156 |
+
|
| 157 |
+
return {
|
| 158 |
+
"category": category.lower(),
|
| 159 |
+
"description": description.lower(),
|
| 160 |
+
"min_height": round(min_height, 4),
|
| 161 |
+
"max_height": round(max_height, 4),
|
| 162 |
+
"min_mass": round(min_mass, 4),
|
| 163 |
+
"max_mass": round(max_mass, 4),
|
| 164 |
+
"mu1": round(mu1, 2),
|
| 165 |
+
"mu2": round(mu2, 2),
|
| 166 |
+
"version": VERSION,
|
| 167 |
+
"generate_time": datetime.now().strftime("%Y%m%d%H%M%S"),
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
def generate_urdf(
|
| 171 |
+
self,
|
| 172 |
+
input_mesh: str,
|
| 173 |
+
output_dir: str,
|
| 174 |
+
attr_dict: dict,
|
| 175 |
+
output_name: str = None,
|
| 176 |
+
) -> str:
|
| 177 |
+
"""Generate a URDF file for a given mesh with specified attributes.
|
| 178 |
+
|
| 179 |
+
Args:
|
| 180 |
+
input_mesh (str): Path to the input mesh file.
|
| 181 |
+
output_dir (str): Directory to store the generated URDF
|
| 182 |
+
and processed mesh.
|
| 183 |
+
attr_dict (dict): Dictionary containing attributes like height,
|
| 184 |
+
mass, and friction coefficients.
|
| 185 |
+
output_name (str, optional): Name for the generated URDF and robot.
|
| 186 |
+
|
| 187 |
+
Returns:
|
| 188 |
+
str: Path to the generated URDF file.
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
# 1. Load and normalize the mesh
|
| 192 |
+
mesh = trimesh.load(input_mesh)
|
| 193 |
+
mesh_scale = np.ptp(mesh.vertices, axis=0).max()
|
| 194 |
+
mesh.vertices /= mesh_scale # Normalize to [-0.5, 0.5]
|
| 195 |
+
raw_height = np.ptp(mesh.vertices, axis=0)[1]
|
| 196 |
+
|
| 197 |
+
# 2. Scale the mesh to real height
|
| 198 |
+
real_height = attr_dict["real_height"]
|
| 199 |
+
scale = round(real_height / raw_height, 6)
|
| 200 |
+
mesh = mesh.apply_scale(scale)
|
| 201 |
+
|
| 202 |
+
# 3. Prepare output directories and save scaled mesh
|
| 203 |
+
mesh_folder = os.path.join(output_dir, self.output_mesh_dir)
|
| 204 |
+
os.makedirs(mesh_folder, exist_ok=True)
|
| 205 |
+
|
| 206 |
+
obj_name = os.path.basename(input_mesh)
|
| 207 |
+
mesh_output_path = os.path.join(mesh_folder, obj_name)
|
| 208 |
+
mesh.export(mesh_output_path)
|
| 209 |
+
|
| 210 |
+
# 4. Copy additional mesh files, if any
|
| 211 |
+
input_dir = os.path.dirname(input_mesh)
|
| 212 |
+
for file in self.mesh_file_list:
|
| 213 |
+
src_file = os.path.join(input_dir, file)
|
| 214 |
+
dest_file = os.path.join(mesh_folder, file)
|
| 215 |
+
if os.path.isfile(src_file):
|
| 216 |
+
shutil.copy(src_file, dest_file)
|
| 217 |
+
|
| 218 |
+
# 5. Determine output name
|
| 219 |
+
if output_name is None:
|
| 220 |
+
output_name = os.path.splitext(obj_name)[0]
|
| 221 |
+
|
| 222 |
+
# 6. Load URDF template and update attributes
|
| 223 |
+
robot = ET.fromstring(URDF_TEMPLATE)
|
| 224 |
+
robot.set("name", output_name)
|
| 225 |
+
|
| 226 |
+
link = robot.find("link")
|
| 227 |
+
if link is None:
|
| 228 |
+
raise ValueError("URDF template is missing 'link' element.")
|
| 229 |
+
link.set("name", output_name)
|
| 230 |
+
|
| 231 |
+
# Update visual geometry
|
| 232 |
+
visual = link.find("visual/geometry/mesh")
|
| 233 |
+
if visual is not None:
|
| 234 |
+
visual.set(
|
| 235 |
+
"filename", os.path.join(self.output_mesh_dir, obj_name)
|
| 236 |
+
)
|
| 237 |
+
visual.set("scale", "1.0 1.0 1.0")
|
| 238 |
+
|
| 239 |
+
# Update collision geometry
|
| 240 |
+
collision = link.find("collision/geometry/mesh")
|
| 241 |
+
if collision is not None:
|
| 242 |
+
collision.set(
|
| 243 |
+
"filename", os.path.join(self.output_mesh_dir, obj_name)
|
| 244 |
+
)
|
| 245 |
+
collision.set("scale", "1.0 1.0 1.0")
|
| 246 |
+
|
| 247 |
+
# Update friction coefficients
|
| 248 |
+
gazebo = link.find("collision/gazebo")
|
| 249 |
+
if gazebo is not None:
|
| 250 |
+
for param, key in zip(["mu1", "mu2"], ["mu1", "mu2"]):
|
| 251 |
+
element = gazebo.find(param)
|
| 252 |
+
if element is not None:
|
| 253 |
+
element.text = f"{attr_dict[key]:.2f}"
|
| 254 |
+
|
| 255 |
+
# Update mass
|
| 256 |
+
inertial = link.find("inertial/mass")
|
| 257 |
+
if inertial is not None:
|
| 258 |
+
mass_value = (attr_dict["min_mass"] + attr_dict["max_mass"]) / 2
|
| 259 |
+
inertial.set("value", f"{mass_value:.4f}")
|
| 260 |
+
|
| 261 |
+
# Add extra_info element to the link
|
| 262 |
+
extra_info = link.find("extra_info/scale")
|
| 263 |
+
if extra_info is not None:
|
| 264 |
+
extra_info.text = f"{scale:.6f}"
|
| 265 |
+
|
| 266 |
+
for key in self.attrs_name:
|
| 267 |
+
extra_info = link.find(f"extra_info/{key}")
|
| 268 |
+
if extra_info is not None and key in attr_dict:
|
| 269 |
+
extra_info.text = f"{attr_dict[key]}"
|
| 270 |
+
|
| 271 |
+
# 7. Write URDF to file
|
| 272 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 273 |
+
urdf_path = os.path.join(output_dir, f"{output_name}.urdf")
|
| 274 |
+
tree = ET.ElementTree(robot)
|
| 275 |
+
tree.write(urdf_path, encoding="utf-8", xml_declaration=True)
|
| 276 |
+
|
| 277 |
+
logger.info(f"URDF file saved to {urdf_path}")
|
| 278 |
+
|
| 279 |
+
return urdf_path
|
| 280 |
+
|
| 281 |
+
@staticmethod
|
| 282 |
+
def get_attr_from_urdf(
|
| 283 |
+
urdf_path: str,
|
| 284 |
+
attr_root: str = ".//link/extra_info",
|
| 285 |
+
attr_name: str = "scale",
|
| 286 |
+
) -> float:
|
| 287 |
+
if not os.path.exists(urdf_path):
|
| 288 |
+
raise FileNotFoundError(f"URDF file not found: {urdf_path}")
|
| 289 |
+
|
| 290 |
+
mesh_scale = 1.0
|
| 291 |
+
tree = ET.parse(urdf_path)
|
| 292 |
+
root = tree.getroot()
|
| 293 |
+
extra_info = root.find(attr_root)
|
| 294 |
+
if extra_info is not None:
|
| 295 |
+
scale_element = extra_info.find(attr_name)
|
| 296 |
+
if scale_element is not None:
|
| 297 |
+
mesh_scale = float(scale_element.text)
|
| 298 |
+
|
| 299 |
+
return mesh_scale
|
| 300 |
+
|
| 301 |
+
@staticmethod
|
| 302 |
+
def add_quality_tag(
|
| 303 |
+
urdf_path: str, results, output_path: str = None
|
| 304 |
+
) -> None:
|
| 305 |
+
if output_path is None:
|
| 306 |
+
output_path = urdf_path
|
| 307 |
+
|
| 308 |
+
tree = ET.parse(urdf_path)
|
| 309 |
+
root = tree.getroot()
|
| 310 |
+
custom_data = ET.SubElement(root, "custom_data")
|
| 311 |
+
quality = ET.SubElement(custom_data, "quality")
|
| 312 |
+
for key, value in results:
|
| 313 |
+
checker_tag = ET.SubElement(quality, key)
|
| 314 |
+
checker_tag.text = str(value)
|
| 315 |
+
|
| 316 |
+
rough_string = ET.tostring(root, encoding="utf-8")
|
| 317 |
+
formatted_string = parseString(rough_string).toprettyxml(indent=" ")
|
| 318 |
+
cleaned_string = "\n".join(
|
| 319 |
+
[line for line in formatted_string.splitlines() if line.strip()]
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 323 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 324 |
+
f.write(cleaned_string)
|
| 325 |
+
|
| 326 |
+
logger.info(f"URDF files saved to {output_path}")
|
| 327 |
+
|
| 328 |
+
def get_estimated_attributes(self, asset_attrs: dict):
|
| 329 |
+
estimated_attrs = {
|
| 330 |
+
"height": round(
|
| 331 |
+
(asset_attrs["min_height"] + asset_attrs["max_height"]) / 2, 4
|
| 332 |
+
),
|
| 333 |
+
"mass": round(
|
| 334 |
+
(asset_attrs["min_mass"] + asset_attrs["max_mass"]) / 2, 4
|
| 335 |
+
),
|
| 336 |
+
"mu": round((asset_attrs["mu1"] + asset_attrs["mu2"]) / 2, 4),
|
| 337 |
+
"category": asset_attrs["category"],
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
return estimated_attrs
|
| 341 |
+
|
| 342 |
+
def __call__(
|
| 343 |
+
self,
|
| 344 |
+
mesh_path: str,
|
| 345 |
+
output_root: str,
|
| 346 |
+
text_prompt: str = None,
|
| 347 |
+
category: str = "unknown",
|
| 348 |
+
**kwargs,
|
| 349 |
+
):
|
| 350 |
+
if text_prompt is None or len(text_prompt) == 0:
|
| 351 |
+
text_prompt = self.prompt_template
|
| 352 |
+
text_prompt = text_prompt.format(category=category.lower())
|
| 353 |
+
|
| 354 |
+
image_path = render_asset3d(
|
| 355 |
+
mesh_path,
|
| 356 |
+
output_root,
|
| 357 |
+
num_images=self.render_view_num,
|
| 358 |
+
output_subdir=self.output_render_dir,
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
+
# Hardcode tmp because of the openrouter can't input multi images.
|
| 362 |
+
if "openrouter" in self.gpt_client.endpoint:
|
| 363 |
+
from embodied_gen.utils.process_media import (
|
| 364 |
+
combine_images_to_base64,
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
+
image_path = combine_images_to_base64(image_path)
|
| 368 |
+
|
| 369 |
+
response = self.gpt_client.query(text_prompt, image_path)
|
| 370 |
+
if response is None:
|
| 371 |
+
asset_attrs = {
|
| 372 |
+
"category": category.lower(),
|
| 373 |
+
"description": category.lower(),
|
| 374 |
+
"min_height": 1,
|
| 375 |
+
"max_height": 1,
|
| 376 |
+
"min_mass": 1,
|
| 377 |
+
"max_mass": 1,
|
| 378 |
+
"mu1": 0.8,
|
| 379 |
+
"mu2": 0.6,
|
| 380 |
+
"version": VERSION,
|
| 381 |
+
"generate_time": datetime.now().strftime("%Y%m%d%H%M%S"),
|
| 382 |
+
}
|
| 383 |
+
else:
|
| 384 |
+
asset_attrs = self.parse_response(response)
|
| 385 |
+
for key in self.attrs_name:
|
| 386 |
+
if key in kwargs:
|
| 387 |
+
asset_attrs[key] = kwargs[key]
|
| 388 |
+
|
| 389 |
+
asset_attrs["real_height"] = round(
|
| 390 |
+
(asset_attrs["min_height"] + asset_attrs["max_height"]) / 2, 4
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
self.estimated_attrs = self.get_estimated_attributes(asset_attrs)
|
| 394 |
+
|
| 395 |
+
urdf_path = self.generate_urdf(mesh_path, output_root, asset_attrs)
|
| 396 |
+
|
| 397 |
+
logger.info(f"response: {response}")
|
| 398 |
+
|
| 399 |
+
return urdf_path
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
if __name__ == "__main__":
|
| 403 |
+
urdf_gen = URDFGenerator(GPT_CLIENT, render_view_num=4)
|
| 404 |
+
urdf_path = urdf_gen(
|
| 405 |
+
mesh_path="outputs/imageto3d/cma/o5/URDF_o5/mesh/o5.obj",
|
| 406 |
+
output_root="outputs/test_urdf",
|
| 407 |
+
# category="coffee machine",
|
| 408 |
+
# min_height=1.0,
|
| 409 |
+
# max_height=1.2,
|
| 410 |
+
version=VERSION,
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
# zip_files(
|
| 414 |
+
# input_paths=[
|
| 415 |
+
# "scripts/apps/tmp/2umpdum3e5n/URDF_sample/mesh",
|
| 416 |
+
# "scripts/apps/tmp/2umpdum3e5n/URDF_sample/sample.urdf"
|
| 417 |
+
# ],
|
| 418 |
+
# output_zip="zip.zip"
|
| 419 |
+
# )
|
requirements.txt
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.4.0
|
| 2 |
+
torchvision==0.19.0
|
| 3 |
+
xformers==0.0.27.post2
|
| 4 |
+
pytorch-lightning==2.4.0
|
| 5 |
+
spconv-cu120==2.3.6
|
| 6 |
+
triton==2.1.0
|
| 7 |
+
dataclasses_json
|
| 8 |
+
easydict
|
| 9 |
+
opencv-python>4.5
|
| 10 |
+
imageio==2.36.1
|
| 11 |
+
imageio-ffmpeg==0.5.1
|
| 12 |
+
rembg==2.0.61
|
| 13 |
+
trimesh==4.4.4
|
| 14 |
+
moviepy==1.0.3
|
| 15 |
+
pymeshfix==0.17.0
|
| 16 |
+
igraph==0.11.8
|
| 17 |
+
pyvista==0.36.1
|
| 18 |
+
openai==1.58.1
|
| 19 |
+
transformers==4.42.4
|
| 20 |
+
gradio==5.12.0
|
| 21 |
+
sentencepiece==0.2.0
|
| 22 |
+
diffusers==0.31.0
|
| 23 |
+
xatlas==0.0.9
|
| 24 |
+
onnxruntime==1.20.1
|
| 25 |
+
tenacity==8.2.2
|
| 26 |
+
accelerate==0.33.0
|
| 27 |
+
basicsr==1.4.2
|
| 28 |
+
realesrgan==0.3.0
|
| 29 |
+
pydantic==2.9.2
|
| 30 |
+
vtk==9.3.1
|
| 31 |
+
spaces
|
| 32 |
+
utils3d@git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
|
| 33 |
+
clip@git+https://github.com/openai/CLIP.git
|
| 34 |
+
kolors@git+https://github.com/Kwai-Kolors/Kolors.git#egg=038818d
|
| 35 |
+
segment-anything@git+https://github.com/facebookresearch/segment-anything.git#egg=dca509f
|
| 36 |
+
# https://github.com/nerfstudio-project/gsplat/releases/download/v1.5.0/gsplat-1.5.0+pt24cu121-cp310-cp310-linux_x86_64.whl
|
| 37 |
+
https://github.com/nerfstudio-project/gsplat/releases/download/v1.5.0/gsplat-1.5.0+pt24cu118-cp310-cp310-linux_x86_64.whl
|
| 38 |
+
# https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 39 |
+
https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu11torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 40 |
+
https://huggingface.co/spaces/xinjjj/ImgRoboAssetGen/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl
|
| 41 |
+
https://huggingface.co/spaces/xinjjj/ImgRoboAssetGen/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl
|
| 42 |
+
https://huggingface.co/spaces/xinjjj/ImgRoboAssetGen/resolve/main/wheels/kaolin-0.16.0-cp310-cp310-linux_x86_64.whl
|
thirdparty/TRELLIS/trellis/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from . import models
|
| 2 |
+
from . import modules
|
| 3 |
+
from . import pipelines
|
| 4 |
+
from . import renderers
|
| 5 |
+
from . import representations
|
| 6 |
+
from . import utils
|
thirdparty/TRELLIS/trellis/models/__init__.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
|
| 3 |
+
__attributes = {
|
| 4 |
+
'SparseStructureEncoder': 'sparse_structure_vae',
|
| 5 |
+
'SparseStructureDecoder': 'sparse_structure_vae',
|
| 6 |
+
'SparseStructureFlowModel': 'sparse_structure_flow',
|
| 7 |
+
'SLatEncoder': 'structured_latent_vae',
|
| 8 |
+
'SLatGaussianDecoder': 'structured_latent_vae',
|
| 9 |
+
'SLatRadianceFieldDecoder': 'structured_latent_vae',
|
| 10 |
+
'SLatMeshDecoder': 'structured_latent_vae',
|
| 11 |
+
'SLatFlowModel': 'structured_latent_flow',
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
__submodules = []
|
| 15 |
+
|
| 16 |
+
__all__ = list(__attributes.keys()) + __submodules
|
| 17 |
+
|
| 18 |
+
def __getattr__(name):
|
| 19 |
+
if name not in globals():
|
| 20 |
+
if name in __attributes:
|
| 21 |
+
module_name = __attributes[name]
|
| 22 |
+
module = importlib.import_module(f".{module_name}", __name__)
|
| 23 |
+
globals()[name] = getattr(module, name)
|
| 24 |
+
elif name in __submodules:
|
| 25 |
+
module = importlib.import_module(f".{name}", __name__)
|
| 26 |
+
globals()[name] = module
|
| 27 |
+
else:
|
| 28 |
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
| 29 |
+
return globals()[name]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def from_pretrained(path: str, **kwargs):
|
| 33 |
+
"""
|
| 34 |
+
Load a model from a pretrained checkpoint.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
path: The path to the checkpoint. Can be either local path or a Hugging Face model name.
|
| 38 |
+
NOTE: config file and model file should take the name f'{path}.json' and f'{path}.safetensors' respectively.
|
| 39 |
+
**kwargs: Additional arguments for the model constructor.
|
| 40 |
+
"""
|
| 41 |
+
import os
|
| 42 |
+
import json
|
| 43 |
+
from safetensors.torch import load_file
|
| 44 |
+
is_local = os.path.exists(f"{path}.json") and os.path.exists(f"{path}.safetensors")
|
| 45 |
+
|
| 46 |
+
if is_local:
|
| 47 |
+
config_file = f"{path}.json"
|
| 48 |
+
model_file = f"{path}.safetensors"
|
| 49 |
+
else:
|
| 50 |
+
from huggingface_hub import hf_hub_download
|
| 51 |
+
path_parts = path.split('/')
|
| 52 |
+
repo_id = f'{path_parts[0]}/{path_parts[1]}'
|
| 53 |
+
model_name = '/'.join(path_parts[2:])
|
| 54 |
+
config_file = hf_hub_download(repo_id, f"{model_name}.json")
|
| 55 |
+
model_file = hf_hub_download(repo_id, f"{model_name}.safetensors")
|
| 56 |
+
|
| 57 |
+
with open(config_file, 'r') as f:
|
| 58 |
+
config = json.load(f)
|
| 59 |
+
model = __getattr__(config['name'])(**config['args'], **kwargs)
|
| 60 |
+
model.load_state_dict(load_file(model_file))
|
| 61 |
+
|
| 62 |
+
return model
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# For Pylance
|
| 66 |
+
if __name__ == '__main__':
|
| 67 |
+
from .sparse_structure_vae import SparseStructureEncoder, SparseStructureDecoder
|
| 68 |
+
from .sparse_structure_flow import SparseStructureFlowModel
|
| 69 |
+
from .structured_latent_vae import SLatEncoder, SLatGaussianDecoder, SLatRadianceFieldDecoder, SLatMeshDecoder
|
| 70 |
+
from .structured_latent_flow import SLatFlowModel
|
thirdparty/TRELLIS/trellis/models/sparse_structure_flow.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ..modules.utils import convert_module_to_f16, convert_module_to_f32
|
| 7 |
+
from ..modules.transformer import AbsolutePositionEmbedder, ModulatedTransformerCrossBlock
|
| 8 |
+
from ..modules.spatial import patchify, unpatchify
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TimestepEmbedder(nn.Module):
|
| 12 |
+
"""
|
| 13 |
+
Embeds scalar timesteps into vector representations.
|
| 14 |
+
"""
|
| 15 |
+
def __init__(self, hidden_size, frequency_embedding_size=256):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.mlp = nn.Sequential(
|
| 18 |
+
nn.Linear(frequency_embedding_size, hidden_size, bias=True),
|
| 19 |
+
nn.SiLU(),
|
| 20 |
+
nn.Linear(hidden_size, hidden_size, bias=True),
|
| 21 |
+
)
|
| 22 |
+
self.frequency_embedding_size = frequency_embedding_size
|
| 23 |
+
|
| 24 |
+
@staticmethod
|
| 25 |
+
def timestep_embedding(t, dim, max_period=10000):
|
| 26 |
+
"""
|
| 27 |
+
Create sinusoidal timestep embeddings.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
t: a 1-D Tensor of N indices, one per batch element.
|
| 31 |
+
These may be fractional.
|
| 32 |
+
dim: the dimension of the output.
|
| 33 |
+
max_period: controls the minimum frequency of the embeddings.
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
an (N, D) Tensor of positional embeddings.
|
| 37 |
+
"""
|
| 38 |
+
# https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py
|
| 39 |
+
half = dim // 2
|
| 40 |
+
freqs = torch.exp(
|
| 41 |
+
-np.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
|
| 42 |
+
).to(device=t.device)
|
| 43 |
+
args = t[:, None].float() * freqs[None]
|
| 44 |
+
embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
|
| 45 |
+
if dim % 2:
|
| 46 |
+
embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
|
| 47 |
+
return embedding
|
| 48 |
+
|
| 49 |
+
def forward(self, t):
|
| 50 |
+
t_freq = self.timestep_embedding(t, self.frequency_embedding_size)
|
| 51 |
+
t_emb = self.mlp(t_freq)
|
| 52 |
+
return t_emb
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class SparseStructureFlowModel(nn.Module):
|
| 56 |
+
def __init__(
|
| 57 |
+
self,
|
| 58 |
+
resolution: int,
|
| 59 |
+
in_channels: int,
|
| 60 |
+
model_channels: int,
|
| 61 |
+
cond_channels: int,
|
| 62 |
+
out_channels: int,
|
| 63 |
+
num_blocks: int,
|
| 64 |
+
num_heads: Optional[int] = None,
|
| 65 |
+
num_head_channels: Optional[int] = 64,
|
| 66 |
+
mlp_ratio: float = 4,
|
| 67 |
+
patch_size: int = 2,
|
| 68 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 69 |
+
use_fp16: bool = False,
|
| 70 |
+
use_checkpoint: bool = False,
|
| 71 |
+
share_mod: bool = False,
|
| 72 |
+
qk_rms_norm: bool = False,
|
| 73 |
+
qk_rms_norm_cross: bool = False,
|
| 74 |
+
):
|
| 75 |
+
super().__init__()
|
| 76 |
+
self.resolution = resolution
|
| 77 |
+
self.in_channels = in_channels
|
| 78 |
+
self.model_channels = model_channels
|
| 79 |
+
self.cond_channels = cond_channels
|
| 80 |
+
self.out_channels = out_channels
|
| 81 |
+
self.num_blocks = num_blocks
|
| 82 |
+
self.num_heads = num_heads or model_channels // num_head_channels
|
| 83 |
+
self.mlp_ratio = mlp_ratio
|
| 84 |
+
self.patch_size = patch_size
|
| 85 |
+
self.pe_mode = pe_mode
|
| 86 |
+
self.use_fp16 = use_fp16
|
| 87 |
+
self.use_checkpoint = use_checkpoint
|
| 88 |
+
self.share_mod = share_mod
|
| 89 |
+
self.qk_rms_norm = qk_rms_norm
|
| 90 |
+
self.qk_rms_norm_cross = qk_rms_norm_cross
|
| 91 |
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
| 92 |
+
|
| 93 |
+
self.t_embedder = TimestepEmbedder(model_channels)
|
| 94 |
+
if share_mod:
|
| 95 |
+
self.adaLN_modulation = nn.Sequential(
|
| 96 |
+
nn.SiLU(),
|
| 97 |
+
nn.Linear(model_channels, 6 * model_channels, bias=True)
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if pe_mode == "ape":
|
| 101 |
+
pos_embedder = AbsolutePositionEmbedder(model_channels, 3)
|
| 102 |
+
coords = torch.meshgrid(*[torch.arange(res, device=self.device) for res in [resolution // patch_size] * 3], indexing='ij')
|
| 103 |
+
coords = torch.stack(coords, dim=-1).reshape(-1, 3)
|
| 104 |
+
pos_emb = pos_embedder(coords)
|
| 105 |
+
self.register_buffer("pos_emb", pos_emb)
|
| 106 |
+
|
| 107 |
+
self.input_layer = nn.Linear(in_channels * patch_size**3, model_channels)
|
| 108 |
+
|
| 109 |
+
self.blocks = nn.ModuleList([
|
| 110 |
+
ModulatedTransformerCrossBlock(
|
| 111 |
+
model_channels,
|
| 112 |
+
cond_channels,
|
| 113 |
+
num_heads=self.num_heads,
|
| 114 |
+
mlp_ratio=self.mlp_ratio,
|
| 115 |
+
attn_mode='full',
|
| 116 |
+
use_checkpoint=self.use_checkpoint,
|
| 117 |
+
use_rope=(pe_mode == "rope"),
|
| 118 |
+
share_mod=share_mod,
|
| 119 |
+
qk_rms_norm=self.qk_rms_norm,
|
| 120 |
+
qk_rms_norm_cross=self.qk_rms_norm_cross,
|
| 121 |
+
)
|
| 122 |
+
for _ in range(num_blocks)
|
| 123 |
+
])
|
| 124 |
+
|
| 125 |
+
self.out_layer = nn.Linear(model_channels, out_channels * patch_size**3)
|
| 126 |
+
|
| 127 |
+
self.initialize_weights()
|
| 128 |
+
if use_fp16:
|
| 129 |
+
self.convert_to_fp16()
|
| 130 |
+
|
| 131 |
+
@property
|
| 132 |
+
def device(self) -> torch.device:
|
| 133 |
+
"""
|
| 134 |
+
Return the device of the model.
|
| 135 |
+
"""
|
| 136 |
+
return next(self.parameters()).device
|
| 137 |
+
|
| 138 |
+
def convert_to_fp16(self) -> None:
|
| 139 |
+
"""
|
| 140 |
+
Convert the torso of the model to float16.
|
| 141 |
+
"""
|
| 142 |
+
self.blocks.apply(convert_module_to_f16)
|
| 143 |
+
|
| 144 |
+
def convert_to_fp32(self) -> None:
|
| 145 |
+
"""
|
| 146 |
+
Convert the torso of the model to float32.
|
| 147 |
+
"""
|
| 148 |
+
self.blocks.apply(convert_module_to_f32)
|
| 149 |
+
|
| 150 |
+
def initialize_weights(self) -> None:
|
| 151 |
+
# Initialize transformer layers:
|
| 152 |
+
def _basic_init(module):
|
| 153 |
+
if isinstance(module, nn.Linear):
|
| 154 |
+
torch.nn.init.xavier_uniform_(module.weight)
|
| 155 |
+
if module.bias is not None:
|
| 156 |
+
nn.init.constant_(module.bias, 0)
|
| 157 |
+
self.apply(_basic_init)
|
| 158 |
+
|
| 159 |
+
# Initialize timestep embedding MLP:
|
| 160 |
+
nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
|
| 161 |
+
nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
|
| 162 |
+
|
| 163 |
+
# Zero-out adaLN modulation layers in DiT blocks:
|
| 164 |
+
if self.share_mod:
|
| 165 |
+
nn.init.constant_(self.adaLN_modulation[-1].weight, 0)
|
| 166 |
+
nn.init.constant_(self.adaLN_modulation[-1].bias, 0)
|
| 167 |
+
else:
|
| 168 |
+
for block in self.blocks:
|
| 169 |
+
nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
|
| 170 |
+
nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
|
| 171 |
+
|
| 172 |
+
# Zero-out output layers:
|
| 173 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 174 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 175 |
+
|
| 176 |
+
def forward(self, x: torch.Tensor, t: torch.Tensor, cond: torch.Tensor) -> torch.Tensor:
|
| 177 |
+
assert [*x.shape] == [x.shape[0], self.in_channels, *[self.resolution] * 3], \
|
| 178 |
+
f"Input shape mismatch, got {x.shape}, expected {[x.shape[0], self.in_channels, *[self.resolution] * 3]}"
|
| 179 |
+
|
| 180 |
+
h = patchify(x, self.patch_size)
|
| 181 |
+
h = h.view(*h.shape[:2], -1).permute(0, 2, 1).contiguous()
|
| 182 |
+
|
| 183 |
+
h = self.input_layer(h)
|
| 184 |
+
h = h + self.pos_emb[None]
|
| 185 |
+
t_emb = self.t_embedder(t)
|
| 186 |
+
if self.share_mod:
|
| 187 |
+
t_emb = self.adaLN_modulation(t_emb)
|
| 188 |
+
t_emb = t_emb.type(self.dtype)
|
| 189 |
+
h = h.type(self.dtype)
|
| 190 |
+
cond = cond.type(self.dtype)
|
| 191 |
+
for block in self.blocks:
|
| 192 |
+
h = block(h, t_emb, cond)
|
| 193 |
+
h = h.type(x.dtype)
|
| 194 |
+
h = F.layer_norm(h, h.shape[-1:])
|
| 195 |
+
h = self.out_layer(h)
|
| 196 |
+
|
| 197 |
+
h = h.permute(0, 2, 1).view(h.shape[0], h.shape[2], *[self.resolution // self.patch_size] * 3)
|
| 198 |
+
h = unpatchify(h, self.patch_size).contiguous()
|
| 199 |
+
|
| 200 |
+
return h
|
thirdparty/TRELLIS/trellis/models/sparse_structure_vae.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from ..modules.norm import GroupNorm32, ChannelLayerNorm32
|
| 6 |
+
from ..modules.spatial import pixel_shuffle_3d
|
| 7 |
+
from ..modules.utils import zero_module, convert_module_to_f16, convert_module_to_f32
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def norm_layer(norm_type: str, *args, **kwargs) -> nn.Module:
|
| 11 |
+
"""
|
| 12 |
+
Return a normalization layer.
|
| 13 |
+
"""
|
| 14 |
+
if norm_type == "group":
|
| 15 |
+
return GroupNorm32(32, *args, **kwargs)
|
| 16 |
+
elif norm_type == "layer":
|
| 17 |
+
return ChannelLayerNorm32(*args, **kwargs)
|
| 18 |
+
else:
|
| 19 |
+
raise ValueError(f"Invalid norm type {norm_type}")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class ResBlock3d(nn.Module):
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
channels: int,
|
| 26 |
+
out_channels: Optional[int] = None,
|
| 27 |
+
norm_type: Literal["group", "layer"] = "layer",
|
| 28 |
+
):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.channels = channels
|
| 31 |
+
self.out_channels = out_channels or channels
|
| 32 |
+
|
| 33 |
+
self.norm1 = norm_layer(norm_type, channels)
|
| 34 |
+
self.norm2 = norm_layer(norm_type, self.out_channels)
|
| 35 |
+
self.conv1 = nn.Conv3d(channels, self.out_channels, 3, padding=1)
|
| 36 |
+
self.conv2 = zero_module(nn.Conv3d(self.out_channels, self.out_channels, 3, padding=1))
|
| 37 |
+
self.skip_connection = nn.Conv3d(channels, self.out_channels, 1) if channels != self.out_channels else nn.Identity()
|
| 38 |
+
|
| 39 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 40 |
+
h = self.norm1(x)
|
| 41 |
+
h = F.silu(h)
|
| 42 |
+
h = self.conv1(h)
|
| 43 |
+
h = self.norm2(h)
|
| 44 |
+
h = F.silu(h)
|
| 45 |
+
h = self.conv2(h)
|
| 46 |
+
h = h + self.skip_connection(x)
|
| 47 |
+
return h
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class DownsampleBlock3d(nn.Module):
|
| 51 |
+
def __init__(
|
| 52 |
+
self,
|
| 53 |
+
in_channels: int,
|
| 54 |
+
out_channels: int,
|
| 55 |
+
mode: Literal["conv", "avgpool"] = "conv",
|
| 56 |
+
):
|
| 57 |
+
assert mode in ["conv", "avgpool"], f"Invalid mode {mode}"
|
| 58 |
+
|
| 59 |
+
super().__init__()
|
| 60 |
+
self.in_channels = in_channels
|
| 61 |
+
self.out_channels = out_channels
|
| 62 |
+
|
| 63 |
+
if mode == "conv":
|
| 64 |
+
self.conv = nn.Conv3d(in_channels, out_channels, 2, stride=2)
|
| 65 |
+
elif mode == "avgpool":
|
| 66 |
+
assert in_channels == out_channels, "Pooling mode requires in_channels to be equal to out_channels"
|
| 67 |
+
|
| 68 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 69 |
+
if hasattr(self, "conv"):
|
| 70 |
+
return self.conv(x)
|
| 71 |
+
else:
|
| 72 |
+
return F.avg_pool3d(x, 2)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class UpsampleBlock3d(nn.Module):
|
| 76 |
+
def __init__(
|
| 77 |
+
self,
|
| 78 |
+
in_channels: int,
|
| 79 |
+
out_channels: int,
|
| 80 |
+
mode: Literal["conv", "nearest"] = "conv",
|
| 81 |
+
):
|
| 82 |
+
assert mode in ["conv", "nearest"], f"Invalid mode {mode}"
|
| 83 |
+
|
| 84 |
+
super().__init__()
|
| 85 |
+
self.in_channels = in_channels
|
| 86 |
+
self.out_channels = out_channels
|
| 87 |
+
|
| 88 |
+
if mode == "conv":
|
| 89 |
+
self.conv = nn.Conv3d(in_channels, out_channels*8, 3, padding=1)
|
| 90 |
+
elif mode == "nearest":
|
| 91 |
+
assert in_channels == out_channels, "Nearest mode requires in_channels to be equal to out_channels"
|
| 92 |
+
|
| 93 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 94 |
+
if hasattr(self, "conv"):
|
| 95 |
+
x = self.conv(x)
|
| 96 |
+
return pixel_shuffle_3d(x, 2)
|
| 97 |
+
else:
|
| 98 |
+
return F.interpolate(x, scale_factor=2, mode="nearest")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class SparseStructureEncoder(nn.Module):
|
| 102 |
+
"""
|
| 103 |
+
Encoder for Sparse Structure (\mathcal{E}_S in the paper Sec. 3.3).
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
in_channels (int): Channels of the input.
|
| 107 |
+
latent_channels (int): Channels of the latent representation.
|
| 108 |
+
num_res_blocks (int): Number of residual blocks at each resolution.
|
| 109 |
+
channels (List[int]): Channels of the encoder blocks.
|
| 110 |
+
num_res_blocks_middle (int): Number of residual blocks in the middle.
|
| 111 |
+
norm_type (Literal["group", "layer"]): Type of normalization layer.
|
| 112 |
+
use_fp16 (bool): Whether to use FP16.
|
| 113 |
+
"""
|
| 114 |
+
def __init__(
|
| 115 |
+
self,
|
| 116 |
+
in_channels: int,
|
| 117 |
+
latent_channels: int,
|
| 118 |
+
num_res_blocks: int,
|
| 119 |
+
channels: List[int],
|
| 120 |
+
num_res_blocks_middle: int = 2,
|
| 121 |
+
norm_type: Literal["group", "layer"] = "layer",
|
| 122 |
+
use_fp16: bool = False,
|
| 123 |
+
):
|
| 124 |
+
super().__init__()
|
| 125 |
+
self.in_channels = in_channels
|
| 126 |
+
self.latent_channels = latent_channels
|
| 127 |
+
self.num_res_blocks = num_res_blocks
|
| 128 |
+
self.channels = channels
|
| 129 |
+
self.num_res_blocks_middle = num_res_blocks_middle
|
| 130 |
+
self.norm_type = norm_type
|
| 131 |
+
self.use_fp16 = use_fp16
|
| 132 |
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
| 133 |
+
|
| 134 |
+
self.input_layer = nn.Conv3d(in_channels, channels[0], 3, padding=1)
|
| 135 |
+
|
| 136 |
+
self.blocks = nn.ModuleList([])
|
| 137 |
+
for i, ch in enumerate(channels):
|
| 138 |
+
self.blocks.extend([
|
| 139 |
+
ResBlock3d(ch, ch)
|
| 140 |
+
for _ in range(num_res_blocks)
|
| 141 |
+
])
|
| 142 |
+
if i < len(channels) - 1:
|
| 143 |
+
self.blocks.append(
|
| 144 |
+
DownsampleBlock3d(ch, channels[i+1])
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
self.middle_block = nn.Sequential(*[
|
| 148 |
+
ResBlock3d(channels[-1], channels[-1])
|
| 149 |
+
for _ in range(num_res_blocks_middle)
|
| 150 |
+
])
|
| 151 |
+
|
| 152 |
+
self.out_layer = nn.Sequential(
|
| 153 |
+
norm_layer(norm_type, channels[-1]),
|
| 154 |
+
nn.SiLU(),
|
| 155 |
+
nn.Conv3d(channels[-1], latent_channels*2, 3, padding=1)
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
if use_fp16:
|
| 159 |
+
self.convert_to_fp16()
|
| 160 |
+
|
| 161 |
+
@property
|
| 162 |
+
def device(self) -> torch.device:
|
| 163 |
+
"""
|
| 164 |
+
Return the device of the model.
|
| 165 |
+
"""
|
| 166 |
+
return next(self.parameters()).device
|
| 167 |
+
|
| 168 |
+
def convert_to_fp16(self) -> None:
|
| 169 |
+
"""
|
| 170 |
+
Convert the torso of the model to float16.
|
| 171 |
+
"""
|
| 172 |
+
self.use_fp16 = True
|
| 173 |
+
self.dtype = torch.float16
|
| 174 |
+
self.blocks.apply(convert_module_to_f16)
|
| 175 |
+
self.middle_block.apply(convert_module_to_f16)
|
| 176 |
+
|
| 177 |
+
def convert_to_fp32(self) -> None:
|
| 178 |
+
"""
|
| 179 |
+
Convert the torso of the model to float32.
|
| 180 |
+
"""
|
| 181 |
+
self.use_fp16 = False
|
| 182 |
+
self.dtype = torch.float32
|
| 183 |
+
self.blocks.apply(convert_module_to_f32)
|
| 184 |
+
self.middle_block.apply(convert_module_to_f32)
|
| 185 |
+
|
| 186 |
+
def forward(self, x: torch.Tensor, sample_posterior: bool = False, return_raw: bool = False) -> torch.Tensor:
|
| 187 |
+
h = self.input_layer(x)
|
| 188 |
+
h = h.type(self.dtype)
|
| 189 |
+
|
| 190 |
+
for block in self.blocks:
|
| 191 |
+
h = block(h)
|
| 192 |
+
h = self.middle_block(h)
|
| 193 |
+
|
| 194 |
+
h = h.type(x.dtype)
|
| 195 |
+
h = self.out_layer(h)
|
| 196 |
+
|
| 197 |
+
mean, logvar = h.chunk(2, dim=1)
|
| 198 |
+
|
| 199 |
+
if sample_posterior:
|
| 200 |
+
std = torch.exp(0.5 * logvar)
|
| 201 |
+
z = mean + std * torch.randn_like(std)
|
| 202 |
+
else:
|
| 203 |
+
z = mean
|
| 204 |
+
|
| 205 |
+
if return_raw:
|
| 206 |
+
return z, mean, logvar
|
| 207 |
+
return z
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
class SparseStructureDecoder(nn.Module):
|
| 211 |
+
"""
|
| 212 |
+
Decoder for Sparse Structure (\mathcal{D}_S in the paper Sec. 3.3).
|
| 213 |
+
|
| 214 |
+
Args:
|
| 215 |
+
out_channels (int): Channels of the output.
|
| 216 |
+
latent_channels (int): Channels of the latent representation.
|
| 217 |
+
num_res_blocks (int): Number of residual blocks at each resolution.
|
| 218 |
+
channels (List[int]): Channels of the decoder blocks.
|
| 219 |
+
num_res_blocks_middle (int): Number of residual blocks in the middle.
|
| 220 |
+
norm_type (Literal["group", "layer"]): Type of normalization layer.
|
| 221 |
+
use_fp16 (bool): Whether to use FP16.
|
| 222 |
+
"""
|
| 223 |
+
def __init__(
|
| 224 |
+
self,
|
| 225 |
+
out_channels: int,
|
| 226 |
+
latent_channels: int,
|
| 227 |
+
num_res_blocks: int,
|
| 228 |
+
channels: List[int],
|
| 229 |
+
num_res_blocks_middle: int = 2,
|
| 230 |
+
norm_type: Literal["group", "layer"] = "layer",
|
| 231 |
+
use_fp16: bool = False,
|
| 232 |
+
):
|
| 233 |
+
super().__init__()
|
| 234 |
+
self.out_channels = out_channels
|
| 235 |
+
self.latent_channels = latent_channels
|
| 236 |
+
self.num_res_blocks = num_res_blocks
|
| 237 |
+
self.channels = channels
|
| 238 |
+
self.num_res_blocks_middle = num_res_blocks_middle
|
| 239 |
+
self.norm_type = norm_type
|
| 240 |
+
self.use_fp16 = use_fp16
|
| 241 |
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
| 242 |
+
|
| 243 |
+
self.input_layer = nn.Conv3d(latent_channels, channels[0], 3, padding=1)
|
| 244 |
+
|
| 245 |
+
self.middle_block = nn.Sequential(*[
|
| 246 |
+
ResBlock3d(channels[0], channels[0])
|
| 247 |
+
for _ in range(num_res_blocks_middle)
|
| 248 |
+
])
|
| 249 |
+
|
| 250 |
+
self.blocks = nn.ModuleList([])
|
| 251 |
+
for i, ch in enumerate(channels):
|
| 252 |
+
self.blocks.extend([
|
| 253 |
+
ResBlock3d(ch, ch)
|
| 254 |
+
for _ in range(num_res_blocks)
|
| 255 |
+
])
|
| 256 |
+
if i < len(channels) - 1:
|
| 257 |
+
self.blocks.append(
|
| 258 |
+
UpsampleBlock3d(ch, channels[i+1])
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
self.out_layer = nn.Sequential(
|
| 262 |
+
norm_layer(norm_type, channels[-1]),
|
| 263 |
+
nn.SiLU(),
|
| 264 |
+
nn.Conv3d(channels[-1], out_channels, 3, padding=1)
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
if use_fp16:
|
| 268 |
+
self.convert_to_fp16()
|
| 269 |
+
|
| 270 |
+
@property
|
| 271 |
+
def device(self) -> torch.device:
|
| 272 |
+
"""
|
| 273 |
+
Return the device of the model.
|
| 274 |
+
"""
|
| 275 |
+
return next(self.parameters()).device
|
| 276 |
+
|
| 277 |
+
def convert_to_fp16(self) -> None:
|
| 278 |
+
"""
|
| 279 |
+
Convert the torso of the model to float16.
|
| 280 |
+
"""
|
| 281 |
+
self.use_fp16 = True
|
| 282 |
+
self.dtype = torch.float16
|
| 283 |
+
self.blocks.apply(convert_module_to_f16)
|
| 284 |
+
self.middle_block.apply(convert_module_to_f16)
|
| 285 |
+
|
| 286 |
+
def convert_to_fp32(self) -> None:
|
| 287 |
+
"""
|
| 288 |
+
Convert the torso of the model to float32.
|
| 289 |
+
"""
|
| 290 |
+
self.use_fp16 = False
|
| 291 |
+
self.dtype = torch.float32
|
| 292 |
+
self.blocks.apply(convert_module_to_f32)
|
| 293 |
+
self.middle_block.apply(convert_module_to_f32)
|
| 294 |
+
|
| 295 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 296 |
+
h = self.input_layer(x)
|
| 297 |
+
|
| 298 |
+
h = h.type(self.dtype)
|
| 299 |
+
|
| 300 |
+
h = self.middle_block(h)
|
| 301 |
+
for block in self.blocks:
|
| 302 |
+
h = block(h)
|
| 303 |
+
|
| 304 |
+
h = h.type(x.dtype)
|
| 305 |
+
h = self.out_layer(h)
|
| 306 |
+
return h
|
thirdparty/TRELLIS/trellis/models/structured_latent_flow.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ..modules.utils import zero_module, convert_module_to_f16, convert_module_to_f32
|
| 7 |
+
from ..modules.transformer import AbsolutePositionEmbedder
|
| 8 |
+
from ..modules.norm import LayerNorm32
|
| 9 |
+
from ..modules import sparse as sp
|
| 10 |
+
from ..modules.sparse.transformer import ModulatedSparseTransformerCrossBlock
|
| 11 |
+
from .sparse_structure_flow import TimestepEmbedder
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class SparseResBlock3d(nn.Module):
|
| 15 |
+
def __init__(
|
| 16 |
+
self,
|
| 17 |
+
channels: int,
|
| 18 |
+
emb_channels: int,
|
| 19 |
+
out_channels: Optional[int] = None,
|
| 20 |
+
downsample: bool = False,
|
| 21 |
+
upsample: bool = False,
|
| 22 |
+
):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.channels = channels
|
| 25 |
+
self.emb_channels = emb_channels
|
| 26 |
+
self.out_channels = out_channels or channels
|
| 27 |
+
self.downsample = downsample
|
| 28 |
+
self.upsample = upsample
|
| 29 |
+
|
| 30 |
+
assert not (downsample and upsample), "Cannot downsample and upsample at the same time"
|
| 31 |
+
|
| 32 |
+
self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6)
|
| 33 |
+
self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6)
|
| 34 |
+
self.conv1 = sp.SparseConv3d(channels, self.out_channels, 3)
|
| 35 |
+
self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3))
|
| 36 |
+
self.emb_layers = nn.Sequential(
|
| 37 |
+
nn.SiLU(),
|
| 38 |
+
nn.Linear(emb_channels, 2 * self.out_channels, bias=True),
|
| 39 |
+
)
|
| 40 |
+
self.skip_connection = sp.SparseLinear(channels, self.out_channels) if channels != self.out_channels else nn.Identity()
|
| 41 |
+
self.updown = None
|
| 42 |
+
if self.downsample:
|
| 43 |
+
self.updown = sp.SparseDownsample(2)
|
| 44 |
+
elif self.upsample:
|
| 45 |
+
self.updown = sp.SparseUpsample(2)
|
| 46 |
+
|
| 47 |
+
def _updown(self, x: sp.SparseTensor) -> sp.SparseTensor:
|
| 48 |
+
if self.updown is not None:
|
| 49 |
+
x = self.updown(x)
|
| 50 |
+
return x
|
| 51 |
+
|
| 52 |
+
def forward(self, x: sp.SparseTensor, emb: torch.Tensor) -> sp.SparseTensor:
|
| 53 |
+
emb_out = self.emb_layers(emb).type(x.dtype)
|
| 54 |
+
scale, shift = torch.chunk(emb_out, 2, dim=1)
|
| 55 |
+
|
| 56 |
+
x = self._updown(x)
|
| 57 |
+
h = x.replace(self.norm1(x.feats))
|
| 58 |
+
h = h.replace(F.silu(h.feats))
|
| 59 |
+
h = self.conv1(h)
|
| 60 |
+
h = h.replace(self.norm2(h.feats)) * (1 + scale) + shift
|
| 61 |
+
h = h.replace(F.silu(h.feats))
|
| 62 |
+
h = self.conv2(h)
|
| 63 |
+
h = h + self.skip_connection(x)
|
| 64 |
+
|
| 65 |
+
return h
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class SLatFlowModel(nn.Module):
|
| 69 |
+
def __init__(
|
| 70 |
+
self,
|
| 71 |
+
resolution: int,
|
| 72 |
+
in_channels: int,
|
| 73 |
+
model_channels: int,
|
| 74 |
+
cond_channels: int,
|
| 75 |
+
out_channels: int,
|
| 76 |
+
num_blocks: int,
|
| 77 |
+
num_heads: Optional[int] = None,
|
| 78 |
+
num_head_channels: Optional[int] = 64,
|
| 79 |
+
mlp_ratio: float = 4,
|
| 80 |
+
patch_size: int = 2,
|
| 81 |
+
num_io_res_blocks: int = 2,
|
| 82 |
+
io_block_channels: List[int] = None,
|
| 83 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 84 |
+
use_fp16: bool = False,
|
| 85 |
+
use_checkpoint: bool = False,
|
| 86 |
+
use_skip_connection: bool = True,
|
| 87 |
+
share_mod: bool = False,
|
| 88 |
+
qk_rms_norm: bool = False,
|
| 89 |
+
qk_rms_norm_cross: bool = False,
|
| 90 |
+
):
|
| 91 |
+
super().__init__()
|
| 92 |
+
self.resolution = resolution
|
| 93 |
+
self.in_channels = in_channels
|
| 94 |
+
self.model_channels = model_channels
|
| 95 |
+
self.cond_channels = cond_channels
|
| 96 |
+
self.out_channels = out_channels
|
| 97 |
+
self.num_blocks = num_blocks
|
| 98 |
+
self.num_heads = num_heads or model_channels // num_head_channels
|
| 99 |
+
self.mlp_ratio = mlp_ratio
|
| 100 |
+
self.patch_size = patch_size
|
| 101 |
+
self.num_io_res_blocks = num_io_res_blocks
|
| 102 |
+
self.io_block_channels = io_block_channels
|
| 103 |
+
self.pe_mode = pe_mode
|
| 104 |
+
self.use_fp16 = use_fp16
|
| 105 |
+
self.use_checkpoint = use_checkpoint
|
| 106 |
+
self.use_skip_connection = use_skip_connection
|
| 107 |
+
self.share_mod = share_mod
|
| 108 |
+
self.qk_rms_norm = qk_rms_norm
|
| 109 |
+
self.qk_rms_norm_cross = qk_rms_norm_cross
|
| 110 |
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
| 111 |
+
|
| 112 |
+
assert int(np.log2(patch_size)) == np.log2(patch_size), "Patch size must be a power of 2"
|
| 113 |
+
assert np.log2(patch_size) == len(io_block_channels), "Number of IO ResBlocks must match the number of stages"
|
| 114 |
+
|
| 115 |
+
self.t_embedder = TimestepEmbedder(model_channels)
|
| 116 |
+
if share_mod:
|
| 117 |
+
self.adaLN_modulation = nn.Sequential(
|
| 118 |
+
nn.SiLU(),
|
| 119 |
+
nn.Linear(model_channels, 6 * model_channels, bias=True)
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
if pe_mode == "ape":
|
| 123 |
+
self.pos_embedder = AbsolutePositionEmbedder(model_channels)
|
| 124 |
+
|
| 125 |
+
self.input_layer = sp.SparseLinear(in_channels, io_block_channels[0])
|
| 126 |
+
self.input_blocks = nn.ModuleList([])
|
| 127 |
+
for chs, next_chs in zip(io_block_channels, io_block_channels[1:] + [model_channels]):
|
| 128 |
+
self.input_blocks.extend([
|
| 129 |
+
SparseResBlock3d(
|
| 130 |
+
chs,
|
| 131 |
+
model_channels,
|
| 132 |
+
out_channels=chs,
|
| 133 |
+
)
|
| 134 |
+
for _ in range(num_io_res_blocks-1)
|
| 135 |
+
])
|
| 136 |
+
self.input_blocks.append(
|
| 137 |
+
SparseResBlock3d(
|
| 138 |
+
chs,
|
| 139 |
+
model_channels,
|
| 140 |
+
out_channels=next_chs,
|
| 141 |
+
downsample=True,
|
| 142 |
+
)
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
self.blocks = nn.ModuleList([
|
| 146 |
+
ModulatedSparseTransformerCrossBlock(
|
| 147 |
+
model_channels,
|
| 148 |
+
cond_channels,
|
| 149 |
+
num_heads=self.num_heads,
|
| 150 |
+
mlp_ratio=self.mlp_ratio,
|
| 151 |
+
attn_mode='full',
|
| 152 |
+
use_checkpoint=self.use_checkpoint,
|
| 153 |
+
use_rope=(pe_mode == "rope"),
|
| 154 |
+
share_mod=self.share_mod,
|
| 155 |
+
qk_rms_norm=self.qk_rms_norm,
|
| 156 |
+
qk_rms_norm_cross=self.qk_rms_norm_cross,
|
| 157 |
+
)
|
| 158 |
+
for _ in range(num_blocks)
|
| 159 |
+
])
|
| 160 |
+
|
| 161 |
+
self.out_blocks = nn.ModuleList([])
|
| 162 |
+
for chs, prev_chs in zip(reversed(io_block_channels), [model_channels] + list(reversed(io_block_channels[1:]))):
|
| 163 |
+
self.out_blocks.append(
|
| 164 |
+
SparseResBlock3d(
|
| 165 |
+
prev_chs * 2 if self.use_skip_connection else prev_chs,
|
| 166 |
+
model_channels,
|
| 167 |
+
out_channels=chs,
|
| 168 |
+
upsample=True,
|
| 169 |
+
)
|
| 170 |
+
)
|
| 171 |
+
self.out_blocks.extend([
|
| 172 |
+
SparseResBlock3d(
|
| 173 |
+
chs * 2 if self.use_skip_connection else chs,
|
| 174 |
+
model_channels,
|
| 175 |
+
out_channels=chs,
|
| 176 |
+
)
|
| 177 |
+
for _ in range(num_io_res_blocks-1)
|
| 178 |
+
])
|
| 179 |
+
self.out_layer = sp.SparseLinear(io_block_channels[0], out_channels)
|
| 180 |
+
|
| 181 |
+
self.initialize_weights()
|
| 182 |
+
if use_fp16:
|
| 183 |
+
self.convert_to_fp16()
|
| 184 |
+
|
| 185 |
+
@property
|
| 186 |
+
def device(self) -> torch.device:
|
| 187 |
+
"""
|
| 188 |
+
Return the device of the model.
|
| 189 |
+
"""
|
| 190 |
+
return next(self.parameters()).device
|
| 191 |
+
|
| 192 |
+
def convert_to_fp16(self) -> None:
|
| 193 |
+
"""
|
| 194 |
+
Convert the torso of the model to float16.
|
| 195 |
+
"""
|
| 196 |
+
self.input_blocks.apply(convert_module_to_f16)
|
| 197 |
+
self.blocks.apply(convert_module_to_f16)
|
| 198 |
+
self.out_blocks.apply(convert_module_to_f16)
|
| 199 |
+
|
| 200 |
+
def convert_to_fp32(self) -> None:
|
| 201 |
+
"""
|
| 202 |
+
Convert the torso of the model to float32.
|
| 203 |
+
"""
|
| 204 |
+
self.input_blocks.apply(convert_module_to_f32)
|
| 205 |
+
self.blocks.apply(convert_module_to_f32)
|
| 206 |
+
self.out_blocks.apply(convert_module_to_f32)
|
| 207 |
+
|
| 208 |
+
def initialize_weights(self) -> None:
|
| 209 |
+
# Initialize transformer layers:
|
| 210 |
+
def _basic_init(module):
|
| 211 |
+
if isinstance(module, nn.Linear):
|
| 212 |
+
torch.nn.init.xavier_uniform_(module.weight)
|
| 213 |
+
if module.bias is not None:
|
| 214 |
+
nn.init.constant_(module.bias, 0)
|
| 215 |
+
self.apply(_basic_init)
|
| 216 |
+
|
| 217 |
+
# Initialize timestep embedding MLP:
|
| 218 |
+
nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
|
| 219 |
+
nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
|
| 220 |
+
|
| 221 |
+
# Zero-out adaLN modulation layers in DiT blocks:
|
| 222 |
+
if self.share_mod:
|
| 223 |
+
nn.init.constant_(self.adaLN_modulation[-1].weight, 0)
|
| 224 |
+
nn.init.constant_(self.adaLN_modulation[-1].bias, 0)
|
| 225 |
+
else:
|
| 226 |
+
for block in self.blocks:
|
| 227 |
+
nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
|
| 228 |
+
nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
|
| 229 |
+
|
| 230 |
+
# Zero-out output layers:
|
| 231 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 232 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 233 |
+
|
| 234 |
+
def forward(self, x: sp.SparseTensor, t: torch.Tensor, cond: torch.Tensor) -> sp.SparseTensor:
|
| 235 |
+
h = self.input_layer(x).type(self.dtype)
|
| 236 |
+
t_emb = self.t_embedder(t)
|
| 237 |
+
if self.share_mod:
|
| 238 |
+
t_emb = self.adaLN_modulation(t_emb)
|
| 239 |
+
t_emb = t_emb.type(self.dtype)
|
| 240 |
+
cond = cond.type(self.dtype)
|
| 241 |
+
|
| 242 |
+
skips = []
|
| 243 |
+
# pack with input blocks
|
| 244 |
+
for block in self.input_blocks:
|
| 245 |
+
h = block(h, t_emb)
|
| 246 |
+
skips.append(h.feats)
|
| 247 |
+
|
| 248 |
+
if self.pe_mode == "ape":
|
| 249 |
+
h = h + self.pos_embedder(h.coords[:, 1:]).type(self.dtype)
|
| 250 |
+
for block in self.blocks:
|
| 251 |
+
h = block(h, t_emb, cond)
|
| 252 |
+
|
| 253 |
+
# unpack with output blocks
|
| 254 |
+
for block, skip in zip(self.out_blocks, reversed(skips)):
|
| 255 |
+
if self.use_skip_connection:
|
| 256 |
+
h = block(h.replace(torch.cat([h.feats, skip], dim=1)), t_emb)
|
| 257 |
+
else:
|
| 258 |
+
h = block(h, t_emb)
|
| 259 |
+
|
| 260 |
+
h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:]))
|
| 261 |
+
h = self.out_layer(h.type(x.dtype))
|
| 262 |
+
return h
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/__init__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .encoder import SLatEncoder
|
| 2 |
+
from .decoder_gs import SLatGaussianDecoder
|
| 3 |
+
from .decoder_rf import SLatRadianceFieldDecoder
|
| 4 |
+
from .decoder_mesh import SLatMeshDecoder
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/base.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from ...modules.utils import convert_module_to_f16, convert_module_to_f32
|
| 5 |
+
from ...modules import sparse as sp
|
| 6 |
+
from ...modules.transformer import AbsolutePositionEmbedder
|
| 7 |
+
from ...modules.sparse.transformer import SparseTransformerBlock
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def block_attn_config(self):
|
| 11 |
+
"""
|
| 12 |
+
Return the attention configuration of the model.
|
| 13 |
+
"""
|
| 14 |
+
for i in range(self.num_blocks):
|
| 15 |
+
if self.attn_mode == "shift_window":
|
| 16 |
+
yield "serialized", self.window_size, 0, (16 * (i % 2),) * 3, sp.SerializeMode.Z_ORDER
|
| 17 |
+
elif self.attn_mode == "shift_sequence":
|
| 18 |
+
yield "serialized", self.window_size, self.window_size // 2 * (i % 2), (0, 0, 0), sp.SerializeMode.Z_ORDER
|
| 19 |
+
elif self.attn_mode == "shift_order":
|
| 20 |
+
yield "serialized", self.window_size, 0, (0, 0, 0), sp.SerializeModes[i % 4]
|
| 21 |
+
elif self.attn_mode == "full":
|
| 22 |
+
yield "full", None, None, None, None
|
| 23 |
+
elif self.attn_mode == "swin":
|
| 24 |
+
yield "windowed", self.window_size, None, self.window_size // 2 * (i % 2), None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class SparseTransformerBase(nn.Module):
|
| 28 |
+
"""
|
| 29 |
+
Sparse Transformer without output layers.
|
| 30 |
+
Serve as the base class for encoder and decoder.
|
| 31 |
+
"""
|
| 32 |
+
def __init__(
|
| 33 |
+
self,
|
| 34 |
+
in_channels: int,
|
| 35 |
+
model_channels: int,
|
| 36 |
+
num_blocks: int,
|
| 37 |
+
num_heads: Optional[int] = None,
|
| 38 |
+
num_head_channels: Optional[int] = 64,
|
| 39 |
+
mlp_ratio: float = 4.0,
|
| 40 |
+
attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "full",
|
| 41 |
+
window_size: Optional[int] = None,
|
| 42 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 43 |
+
use_fp16: bool = False,
|
| 44 |
+
use_checkpoint: bool = False,
|
| 45 |
+
qk_rms_norm: bool = False,
|
| 46 |
+
):
|
| 47 |
+
super().__init__()
|
| 48 |
+
self.in_channels = in_channels
|
| 49 |
+
self.model_channels = model_channels
|
| 50 |
+
self.num_blocks = num_blocks
|
| 51 |
+
self.window_size = window_size
|
| 52 |
+
self.num_heads = num_heads or model_channels // num_head_channels
|
| 53 |
+
self.mlp_ratio = mlp_ratio
|
| 54 |
+
self.attn_mode = attn_mode
|
| 55 |
+
self.pe_mode = pe_mode
|
| 56 |
+
self.use_fp16 = use_fp16
|
| 57 |
+
self.use_checkpoint = use_checkpoint
|
| 58 |
+
self.qk_rms_norm = qk_rms_norm
|
| 59 |
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
| 60 |
+
|
| 61 |
+
if pe_mode == "ape":
|
| 62 |
+
self.pos_embedder = AbsolutePositionEmbedder(model_channels)
|
| 63 |
+
|
| 64 |
+
self.input_layer = sp.SparseLinear(in_channels, model_channels)
|
| 65 |
+
self.blocks = nn.ModuleList([
|
| 66 |
+
SparseTransformerBlock(
|
| 67 |
+
model_channels,
|
| 68 |
+
num_heads=self.num_heads,
|
| 69 |
+
mlp_ratio=self.mlp_ratio,
|
| 70 |
+
attn_mode=attn_mode,
|
| 71 |
+
window_size=window_size,
|
| 72 |
+
shift_sequence=shift_sequence,
|
| 73 |
+
shift_window=shift_window,
|
| 74 |
+
serialize_mode=serialize_mode,
|
| 75 |
+
use_checkpoint=self.use_checkpoint,
|
| 76 |
+
use_rope=(pe_mode == "rope"),
|
| 77 |
+
qk_rms_norm=self.qk_rms_norm,
|
| 78 |
+
)
|
| 79 |
+
for attn_mode, window_size, shift_sequence, shift_window, serialize_mode in block_attn_config(self)
|
| 80 |
+
])
|
| 81 |
+
|
| 82 |
+
@property
|
| 83 |
+
def device(self) -> torch.device:
|
| 84 |
+
"""
|
| 85 |
+
Return the device of the model.
|
| 86 |
+
"""
|
| 87 |
+
return next(self.parameters()).device
|
| 88 |
+
|
| 89 |
+
def convert_to_fp16(self) -> None:
|
| 90 |
+
"""
|
| 91 |
+
Convert the torso of the model to float16.
|
| 92 |
+
"""
|
| 93 |
+
self.blocks.apply(convert_module_to_f16)
|
| 94 |
+
|
| 95 |
+
def convert_to_fp32(self) -> None:
|
| 96 |
+
"""
|
| 97 |
+
Convert the torso of the model to float32.
|
| 98 |
+
"""
|
| 99 |
+
self.blocks.apply(convert_module_to_f32)
|
| 100 |
+
|
| 101 |
+
def initialize_weights(self) -> None:
|
| 102 |
+
# Initialize transformer layers:
|
| 103 |
+
def _basic_init(module):
|
| 104 |
+
if isinstance(module, nn.Linear):
|
| 105 |
+
torch.nn.init.xavier_uniform_(module.weight)
|
| 106 |
+
if module.bias is not None:
|
| 107 |
+
nn.init.constant_(module.bias, 0)
|
| 108 |
+
self.apply(_basic_init)
|
| 109 |
+
|
| 110 |
+
def forward(self, x: sp.SparseTensor) -> sp.SparseTensor:
|
| 111 |
+
h = self.input_layer(x)
|
| 112 |
+
if self.pe_mode == "ape":
|
| 113 |
+
h = h + self.pos_embedder(x.coords[:, 1:])
|
| 114 |
+
h = h.type(self.dtype)
|
| 115 |
+
for block in self.blocks:
|
| 116 |
+
h = block(h)
|
| 117 |
+
return h
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_gs.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from ...modules import sparse as sp
|
| 6 |
+
from ...utils.random_utils import hammersley_sequence
|
| 7 |
+
from .base import SparseTransformerBase
|
| 8 |
+
from ...representations import Gaussian
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SLatGaussianDecoder(SparseTransformerBase):
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
resolution: int,
|
| 15 |
+
model_channels: int,
|
| 16 |
+
latent_channels: int,
|
| 17 |
+
num_blocks: int,
|
| 18 |
+
num_heads: Optional[int] = None,
|
| 19 |
+
num_head_channels: Optional[int] = 64,
|
| 20 |
+
mlp_ratio: float = 4,
|
| 21 |
+
attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "swin",
|
| 22 |
+
window_size: int = 8,
|
| 23 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 24 |
+
use_fp16: bool = False,
|
| 25 |
+
use_checkpoint: bool = False,
|
| 26 |
+
qk_rms_norm: bool = False,
|
| 27 |
+
representation_config: dict = None,
|
| 28 |
+
):
|
| 29 |
+
super().__init__(
|
| 30 |
+
in_channels=latent_channels,
|
| 31 |
+
model_channels=model_channels,
|
| 32 |
+
num_blocks=num_blocks,
|
| 33 |
+
num_heads=num_heads,
|
| 34 |
+
num_head_channels=num_head_channels,
|
| 35 |
+
mlp_ratio=mlp_ratio,
|
| 36 |
+
attn_mode=attn_mode,
|
| 37 |
+
window_size=window_size,
|
| 38 |
+
pe_mode=pe_mode,
|
| 39 |
+
use_fp16=use_fp16,
|
| 40 |
+
use_checkpoint=use_checkpoint,
|
| 41 |
+
qk_rms_norm=qk_rms_norm,
|
| 42 |
+
)
|
| 43 |
+
self.resolution = resolution
|
| 44 |
+
self.rep_config = representation_config
|
| 45 |
+
self._calc_layout()
|
| 46 |
+
self.out_layer = sp.SparseLinear(model_channels, self.out_channels)
|
| 47 |
+
self._build_perturbation()
|
| 48 |
+
|
| 49 |
+
self.initialize_weights()
|
| 50 |
+
if use_fp16:
|
| 51 |
+
self.convert_to_fp16()
|
| 52 |
+
|
| 53 |
+
def initialize_weights(self) -> None:
|
| 54 |
+
super().initialize_weights()
|
| 55 |
+
# Zero-out output layers:
|
| 56 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 57 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 58 |
+
|
| 59 |
+
def _build_perturbation(self) -> None:
|
| 60 |
+
perturbation = [hammersley_sequence(3, i, self.rep_config['num_gaussians']) for i in range(self.rep_config['num_gaussians'])]
|
| 61 |
+
perturbation = torch.tensor(perturbation).float() * 2 - 1
|
| 62 |
+
perturbation = perturbation / self.rep_config['voxel_size']
|
| 63 |
+
perturbation = torch.atanh(perturbation).to(self.device)
|
| 64 |
+
self.register_buffer('offset_perturbation', perturbation)
|
| 65 |
+
|
| 66 |
+
def _calc_layout(self) -> None:
|
| 67 |
+
self.layout = {
|
| 68 |
+
'_xyz' : {'shape': (self.rep_config['num_gaussians'], 3), 'size': self.rep_config['num_gaussians'] * 3},
|
| 69 |
+
'_features_dc' : {'shape': (self.rep_config['num_gaussians'], 1, 3), 'size': self.rep_config['num_gaussians'] * 3},
|
| 70 |
+
'_scaling' : {'shape': (self.rep_config['num_gaussians'], 3), 'size': self.rep_config['num_gaussians'] * 3},
|
| 71 |
+
'_rotation' : {'shape': (self.rep_config['num_gaussians'], 4), 'size': self.rep_config['num_gaussians'] * 4},
|
| 72 |
+
'_opacity' : {'shape': (self.rep_config['num_gaussians'], 1), 'size': self.rep_config['num_gaussians']},
|
| 73 |
+
}
|
| 74 |
+
start = 0
|
| 75 |
+
for k, v in self.layout.items():
|
| 76 |
+
v['range'] = (start, start + v['size'])
|
| 77 |
+
start += v['size']
|
| 78 |
+
self.out_channels = start
|
| 79 |
+
|
| 80 |
+
def to_representation(self, x: sp.SparseTensor) -> List[Gaussian]:
|
| 81 |
+
"""
|
| 82 |
+
Convert a batch of network outputs to 3D representations.
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
x: The [N x * x C] sparse tensor output by the network.
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
list of representations
|
| 89 |
+
"""
|
| 90 |
+
ret = []
|
| 91 |
+
for i in range(x.shape[0]):
|
| 92 |
+
representation = Gaussian(
|
| 93 |
+
sh_degree=0,
|
| 94 |
+
aabb=[-0.5, -0.5, -0.5, 1.0, 1.0, 1.0],
|
| 95 |
+
mininum_kernel_size = self.rep_config['3d_filter_kernel_size'],
|
| 96 |
+
scaling_bias = self.rep_config['scaling_bias'],
|
| 97 |
+
opacity_bias = self.rep_config['opacity_bias'],
|
| 98 |
+
scaling_activation = self.rep_config['scaling_activation']
|
| 99 |
+
)
|
| 100 |
+
xyz = (x.coords[x.layout[i]][:, 1:].float() + 0.5) / self.resolution
|
| 101 |
+
for k, v in self.layout.items():
|
| 102 |
+
if k == '_xyz':
|
| 103 |
+
offset = x.feats[x.layout[i]][:, v['range'][0]:v['range'][1]].reshape(-1, *v['shape'])
|
| 104 |
+
offset = offset * self.rep_config['lr'][k]
|
| 105 |
+
if self.rep_config['perturb_offset']:
|
| 106 |
+
offset = offset + self.offset_perturbation
|
| 107 |
+
offset = torch.tanh(offset) / self.resolution * 0.5 * self.rep_config['voxel_size']
|
| 108 |
+
_xyz = xyz.unsqueeze(1) + offset
|
| 109 |
+
setattr(representation, k, _xyz.flatten(0, 1))
|
| 110 |
+
else:
|
| 111 |
+
feats = x.feats[x.layout[i]][:, v['range'][0]:v['range'][1]].reshape(-1, *v['shape']).flatten(0, 1)
|
| 112 |
+
feats = feats * self.rep_config['lr'][k]
|
| 113 |
+
setattr(representation, k, feats)
|
| 114 |
+
ret.append(representation)
|
| 115 |
+
return ret
|
| 116 |
+
|
| 117 |
+
def forward(self, x: sp.SparseTensor) -> List[Gaussian]:
|
| 118 |
+
h = super().forward(x)
|
| 119 |
+
h = h.type(x.dtype)
|
| 120 |
+
h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:]))
|
| 121 |
+
h = self.out_layer(h)
|
| 122 |
+
return self.to_representation(h)
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_mesh.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ...modules.utils import zero_module, convert_module_to_f16, convert_module_to_f32
|
| 7 |
+
from ...modules import sparse as sp
|
| 8 |
+
from .base import SparseTransformerBase
|
| 9 |
+
from ...representations import MeshExtractResult
|
| 10 |
+
from ...representations.mesh import SparseFeatures2Mesh
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class SparseSubdivideBlock3d(nn.Module):
|
| 14 |
+
"""
|
| 15 |
+
A 3D subdivide block that can subdivide the sparse tensor.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
channels: channels in the inputs and outputs.
|
| 19 |
+
out_channels: if specified, the number of output channels.
|
| 20 |
+
num_groups: the number of groups for the group norm.
|
| 21 |
+
"""
|
| 22 |
+
def __init__(
|
| 23 |
+
self,
|
| 24 |
+
channels: int,
|
| 25 |
+
resolution: int,
|
| 26 |
+
out_channels: Optional[int] = None,
|
| 27 |
+
num_groups: int = 32
|
| 28 |
+
):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.channels = channels
|
| 31 |
+
self.resolution = resolution
|
| 32 |
+
self.out_resolution = resolution * 2
|
| 33 |
+
self.out_channels = out_channels or channels
|
| 34 |
+
|
| 35 |
+
self.act_layers = nn.Sequential(
|
| 36 |
+
sp.SparseGroupNorm32(num_groups, channels),
|
| 37 |
+
sp.SparseSiLU()
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
self.sub = sp.SparseSubdivide()
|
| 41 |
+
|
| 42 |
+
self.out_layers = nn.Sequential(
|
| 43 |
+
sp.SparseConv3d(channels, self.out_channels, 3, indice_key=f"res_{self.out_resolution}"),
|
| 44 |
+
sp.SparseGroupNorm32(num_groups, self.out_channels),
|
| 45 |
+
sp.SparseSiLU(),
|
| 46 |
+
zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3, indice_key=f"res_{self.out_resolution}")),
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if self.out_channels == channels:
|
| 50 |
+
self.skip_connection = nn.Identity()
|
| 51 |
+
else:
|
| 52 |
+
self.skip_connection = sp.SparseConv3d(channels, self.out_channels, 1, indice_key=f"res_{self.out_resolution}")
|
| 53 |
+
|
| 54 |
+
def forward(self, x: sp.SparseTensor) -> sp.SparseTensor:
|
| 55 |
+
"""
|
| 56 |
+
Apply the block to a Tensor, conditioned on a timestep embedding.
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
x: an [N x C x ...] Tensor of features.
|
| 60 |
+
Returns:
|
| 61 |
+
an [N x C x ...] Tensor of outputs.
|
| 62 |
+
"""
|
| 63 |
+
h = self.act_layers(x)
|
| 64 |
+
h = self.sub(h)
|
| 65 |
+
x = self.sub(x)
|
| 66 |
+
h = self.out_layers(h)
|
| 67 |
+
h = h + self.skip_connection(x)
|
| 68 |
+
return h
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class SLatMeshDecoder(SparseTransformerBase):
|
| 72 |
+
def __init__(
|
| 73 |
+
self,
|
| 74 |
+
resolution: int,
|
| 75 |
+
model_channels: int,
|
| 76 |
+
latent_channels: int,
|
| 77 |
+
num_blocks: int,
|
| 78 |
+
num_heads: Optional[int] = None,
|
| 79 |
+
num_head_channels: Optional[int] = 64,
|
| 80 |
+
mlp_ratio: float = 4,
|
| 81 |
+
attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "swin",
|
| 82 |
+
window_size: int = 8,
|
| 83 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 84 |
+
use_fp16: bool = False,
|
| 85 |
+
use_checkpoint: bool = False,
|
| 86 |
+
qk_rms_norm: bool = False,
|
| 87 |
+
representation_config: dict = None,
|
| 88 |
+
):
|
| 89 |
+
super().__init__(
|
| 90 |
+
in_channels=latent_channels,
|
| 91 |
+
model_channels=model_channels,
|
| 92 |
+
num_blocks=num_blocks,
|
| 93 |
+
num_heads=num_heads,
|
| 94 |
+
num_head_channels=num_head_channels,
|
| 95 |
+
mlp_ratio=mlp_ratio,
|
| 96 |
+
attn_mode=attn_mode,
|
| 97 |
+
window_size=window_size,
|
| 98 |
+
pe_mode=pe_mode,
|
| 99 |
+
use_fp16=use_fp16,
|
| 100 |
+
use_checkpoint=use_checkpoint,
|
| 101 |
+
qk_rms_norm=qk_rms_norm,
|
| 102 |
+
)
|
| 103 |
+
self.resolution = resolution
|
| 104 |
+
self.rep_config = representation_config
|
| 105 |
+
self.mesh_extractor = SparseFeatures2Mesh(res=self.resolution*4, use_color=self.rep_config.get('use_color', False))
|
| 106 |
+
self.out_channels = self.mesh_extractor.feats_channels
|
| 107 |
+
self.upsample = nn.ModuleList([
|
| 108 |
+
SparseSubdivideBlock3d(
|
| 109 |
+
channels=model_channels,
|
| 110 |
+
resolution=resolution,
|
| 111 |
+
out_channels=model_channels // 4
|
| 112 |
+
),
|
| 113 |
+
SparseSubdivideBlock3d(
|
| 114 |
+
channels=model_channels // 4,
|
| 115 |
+
resolution=resolution * 2,
|
| 116 |
+
out_channels=model_channels // 8
|
| 117 |
+
)
|
| 118 |
+
])
|
| 119 |
+
self.out_layer = sp.SparseLinear(model_channels // 8, self.out_channels)
|
| 120 |
+
|
| 121 |
+
self.initialize_weights()
|
| 122 |
+
if use_fp16:
|
| 123 |
+
self.convert_to_fp16()
|
| 124 |
+
|
| 125 |
+
def initialize_weights(self) -> None:
|
| 126 |
+
super().initialize_weights()
|
| 127 |
+
# Zero-out output layers:
|
| 128 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 129 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 130 |
+
|
| 131 |
+
def convert_to_fp16(self) -> None:
|
| 132 |
+
"""
|
| 133 |
+
Convert the torso of the model to float16.
|
| 134 |
+
"""
|
| 135 |
+
super().convert_to_fp16()
|
| 136 |
+
self.upsample.apply(convert_module_to_f16)
|
| 137 |
+
|
| 138 |
+
def convert_to_fp32(self) -> None:
|
| 139 |
+
"""
|
| 140 |
+
Convert the torso of the model to float32.
|
| 141 |
+
"""
|
| 142 |
+
super().convert_to_fp32()
|
| 143 |
+
self.upsample.apply(convert_module_to_f32)
|
| 144 |
+
|
| 145 |
+
def to_representation(self, x: sp.SparseTensor) -> List[MeshExtractResult]:
|
| 146 |
+
"""
|
| 147 |
+
Convert a batch of network outputs to 3D representations.
|
| 148 |
+
|
| 149 |
+
Args:
|
| 150 |
+
x: The [N x * x C] sparse tensor output by the network.
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
list of representations
|
| 154 |
+
"""
|
| 155 |
+
ret = []
|
| 156 |
+
for i in range(x.shape[0]):
|
| 157 |
+
mesh = self.mesh_extractor(x[i], training=self.training)
|
| 158 |
+
ret.append(mesh)
|
| 159 |
+
return ret
|
| 160 |
+
|
| 161 |
+
def forward(self, x: sp.SparseTensor) -> List[MeshExtractResult]:
|
| 162 |
+
h = super().forward(x)
|
| 163 |
+
for block in self.upsample:
|
| 164 |
+
h = block(h)
|
| 165 |
+
h = h.type(x.dtype)
|
| 166 |
+
h = self.out_layer(h)
|
| 167 |
+
return self.to_representation(h)
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/decoder_rf.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ...modules import sparse as sp
|
| 7 |
+
from .base import SparseTransformerBase
|
| 8 |
+
from ...representations import Strivec
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SLatRadianceFieldDecoder(SparseTransformerBase):
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
resolution: int,
|
| 15 |
+
model_channels: int,
|
| 16 |
+
latent_channels: int,
|
| 17 |
+
num_blocks: int,
|
| 18 |
+
num_heads: Optional[int] = None,
|
| 19 |
+
num_head_channels: Optional[int] = 64,
|
| 20 |
+
mlp_ratio: float = 4,
|
| 21 |
+
attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "swin",
|
| 22 |
+
window_size: int = 8,
|
| 23 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 24 |
+
use_fp16: bool = False,
|
| 25 |
+
use_checkpoint: bool = False,
|
| 26 |
+
qk_rms_norm: bool = False,
|
| 27 |
+
representation_config: dict = None,
|
| 28 |
+
):
|
| 29 |
+
super().__init__(
|
| 30 |
+
in_channels=latent_channels,
|
| 31 |
+
model_channels=model_channels,
|
| 32 |
+
num_blocks=num_blocks,
|
| 33 |
+
num_heads=num_heads,
|
| 34 |
+
num_head_channels=num_head_channels,
|
| 35 |
+
mlp_ratio=mlp_ratio,
|
| 36 |
+
attn_mode=attn_mode,
|
| 37 |
+
window_size=window_size,
|
| 38 |
+
pe_mode=pe_mode,
|
| 39 |
+
use_fp16=use_fp16,
|
| 40 |
+
use_checkpoint=use_checkpoint,
|
| 41 |
+
qk_rms_norm=qk_rms_norm,
|
| 42 |
+
)
|
| 43 |
+
self.resolution = resolution
|
| 44 |
+
self.rep_config = representation_config
|
| 45 |
+
self._calc_layout()
|
| 46 |
+
self.out_layer = sp.SparseLinear(model_channels, self.out_channels)
|
| 47 |
+
|
| 48 |
+
self.initialize_weights()
|
| 49 |
+
if use_fp16:
|
| 50 |
+
self.convert_to_fp16()
|
| 51 |
+
|
| 52 |
+
def initialize_weights(self) -> None:
|
| 53 |
+
super().initialize_weights()
|
| 54 |
+
# Zero-out output layers:
|
| 55 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 56 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 57 |
+
|
| 58 |
+
def _calc_layout(self) -> None:
|
| 59 |
+
self.layout = {
|
| 60 |
+
'trivec': {'shape': (self.rep_config['rank'], 3, self.rep_config['dim']), 'size': self.rep_config['rank'] * 3 * self.rep_config['dim']},
|
| 61 |
+
'density': {'shape': (self.rep_config['rank'],), 'size': self.rep_config['rank']},
|
| 62 |
+
'features_dc': {'shape': (self.rep_config['rank'], 1, 3), 'size': self.rep_config['rank'] * 3},
|
| 63 |
+
}
|
| 64 |
+
start = 0
|
| 65 |
+
for k, v in self.layout.items():
|
| 66 |
+
v['range'] = (start, start + v['size'])
|
| 67 |
+
start += v['size']
|
| 68 |
+
self.out_channels = start
|
| 69 |
+
|
| 70 |
+
def to_representation(self, x: sp.SparseTensor) -> List[Strivec]:
|
| 71 |
+
"""
|
| 72 |
+
Convert a batch of network outputs to 3D representations.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
x: The [N x * x C] sparse tensor output by the network.
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
list of representations
|
| 79 |
+
"""
|
| 80 |
+
ret = []
|
| 81 |
+
for i in range(x.shape[0]):
|
| 82 |
+
representation = Strivec(
|
| 83 |
+
sh_degree=0,
|
| 84 |
+
resolution=self.resolution,
|
| 85 |
+
aabb=[-0.5, -0.5, -0.5, 1, 1, 1],
|
| 86 |
+
rank=self.rep_config['rank'],
|
| 87 |
+
dim=self.rep_config['dim'],
|
| 88 |
+
device='cuda',
|
| 89 |
+
)
|
| 90 |
+
representation.density_shift = 0.0
|
| 91 |
+
representation.position = (x.coords[x.layout[i]][:, 1:].float() + 0.5) / self.resolution
|
| 92 |
+
representation.depth = torch.full((representation.position.shape[0], 1), int(np.log2(self.resolution)), dtype=torch.uint8, device='cuda')
|
| 93 |
+
for k, v in self.layout.items():
|
| 94 |
+
setattr(representation, k, x.feats[x.layout[i]][:, v['range'][0]:v['range'][1]].reshape(-1, *v['shape']))
|
| 95 |
+
representation.trivec = representation.trivec + 1
|
| 96 |
+
ret.append(representation)
|
| 97 |
+
return ret
|
| 98 |
+
|
| 99 |
+
def forward(self, x: sp.SparseTensor) -> List[Strivec]:
|
| 100 |
+
h = super().forward(x)
|
| 101 |
+
h = h.type(x.dtype)
|
| 102 |
+
h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:]))
|
| 103 |
+
h = self.out_layer(h)
|
| 104 |
+
return self.to_representation(h)
|
thirdparty/TRELLIS/trellis/models/structured_latent_vae/encoder.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from ...modules import sparse as sp
|
| 6 |
+
from .base import SparseTransformerBase
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class SLatEncoder(SparseTransformerBase):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
resolution: int,
|
| 13 |
+
in_channels: int,
|
| 14 |
+
model_channels: int,
|
| 15 |
+
latent_channels: int,
|
| 16 |
+
num_blocks: int,
|
| 17 |
+
num_heads: Optional[int] = None,
|
| 18 |
+
num_head_channels: Optional[int] = 64,
|
| 19 |
+
mlp_ratio: float = 4,
|
| 20 |
+
attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "swin",
|
| 21 |
+
window_size: int = 8,
|
| 22 |
+
pe_mode: Literal["ape", "rope"] = "ape",
|
| 23 |
+
use_fp16: bool = False,
|
| 24 |
+
use_checkpoint: bool = False,
|
| 25 |
+
qk_rms_norm: bool = False,
|
| 26 |
+
):
|
| 27 |
+
super().__init__(
|
| 28 |
+
in_channels=in_channels,
|
| 29 |
+
model_channels=model_channels,
|
| 30 |
+
num_blocks=num_blocks,
|
| 31 |
+
num_heads=num_heads,
|
| 32 |
+
num_head_channels=num_head_channels,
|
| 33 |
+
mlp_ratio=mlp_ratio,
|
| 34 |
+
attn_mode=attn_mode,
|
| 35 |
+
window_size=window_size,
|
| 36 |
+
pe_mode=pe_mode,
|
| 37 |
+
use_fp16=use_fp16,
|
| 38 |
+
use_checkpoint=use_checkpoint,
|
| 39 |
+
qk_rms_norm=qk_rms_norm,
|
| 40 |
+
)
|
| 41 |
+
self.resolution = resolution
|
| 42 |
+
self.out_layer = sp.SparseLinear(model_channels, 2 * latent_channels)
|
| 43 |
+
|
| 44 |
+
self.initialize_weights()
|
| 45 |
+
if use_fp16:
|
| 46 |
+
self.convert_to_fp16()
|
| 47 |
+
|
| 48 |
+
def initialize_weights(self) -> None:
|
| 49 |
+
super().initialize_weights()
|
| 50 |
+
# Zero-out output layers:
|
| 51 |
+
nn.init.constant_(self.out_layer.weight, 0)
|
| 52 |
+
nn.init.constant_(self.out_layer.bias, 0)
|
| 53 |
+
|
| 54 |
+
def forward(self, x: sp.SparseTensor, sample_posterior=True, return_raw=False):
|
| 55 |
+
h = super().forward(x)
|
| 56 |
+
h = h.type(x.dtype)
|
| 57 |
+
h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:]))
|
| 58 |
+
h = self.out_layer(h)
|
| 59 |
+
|
| 60 |
+
# Sample from the posterior distribution
|
| 61 |
+
mean, logvar = h.feats.chunk(2, dim=-1)
|
| 62 |
+
if sample_posterior:
|
| 63 |
+
std = torch.exp(0.5 * logvar)
|
| 64 |
+
z = mean + std * torch.randn_like(std)
|
| 65 |
+
else:
|
| 66 |
+
z = mean
|
| 67 |
+
z = h.replace(z)
|
| 68 |
+
|
| 69 |
+
if return_raw:
|
| 70 |
+
return z, mean, logvar
|
| 71 |
+
else:
|
| 72 |
+
return z
|
thirdparty/TRELLIS/trellis/modules/attention/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
|
| 3 |
+
BACKEND = 'flash_attn'
|
| 4 |
+
DEBUG = False
|
| 5 |
+
|
| 6 |
+
def __from_env():
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
global BACKEND
|
| 10 |
+
global DEBUG
|
| 11 |
+
|
| 12 |
+
env_attn_backend = os.environ.get('ATTN_BACKEND')
|
| 13 |
+
env_sttn_debug = os.environ.get('ATTN_DEBUG')
|
| 14 |
+
|
| 15 |
+
if env_attn_backend is not None and env_attn_backend in ['xformers', 'flash_attn', 'sdpa', 'naive']:
|
| 16 |
+
BACKEND = env_attn_backend
|
| 17 |
+
if env_sttn_debug is not None:
|
| 18 |
+
DEBUG = env_sttn_debug == '1'
|
| 19 |
+
|
| 20 |
+
print(f"[ATTENTION] Using backend: {BACKEND}")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
__from_env()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def set_backend(backend: Literal['xformers', 'flash_attn']):
|
| 27 |
+
global BACKEND
|
| 28 |
+
BACKEND = backend
|
| 29 |
+
|
| 30 |
+
def set_debug(debug: bool):
|
| 31 |
+
global DEBUG
|
| 32 |
+
DEBUG = debug
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
from .full_attn import *
|
| 36 |
+
from .modules import *
|
thirdparty/TRELLIS/trellis/modules/attention/full_attn.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import math
|
| 4 |
+
from . import DEBUG, BACKEND
|
| 5 |
+
|
| 6 |
+
if BACKEND == 'xformers':
|
| 7 |
+
import xformers.ops as xops
|
| 8 |
+
elif BACKEND == 'flash_attn':
|
| 9 |
+
import flash_attn
|
| 10 |
+
elif BACKEND == 'sdpa':
|
| 11 |
+
from torch.nn.functional import scaled_dot_product_attention as sdpa
|
| 12 |
+
elif BACKEND == 'naive':
|
| 13 |
+
pass
|
| 14 |
+
else:
|
| 15 |
+
raise ValueError(f"Unknown attention backend: {BACKEND}")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
__all__ = [
|
| 19 |
+
'scaled_dot_product_attention',
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _naive_sdpa(q, k, v):
|
| 24 |
+
"""
|
| 25 |
+
Naive implementation of scaled dot product attention.
|
| 26 |
+
"""
|
| 27 |
+
q = q.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 28 |
+
k = k.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 29 |
+
v = v.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 30 |
+
scale_factor = 1 / math.sqrt(q.size(-1))
|
| 31 |
+
attn_weight = q @ k.transpose(-2, -1) * scale_factor
|
| 32 |
+
attn_weight = torch.softmax(attn_weight, dim=-1)
|
| 33 |
+
out = attn_weight @ v
|
| 34 |
+
out = out.permute(0, 2, 1, 3) # [N, L, H, C]
|
| 35 |
+
return out
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@overload
|
| 39 |
+
def scaled_dot_product_attention(qkv: torch.Tensor) -> torch.Tensor:
|
| 40 |
+
"""
|
| 41 |
+
Apply scaled dot product attention.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
qkv (torch.Tensor): A [N, L, 3, H, C] tensor containing Qs, Ks, and Vs.
|
| 45 |
+
"""
|
| 46 |
+
...
|
| 47 |
+
|
| 48 |
+
@overload
|
| 49 |
+
def scaled_dot_product_attention(q: torch.Tensor, kv: torch.Tensor) -> torch.Tensor:
|
| 50 |
+
"""
|
| 51 |
+
Apply scaled dot product attention.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
q (torch.Tensor): A [N, L, H, C] tensor containing Qs.
|
| 55 |
+
kv (torch.Tensor): A [N, L, 2, H, C] tensor containing Ks and Vs.
|
| 56 |
+
"""
|
| 57 |
+
...
|
| 58 |
+
|
| 59 |
+
@overload
|
| 60 |
+
def scaled_dot_product_attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.Tensor:
|
| 61 |
+
"""
|
| 62 |
+
Apply scaled dot product attention.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
q (torch.Tensor): A [N, L, H, Ci] tensor containing Qs.
|
| 66 |
+
k (torch.Tensor): A [N, L, H, Ci] tensor containing Ks.
|
| 67 |
+
v (torch.Tensor): A [N, L, H, Co] tensor containing Vs.
|
| 68 |
+
|
| 69 |
+
Note:
|
| 70 |
+
k and v are assumed to have the same coordinate map.
|
| 71 |
+
"""
|
| 72 |
+
...
|
| 73 |
+
|
| 74 |
+
def scaled_dot_product_attention(*args, **kwargs):
|
| 75 |
+
arg_names_dict = {
|
| 76 |
+
1: ['qkv'],
|
| 77 |
+
2: ['q', 'kv'],
|
| 78 |
+
3: ['q', 'k', 'v']
|
| 79 |
+
}
|
| 80 |
+
num_all_args = len(args) + len(kwargs)
|
| 81 |
+
assert num_all_args in arg_names_dict, f"Invalid number of arguments, got {num_all_args}, expected 1, 2, or 3"
|
| 82 |
+
for key in arg_names_dict[num_all_args][len(args):]:
|
| 83 |
+
assert key in kwargs, f"Missing argument {key}"
|
| 84 |
+
|
| 85 |
+
if num_all_args == 1:
|
| 86 |
+
qkv = args[0] if len(args) > 0 else kwargs['qkv']
|
| 87 |
+
assert len(qkv.shape) == 5 and qkv.shape[2] == 3, f"Invalid shape for qkv, got {qkv.shape}, expected [N, L, 3, H, C]"
|
| 88 |
+
device = qkv.device
|
| 89 |
+
|
| 90 |
+
elif num_all_args == 2:
|
| 91 |
+
q = args[0] if len(args) > 0 else kwargs['q']
|
| 92 |
+
kv = args[1] if len(args) > 1 else kwargs['kv']
|
| 93 |
+
assert q.shape[0] == kv.shape[0], f"Batch size mismatch, got {q.shape[0]} and {kv.shape[0]}"
|
| 94 |
+
assert len(q.shape) == 4, f"Invalid shape for q, got {q.shape}, expected [N, L, H, C]"
|
| 95 |
+
assert len(kv.shape) == 5, f"Invalid shape for kv, got {kv.shape}, expected [N, L, 2, H, C]"
|
| 96 |
+
device = q.device
|
| 97 |
+
|
| 98 |
+
elif num_all_args == 3:
|
| 99 |
+
q = args[0] if len(args) > 0 else kwargs['q']
|
| 100 |
+
k = args[1] if len(args) > 1 else kwargs['k']
|
| 101 |
+
v = args[2] if len(args) > 2 else kwargs['v']
|
| 102 |
+
assert q.shape[0] == k.shape[0] == v.shape[0], f"Batch size mismatch, got {q.shape[0]}, {k.shape[0]}, and {v.shape[0]}"
|
| 103 |
+
assert len(q.shape) == 4, f"Invalid shape for q, got {q.shape}, expected [N, L, H, Ci]"
|
| 104 |
+
assert len(k.shape) == 4, f"Invalid shape for k, got {k.shape}, expected [N, L, H, Ci]"
|
| 105 |
+
assert len(v.shape) == 4, f"Invalid shape for v, got {v.shape}, expected [N, L, H, Co]"
|
| 106 |
+
device = q.device
|
| 107 |
+
|
| 108 |
+
if BACKEND == 'xformers':
|
| 109 |
+
if num_all_args == 1:
|
| 110 |
+
q, k, v = qkv.unbind(dim=2)
|
| 111 |
+
elif num_all_args == 2:
|
| 112 |
+
k, v = kv.unbind(dim=2)
|
| 113 |
+
out = xops.memory_efficient_attention(q, k, v)
|
| 114 |
+
elif BACKEND == 'flash_attn':
|
| 115 |
+
if num_all_args == 1:
|
| 116 |
+
out = flash_attn.flash_attn_qkvpacked_func(qkv)
|
| 117 |
+
elif num_all_args == 2:
|
| 118 |
+
out = flash_attn.flash_attn_kvpacked_func(q, kv)
|
| 119 |
+
elif num_all_args == 3:
|
| 120 |
+
out = flash_attn.flash_attn_func(q, k, v)
|
| 121 |
+
elif BACKEND == 'sdpa':
|
| 122 |
+
if num_all_args == 1:
|
| 123 |
+
q, k, v = qkv.unbind(dim=2)
|
| 124 |
+
elif num_all_args == 2:
|
| 125 |
+
k, v = kv.unbind(dim=2)
|
| 126 |
+
q = q.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 127 |
+
k = k.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 128 |
+
v = v.permute(0, 2, 1, 3) # [N, H, L, C]
|
| 129 |
+
out = sdpa(q, k, v) # [N, H, L, C]
|
| 130 |
+
out = out.permute(0, 2, 1, 3) # [N, L, H, C]
|
| 131 |
+
elif BACKEND == 'naive':
|
| 132 |
+
if num_all_args == 1:
|
| 133 |
+
q, k, v = qkv.unbind(dim=2)
|
| 134 |
+
elif num_all_args == 2:
|
| 135 |
+
k, v = kv.unbind(dim=2)
|
| 136 |
+
out = _naive_sdpa(q, k, v)
|
| 137 |
+
else:
|
| 138 |
+
raise ValueError(f"Unknown attention module: {BACKEND}")
|
| 139 |
+
|
| 140 |
+
return out
|
thirdparty/TRELLIS/trellis/modules/attention/modules.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from .full_attn import scaled_dot_product_attention
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class MultiHeadRMSNorm(nn.Module):
|
| 9 |
+
def __init__(self, dim: int, heads: int):
|
| 10 |
+
super().__init__()
|
| 11 |
+
self.scale = dim ** 0.5
|
| 12 |
+
self.gamma = nn.Parameter(torch.ones(heads, dim))
|
| 13 |
+
|
| 14 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 15 |
+
return (F.normalize(x.float(), dim = -1) * self.gamma * self.scale).to(x.dtype)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class RotaryPositionEmbedder(nn.Module):
|
| 19 |
+
def __init__(self, hidden_size: int, in_channels: int = 3):
|
| 20 |
+
super().__init__()
|
| 21 |
+
assert hidden_size % 2 == 0, "Hidden size must be divisible by 2"
|
| 22 |
+
self.hidden_size = hidden_size
|
| 23 |
+
self.in_channels = in_channels
|
| 24 |
+
self.freq_dim = hidden_size // in_channels // 2
|
| 25 |
+
self.freqs = torch.arange(self.freq_dim, dtype=torch.float32) / self.freq_dim
|
| 26 |
+
self.freqs = 1.0 / (10000 ** self.freqs)
|
| 27 |
+
|
| 28 |
+
def _get_phases(self, indices: torch.Tensor) -> torch.Tensor:
|
| 29 |
+
self.freqs = self.freqs.to(indices.device)
|
| 30 |
+
phases = torch.outer(indices, self.freqs)
|
| 31 |
+
phases = torch.polar(torch.ones_like(phases), phases)
|
| 32 |
+
return phases
|
| 33 |
+
|
| 34 |
+
def _rotary_embedding(self, x: torch.Tensor, phases: torch.Tensor) -> torch.Tensor:
|
| 35 |
+
x_complex = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2))
|
| 36 |
+
x_rotated = x_complex * phases
|
| 37 |
+
x_embed = torch.view_as_real(x_rotated).reshape(*x_rotated.shape[:-1], -1).to(x.dtype)
|
| 38 |
+
return x_embed
|
| 39 |
+
|
| 40 |
+
def forward(self, q: torch.Tensor, k: torch.Tensor, indices: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 41 |
+
"""
|
| 42 |
+
Args:
|
| 43 |
+
q (sp.SparseTensor): [..., N, D] tensor of queries
|
| 44 |
+
k (sp.SparseTensor): [..., N, D] tensor of keys
|
| 45 |
+
indices (torch.Tensor): [..., N, C] tensor of spatial positions
|
| 46 |
+
"""
|
| 47 |
+
if indices is None:
|
| 48 |
+
indices = torch.arange(q.shape[-2], device=q.device)
|
| 49 |
+
if len(q.shape) > 2:
|
| 50 |
+
indices = indices.unsqueeze(0).expand(q.shape[:-2] + (-1,))
|
| 51 |
+
|
| 52 |
+
phases = self._get_phases(indices.reshape(-1)).reshape(*indices.shape[:-1], -1)
|
| 53 |
+
if phases.shape[1] < self.hidden_size // 2:
|
| 54 |
+
phases = torch.cat([phases, torch.polar(
|
| 55 |
+
torch.ones(*phases.shape[:-1], self.hidden_size // 2 - phases.shape[1], device=phases.device),
|
| 56 |
+
torch.zeros(*phases.shape[:-1], self.hidden_size // 2 - phases.shape[1], device=phases.device)
|
| 57 |
+
)], dim=-1)
|
| 58 |
+
q_embed = self._rotary_embedding(q, phases)
|
| 59 |
+
k_embed = self._rotary_embedding(k, phases)
|
| 60 |
+
return q_embed, k_embed
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class MultiHeadAttention(nn.Module):
|
| 64 |
+
def __init__(
|
| 65 |
+
self,
|
| 66 |
+
channels: int,
|
| 67 |
+
num_heads: int,
|
| 68 |
+
ctx_channels: Optional[int]=None,
|
| 69 |
+
type: Literal["self", "cross"] = "self",
|
| 70 |
+
attn_mode: Literal["full", "windowed"] = "full",
|
| 71 |
+
window_size: Optional[int] = None,
|
| 72 |
+
shift_window: Optional[Tuple[int, int, int]] = None,
|
| 73 |
+
qkv_bias: bool = True,
|
| 74 |
+
use_rope: bool = False,
|
| 75 |
+
qk_rms_norm: bool = False,
|
| 76 |
+
):
|
| 77 |
+
super().__init__()
|
| 78 |
+
assert channels % num_heads == 0
|
| 79 |
+
assert type in ["self", "cross"], f"Invalid attention type: {type}"
|
| 80 |
+
assert attn_mode in ["full", "windowed"], f"Invalid attention mode: {attn_mode}"
|
| 81 |
+
assert type == "self" or attn_mode == "full", "Cross-attention only supports full attention"
|
| 82 |
+
|
| 83 |
+
if attn_mode == "windowed":
|
| 84 |
+
raise NotImplementedError("Windowed attention is not yet implemented")
|
| 85 |
+
|
| 86 |
+
self.channels = channels
|
| 87 |
+
self.head_dim = channels // num_heads
|
| 88 |
+
self.ctx_channels = ctx_channels if ctx_channels is not None else channels
|
| 89 |
+
self.num_heads = num_heads
|
| 90 |
+
self._type = type
|
| 91 |
+
self.attn_mode = attn_mode
|
| 92 |
+
self.window_size = window_size
|
| 93 |
+
self.shift_window = shift_window
|
| 94 |
+
self.use_rope = use_rope
|
| 95 |
+
self.qk_rms_norm = qk_rms_norm
|
| 96 |
+
|
| 97 |
+
if self._type == "self":
|
| 98 |
+
self.to_qkv = nn.Linear(channels, channels * 3, bias=qkv_bias)
|
| 99 |
+
else:
|
| 100 |
+
self.to_q = nn.Linear(channels, channels, bias=qkv_bias)
|
| 101 |
+
self.to_kv = nn.Linear(self.ctx_channels, channels * 2, bias=qkv_bias)
|
| 102 |
+
|
| 103 |
+
if self.qk_rms_norm:
|
| 104 |
+
self.q_rms_norm = MultiHeadRMSNorm(self.head_dim, num_heads)
|
| 105 |
+
self.k_rms_norm = MultiHeadRMSNorm(self.head_dim, num_heads)
|
| 106 |
+
|
| 107 |
+
self.to_out = nn.Linear(channels, channels)
|
| 108 |
+
|
| 109 |
+
if use_rope:
|
| 110 |
+
self.rope = RotaryPositionEmbedder(channels)
|
| 111 |
+
|
| 112 |
+
def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None, indices: Optional[torch.Tensor] = None) -> torch.Tensor:
|
| 113 |
+
B, L, C = x.shape
|
| 114 |
+
if self._type == "self":
|
| 115 |
+
qkv = self.to_qkv(x)
|
| 116 |
+
qkv = qkv.reshape(B, L, 3, self.num_heads, -1)
|
| 117 |
+
if self.use_rope:
|
| 118 |
+
q, k, v = qkv.unbind(dim=2)
|
| 119 |
+
q, k = self.rope(q, k, indices)
|
| 120 |
+
qkv = torch.stack([q, k, v], dim=2)
|
| 121 |
+
if self.attn_mode == "full":
|
| 122 |
+
if self.qk_rms_norm:
|
| 123 |
+
q, k, v = qkv.unbind(dim=2)
|
| 124 |
+
q = self.q_rms_norm(q)
|
| 125 |
+
k = self.k_rms_norm(k)
|
| 126 |
+
h = scaled_dot_product_attention(q, k, v)
|
| 127 |
+
else:
|
| 128 |
+
h = scaled_dot_product_attention(qkv)
|
| 129 |
+
elif self.attn_mode == "windowed":
|
| 130 |
+
raise NotImplementedError("Windowed attention is not yet implemented")
|
| 131 |
+
else:
|
| 132 |
+
Lkv = context.shape[1]
|
| 133 |
+
q = self.to_q(x)
|
| 134 |
+
kv = self.to_kv(context)
|
| 135 |
+
q = q.reshape(B, L, self.num_heads, -1)
|
| 136 |
+
kv = kv.reshape(B, Lkv, 2, self.num_heads, -1)
|
| 137 |
+
if self.qk_rms_norm:
|
| 138 |
+
q = self.q_rms_norm(q)
|
| 139 |
+
k, v = kv.unbind(dim=2)
|
| 140 |
+
k = self.k_rms_norm(k)
|
| 141 |
+
h = scaled_dot_product_attention(q, k, v)
|
| 142 |
+
else:
|
| 143 |
+
h = scaled_dot_product_attention(q, kv)
|
| 144 |
+
h = h.reshape(B, L, -1)
|
| 145 |
+
h = self.to_out(h)
|
| 146 |
+
return h
|
thirdparty/TRELLIS/trellis/modules/norm.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class LayerNorm32(nn.LayerNorm):
|
| 6 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 7 |
+
return super().forward(x.float()).type(x.dtype)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class GroupNorm32(nn.GroupNorm):
|
| 11 |
+
"""
|
| 12 |
+
A GroupNorm layer that converts to float32 before the forward pass.
|
| 13 |
+
"""
|
| 14 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 15 |
+
return super().forward(x.float()).type(x.dtype)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ChannelLayerNorm32(LayerNorm32):
|
| 19 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 20 |
+
DIM = x.dim()
|
| 21 |
+
x = x.permute(0, *range(2, DIM), 1).contiguous()
|
| 22 |
+
x = super().forward(x)
|
| 23 |
+
x = x.permute(0, DIM-1, *range(1, DIM-1)).contiguous()
|
| 24 |
+
return x
|
| 25 |
+
|
thirdparty/TRELLIS/trellis/modules/sparse/__init__.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
|
| 3 |
+
BACKEND = 'spconv'
|
| 4 |
+
DEBUG = False
|
| 5 |
+
ATTN = 'flash_attn'
|
| 6 |
+
|
| 7 |
+
def __from_env():
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
global BACKEND
|
| 11 |
+
global DEBUG
|
| 12 |
+
global ATTN
|
| 13 |
+
|
| 14 |
+
env_sparse_backend = os.environ.get('SPARSE_BACKEND')
|
| 15 |
+
env_sparse_debug = os.environ.get('SPARSE_DEBUG')
|
| 16 |
+
env_sparse_attn = os.environ.get('SPARSE_ATTN_BACKEND')
|
| 17 |
+
if env_sparse_attn is None:
|
| 18 |
+
env_sparse_attn = os.environ.get('ATTN_BACKEND')
|
| 19 |
+
|
| 20 |
+
if env_sparse_backend is not None and env_sparse_backend in ['spconv', 'torchsparse']:
|
| 21 |
+
BACKEND = env_sparse_backend
|
| 22 |
+
if env_sparse_debug is not None:
|
| 23 |
+
DEBUG = env_sparse_debug == '1'
|
| 24 |
+
if env_sparse_attn is not None and env_sparse_attn in ['xformers', 'flash_attn']:
|
| 25 |
+
ATTN = env_sparse_attn
|
| 26 |
+
|
| 27 |
+
print(f"[SPARSE] Backend: {BACKEND}, Attention: {ATTN}")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
__from_env()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def set_backend(backend: Literal['spconv', 'torchsparse']):
|
| 34 |
+
global BACKEND
|
| 35 |
+
BACKEND = backend
|
| 36 |
+
|
| 37 |
+
def set_debug(debug: bool):
|
| 38 |
+
global DEBUG
|
| 39 |
+
DEBUG = debug
|
| 40 |
+
|
| 41 |
+
def set_attn(attn: Literal['xformers', 'flash_attn']):
|
| 42 |
+
global ATTN
|
| 43 |
+
ATTN = attn
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
import importlib
|
| 47 |
+
|
| 48 |
+
__attributes = {
|
| 49 |
+
'SparseTensor': 'basic',
|
| 50 |
+
'sparse_batch_broadcast': 'basic',
|
| 51 |
+
'sparse_batch_op': 'basic',
|
| 52 |
+
'sparse_cat': 'basic',
|
| 53 |
+
'sparse_unbind': 'basic',
|
| 54 |
+
'SparseGroupNorm': 'norm',
|
| 55 |
+
'SparseLayerNorm': 'norm',
|
| 56 |
+
'SparseGroupNorm32': 'norm',
|
| 57 |
+
'SparseLayerNorm32': 'norm',
|
| 58 |
+
'SparseReLU': 'nonlinearity',
|
| 59 |
+
'SparseSiLU': 'nonlinearity',
|
| 60 |
+
'SparseGELU': 'nonlinearity',
|
| 61 |
+
'SparseActivation': 'nonlinearity',
|
| 62 |
+
'SparseLinear': 'linear',
|
| 63 |
+
'sparse_scaled_dot_product_attention': 'attention',
|
| 64 |
+
'SerializeMode': 'attention',
|
| 65 |
+
'sparse_serialized_scaled_dot_product_self_attention': 'attention',
|
| 66 |
+
'sparse_windowed_scaled_dot_product_self_attention': 'attention',
|
| 67 |
+
'SparseMultiHeadAttention': 'attention',
|
| 68 |
+
'SparseConv3d': 'conv',
|
| 69 |
+
'SparseInverseConv3d': 'conv',
|
| 70 |
+
'SparseDownsample': 'spatial',
|
| 71 |
+
'SparseUpsample': 'spatial',
|
| 72 |
+
'SparseSubdivide' : 'spatial'
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
__submodules = ['transformer']
|
| 76 |
+
|
| 77 |
+
__all__ = list(__attributes.keys()) + __submodules
|
| 78 |
+
|
| 79 |
+
def __getattr__(name):
|
| 80 |
+
if name not in globals():
|
| 81 |
+
if name in __attributes:
|
| 82 |
+
module_name = __attributes[name]
|
| 83 |
+
module = importlib.import_module(f".{module_name}", __name__)
|
| 84 |
+
globals()[name] = getattr(module, name)
|
| 85 |
+
elif name in __submodules:
|
| 86 |
+
module = importlib.import_module(f".{name}", __name__)
|
| 87 |
+
globals()[name] = module
|
| 88 |
+
else:
|
| 89 |
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
| 90 |
+
return globals()[name]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# For Pylance
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
from .basic import *
|
| 96 |
+
from .norm import *
|
| 97 |
+
from .nonlinearity import *
|
| 98 |
+
from .linear import *
|
| 99 |
+
from .attention import *
|
| 100 |
+
from .conv import *
|
| 101 |
+
from .spatial import *
|
| 102 |
+
import transformer
|
thirdparty/TRELLIS/trellis/modules/sparse/attention/__init__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .full_attn import *
|
| 2 |
+
from .serialized_attn import *
|
| 3 |
+
from .windowed_attn import *
|
| 4 |
+
from .modules import *
|
thirdparty/TRELLIS/trellis/modules/sparse/attention/full_attn.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
from .. import SparseTensor
|
| 4 |
+
from .. import DEBUG, ATTN
|
| 5 |
+
|
| 6 |
+
if ATTN == 'xformers':
|
| 7 |
+
import xformers.ops as xops
|
| 8 |
+
elif ATTN == 'flash_attn':
|
| 9 |
+
import flash_attn
|
| 10 |
+
else:
|
| 11 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
__all__ = [
|
| 15 |
+
'sparse_scaled_dot_product_attention',
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@overload
|
| 20 |
+
def sparse_scaled_dot_product_attention(qkv: SparseTensor) -> SparseTensor:
|
| 21 |
+
"""
|
| 22 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
qkv (SparseTensor): A [N, *, 3, H, C] sparse tensor containing Qs, Ks, and Vs.
|
| 26 |
+
"""
|
| 27 |
+
...
|
| 28 |
+
|
| 29 |
+
@overload
|
| 30 |
+
def sparse_scaled_dot_product_attention(q: SparseTensor, kv: Union[SparseTensor, torch.Tensor]) -> SparseTensor:
|
| 31 |
+
"""
|
| 32 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
q (SparseTensor): A [N, *, H, C] sparse tensor containing Qs.
|
| 36 |
+
kv (SparseTensor or torch.Tensor): A [N, *, 2, H, C] sparse tensor or a [N, L, 2, H, C] dense tensor containing Ks and Vs.
|
| 37 |
+
"""
|
| 38 |
+
...
|
| 39 |
+
|
| 40 |
+
@overload
|
| 41 |
+
def sparse_scaled_dot_product_attention(q: torch.Tensor, kv: SparseTensor) -> torch.Tensor:
|
| 42 |
+
"""
|
| 43 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
q (SparseTensor): A [N, L, H, C] dense tensor containing Qs.
|
| 47 |
+
kv (SparseTensor or torch.Tensor): A [N, *, 2, H, C] sparse tensor containing Ks and Vs.
|
| 48 |
+
"""
|
| 49 |
+
...
|
| 50 |
+
|
| 51 |
+
@overload
|
| 52 |
+
def sparse_scaled_dot_product_attention(q: SparseTensor, k: SparseTensor, v: SparseTensor) -> SparseTensor:
|
| 53 |
+
"""
|
| 54 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
q (SparseTensor): A [N, *, H, Ci] sparse tensor containing Qs.
|
| 58 |
+
k (SparseTensor): A [N, *, H, Ci] sparse tensor containing Ks.
|
| 59 |
+
v (SparseTensor): A [N, *, H, Co] sparse tensor containing Vs.
|
| 60 |
+
|
| 61 |
+
Note:
|
| 62 |
+
k and v are assumed to have the same coordinate map.
|
| 63 |
+
"""
|
| 64 |
+
...
|
| 65 |
+
|
| 66 |
+
@overload
|
| 67 |
+
def sparse_scaled_dot_product_attention(q: SparseTensor, k: torch.Tensor, v: torch.Tensor) -> SparseTensor:
|
| 68 |
+
"""
|
| 69 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
q (SparseTensor): A [N, *, H, Ci] sparse tensor containing Qs.
|
| 73 |
+
k (torch.Tensor): A [N, L, H, Ci] dense tensor containing Ks.
|
| 74 |
+
v (torch.Tensor): A [N, L, H, Co] dense tensor containing Vs.
|
| 75 |
+
"""
|
| 76 |
+
...
|
| 77 |
+
|
| 78 |
+
@overload
|
| 79 |
+
def sparse_scaled_dot_product_attention(q: torch.Tensor, k: SparseTensor, v: SparseTensor) -> torch.Tensor:
|
| 80 |
+
"""
|
| 81 |
+
Apply scaled dot product attention to a sparse tensor.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
q (torch.Tensor): A [N, L, H, Ci] dense tensor containing Qs.
|
| 85 |
+
k (SparseTensor): A [N, *, H, Ci] sparse tensor containing Ks.
|
| 86 |
+
v (SparseTensor): A [N, *, H, Co] sparse tensor containing Vs.
|
| 87 |
+
"""
|
| 88 |
+
...
|
| 89 |
+
|
| 90 |
+
def sparse_scaled_dot_product_attention(*args, **kwargs):
|
| 91 |
+
arg_names_dict = {
|
| 92 |
+
1: ['qkv'],
|
| 93 |
+
2: ['q', 'kv'],
|
| 94 |
+
3: ['q', 'k', 'v']
|
| 95 |
+
}
|
| 96 |
+
num_all_args = len(args) + len(kwargs)
|
| 97 |
+
assert num_all_args in arg_names_dict, f"Invalid number of arguments, got {num_all_args}, expected 1, 2, or 3"
|
| 98 |
+
for key in arg_names_dict[num_all_args][len(args):]:
|
| 99 |
+
assert key in kwargs, f"Missing argument {key}"
|
| 100 |
+
|
| 101 |
+
if num_all_args == 1:
|
| 102 |
+
qkv = args[0] if len(args) > 0 else kwargs['qkv']
|
| 103 |
+
assert isinstance(qkv, SparseTensor), f"qkv must be a SparseTensor, got {type(qkv)}"
|
| 104 |
+
assert len(qkv.shape) == 4 and qkv.shape[1] == 3, f"Invalid shape for qkv, got {qkv.shape}, expected [N, *, 3, H, C]"
|
| 105 |
+
device = qkv.device
|
| 106 |
+
|
| 107 |
+
s = qkv
|
| 108 |
+
q_seqlen = [qkv.layout[i].stop - qkv.layout[i].start for i in range(qkv.shape[0])]
|
| 109 |
+
kv_seqlen = q_seqlen
|
| 110 |
+
qkv = qkv.feats # [T, 3, H, C]
|
| 111 |
+
|
| 112 |
+
elif num_all_args == 2:
|
| 113 |
+
q = args[0] if len(args) > 0 else kwargs['q']
|
| 114 |
+
kv = args[1] if len(args) > 1 else kwargs['kv']
|
| 115 |
+
assert isinstance(q, SparseTensor) and isinstance(kv, (SparseTensor, torch.Tensor)) or \
|
| 116 |
+
isinstance(q, torch.Tensor) and isinstance(kv, SparseTensor), \
|
| 117 |
+
f"Invalid types, got {type(q)} and {type(kv)}"
|
| 118 |
+
assert q.shape[0] == kv.shape[0], f"Batch size mismatch, got {q.shape[0]} and {kv.shape[0]}"
|
| 119 |
+
device = q.device
|
| 120 |
+
|
| 121 |
+
if isinstance(q, SparseTensor):
|
| 122 |
+
assert len(q.shape) == 3, f"Invalid shape for q, got {q.shape}, expected [N, *, H, C]"
|
| 123 |
+
s = q
|
| 124 |
+
q_seqlen = [q.layout[i].stop - q.layout[i].start for i in range(q.shape[0])]
|
| 125 |
+
q = q.feats # [T_Q, H, C]
|
| 126 |
+
else:
|
| 127 |
+
assert len(q.shape) == 4, f"Invalid shape for q, got {q.shape}, expected [N, L, H, C]"
|
| 128 |
+
s = None
|
| 129 |
+
N, L, H, C = q.shape
|
| 130 |
+
q_seqlen = [L] * N
|
| 131 |
+
q = q.reshape(N * L, H, C) # [T_Q, H, C]
|
| 132 |
+
|
| 133 |
+
if isinstance(kv, SparseTensor):
|
| 134 |
+
assert len(kv.shape) == 4 and kv.shape[1] == 2, f"Invalid shape for kv, got {kv.shape}, expected [N, *, 2, H, C]"
|
| 135 |
+
kv_seqlen = [kv.layout[i].stop - kv.layout[i].start for i in range(kv.shape[0])]
|
| 136 |
+
kv = kv.feats # [T_KV, 2, H, C]
|
| 137 |
+
else:
|
| 138 |
+
assert len(kv.shape) == 5, f"Invalid shape for kv, got {kv.shape}, expected [N, L, 2, H, C]"
|
| 139 |
+
N, L, _, H, C = kv.shape
|
| 140 |
+
kv_seqlen = [L] * N
|
| 141 |
+
kv = kv.reshape(N * L, 2, H, C) # [T_KV, 2, H, C]
|
| 142 |
+
|
| 143 |
+
elif num_all_args == 3:
|
| 144 |
+
q = args[0] if len(args) > 0 else kwargs['q']
|
| 145 |
+
k = args[1] if len(args) > 1 else kwargs['k']
|
| 146 |
+
v = args[2] if len(args) > 2 else kwargs['v']
|
| 147 |
+
assert isinstance(q, SparseTensor) and isinstance(k, (SparseTensor, torch.Tensor)) and type(k) == type(v) or \
|
| 148 |
+
isinstance(q, torch.Tensor) and isinstance(k, SparseTensor) and isinstance(v, SparseTensor), \
|
| 149 |
+
f"Invalid types, got {type(q)}, {type(k)}, and {type(v)}"
|
| 150 |
+
assert q.shape[0] == k.shape[0] == v.shape[0], f"Batch size mismatch, got {q.shape[0]}, {k.shape[0]}, and {v.shape[0]}"
|
| 151 |
+
device = q.device
|
| 152 |
+
|
| 153 |
+
if isinstance(q, SparseTensor):
|
| 154 |
+
assert len(q.shape) == 3, f"Invalid shape for q, got {q.shape}, expected [N, *, H, Ci]"
|
| 155 |
+
s = q
|
| 156 |
+
q_seqlen = [q.layout[i].stop - q.layout[i].start for i in range(q.shape[0])]
|
| 157 |
+
q = q.feats # [T_Q, H, Ci]
|
| 158 |
+
else:
|
| 159 |
+
assert len(q.shape) == 4, f"Invalid shape for q, got {q.shape}, expected [N, L, H, Ci]"
|
| 160 |
+
s = None
|
| 161 |
+
N, L, H, CI = q.shape
|
| 162 |
+
q_seqlen = [L] * N
|
| 163 |
+
q = q.reshape(N * L, H, CI) # [T_Q, H, Ci]
|
| 164 |
+
|
| 165 |
+
if isinstance(k, SparseTensor):
|
| 166 |
+
assert len(k.shape) == 3, f"Invalid shape for k, got {k.shape}, expected [N, *, H, Ci]"
|
| 167 |
+
assert len(v.shape) == 3, f"Invalid shape for v, got {v.shape}, expected [N, *, H, Co]"
|
| 168 |
+
kv_seqlen = [k.layout[i].stop - k.layout[i].start for i in range(k.shape[0])]
|
| 169 |
+
k = k.feats # [T_KV, H, Ci]
|
| 170 |
+
v = v.feats # [T_KV, H, Co]
|
| 171 |
+
else:
|
| 172 |
+
assert len(k.shape) == 4, f"Invalid shape for k, got {k.shape}, expected [N, L, H, Ci]"
|
| 173 |
+
assert len(v.shape) == 4, f"Invalid shape for v, got {v.shape}, expected [N, L, H, Co]"
|
| 174 |
+
N, L, H, CI, CO = *k.shape, v.shape[-1]
|
| 175 |
+
kv_seqlen = [L] * N
|
| 176 |
+
k = k.reshape(N * L, H, CI) # [T_KV, H, Ci]
|
| 177 |
+
v = v.reshape(N * L, H, CO) # [T_KV, H, Co]
|
| 178 |
+
|
| 179 |
+
if DEBUG:
|
| 180 |
+
if s is not None:
|
| 181 |
+
for i in range(s.shape[0]):
|
| 182 |
+
assert (s.coords[s.layout[i]] == i).all(), f"SparseScaledDotProductSelfAttention: batch index mismatch"
|
| 183 |
+
if num_all_args in [2, 3]:
|
| 184 |
+
assert q.shape[:2] == [1, sum(q_seqlen)], f"SparseScaledDotProductSelfAttention: q shape mismatch"
|
| 185 |
+
if num_all_args == 3:
|
| 186 |
+
assert k.shape[:2] == [1, sum(kv_seqlen)], f"SparseScaledDotProductSelfAttention: k shape mismatch"
|
| 187 |
+
assert v.shape[:2] == [1, sum(kv_seqlen)], f"SparseScaledDotProductSelfAttention: v shape mismatch"
|
| 188 |
+
|
| 189 |
+
if ATTN == 'xformers':
|
| 190 |
+
if num_all_args == 1:
|
| 191 |
+
q, k, v = qkv.unbind(dim=1)
|
| 192 |
+
elif num_all_args == 2:
|
| 193 |
+
k, v = kv.unbind(dim=1)
|
| 194 |
+
q = q.unsqueeze(0)
|
| 195 |
+
k = k.unsqueeze(0)
|
| 196 |
+
v = v.unsqueeze(0)
|
| 197 |
+
mask = xops.fmha.BlockDiagonalMask.from_seqlens(q_seqlen, kv_seqlen)
|
| 198 |
+
out = xops.memory_efficient_attention(q, k, v, mask)[0]
|
| 199 |
+
elif ATTN == 'flash_attn':
|
| 200 |
+
cu_seqlens_q = torch.cat([torch.tensor([0]), torch.cumsum(torch.tensor(q_seqlen), dim=0)]).int().to(device)
|
| 201 |
+
if num_all_args in [2, 3]:
|
| 202 |
+
cu_seqlens_kv = torch.cat([torch.tensor([0]), torch.cumsum(torch.tensor(kv_seqlen), dim=0)]).int().to(device)
|
| 203 |
+
if num_all_args == 1:
|
| 204 |
+
out = flash_attn.flash_attn_varlen_qkvpacked_func(qkv, cu_seqlens_q, max(q_seqlen))
|
| 205 |
+
elif num_all_args == 2:
|
| 206 |
+
out = flash_attn.flash_attn_varlen_kvpacked_func(q, kv, cu_seqlens_q, cu_seqlens_kv, max(q_seqlen), max(kv_seqlen))
|
| 207 |
+
elif num_all_args == 3:
|
| 208 |
+
out = flash_attn.flash_attn_varlen_func(q, k, v, cu_seqlens_q, cu_seqlens_kv, max(q_seqlen), max(kv_seqlen))
|
| 209 |
+
else:
|
| 210 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 211 |
+
|
| 212 |
+
if s is not None:
|
| 213 |
+
return s.replace(out)
|
| 214 |
+
else:
|
| 215 |
+
return out.reshape(N, L, H, -1)
|
thirdparty/TRELLIS/trellis/modules/sparse/attention/modules.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from .. import SparseTensor
|
| 6 |
+
from .full_attn import sparse_scaled_dot_product_attention
|
| 7 |
+
from .serialized_attn import SerializeMode, sparse_serialized_scaled_dot_product_self_attention
|
| 8 |
+
from .windowed_attn import sparse_windowed_scaled_dot_product_self_attention
|
| 9 |
+
from ...attention import RotaryPositionEmbedder
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class SparseMultiHeadRMSNorm(nn.Module):
|
| 13 |
+
def __init__(self, dim: int, heads: int):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.scale = dim ** 0.5
|
| 16 |
+
self.gamma = nn.Parameter(torch.ones(heads, dim))
|
| 17 |
+
|
| 18 |
+
def forward(self, x: Union[SparseTensor, torch.Tensor]) -> Union[SparseTensor, torch.Tensor]:
|
| 19 |
+
x_type = x.dtype
|
| 20 |
+
x = x.float()
|
| 21 |
+
if isinstance(x, SparseTensor):
|
| 22 |
+
x = x.replace(F.normalize(x.feats, dim=-1))
|
| 23 |
+
else:
|
| 24 |
+
x = F.normalize(x, dim=-1)
|
| 25 |
+
return (x * self.gamma * self.scale).to(x_type)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class SparseMultiHeadAttention(nn.Module):
|
| 29 |
+
def __init__(
|
| 30 |
+
self,
|
| 31 |
+
channels: int,
|
| 32 |
+
num_heads: int,
|
| 33 |
+
ctx_channels: Optional[int] = None,
|
| 34 |
+
type: Literal["self", "cross"] = "self",
|
| 35 |
+
attn_mode: Literal["full", "serialized", "windowed"] = "full",
|
| 36 |
+
window_size: Optional[int] = None,
|
| 37 |
+
shift_sequence: Optional[int] = None,
|
| 38 |
+
shift_window: Optional[Tuple[int, int, int]] = None,
|
| 39 |
+
serialize_mode: Optional[SerializeMode] = None,
|
| 40 |
+
qkv_bias: bool = True,
|
| 41 |
+
use_rope: bool = False,
|
| 42 |
+
qk_rms_norm: bool = False,
|
| 43 |
+
):
|
| 44 |
+
super().__init__()
|
| 45 |
+
assert channels % num_heads == 0
|
| 46 |
+
assert type in ["self", "cross"], f"Invalid attention type: {type}"
|
| 47 |
+
assert attn_mode in ["full", "serialized", "windowed"], f"Invalid attention mode: {attn_mode}"
|
| 48 |
+
assert type == "self" or attn_mode == "full", "Cross-attention only supports full attention"
|
| 49 |
+
assert type == "self" or use_rope is False, "Rotary position embeddings only supported for self-attention"
|
| 50 |
+
self.channels = channels
|
| 51 |
+
self.ctx_channels = ctx_channels if ctx_channels is not None else channels
|
| 52 |
+
self.num_heads = num_heads
|
| 53 |
+
self._type = type
|
| 54 |
+
self.attn_mode = attn_mode
|
| 55 |
+
self.window_size = window_size
|
| 56 |
+
self.shift_sequence = shift_sequence
|
| 57 |
+
self.shift_window = shift_window
|
| 58 |
+
self.serialize_mode = serialize_mode
|
| 59 |
+
self.use_rope = use_rope
|
| 60 |
+
self.qk_rms_norm = qk_rms_norm
|
| 61 |
+
|
| 62 |
+
if self._type == "self":
|
| 63 |
+
self.to_qkv = nn.Linear(channels, channels * 3, bias=qkv_bias)
|
| 64 |
+
else:
|
| 65 |
+
self.to_q = nn.Linear(channels, channels, bias=qkv_bias)
|
| 66 |
+
self.to_kv = nn.Linear(self.ctx_channels, channels * 2, bias=qkv_bias)
|
| 67 |
+
|
| 68 |
+
if self.qk_rms_norm:
|
| 69 |
+
self.q_rms_norm = SparseMultiHeadRMSNorm(channels // num_heads, num_heads)
|
| 70 |
+
self.k_rms_norm = SparseMultiHeadRMSNorm(channels // num_heads, num_heads)
|
| 71 |
+
|
| 72 |
+
self.to_out = nn.Linear(channels, channels)
|
| 73 |
+
|
| 74 |
+
if use_rope:
|
| 75 |
+
self.rope = RotaryPositionEmbedder(channels)
|
| 76 |
+
|
| 77 |
+
@staticmethod
|
| 78 |
+
def _linear(module: nn.Linear, x: Union[SparseTensor, torch.Tensor]) -> Union[SparseTensor, torch.Tensor]:
|
| 79 |
+
if isinstance(x, SparseTensor):
|
| 80 |
+
return x.replace(module(x.feats))
|
| 81 |
+
else:
|
| 82 |
+
return module(x)
|
| 83 |
+
|
| 84 |
+
@staticmethod
|
| 85 |
+
def _reshape_chs(x: Union[SparseTensor, torch.Tensor], shape: Tuple[int, ...]) -> Union[SparseTensor, torch.Tensor]:
|
| 86 |
+
if isinstance(x, SparseTensor):
|
| 87 |
+
return x.reshape(*shape)
|
| 88 |
+
else:
|
| 89 |
+
return x.reshape(*x.shape[:2], *shape)
|
| 90 |
+
|
| 91 |
+
def _fused_pre(self, x: Union[SparseTensor, torch.Tensor], num_fused: int) -> Union[SparseTensor, torch.Tensor]:
|
| 92 |
+
if isinstance(x, SparseTensor):
|
| 93 |
+
x_feats = x.feats.unsqueeze(0)
|
| 94 |
+
else:
|
| 95 |
+
x_feats = x
|
| 96 |
+
x_feats = x_feats.reshape(*x_feats.shape[:2], num_fused, self.num_heads, -1)
|
| 97 |
+
return x.replace(x_feats.squeeze(0)) if isinstance(x, SparseTensor) else x_feats
|
| 98 |
+
|
| 99 |
+
def _rope(self, qkv: SparseTensor) -> SparseTensor:
|
| 100 |
+
q, k, v = qkv.feats.unbind(dim=1) # [T, H, C]
|
| 101 |
+
q, k = self.rope(q, k, qkv.coords[:, 1:])
|
| 102 |
+
qkv = qkv.replace(torch.stack([q, k, v], dim=1))
|
| 103 |
+
return qkv
|
| 104 |
+
|
| 105 |
+
def forward(self, x: Union[SparseTensor, torch.Tensor], context: Optional[Union[SparseTensor, torch.Tensor]] = None) -> Union[SparseTensor, torch.Tensor]:
|
| 106 |
+
if self._type == "self":
|
| 107 |
+
qkv = self._linear(self.to_qkv, x)
|
| 108 |
+
qkv = self._fused_pre(qkv, num_fused=3)
|
| 109 |
+
if self.use_rope:
|
| 110 |
+
qkv = self._rope(qkv)
|
| 111 |
+
if self.qk_rms_norm:
|
| 112 |
+
q, k, v = qkv.unbind(dim=1)
|
| 113 |
+
q = self.q_rms_norm(q)
|
| 114 |
+
k = self.k_rms_norm(k)
|
| 115 |
+
qkv = qkv.replace(torch.stack([q.feats, k.feats, v.feats], dim=1))
|
| 116 |
+
if self.attn_mode == "full":
|
| 117 |
+
h = sparse_scaled_dot_product_attention(qkv)
|
| 118 |
+
elif self.attn_mode == "serialized":
|
| 119 |
+
h = sparse_serialized_scaled_dot_product_self_attention(
|
| 120 |
+
qkv, self.window_size, serialize_mode=self.serialize_mode, shift_sequence=self.shift_sequence, shift_window=self.shift_window
|
| 121 |
+
)
|
| 122 |
+
elif self.attn_mode == "windowed":
|
| 123 |
+
h = sparse_windowed_scaled_dot_product_self_attention(
|
| 124 |
+
qkv, self.window_size, shift_window=self.shift_window
|
| 125 |
+
)
|
| 126 |
+
else:
|
| 127 |
+
q = self._linear(self.to_q, x)
|
| 128 |
+
q = self._reshape_chs(q, (self.num_heads, -1))
|
| 129 |
+
kv = self._linear(self.to_kv, context)
|
| 130 |
+
kv = self._fused_pre(kv, num_fused=2)
|
| 131 |
+
if self.qk_rms_norm:
|
| 132 |
+
q = self.q_rms_norm(q)
|
| 133 |
+
k, v = kv.unbind(dim=1)
|
| 134 |
+
k = self.k_rms_norm(k)
|
| 135 |
+
kv = kv.replace(torch.stack([k.feats, v.feats], dim=1))
|
| 136 |
+
h = sparse_scaled_dot_product_attention(q, kv)
|
| 137 |
+
h = self._reshape_chs(h, (-1,))
|
| 138 |
+
h = self._linear(self.to_out, h)
|
| 139 |
+
return h
|
thirdparty/TRELLIS/trellis/modules/sparse/attention/serialized_attn.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
from enum import Enum
|
| 3 |
+
import torch
|
| 4 |
+
import math
|
| 5 |
+
from .. import SparseTensor
|
| 6 |
+
from .. import DEBUG, ATTN
|
| 7 |
+
|
| 8 |
+
if ATTN == 'xformers':
|
| 9 |
+
import xformers.ops as xops
|
| 10 |
+
elif ATTN == 'flash_attn':
|
| 11 |
+
import flash_attn
|
| 12 |
+
else:
|
| 13 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
__all__ = [
|
| 17 |
+
'sparse_serialized_scaled_dot_product_self_attention',
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class SerializeMode(Enum):
|
| 22 |
+
Z_ORDER = 0
|
| 23 |
+
Z_ORDER_TRANSPOSED = 1
|
| 24 |
+
HILBERT = 2
|
| 25 |
+
HILBERT_TRANSPOSED = 3
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
SerializeModes = [
|
| 29 |
+
SerializeMode.Z_ORDER,
|
| 30 |
+
SerializeMode.Z_ORDER_TRANSPOSED,
|
| 31 |
+
SerializeMode.HILBERT,
|
| 32 |
+
SerializeMode.HILBERT_TRANSPOSED
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def calc_serialization(
|
| 37 |
+
tensor: SparseTensor,
|
| 38 |
+
window_size: int,
|
| 39 |
+
serialize_mode: SerializeMode = SerializeMode.Z_ORDER,
|
| 40 |
+
shift_sequence: int = 0,
|
| 41 |
+
shift_window: Tuple[int, int, int] = (0, 0, 0)
|
| 42 |
+
) -> Tuple[torch.Tensor, torch.Tensor, List[int]]:
|
| 43 |
+
"""
|
| 44 |
+
Calculate serialization and partitioning for a set of coordinates.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
tensor (SparseTensor): The input tensor.
|
| 48 |
+
window_size (int): The window size to use.
|
| 49 |
+
serialize_mode (SerializeMode): The serialization mode to use.
|
| 50 |
+
shift_sequence (int): The shift of serialized sequence.
|
| 51 |
+
shift_window (Tuple[int, int, int]): The shift of serialized coordinates.
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
(torch.Tensor, torch.Tensor): Forwards and backwards indices.
|
| 55 |
+
"""
|
| 56 |
+
fwd_indices = []
|
| 57 |
+
bwd_indices = []
|
| 58 |
+
seq_lens = []
|
| 59 |
+
seq_batch_indices = []
|
| 60 |
+
offsets = [0]
|
| 61 |
+
|
| 62 |
+
if 'vox2seq' not in globals():
|
| 63 |
+
import vox2seq
|
| 64 |
+
|
| 65 |
+
# Serialize the input
|
| 66 |
+
serialize_coords = tensor.coords[:, 1:].clone()
|
| 67 |
+
serialize_coords += torch.tensor(shift_window, dtype=torch.int32, device=tensor.device).reshape(1, 3)
|
| 68 |
+
if serialize_mode == SerializeMode.Z_ORDER:
|
| 69 |
+
code = vox2seq.encode(serialize_coords, mode='z_order', permute=[0, 1, 2])
|
| 70 |
+
elif serialize_mode == SerializeMode.Z_ORDER_TRANSPOSED:
|
| 71 |
+
code = vox2seq.encode(serialize_coords, mode='z_order', permute=[1, 0, 2])
|
| 72 |
+
elif serialize_mode == SerializeMode.HILBERT:
|
| 73 |
+
code = vox2seq.encode(serialize_coords, mode='hilbert', permute=[0, 1, 2])
|
| 74 |
+
elif serialize_mode == SerializeMode.HILBERT_TRANSPOSED:
|
| 75 |
+
code = vox2seq.encode(serialize_coords, mode='hilbert', permute=[1, 0, 2])
|
| 76 |
+
else:
|
| 77 |
+
raise ValueError(f"Unknown serialize mode: {serialize_mode}")
|
| 78 |
+
|
| 79 |
+
for bi, s in enumerate(tensor.layout):
|
| 80 |
+
num_points = s.stop - s.start
|
| 81 |
+
num_windows = (num_points + window_size - 1) // window_size
|
| 82 |
+
valid_window_size = num_points / num_windows
|
| 83 |
+
to_ordered = torch.argsort(code[s.start:s.stop])
|
| 84 |
+
if num_windows == 1:
|
| 85 |
+
fwd_indices.append(to_ordered)
|
| 86 |
+
bwd_indices.append(torch.zeros_like(to_ordered).scatter_(0, to_ordered, torch.arange(num_points, device=tensor.device)))
|
| 87 |
+
fwd_indices[-1] += s.start
|
| 88 |
+
bwd_indices[-1] += offsets[-1]
|
| 89 |
+
seq_lens.append(num_points)
|
| 90 |
+
seq_batch_indices.append(bi)
|
| 91 |
+
offsets.append(offsets[-1] + seq_lens[-1])
|
| 92 |
+
else:
|
| 93 |
+
# Partition the input
|
| 94 |
+
offset = 0
|
| 95 |
+
mids = [(i + 0.5) * valid_window_size + shift_sequence for i in range(num_windows)]
|
| 96 |
+
split = [math.floor(i * valid_window_size + shift_sequence) for i in range(num_windows + 1)]
|
| 97 |
+
bwd_index = torch.zeros((num_points,), dtype=torch.int64, device=tensor.device)
|
| 98 |
+
for i in range(num_windows):
|
| 99 |
+
mid = mids[i]
|
| 100 |
+
valid_start = split[i]
|
| 101 |
+
valid_end = split[i + 1]
|
| 102 |
+
padded_start = math.floor(mid - 0.5 * window_size)
|
| 103 |
+
padded_end = padded_start + window_size
|
| 104 |
+
fwd_indices.append(to_ordered[torch.arange(padded_start, padded_end, device=tensor.device) % num_points])
|
| 105 |
+
offset += valid_start - padded_start
|
| 106 |
+
bwd_index.scatter_(0, fwd_indices[-1][valid_start-padded_start:valid_end-padded_start], torch.arange(offset, offset + valid_end - valid_start, device=tensor.device))
|
| 107 |
+
offset += padded_end - valid_start
|
| 108 |
+
fwd_indices[-1] += s.start
|
| 109 |
+
seq_lens.extend([window_size] * num_windows)
|
| 110 |
+
seq_batch_indices.extend([bi] * num_windows)
|
| 111 |
+
bwd_indices.append(bwd_index + offsets[-1])
|
| 112 |
+
offsets.append(offsets[-1] + num_windows * window_size)
|
| 113 |
+
|
| 114 |
+
fwd_indices = torch.cat(fwd_indices)
|
| 115 |
+
bwd_indices = torch.cat(bwd_indices)
|
| 116 |
+
|
| 117 |
+
return fwd_indices, bwd_indices, seq_lens, seq_batch_indices
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def sparse_serialized_scaled_dot_product_self_attention(
|
| 121 |
+
qkv: SparseTensor,
|
| 122 |
+
window_size: int,
|
| 123 |
+
serialize_mode: SerializeMode = SerializeMode.Z_ORDER,
|
| 124 |
+
shift_sequence: int = 0,
|
| 125 |
+
shift_window: Tuple[int, int, int] = (0, 0, 0)
|
| 126 |
+
) -> SparseTensor:
|
| 127 |
+
"""
|
| 128 |
+
Apply serialized scaled dot product self attention to a sparse tensor.
|
| 129 |
+
|
| 130 |
+
Args:
|
| 131 |
+
qkv (SparseTensor): [N, *, 3, H, C] sparse tensor containing Qs, Ks, and Vs.
|
| 132 |
+
window_size (int): The window size to use.
|
| 133 |
+
serialize_mode (SerializeMode): The serialization mode to use.
|
| 134 |
+
shift_sequence (int): The shift of serialized sequence.
|
| 135 |
+
shift_window (Tuple[int, int, int]): The shift of serialized coordinates.
|
| 136 |
+
shift (int): The shift to use.
|
| 137 |
+
"""
|
| 138 |
+
assert len(qkv.shape) == 4 and qkv.shape[1] == 3, f"Invalid shape for qkv, got {qkv.shape}, expected [N, *, 3, H, C]"
|
| 139 |
+
|
| 140 |
+
serialization_spatial_cache_name = f'serialization_{serialize_mode}_{window_size}_{shift_sequence}_{shift_window}'
|
| 141 |
+
serialization_spatial_cache = qkv.get_spatial_cache(serialization_spatial_cache_name)
|
| 142 |
+
if serialization_spatial_cache is None:
|
| 143 |
+
fwd_indices, bwd_indices, seq_lens, seq_batch_indices = calc_serialization(qkv, window_size, serialize_mode, shift_sequence, shift_window)
|
| 144 |
+
qkv.register_spatial_cache(serialization_spatial_cache_name, (fwd_indices, bwd_indices, seq_lens, seq_batch_indices))
|
| 145 |
+
else:
|
| 146 |
+
fwd_indices, bwd_indices, seq_lens, seq_batch_indices = serialization_spatial_cache
|
| 147 |
+
|
| 148 |
+
M = fwd_indices.shape[0]
|
| 149 |
+
T = qkv.feats.shape[0]
|
| 150 |
+
H = qkv.feats.shape[2]
|
| 151 |
+
C = qkv.feats.shape[3]
|
| 152 |
+
|
| 153 |
+
qkv_feats = qkv.feats[fwd_indices] # [M, 3, H, C]
|
| 154 |
+
|
| 155 |
+
if DEBUG:
|
| 156 |
+
start = 0
|
| 157 |
+
qkv_coords = qkv.coords[fwd_indices]
|
| 158 |
+
for i in range(len(seq_lens)):
|
| 159 |
+
assert (qkv_coords[start:start+seq_lens[i], 0] == seq_batch_indices[i]).all(), f"SparseWindowedScaledDotProductSelfAttention: batch index mismatch"
|
| 160 |
+
start += seq_lens[i]
|
| 161 |
+
|
| 162 |
+
if all([seq_len == window_size for seq_len in seq_lens]):
|
| 163 |
+
B = len(seq_lens)
|
| 164 |
+
N = window_size
|
| 165 |
+
qkv_feats = qkv_feats.reshape(B, N, 3, H, C)
|
| 166 |
+
if ATTN == 'xformers':
|
| 167 |
+
q, k, v = qkv_feats.unbind(dim=2) # [B, N, H, C]
|
| 168 |
+
out = xops.memory_efficient_attention(q, k, v) # [B, N, H, C]
|
| 169 |
+
elif ATTN == 'flash_attn':
|
| 170 |
+
out = flash_attn.flash_attn_qkvpacked_func(qkv_feats) # [B, N, H, C]
|
| 171 |
+
else:
|
| 172 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 173 |
+
out = out.reshape(B * N, H, C) # [M, H, C]
|
| 174 |
+
else:
|
| 175 |
+
if ATTN == 'xformers':
|
| 176 |
+
q, k, v = qkv_feats.unbind(dim=1) # [M, H, C]
|
| 177 |
+
q = q.unsqueeze(0) # [1, M, H, C]
|
| 178 |
+
k = k.unsqueeze(0) # [1, M, H, C]
|
| 179 |
+
v = v.unsqueeze(0) # [1, M, H, C]
|
| 180 |
+
mask = xops.fmha.BlockDiagonalMask.from_seqlens(seq_lens)
|
| 181 |
+
out = xops.memory_efficient_attention(q, k, v, mask)[0] # [M, H, C]
|
| 182 |
+
elif ATTN == 'flash_attn':
|
| 183 |
+
cu_seqlens = torch.cat([torch.tensor([0]), torch.cumsum(torch.tensor(seq_lens), dim=0)], dim=0) \
|
| 184 |
+
.to(qkv.device).int()
|
| 185 |
+
out = flash_attn.flash_attn_varlen_qkvpacked_func(qkv_feats, cu_seqlens, max(seq_lens)) # [M, H, C]
|
| 186 |
+
|
| 187 |
+
out = out[bwd_indices] # [T, H, C]
|
| 188 |
+
|
| 189 |
+
if DEBUG:
|
| 190 |
+
qkv_coords = qkv_coords[bwd_indices]
|
| 191 |
+
assert torch.equal(qkv_coords, qkv.coords), "SparseWindowedScaledDotProductSelfAttention: coordinate mismatch"
|
| 192 |
+
|
| 193 |
+
return qkv.replace(out)
|
thirdparty/TRELLIS/trellis/modules/sparse/attention/windowed_attn.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import *
|
| 2 |
+
import torch
|
| 3 |
+
import math
|
| 4 |
+
from .. import SparseTensor
|
| 5 |
+
from .. import DEBUG, ATTN
|
| 6 |
+
|
| 7 |
+
if ATTN == 'xformers':
|
| 8 |
+
import xformers.ops as xops
|
| 9 |
+
elif ATTN == 'flash_attn':
|
| 10 |
+
import flash_attn
|
| 11 |
+
else:
|
| 12 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
__all__ = [
|
| 16 |
+
'sparse_windowed_scaled_dot_product_self_attention',
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def calc_window_partition(
|
| 21 |
+
tensor: SparseTensor,
|
| 22 |
+
window_size: Union[int, Tuple[int, ...]],
|
| 23 |
+
shift_window: Union[int, Tuple[int, ...]] = 0
|
| 24 |
+
) -> Tuple[torch.Tensor, torch.Tensor, List[int], List[int]]:
|
| 25 |
+
"""
|
| 26 |
+
Calculate serialization and partitioning for a set of coordinates.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
tensor (SparseTensor): The input tensor.
|
| 30 |
+
window_size (int): The window size to use.
|
| 31 |
+
shift_window (Tuple[int, ...]): The shift of serialized coordinates.
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
(torch.Tensor): Forwards indices.
|
| 35 |
+
(torch.Tensor): Backwards indices.
|
| 36 |
+
(List[int]): Sequence lengths.
|
| 37 |
+
(List[int]): Sequence batch indices.
|
| 38 |
+
"""
|
| 39 |
+
DIM = tensor.coords.shape[1] - 1
|
| 40 |
+
shift_window = (shift_window,) * DIM if isinstance(shift_window, int) else shift_window
|
| 41 |
+
window_size = (window_size,) * DIM if isinstance(window_size, int) else window_size
|
| 42 |
+
shifted_coords = tensor.coords.clone().detach()
|
| 43 |
+
shifted_coords[:, 1:] += torch.tensor(shift_window, device=tensor.device, dtype=torch.int32).unsqueeze(0)
|
| 44 |
+
|
| 45 |
+
MAX_COORDS = shifted_coords[:, 1:].max(dim=0).values.tolist()
|
| 46 |
+
NUM_WINDOWS = [math.ceil((mc + 1) / ws) for mc, ws in zip(MAX_COORDS, window_size)]
|
| 47 |
+
OFFSET = torch.cumprod(torch.tensor([1] + NUM_WINDOWS[::-1]), dim=0).tolist()[::-1]
|
| 48 |
+
|
| 49 |
+
shifted_coords[:, 1:] //= torch.tensor(window_size, device=tensor.device, dtype=torch.int32).unsqueeze(0)
|
| 50 |
+
shifted_indices = (shifted_coords * torch.tensor(OFFSET, device=tensor.device, dtype=torch.int32).unsqueeze(0)).sum(dim=1)
|
| 51 |
+
fwd_indices = torch.argsort(shifted_indices)
|
| 52 |
+
bwd_indices = torch.empty_like(fwd_indices)
|
| 53 |
+
bwd_indices[fwd_indices] = torch.arange(fwd_indices.shape[0], device=tensor.device)
|
| 54 |
+
seq_lens = torch.bincount(shifted_indices)
|
| 55 |
+
seq_batch_indices = torch.arange(seq_lens.shape[0], device=tensor.device, dtype=torch.int32) // OFFSET[0]
|
| 56 |
+
mask = seq_lens != 0
|
| 57 |
+
seq_lens = seq_lens[mask].tolist()
|
| 58 |
+
seq_batch_indices = seq_batch_indices[mask].tolist()
|
| 59 |
+
|
| 60 |
+
return fwd_indices, bwd_indices, seq_lens, seq_batch_indices
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def sparse_windowed_scaled_dot_product_self_attention(
|
| 64 |
+
qkv: SparseTensor,
|
| 65 |
+
window_size: int,
|
| 66 |
+
shift_window: Tuple[int, int, int] = (0, 0, 0)
|
| 67 |
+
) -> SparseTensor:
|
| 68 |
+
"""
|
| 69 |
+
Apply windowed scaled dot product self attention to a sparse tensor.
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
qkv (SparseTensor): [N, *, 3, H, C] sparse tensor containing Qs, Ks, and Vs.
|
| 73 |
+
window_size (int): The window size to use.
|
| 74 |
+
shift_window (Tuple[int, int, int]): The shift of serialized coordinates.
|
| 75 |
+
shift (int): The shift to use.
|
| 76 |
+
"""
|
| 77 |
+
assert len(qkv.shape) == 4 and qkv.shape[1] == 3, f"Invalid shape for qkv, got {qkv.shape}, expected [N, *, 3, H, C]"
|
| 78 |
+
|
| 79 |
+
serialization_spatial_cache_name = f'window_partition_{window_size}_{shift_window}'
|
| 80 |
+
serialization_spatial_cache = qkv.get_spatial_cache(serialization_spatial_cache_name)
|
| 81 |
+
if serialization_spatial_cache is None:
|
| 82 |
+
fwd_indices, bwd_indices, seq_lens, seq_batch_indices = calc_window_partition(qkv, window_size, shift_window)
|
| 83 |
+
qkv.register_spatial_cache(serialization_spatial_cache_name, (fwd_indices, bwd_indices, seq_lens, seq_batch_indices))
|
| 84 |
+
else:
|
| 85 |
+
fwd_indices, bwd_indices, seq_lens, seq_batch_indices = serialization_spatial_cache
|
| 86 |
+
|
| 87 |
+
M = fwd_indices.shape[0]
|
| 88 |
+
T = qkv.feats.shape[0]
|
| 89 |
+
H = qkv.feats.shape[2]
|
| 90 |
+
C = qkv.feats.shape[3]
|
| 91 |
+
|
| 92 |
+
qkv_feats = qkv.feats[fwd_indices] # [M, 3, H, C]
|
| 93 |
+
|
| 94 |
+
if DEBUG:
|
| 95 |
+
start = 0
|
| 96 |
+
qkv_coords = qkv.coords[fwd_indices]
|
| 97 |
+
for i in range(len(seq_lens)):
|
| 98 |
+
seq_coords = qkv_coords[start:start+seq_lens[i]]
|
| 99 |
+
assert (seq_coords[:, 0] == seq_batch_indices[i]).all(), f"SparseWindowedScaledDotProductSelfAttention: batch index mismatch"
|
| 100 |
+
assert (seq_coords[:, 1:].max(dim=0).values - seq_coords[:, 1:].min(dim=0).values < window_size).all(), \
|
| 101 |
+
f"SparseWindowedScaledDotProductSelfAttention: window size exceeded"
|
| 102 |
+
start += seq_lens[i]
|
| 103 |
+
|
| 104 |
+
if all([seq_len == window_size for seq_len in seq_lens]):
|
| 105 |
+
B = len(seq_lens)
|
| 106 |
+
N = window_size
|
| 107 |
+
qkv_feats = qkv_feats.reshape(B, N, 3, H, C)
|
| 108 |
+
if ATTN == 'xformers':
|
| 109 |
+
q, k, v = qkv_feats.unbind(dim=2) # [B, N, H, C]
|
| 110 |
+
out = xops.memory_efficient_attention(q, k, v) # [B, N, H, C]
|
| 111 |
+
elif ATTN == 'flash_attn':
|
| 112 |
+
out = flash_attn.flash_attn_qkvpacked_func(qkv_feats) # [B, N, H, C]
|
| 113 |
+
else:
|
| 114 |
+
raise ValueError(f"Unknown attention module: {ATTN}")
|
| 115 |
+
out = out.reshape(B * N, H, C) # [M, H, C]
|
| 116 |
+
else:
|
| 117 |
+
if ATTN == 'xformers':
|
| 118 |
+
q, k, v = qkv_feats.unbind(dim=1) # [M, H, C]
|
| 119 |
+
q = q.unsqueeze(0) # [1, M, H, C]
|
| 120 |
+
k = k.unsqueeze(0) # [1, M, H, C]
|
| 121 |
+
v = v.unsqueeze(0) # [1, M, H, C]
|
| 122 |
+
mask = xops.fmha.BlockDiagonalMask.from_seqlens(seq_lens)
|
| 123 |
+
out = xops.memory_efficient_attention(q, k, v, mask)[0] # [M, H, C]
|
| 124 |
+
elif ATTN == 'flash_attn':
|
| 125 |
+
cu_seqlens = torch.cat([torch.tensor([0]), torch.cumsum(torch.tensor(seq_lens), dim=0)], dim=0) \
|
| 126 |
+
.to(qkv.device).int()
|
| 127 |
+
out = flash_attn.flash_attn_varlen_qkvpacked_func(qkv_feats, cu_seqlens, max(seq_lens)) # [M, H, C]
|
| 128 |
+
|
| 129 |
+
out = out[bwd_indices] # [T, H, C]
|
| 130 |
+
|
| 131 |
+
if DEBUG:
|
| 132 |
+
qkv_coords = qkv_coords[bwd_indices]
|
| 133 |
+
assert torch.equal(qkv_coords, qkv.coords), "SparseWindowedScaledDotProductSelfAttention: coordinate mismatch"
|
| 134 |
+
|
| 135 |
+
return qkv.replace(out)
|