Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
b00b3bb
1
Parent(s):
75eb903
add blueprint, fix bug where it wouldn't use the preprocessed image
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ from queue import SimpleQueue
|
|
| 16 |
from typing import Any
|
| 17 |
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 18 |
import rerun as rr
|
|
|
|
| 19 |
from gradio_rerun import Rerun
|
| 20 |
|
| 21 |
import src
|
|
@@ -156,15 +157,10 @@ def preprocess(input_image, do_remove_background):
|
|
| 156 |
|
| 157 |
|
| 158 |
def pipeline_callback(output_queue: SimpleQueue, pipe: Any, step_index: int, timestep: float, callback_kwargs: dict[str, Any]) -> dict[str, Any]:
|
| 159 |
-
rr.set_time_sequence("iteration", step_index)
|
| 160 |
-
rr.set_time_seconds("timestep", timestep)
|
| 161 |
latents = callback_kwargs["latents"]
|
| 162 |
image = pipe.vae.decode(latents / pipe.vae.config.scaling_factor, return_dict=False)[0] # type: ignore[attr-defined]
|
| 163 |
image = pipe.image_processor.postprocess(image, output_type="np").squeeze() # type: ignore[attr-defined]
|
| 164 |
|
| 165 |
-
# rr.log("mvs/image", rr.Image(image))
|
| 166 |
-
# rr.log("mvs/latents", rr.Tensor(latents.squeeze()))
|
| 167 |
-
|
| 168 |
output_queue.put(("log", "mvs/image", rr.Image(image)))
|
| 169 |
output_queue.put(("log", "mvs/latents", rr.Tensor(latents.squeeze())))
|
| 170 |
|
|
@@ -174,6 +170,7 @@ def generate_mvs(input_image, sample_steps, sample_seed):
|
|
| 174 |
|
| 175 |
seed_everything(sample_seed)
|
| 176 |
|
|
|
|
| 177 |
def thread_target(output_queue, input_image, sample_steps):
|
| 178 |
z123_image = pipeline(
|
| 179 |
input_image,
|
|
@@ -182,6 +179,7 @@ def generate_mvs(input_image, sample_steps, sample_seed):
|
|
| 182 |
).images[0]
|
| 183 |
output_queue.put(("z123_image", z123_image))
|
| 184 |
|
|
|
|
| 185 |
output_queue = SimpleQueue()
|
| 186 |
z123_thread = threading.Thread(
|
| 187 |
target=thread_target,
|
|
@@ -197,11 +195,10 @@ def generate_mvs(input_image, sample_steps, sample_seed):
|
|
| 197 |
while True:
|
| 198 |
msg = output_queue.get()
|
| 199 |
yield msg
|
| 200 |
-
if msg[0] == "z123_image":
|
| 201 |
-
break
|
| 202 |
z123_thread.join()
|
| 203 |
|
| 204 |
-
|
| 205 |
def make3d(images: Image.Image):
|
| 206 |
output_queue = SimpleQueue()
|
| 207 |
handle = threading.Thread(target=_make3d, args=[output_queue, images])
|
|
@@ -214,7 +211,6 @@ def make3d(images: Image.Image):
|
|
| 214 |
handle.join()
|
| 215 |
|
| 216 |
def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
| 217 |
-
print(f'type(images)={type(images)}')
|
| 218 |
global model
|
| 219 |
if IS_FLEXICUBES:
|
| 220 |
model.init_flexicubes_geometry(device, use_renderer=False)
|
|
@@ -226,15 +222,11 @@ def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
| 226 |
|
| 227 |
input_cameras = get_zero123plus_input_cameras(batch_size=1, radius=4.0).to(device)
|
| 228 |
render_cameras = get_render_cameras(batch_size=1, radius=2.5, is_flexicubes=IS_FLEXICUBES).to(device)
|
| 229 |
-
print(f'type(input_cameras)={type(input_cameras)}')
|
| 230 |
|
| 231 |
images = images.unsqueeze(0).to(device)
|
| 232 |
images = v2.functional.resize(images, (320, 320), interpolation=3, antialias=True).clamp(0, 1)
|
| 233 |
-
print(f'type(images)={type(images)}')
|
| 234 |
-
|
| 235 |
|
| 236 |
mesh_fpath = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False).name
|
| 237 |
-
print(mesh_fpath)
|
| 238 |
mesh_basename = os.path.basename(mesh_fpath).split('.')[0]
|
| 239 |
mesh_dirname = os.path.dirname(mesh_fpath)
|
| 240 |
video_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.mp4")
|
|
@@ -243,7 +235,6 @@ def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
| 243 |
with torch.no_grad():
|
| 244 |
# get triplane
|
| 245 |
planes = model.forward_planes(images, input_cameras)
|
| 246 |
-
print(f'type(planes)={type(planes)}')
|
| 247 |
|
| 248 |
# get video
|
| 249 |
# chunk_size = 20 if IS_FLEXICUBES else 1
|
|
@@ -283,7 +274,6 @@ def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
| 283 |
use_texture_map=False,
|
| 284 |
**infer_config,
|
| 285 |
)
|
| 286 |
-
print(f'type(mesh_out)={type(mesh_out)}')
|
| 287 |
|
| 288 |
vertices, faces, vertex_colors = mesh_out
|
| 289 |
|
|
@@ -301,10 +291,6 @@ def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
| 301 |
|
| 302 |
vertices = vertices[:, [1, 2, 0]]
|
| 303 |
|
| 304 |
-
print(f'type(vertices)={type(vertices)}')
|
| 305 |
-
print(f'type(faces)={type(faces)}')
|
| 306 |
-
print(f'type(vertex_colors)={type(vertex_colors)}')
|
| 307 |
-
|
| 308 |
save_glb(vertices, faces, vertex_colors, mesh_glb_fpath)
|
| 309 |
save_obj(vertices, faces, vertex_colors, mesh_fpath)
|
| 310 |
|
|
@@ -312,19 +298,37 @@ def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
| 312 |
|
| 313 |
output_queue.put(("mesh", mesh_out))
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
@spaces.GPU
|
| 317 |
@rr.thread_local_stream("InstantMesh")
|
| 318 |
def log_to_rr(input_image, do_remove_background, sample_steps, sample_seed):
|
| 319 |
-
preprocessed_image = preprocess(input_image, do_remove_background)
|
| 320 |
|
| 321 |
stream = rr.binary_stream()
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
rr.log("preprocessed_image", rr.Image(preprocessed_image))
|
| 324 |
|
| 325 |
yield stream.read()
|
| 326 |
|
| 327 |
-
for msg in generate_mvs(
|
| 328 |
if msg[0] == "z123_image":
|
| 329 |
z123_image = msg[1]
|
| 330 |
break
|
|
@@ -426,7 +430,7 @@ with gr.Blocks() as demo:
|
|
| 426 |
examples_per_page=16
|
| 427 |
)
|
| 428 |
|
| 429 |
-
with gr.Column(scale=
|
| 430 |
|
| 431 |
viewer = Rerun(streaming=True, height=800)
|
| 432 |
|
|
|
|
| 16 |
from typing import Any
|
| 17 |
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 18 |
import rerun as rr
|
| 19 |
+
import rerun.blueprint as rrb
|
| 20 |
from gradio_rerun import Rerun
|
| 21 |
|
| 22 |
import src
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
def pipeline_callback(output_queue: SimpleQueue, pipe: Any, step_index: int, timestep: float, callback_kwargs: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
|
|
|
| 160 |
latents = callback_kwargs["latents"]
|
| 161 |
image = pipe.vae.decode(latents / pipe.vae.config.scaling_factor, return_dict=False)[0] # type: ignore[attr-defined]
|
| 162 |
image = pipe.image_processor.postprocess(image, output_type="np").squeeze() # type: ignore[attr-defined]
|
| 163 |
|
|
|
|
|
|
|
|
|
|
| 164 |
output_queue.put(("log", "mvs/image", rr.Image(image)))
|
| 165 |
output_queue.put(("log", "mvs/latents", rr.Tensor(latents.squeeze())))
|
| 166 |
|
|
|
|
| 170 |
|
| 171 |
seed_everything(sample_seed)
|
| 172 |
|
| 173 |
+
|
| 174 |
def thread_target(output_queue, input_image, sample_steps):
|
| 175 |
z123_image = pipeline(
|
| 176 |
input_image,
|
|
|
|
| 179 |
).images[0]
|
| 180 |
output_queue.put(("z123_image", z123_image))
|
| 181 |
|
| 182 |
+
|
| 183 |
output_queue = SimpleQueue()
|
| 184 |
z123_thread = threading.Thread(
|
| 185 |
target=thread_target,
|
|
|
|
| 195 |
while True:
|
| 196 |
msg = output_queue.get()
|
| 197 |
yield msg
|
| 198 |
+
if msg[0] == "z123_image":
|
| 199 |
+
break
|
| 200 |
z123_thread.join()
|
| 201 |
|
|
|
|
| 202 |
def make3d(images: Image.Image):
|
| 203 |
output_queue = SimpleQueue()
|
| 204 |
handle = threading.Thread(target=_make3d, args=[output_queue, images])
|
|
|
|
| 211 |
handle.join()
|
| 212 |
|
| 213 |
def _make3d(output_queue: SimpleQueue, images: Image.Image):
|
|
|
|
| 214 |
global model
|
| 215 |
if IS_FLEXICUBES:
|
| 216 |
model.init_flexicubes_geometry(device, use_renderer=False)
|
|
|
|
| 222 |
|
| 223 |
input_cameras = get_zero123plus_input_cameras(batch_size=1, radius=4.0).to(device)
|
| 224 |
render_cameras = get_render_cameras(batch_size=1, radius=2.5, is_flexicubes=IS_FLEXICUBES).to(device)
|
|
|
|
| 225 |
|
| 226 |
images = images.unsqueeze(0).to(device)
|
| 227 |
images = v2.functional.resize(images, (320, 320), interpolation=3, antialias=True).clamp(0, 1)
|
|
|
|
|
|
|
| 228 |
|
| 229 |
mesh_fpath = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False).name
|
|
|
|
| 230 |
mesh_basename = os.path.basename(mesh_fpath).split('.')[0]
|
| 231 |
mesh_dirname = os.path.dirname(mesh_fpath)
|
| 232 |
video_fpath = os.path.join(mesh_dirname, f"{mesh_basename}.mp4")
|
|
|
|
| 235 |
with torch.no_grad():
|
| 236 |
# get triplane
|
| 237 |
planes = model.forward_planes(images, input_cameras)
|
|
|
|
| 238 |
|
| 239 |
# get video
|
| 240 |
# chunk_size = 20 if IS_FLEXICUBES else 1
|
|
|
|
| 274 |
use_texture_map=False,
|
| 275 |
**infer_config,
|
| 276 |
)
|
|
|
|
| 277 |
|
| 278 |
vertices, faces, vertex_colors = mesh_out
|
| 279 |
|
|
|
|
| 291 |
|
| 292 |
vertices = vertices[:, [1, 2, 0]]
|
| 293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
save_glb(vertices, faces, vertex_colors, mesh_glb_fpath)
|
| 295 |
save_obj(vertices, faces, vertex_colors, mesh_fpath)
|
| 296 |
|
|
|
|
| 298 |
|
| 299 |
output_queue.put(("mesh", mesh_out))
|
| 300 |
|
| 301 |
+
def generate_blueprint() -> rrb.Blueprint:
|
| 302 |
+
return rrb.Blueprint(
|
| 303 |
+
rrb.Horizontal(
|
| 304 |
+
rrb.Spatial3DView(origin="mesh"),
|
| 305 |
+
rrb.Grid(
|
| 306 |
+
rrb.Spatial2DView(origin="z123image"),
|
| 307 |
+
rrb.Spatial2DView(origin="preprocessed_image"),
|
| 308 |
+
rrb.Spatial2DView(origin="mvs/image"),
|
| 309 |
+
rrb.TensorView(origin="mvs/latents"),
|
| 310 |
+
),
|
| 311 |
+
column_shares=[1, 1],
|
| 312 |
+
),
|
| 313 |
+
collapse_panels=True,
|
| 314 |
+
)
|
| 315 |
|
| 316 |
@spaces.GPU
|
| 317 |
@rr.thread_local_stream("InstantMesh")
|
| 318 |
def log_to_rr(input_image, do_remove_background, sample_steps, sample_seed):
|
|
|
|
| 319 |
|
| 320 |
stream = rr.binary_stream()
|
| 321 |
|
| 322 |
+
blueprint = generate_blueprint()
|
| 323 |
+
rr.send_blueprint(blueprint)
|
| 324 |
+
yield stream.read()
|
| 325 |
+
|
| 326 |
+
preprocessed_image = preprocess(input_image, do_remove_background)
|
| 327 |
rr.log("preprocessed_image", rr.Image(preprocessed_image))
|
| 328 |
|
| 329 |
yield stream.read()
|
| 330 |
|
| 331 |
+
for msg in generate_mvs(preprocessed_image, sample_steps, sample_seed):
|
| 332 |
if msg[0] == "z123_image":
|
| 333 |
z123_image = msg[1]
|
| 334 |
break
|
|
|
|
| 430 |
examples_per_page=16
|
| 431 |
)
|
| 432 |
|
| 433 |
+
with gr.Column(scale=2):
|
| 434 |
|
| 435 |
viewer = Rerun(streaming=True, height=800)
|
| 436 |
|