Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,36 +8,38 @@ import gradio as gr
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
# ✅ 必须最早 import spaces(在 torch / 任何 CUDA 初始化之前)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
spaces = None # 不影响本地跑
|
| 15 |
|
| 16 |
-
# ========== 让 Space 能 import 你的工程 ==========
|
| 17 |
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
sys.path.append(PROJECT_ROOT)
|
| 19 |
|
| 20 |
from networks.models import make # noqa: E402
|
| 21 |
|
| 22 |
-
# ====== HF 权重仓库配置 ======
|
| 23 |
WEIGHTS_REPO = "Insta360-Research/DAP-weights"
|
| 24 |
WEIGHTS_FILE = "model.pth"
|
| 25 |
CONFIG_PATH = "config/infer.yaml"
|
| 26 |
|
| 27 |
-
# 先定义全局占位
|
| 28 |
model = None
|
| 29 |
device = "cpu"
|
| 30 |
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def load_model(config_path: str):
|
| 40 |
-
# ✅ torch 放到这里 import,避免在 spaces import 之前触发 CUDA
|
| 41 |
import torch
|
| 42 |
import torch.nn as nn
|
| 43 |
|
|
@@ -65,36 +67,45 @@ def load_model(config_path: str):
|
|
| 65 |
return m
|
| 66 |
|
| 67 |
|
| 68 |
-
# ✅ 启动时加载一次模型
|
| 69 |
model = load_model(CONFIG_PATH)
|
| 70 |
|
|
|
|
| 71 |
@spaces.GPU
|
| 72 |
def predict(img_rgb: np.ndarray):
|
| 73 |
if img_rgb is None:
|
| 74 |
return None, None
|
| 75 |
|
| 76 |
-
import torch
|
| 77 |
|
| 78 |
img = img_rgb.astype(np.float32) / 255.0
|
| 79 |
tensor = torch.from_numpy(img.transpose(2, 0, 1)).unsqueeze(0).to(device)
|
| 80 |
|
| 81 |
-
with torch.
|
| 82 |
outputs = model(tensor)
|
| 83 |
|
| 84 |
if isinstance(outputs, dict) and "pred_depth" in outputs:
|
| 85 |
if "pred_mask" in outputs:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
outputs["pred_depth"][~
|
| 89 |
pred = outputs["pred_depth"][0].detach().cpu().squeeze().numpy()
|
| 90 |
else:
|
| 91 |
pred = outputs[0].detach().cpu().squeeze().numpy()
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
depth_color_rgb =
|
| 98 |
|
| 99 |
return depth_color_rgb, depth_gray
|
| 100 |
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
# ✅ 必须最早 import spaces(在 torch / 任何 CUDA 初始化之前)
|
| 11 |
+
import spaces # 在 HF Spaces 一定存在
|
| 12 |
+
|
| 13 |
+
import matplotlib # 用你的 colormap
|
|
|
|
| 14 |
|
|
|
|
| 15 |
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
sys.path.append(PROJECT_ROOT)
|
| 17 |
|
| 18 |
from networks.models import make # noqa: E402
|
| 19 |
|
|
|
|
| 20 |
WEIGHTS_REPO = "Insta360-Research/DAP-weights"
|
| 21 |
WEIGHTS_FILE = "model.pth"
|
| 22 |
CONFIG_PATH = "config/infer.yaml"
|
| 23 |
|
|
|
|
| 24 |
model = None
|
| 25 |
device = "cpu"
|
| 26 |
|
| 27 |
|
| 28 |
+
|
| 29 |
+
def colorize_depth_matplotlib(depth: np.ndarray, mask: np.ndarray = None, normalize: bool = True, cmap: str = 'Spectral') -> np.ndarray:
|
| 30 |
+
if mask is None:
|
| 31 |
+
depth = np.where(depth > 0, depth, np.nan)
|
| 32 |
+
else:
|
| 33 |
+
depth = np.where((depth > 0) & mask, depth, np.nan)
|
| 34 |
+
|
| 35 |
+
disp = depth / 255.0
|
| 36 |
+
|
| 37 |
+
colored = np.nan_to_num(matplotlib.colormaps[cmap](disp)[..., :3], 0)
|
| 38 |
+
colored = np.ascontiguousarray((colored.clip(0, 1) * 255).astype(np.uint8))
|
| 39 |
+
return colored
|
| 40 |
|
| 41 |
|
| 42 |
def load_model(config_path: str):
|
|
|
|
| 43 |
import torch
|
| 44 |
import torch.nn as nn
|
| 45 |
|
|
|
|
| 67 |
return m
|
| 68 |
|
| 69 |
|
|
|
|
| 70 |
model = load_model(CONFIG_PATH)
|
| 71 |
|
| 72 |
+
|
| 73 |
@spaces.GPU
|
| 74 |
def predict(img_rgb: np.ndarray):
|
| 75 |
if img_rgb is None:
|
| 76 |
return None, None
|
| 77 |
|
| 78 |
+
import torch
|
| 79 |
|
| 80 |
img = img_rgb.astype(np.float32) / 255.0
|
| 81 |
tensor = torch.from_numpy(img.transpose(2, 0, 1)).unsqueeze(0).to(device)
|
| 82 |
|
| 83 |
+
with torch.inference_mode():
|
| 84 |
outputs = model(tensor)
|
| 85 |
|
| 86 |
if isinstance(outputs, dict) and "pred_depth" in outputs:
|
| 87 |
if "pred_mask" in outputs:
|
| 88 |
+
pm = 1 - outputs["pred_mask"]
|
| 89 |
+
pm = (pm > 0.5)
|
| 90 |
+
outputs["pred_depth"][~pm] = 1
|
| 91 |
pred = outputs["pred_depth"][0].detach().cpu().squeeze().numpy()
|
| 92 |
else:
|
| 93 |
pred = outputs[0].detach().cpu().squeeze().numpy()
|
| 94 |
|
| 95 |
+
# 灰度图:如果你 pred 本来就在 0~1,就直接 *255;否则先归一化
|
| 96 |
+
pred = pred.astype(np.float32)
|
| 97 |
+
pred_clip = np.clip(pred, 1e-6, np.nanmax(pred) if np.isfinite(pred).any() else 1.0)
|
| 98 |
+
|
| 99 |
+
# 让灰度输出稳定:用分位数做一次归一化
|
| 100 |
+
lo = np.nanquantile(pred_clip, 0.001)
|
| 101 |
+
hi = np.nanquantile(pred_clip, 0.99)
|
| 102 |
+
pred_norm = (pred_clip - lo) / (hi - lo + 1e-6)
|
| 103 |
+
pred_norm = np.clip(pred_norm, 0.0, 1.0)
|
| 104 |
+
|
| 105 |
+
depth_gray = (pred_norm * 255).astype(np.uint8)
|
| 106 |
|
| 107 |
+
# 彩色图:用你改进的可视化
|
| 108 |
+
depth_color_rgb = colorize_depth_matplotlib(pred_norm, normalize=False, cmap="Spectral")
|
| 109 |
|
| 110 |
return depth_color_rgb, depth_gray
|
| 111 |
|