Spaces:
Sleeping
Sleeping
Dominik Fojcik commited on
Commit ·
c5ab246
1
Parent(s): ba25ce3
Extend and trace ZeroGPU depth inference
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
| 3 |
from functools import lru_cache
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
|
|
@@ -110,7 +111,7 @@ MODEL_CHOICES = [RGB_MODEL_TYPE, DEPTH_SENSOR_MODEL_TYPE]
|
|
| 110 |
OUTPUT_MODE_CHOICES = ["upsample", "original", "specific"]
|
| 111 |
GS_SAMPLE_POINT_NUM = 2000000
|
| 112 |
GS_COORD_DETERMINISTIC_SAMPLING = True
|
| 113 |
-
DEPTH_GPU_DURATION_SECONDS =
|
| 114 |
GS_GPU_DURATION_SECONDS = 240
|
| 115 |
|
| 116 |
LOCAL_DEPTH_MODEL_PATHS = {
|
|
@@ -476,7 +477,14 @@ def _run_depth_inference(
|
|
| 476 |
cy_org: Optional[float],
|
| 477 |
request: gr.Request,
|
| 478 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
_ensure_cuda()
|
|
|
|
| 480 |
if image is None:
|
| 481 |
raise gr.Error("Upload an image or load an example before running inference.")
|
| 482 |
if model_type == DEPTH_SENSOR_MODEL_TYPE and not depth_file:
|
|
@@ -488,6 +496,7 @@ def _run_depth_inference(
|
|
| 488 |
device = torch.device("cuda")
|
| 489 |
image_tensor = image_tensor.to(device)
|
| 490 |
model = _load_model(model_type)
|
|
|
|
| 491 |
|
| 492 |
gt_depth, prompt_depth, gt_depth_mask, use_gt_depth, moge2_intrinsics = prepare_metric_depth_inputs(
|
| 493 |
input_depth_path=depth_file,
|
|
@@ -496,6 +505,7 @@ def _run_depth_inference(
|
|
| 496 |
device=device,
|
| 497 |
moge2_pretrained=_resolve_moge2_source(),
|
| 498 |
)
|
|
|
|
| 499 |
|
| 500 |
gt_disp = depth_to_disparity(gt_depth)
|
| 501 |
prompt_disp = depth_to_disparity(prompt_depth)
|
|
@@ -511,6 +521,7 @@ def _run_depth_inference(
|
|
| 511 |
moge2_pretrained=_resolve_moge2_source(),
|
| 512 |
moge2_intrinsics=moge2_intrinsics,
|
| 513 |
)
|
|
|
|
| 514 |
|
| 515 |
_, _, h, w = image_tensor.shape
|
| 516 |
fx, fy, cx, cy, _ = build_scaled_intrinsics_matrix(
|
|
@@ -530,6 +541,7 @@ def _run_depth_inference(
|
|
| 530 |
enable_skyseg_model=enable_skyseg_model,
|
| 531 |
sky_model_ckpt_path=skyseg_path or str(LOCAL_SKYSEG_PATH),
|
| 532 |
)
|
|
|
|
| 533 |
|
| 534 |
h_out, w_out = resolve_output_size_from_mode(
|
| 535 |
output_resolution_mode=output_resolution_mode,
|
|
@@ -550,6 +562,7 @@ def _run_depth_inference(
|
|
| 550 |
prompt_depth=prompt_disp,
|
| 551 |
prompt_mask=prompt_disp > 0,
|
| 552 |
)
|
|
|
|
| 553 |
pred_depthmap = pred_2d_uniform_depth.permute(0, 2, 1).reshape(1, 1, h_out, w_out)
|
| 554 |
|
| 555 |
pred_depthmap, pred_2d_uniform_depth = apply_sky_mask_to_depth(
|
|
@@ -566,6 +579,7 @@ def _run_depth_inference(
|
|
| 566 |
|
| 567 |
pred_depth_np = pred_depthmap.squeeze(0).squeeze(0).detach().cpu().numpy().astype(np.float32)
|
| 568 |
artifacts = save_demo_artifacts(image_rgb=image_rgb, pred_depth=pred_depth_np, output_dir=output_dir)
|
|
|
|
| 569 |
ply_path, glb_path = export_point_cloud_assets(
|
| 570 |
sampled_coord=query_2d_uniform_coord.squeeze(0).cpu(),
|
| 571 |
sampled_depth=pred_2d_uniform_depth.squeeze(0).squeeze(-1).cpu(),
|
|
@@ -577,6 +591,7 @@ def _run_depth_inference(
|
|
| 577 |
output_dir=output_dir,
|
| 578 |
filter_flying_points=filter_point_cloud,
|
| 579 |
)
|
|
|
|
| 580 |
artifacts = DemoArtifacts(
|
| 581 |
comparison_path=artifacts.comparison_path,
|
| 582 |
color_depth_path=artifacts.color_depth_path,
|
|
|
|
| 3 |
from functools import lru_cache
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
| 6 |
+
import time
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
|
|
|
|
| 111 |
OUTPUT_MODE_CHOICES = ["upsample", "original", "specific"]
|
| 112 |
GS_SAMPLE_POINT_NUM = 2000000
|
| 113 |
GS_COORD_DETERMINISTIC_SAMPLING = True
|
| 114 |
+
DEPTH_GPU_DURATION_SECONDS = 600
|
| 115 |
GS_GPU_DURATION_SECONDS = 240
|
| 116 |
|
| 117 |
LOCAL_DEPTH_MODEL_PATHS = {
|
|
|
|
| 477 |
cy_org: Optional[float],
|
| 478 |
request: gr.Request,
|
| 479 |
):
|
| 480 |
+
started_at = time.perf_counter()
|
| 481 |
+
|
| 482 |
+
def log_stage(stage: str) -> None:
|
| 483 |
+
elapsed = time.perf_counter() - started_at
|
| 484 |
+
print(f"[INFO] Depth inference {stage} after {elapsed:.1f}s", flush=True)
|
| 485 |
+
|
| 486 |
_ensure_cuda()
|
| 487 |
+
log_stage(f"started on {torch.cuda.get_device_name(0)}")
|
| 488 |
if image is None:
|
| 489 |
raise gr.Error("Upload an image or load an example before running inference.")
|
| 490 |
if model_type == DEPTH_SENSOR_MODEL_TYPE and not depth_file:
|
|
|
|
| 496 |
device = torch.device("cuda")
|
| 497 |
image_tensor = image_tensor.to(device)
|
| 498 |
model = _load_model(model_type)
|
| 499 |
+
log_stage("prepared input and model")
|
| 500 |
|
| 501 |
gt_depth, prompt_depth, gt_depth_mask, use_gt_depth, moge2_intrinsics = prepare_metric_depth_inputs(
|
| 502 |
input_depth_path=depth_file,
|
|
|
|
| 505 |
device=device,
|
| 506 |
moge2_pretrained=_resolve_moge2_source(),
|
| 507 |
)
|
| 508 |
+
log_stage("prepared metric depth")
|
| 509 |
|
| 510 |
gt_disp = depth_to_disparity(gt_depth)
|
| 511 |
prompt_disp = depth_to_disparity(prompt_depth)
|
|
|
|
| 521 |
moge2_pretrained=_resolve_moge2_source(),
|
| 522 |
moge2_intrinsics=moge2_intrinsics,
|
| 523 |
)
|
| 524 |
+
log_stage("resolved intrinsics")
|
| 525 |
|
| 526 |
_, _, h, w = image_tensor.shape
|
| 527 |
fx, fy, cx, cy, _ = build_scaled_intrinsics_matrix(
|
|
|
|
| 541 |
enable_skyseg_model=enable_skyseg_model,
|
| 542 |
sky_model_ckpt_path=skyseg_path or str(LOCAL_SKYSEG_PATH),
|
| 543 |
)
|
| 544 |
+
log_stage("prepared sky mask")
|
| 545 |
|
| 546 |
h_out, w_out = resolve_output_size_from_mode(
|
| 547 |
output_resolution_mode=output_resolution_mode,
|
|
|
|
| 562 |
prompt_depth=prompt_disp,
|
| 563 |
prompt_mask=prompt_disp > 0,
|
| 564 |
)
|
| 565 |
+
log_stage("completed model inference")
|
| 566 |
pred_depthmap = pred_2d_uniform_depth.permute(0, 2, 1).reshape(1, 1, h_out, w_out)
|
| 567 |
|
| 568 |
pred_depthmap, pred_2d_uniform_depth = apply_sky_mask_to_depth(
|
|
|
|
| 579 |
|
| 580 |
pred_depth_np = pred_depthmap.squeeze(0).squeeze(0).detach().cpu().numpy().astype(np.float32)
|
| 581 |
artifacts = save_demo_artifacts(image_rgb=image_rgb, pred_depth=pred_depth_np, output_dir=output_dir)
|
| 582 |
+
log_stage("saved depth artifacts")
|
| 583 |
ply_path, glb_path = export_point_cloud_assets(
|
| 584 |
sampled_coord=query_2d_uniform_coord.squeeze(0).cpu(),
|
| 585 |
sampled_depth=pred_2d_uniform_depth.squeeze(0).squeeze(-1).cpu(),
|
|
|
|
| 591 |
output_dir=output_dir,
|
| 592 |
filter_flying_points=filter_point_cloud,
|
| 593 |
)
|
| 594 |
+
log_stage("exported point cloud")
|
| 595 |
artifacts = DemoArtifacts(
|
| 596 |
comparison_path=artifacts.comparison_path,
|
| 597 |
color_depth_path=artifacts.color_depth_path,
|