Download scripts/export_onnx.py from modventures/room-visualizer-v2: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/scripts/export_onnx.py
- Command line
-
hf download hf://spaces/modventures/room-visualizer-v2/scripts/export_onnx.py
-
curl -L -o export_onnx.py https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/scripts/export_onnx.py
3.8 kB
| """Export PyTorch models to ONNX format for accelerated inference. | |
| Usage: | |
| python scripts/export_onnx.py --model seg # export segmentation model | |
| python scripts/export_onnx.py --model depth # export depth model | |
| python scripts/export_onnx.py --model all # export both | |
| Models are saved to the onnx_models/ directory. | |
| Set VISUALIZER_CONFIG env var to use the correct model names from your TOML config. | |
| """ | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| # Add parent dir to path so we can import project modules | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| ONNX_DIR = Path(__file__).resolve().parent.parent / "onnx_models" | |
| def export_segmentation(): | |
| from config.models import ONEFORMER_MODEL_NAME | |
| from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation | |
| print(f"Loading OneFormer: {ONEFORMER_MODEL_NAME} ...") | |
| processor = OneFormerProcessor.from_pretrained(ONEFORMER_MODEL_NAME) | |
| model = OneFormerForUniversalSegmentation.from_pretrained(ONEFORMER_MODEL_NAME) | |
| model.eval() | |
| # Create dummy input (1024x1024 RGB) | |
| dummy_image = Image.fromarray(np.random.randint(0, 255, (1024, 1024, 3), dtype=np.uint8)) | |
| inputs = processor(images=dummy_image, task_inputs=["semantic"], return_tensors="pt") | |
| output_path = ONNX_DIR / "oneformer_seg.onnx" | |
| ONNX_DIR.mkdir(parents=True, exist_ok=True) | |
| print(f"Exporting to {output_path} ...") | |
| input_names = list(inputs.keys()) | |
| torch.onnx.export( | |
| model, | |
| tuple(inputs[k] for k in input_names), | |
| str(output_path), | |
| input_names=input_names, | |
| output_names=["class_queries_logits", "masks_queries_logits"], | |
| dynamic_axes={ | |
| name: {0: "batch", 2: "height", 3: "width"} if inputs[name].ndim == 4 | |
| else {0: "batch"} for name in input_names | |
| }, | |
| opset_version=17, | |
| do_constant_folding=True, | |
| ) | |
| print(f"Segmentation models exported: {output_path} ({output_path.stat().st_size / 1e6:.1f} MB)") | |
| def export_depth(): | |
| from config.models import DEPTH_MODEL_NAME | |
| from transformers import AutoImageProcessor, AutoModelForDepthEstimation | |
| print(f"Loading Depth model: {DEPTH_MODEL_NAME} ...") | |
| processor = AutoImageProcessor.from_pretrained(DEPTH_MODEL_NAME) | |
| model = AutoModelForDepthEstimation.from_pretrained(DEPTH_MODEL_NAME) | |
| model.eval() | |
| # Create dummy input (1024x768 RGB) | |
| dummy_image = Image.fromarray(np.random.randint(0, 255, (768, 1024, 3), dtype=np.uint8)) | |
| inputs = processor(images=dummy_image, return_tensors="pt") | |
| output_path = ONNX_DIR / "depth_anything.onnx" | |
| ONNX_DIR.mkdir(parents=True, exist_ok=True) | |
| print(f"Exporting to {output_path} ...") | |
| input_names = list(inputs.keys()) | |
| torch.onnx.export( | |
| model, | |
| tuple(inputs[k] for k in input_names), | |
| str(output_path), | |
| input_names=input_names, | |
| output_names=["predicted_depth"], | |
| dynamic_axes={ | |
| name: {0: "batch", 2: "height", 3: "width"} if inputs[name].ndim == 4 | |
| else {0: "batch"} for name in input_names | |
| }, | |
| opset_version=17, | |
| do_constant_folding=True, | |
| ) | |
| print(f"Depth model exported: {output_path} ({output_path.stat().st_size / 1e6:.1f} MB)") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Export models to ONNX") | |
| parser.add_argument("--model", choices=["seg", "depth", "all"], required=True) | |
| args = parser.parse_args() | |
| if args.model in ("seg", "all"): | |
| export_segmentation() | |
| if args.model in ("depth", "all"): | |
| export_depth() | |
| print("\nDone! Enable ONNX inference by setting enable_onnx_inference = true in your TOML config.") | |