Download models/onnx_loader.py from modventures/floor-visualizer-gpu: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/models/onnx_loader.py
- Command line
-
hf download hf://spaces/modventures/floor-visualizer-gpu/models/onnx_loader.py
-
curl -L -o onnx_loader.py https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/models/onnx_loader.py
2.5 kB
| """ONNX Runtime model loading for accelerated inference.""" | |
| from pathlib import Path | |
| import state | |
| from config.runtime import ENABLE_ONNX_INFERENCE, ONNX_MODEL_DIR | |
| ONNX_DIR = Path(ONNX_MODEL_DIR) | |
| def _ort_providers() -> list[str]: | |
| """Return available ONNX Runtime execution providers, preferring GPU.""" | |
| providers = [] | |
| if state.device.type == "cuda": | |
| providers.append("CUDAExecutionProvider") | |
| providers.append("CPUExecutionProvider") | |
| return providers | |
| def load_seg_onnx_session(): | |
| """Load the OneFormer ONNX session if available.""" | |
| if not ENABLE_ONNX_INFERENCE: | |
| return None | |
| if state.seg_onnx_session is not None: | |
| return state.seg_onnx_session | |
| model_path = ONNX_DIR / "oneformer_seg.onnx" | |
| if not model_path.exists(): | |
| print(f"ONNX segmentation model not found at {model_path}, falling back to PyTorch.", flush=True) | |
| return None | |
| try: | |
| import onnxruntime as ort | |
| sess_options = ort.SessionOptions() | |
| sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL | |
| state.seg_onnx_session = ort.InferenceSession( | |
| str(model_path), sess_options, providers=_ort_providers(), | |
| ) | |
| print(f"ONNX segmentation model loaded from {model_path}", flush=True) | |
| return state.seg_onnx_session | |
| except Exception as exc: | |
| print(f"Failed to load ONNX segmentation model ({exc}), falling back to PyTorch.", flush=True) | |
| return None | |
| def load_depth_onnx_session(): | |
| """Load the Depth-Anything ONNX session if available.""" | |
| if not ENABLE_ONNX_INFERENCE: | |
| return None | |
| if state.depth_onnx_session is not None: | |
| return state.depth_onnx_session | |
| model_path = ONNX_DIR / "depth_anything.onnx" | |
| if not model_path.exists(): | |
| print(f"ONNX depth model not found at {model_path}, falling back to PyTorch.", flush=True) | |
| return None | |
| try: | |
| import onnxruntime as ort | |
| sess_options = ort.SessionOptions() | |
| sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL | |
| state.depth_onnx_session = ort.InferenceSession( | |
| str(model_path), sess_options, providers=_ort_providers(), | |
| ) | |
| print(f"ONNX depth model loaded from {model_path}", flush=True) | |
| return state.depth_onnx_session | |
| except Exception as exc: | |
| print(f"Failed to load ONNX depth model ({exc}), falling back to PyTorch.", flush=True) | |
| return None | |