diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index 51d07554d..7b46b3429 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -30,4 +30,5 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: _build/ force_orphan: true - # cname: docs.nerf.studio + cname: docs.nerf.studio + if: github.event_name != 'pull_request' diff --git a/.github/workflows/viewer_build_deploy.yml b/.github/workflows/viewer_build_deploy.yml index ebae32df2..da65c20ea 100644 --- a/.github/workflows/viewer_build_deploy.yml +++ b/.github/workflows/viewer_build_deploy.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: ./nerfstudio/viewer/app + working-directory: ./nerfstudio/viewer_legacy/app steps: - name: Checkout code uses: actions/checkout@master @@ -22,7 +22,7 @@ jobs: with: node-version: 17.8.0 cache: 'yarn' - cache-dependency-path: ./nerfstudio/viewer/app/yarn.lock + cache-dependency-path: ./nerfstudio/viewer_legacy/app/yarn.lock - name: Install packages run: yarn install @@ -34,7 +34,7 @@ jobs: uses: actions/upload-artifact@v2 with: name: production-files - path: ./nerfstudio/viewer/app/build + path: ./nerfstudio/viewer_legacy/app/build deploy: name: Deploy @@ -53,13 +53,13 @@ jobs: - name: Install dependencies run: | - pip install -r ./nerfstudio/viewer/app/requirements.txt + pip install -r ./nerfstudio/viewer_legacy/app/requirements.txt - name: Download artifact uses: actions/download-artifact@v2 with: name: production-files - path: ./nerfstudio/viewer/app/build + path: ./nerfstudio/viewer_legacy/app/build - name: Get branch name (merge) if: github.event_name != 'pull_request' @@ -71,7 +71,7 @@ jobs: shell: bash run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV - # TODO: detect file or scheme changes of the viewer and only + # TODO: detect file or scheme changes of the viewer_legacy and only # increment the version.txt file when there is a change. # Update the version.txt code and push to master when things change. # https://github.com/marketplace/actions/changed-files @@ -79,7 +79,7 @@ jobs: # id: changed-files-for-dir1 # uses: tj-actions/changed-files@v29.0.3 # with: - # path: nerfstudio/viewer/app + # path: nerfstudio/viewer_legacy/app # - name: List all added files in dir1 # run: | @@ -88,10 +88,10 @@ jobs: # done - run: | - python ./nerfstudio/viewer/app/run_deploy.py \ + python ./nerfstudio/viewer_legacy/app/run_deploy.py \ --branch-name ${{ env.BRANCH_NAME }} \ --ssh-key-string "$SSH_KEY" \ - --local-folder ./nerfstudio/viewer/app/build \ - --package-json-filename ./nerfstudio/viewer/app/package.json \ + --local-folder ./nerfstudio/viewer_legacy/app/build \ + --package-json-filename ./nerfstudio/viewer_legacy/app/package.json \ --increment-version "False" - run: cat ~/.ssh/config diff --git a/.readthedocs.yaml b/.readthedocs.yaml deleted file mode 100644 index d66aefd16..000000000 --- a/.readthedocs.yaml +++ /dev/null @@ -1,37 +0,0 @@ -# .readthedocs.yaml -# Read the Docs configuration file -# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details - -# Required -version: 2 - -# Set the version of Python and other tools you might need -build: - os: ubuntu-20.04 - tools: - python: '3.9' - # You can also specify other tool versions: - # nodejs: "16" - # rust: "1.55" - # golang: "1.17" - -# Build documentation in the docs/ directory with Sphinx -sphinx: - fail_on_warning: true - configuration: docs/conf.py - -# If using Sphinx, optionally build your docs in additional formats such as PDF -# formats: -# - pdf - -# Optionally declare the Python requirements required to build your docs -python: - install: - # Equivalent to 'pip install .' - - method: pip - path: . - # Equivalent to 'pip install .[docs]' - - method: pip - path: . - extra_requirements: - - docs diff --git a/Dockerfile b/Dockerfile index d73c48734..8c9fe4f31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -135,9 +135,10 @@ RUN git clone --branch v0.4.0 --recursive https://github.com/colmap/pycolmap.git python3.10 -m pip install . && \ cd .. -# Install hloc master (last release (1.3) is too old) as alternative feature detector and matcher option for nerfstudio. +# Install hloc 1.4 as alternative feature detector and matcher option for nerfstudio. RUN git clone --branch master --recursive https://github.com/cvg/Hierarchical-Localization.git && \ cd Hierarchical-Localization && \ + git checkout v1.4 && \ python3.10 -m pip install -e . && \ cd .. @@ -148,8 +149,10 @@ RUN git clone --branch v1.0 --recursive https://github.com/cvg/pyceres.git && \ cd .. # Install pixel perfect sfm. -RUN git clone --branch v1.0 --recursive https://github.com/cvg/pixel-perfect-sfm.git && \ +RUN git clone --recursive https://github.com/cvg/pixel-perfect-sfm.git && \ cd pixel-perfect-sfm && \ + git reset --hard 40f7c1339328b2a0c7cf71f76623fb848e0c0357 && \ + git clean -df && \ python3.10 -m pip install -e . && \ cd .. diff --git a/LICENSE b/LICENSE index 261eeb9e9..ee561dfbe 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright 2023 The Nerfstudio Team Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index b6f05f9be..dc4a0093e 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,27 @@ - - Documentation Status - + + Documentation Status PyPI version - Test Status - + Test Status - Viewer build Status - + Viewer build Status - License - + License

- - + + - nerfstudio + nerfstudio @@ -34,21 +30,18 @@

A collaboration friendly studio for NeRFs

- documentation - + documentation - viewer - + viewer - colab - + colab

@@ -70,13 +63,14 @@ Nerfstudio initially launched as an opensource project by Berkeley students in [ We are committed to providing learning resources to help you understand the basics of (if you're just getting started), and keep up-to-date with (if you're a seasoned veteran) all things NeRF. As researchers, we know just how hard it is to get onboarded with this next-gen technology. So we're here to help with tutorials, documentation, and more! -Have feature requests? Want to add your brand-spankin'-new NeRF model? Have a new dataset? **We welcome [contributions](https://docs.nerf.studio/en/latest/reference/contributing.html)!** Please do not hesitate to reach out to the nerfstudio team with any questions via [Discord](https://discord.gg/uMbNqcraFc). +Have feature requests? Want to add your brand-spankin'-new NeRF model? Have a new dataset? **We welcome [contributions](https://docs.nerf.studio/reference/contributing.html)!** Please do not hesitate to reach out to the nerfstudio team with any questions via [Discord](https://discord.gg/uMbNqcraFc). Have feedback? We'd love for you to fill out our [Nerfstudio Feedback Form](https://forms.gle/sqN5phJN7LfQVwnP9) if you want to let us know who you are, why you are interested in Nerfstudio, or provide any feedback! We hope nerfstudio enables you to build faster :hammer: learn together :books: and contribute to our NeRF community :sparkling_heart:. ## Sponsors + Sponsors of this work includes [Luma AI](https://lumalabs.ai/) and the [BAIR commons](https://bcommons.berkeley.edu/home).

@@ -104,7 +98,6 @@ Sponsors of this work includes [Luma AI](https://lumalabs.ai/) and the [BAIR com

- # Quickstart The quickstart will help you get started with the default vanilla NeRF trained on the classic Blender Lego scene. @@ -118,7 +111,7 @@ You must have an NVIDIA video card with CUDA installed on the system. This libra ### Create environment -Nerfstudio requires `python >= 3.8`. We recommend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/en/latest/miniconda.html) before proceeding. +Nerfstudio requires `python >= 3.8`. We recommend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/miniconda.html) before proceeding. ```bash conda create --name nerfstudio -y python=3.8 @@ -240,17 +233,21 @@ ns-export pointcloud --help Using an existing dataset is great, but likely you want to use your own data! We support various methods for using your own data. Before it can be used in nerfstudio, the camera location and orientations must be determined and then converted into our format using `ns-process-data`. We rely on external tools for this, instructions and information can be found in the documentation. -| Data | Capture Device | Requirements | `ns-process-data` Speed | -| ---------------------------------------------------------------------------------------------------- | -------------- | ----------------------------------------------------------------- | ----------------------- | -| πŸ“· [Images](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#images-and-video) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | -| πŸ“Ή [Video](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#images-and-video) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | -| 🌎 [360 Data](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#360_data) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | -| πŸ“± [Polycam](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#polycam-capture) | IOS with LiDAR | [Polycam App](https://poly.cam/) | πŸ‡ | -| πŸ“± [KIRI Engine](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#kiri-capture) | IOS or Android | [KIRI Engine App](https://www.kiriengine.com/) | πŸ‡ | -| πŸ“± [Record3D](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#record3d-capture) | IOS with LiDAR | [Record3D app](https://record3d.app/) | πŸ‡ | -| πŸ–₯ [Metashape](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#metashape) | Any | [Metashape](https://www.agisoft.com/) | πŸ‡ | -| πŸ–₯ [RealityCapture](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html#realitycapture) | Any | [RealityCapture](https://www.capturingreality.com/realitycapture) | πŸ‡ | -| πŸ›  [Custom](https://docs.nerf.studio/en/latest/quickstart/data_conventions.html) | Any | Camera Poses | πŸ‡ | +| Data | Capture Device | Requirements | `ns-process-data` Speed | +| --------------------------------------------------------------------------------------------- | -------------- | ----------------------------------------------------------------- | ----------------------- | +| πŸ“· [Images](https://docs.nerf.studio/quickstart/custom_dataset.html#images-or-video) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | +| πŸ“Ή [Video](https://docs.nerf.studio/quickstart/custom_dataset.html#images-or-video) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | +| 🌎 [360 Data](https://docs.nerf.studio/quickstart/custom_dataset.html#data-equirectangular) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | +| πŸ“± [Polycam](https://docs.nerf.studio/quickstart/custom_dataset.html#polycam-capture) | IOS with LiDAR | [Polycam App](https://poly.cam/) | πŸ‡ | +| πŸ“± [KIRI Engine](https://docs.nerf.studio/quickstart/custom_dataset.html#kiri-engine-capture) | IOS or Android | [KIRI Engine App](https://www.kiriengine.com/) | πŸ‡ | +| πŸ“± [Record3D](https://docs.nerf.studio/quickstart/custom_dataset.html#record3d-capture) | IOS with LiDAR | [Record3D app](https://record3d.app/) | πŸ‡ | +| πŸ“± [Spectacular AI](https://docs.nerf.studio/quickstart/custom_dataset.html#spectacularai) | IOS, OAK, [others](https://www.spectacularai.com/mapping#supported-devices) | [App](https://apps.apple.com/us/app/spectacular-rec/id6473188128) / [`sai-cli`](https://www.spectacularai.com/mapping) | πŸ‡ | +| πŸ–₯ [Metashape](https://docs.nerf.studio/quickstart/custom_dataset.html#metashape) | Any | [Metashape](https://www.agisoft.com/) | πŸ‡ | +| πŸ–₯ [RealityCapture](https://docs.nerf.studio/quickstart/custom_dataset.html#realitycapture) | Any | [RealityCapture](https://www.capturingreality.com/realitycapture) | πŸ‡ | +| πŸ–₯ [ODM](https://docs.nerf.studio/quickstart/custom_dataset.html#odm) | Any | [ODM](https://github.com/OpenDroneMap/ODM) | πŸ‡ | +| πŸ‘“ [Aria](https://docs.nerf.studio/quickstart/custom_dataset.html#aria) | Aria glasses | [Project Aria](https://projectaria.com/) | πŸ‡ | +| πŸ›  [Custom](https://docs.nerf.studio/quickstart/data_conventions.html) | Any | Camera Poses | πŸ‡ | + ## 5. Advanced Options @@ -280,27 +277,27 @@ We support four different methods to track training progress, using the viewer[t And that's it for getting started with the basics of nerfstudio. -If you're interested in learning more on how to create your own pipelines, develop with the viewer, run benchmarks, and more, please check out some of the quicklinks below or visit our [documentation](https://docs.nerf.studio/en/latest/) directly. +If you're interested in learning more on how to create your own pipelines, develop with the viewer, run benchmarks, and more, please check out some of the quicklinks below or visit our [documentation](https://docs.nerf.studio/) directly. -| Section | Description | -| -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -| [Documentation](https://docs.nerf.studio/en/latest/) | Full API documentation and tutorials | -| [Viewer](https://viewer.nerf.studio/) | Home page for our web viewer | -| πŸŽ’ **Educational** | -| [Model Descriptions](https://docs.nerf.studio/en/latest/nerfology/methods/index.html) | Description of all the models supported by nerfstudio and explanations of component parts. | -| [Component Descriptions](https://docs.nerf.studio/en/latest/nerfology/model_components/index.html) | Interactive notebooks that explain notable/commonly used modules in various models. | -| πŸƒ **Tutorials** | -| [Getting Started](https://docs.nerf.studio/en/latest/quickstart/installation.html) | A more in-depth guide on how to get started with nerfstudio from installation to contributing. | -| [Using the Viewer](https://docs.nerf.studio/en/latest/quickstart/viewer_quickstart.html) | A quick demo video on how to navigate the viewer. | -| [Using Record3D](https://www.youtube.com/watch?v=XwKq7qDQCQk) | Demo video on how to run nerfstudio without using COLMAP. | -| πŸ’» **For Developers** | -| [Creating pipelines](https://docs.nerf.studio/en/latest/developer_guides/pipelines/index.html) | Learn how to easily build new neural rendering pipelines by using and/or implementing new modules. | -| [Creating datasets](https://docs.nerf.studio/en/latest/quickstart/custom_dataset.html) | Have a new dataset? Learn how to run it with nerfstudio. | -| [Contributing](https://docs.nerf.studio/en/latest/reference/contributing.html) | Walk-through for how you can start contributing now. | -| πŸ’– **Community** | -| [Discord](https://discord.gg/uMbNqcraFc) | Join our community to discuss more. We would love to hear from you! | -| [Twitter](https://twitter.com/nerfstudioteam) | Follow us on Twitter @nerfstudioteam to see cool updates and announcements | -| [Feedback Form](TODO) | We welcome any feedback! This is our chance to learn what you all are using Nerfstudio for. | +| Section | Description | +| ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| [Documentation](https://docs.nerf.studio/) | Full API documentation and tutorials | +| [Viewer](https://viewer.nerf.studio/) | Home page for our web viewer | +| πŸŽ’ **Educational** | +| [Model Descriptions](https://docs.nerf.studio/nerfology/methods/index.html) | Description of all the models supported by nerfstudio and explanations of component parts. | +| [Component Descriptions](https://docs.nerf.studio/nerfology/model_components/index.html) | Interactive notebooks that explain notable/commonly used modules in various models. | +| πŸƒ **Tutorials** | +| [Getting Started](https://docs.nerf.studio/quickstart/installation.html) | A more in-depth guide on how to get started with nerfstudio from installation to contributing. | +| [Using the Viewer](https://docs.nerf.studio/quickstart/viewer_quickstart.html) | A quick demo video on how to navigate the viewer. | +| [Using Record3D](https://www.youtube.com/watch?v=XwKq7qDQCQk) | Demo video on how to run nerfstudio without using COLMAP. | +| πŸ’» **For Developers** | +| [Creating pipelines](https://docs.nerf.studio/developer_guides/pipelines/index.html) | Learn how to easily build new neural rendering pipelines by using and/or implementing new modules. | +| [Creating datasets](https://docs.nerf.studio/quickstart/custom_dataset.html) | Have a new dataset? Learn how to run it with nerfstudio. | +| [Contributing](https://docs.nerf.studio/reference/contributing.html) | Walk-through for how you can start contributing now. | +| πŸ’– **Community** | +| [Discord](https://discord.gg/uMbNqcraFc) | Join our community to discuss more. We would love to hear from you! | +| [Twitter](https://twitter.com/nerfstudioteam) | Follow us on Twitter @nerfstudioteam to see cool updates and announcements | +| [Feedback Form](TODO) | We welcome any feedback! This is our chance to learn what you all are using Nerfstudio for. | # Supported Features diff --git a/colab/demo.ipynb b/colab/demo.ipynb index 2d88252ca..7f6364dbd 100644 --- a/colab/demo.ipynb +++ b/colab/demo.ipynb @@ -7,7 +7,7 @@ "id": "view-in-github" }, "source": [ - "\"Open" + "\"Open" ] }, { @@ -18,9 +18,9 @@ "source": [ "

\n", " \n", - " \n", - " \n", - " \"nerfstudio\"\n", + " \n", + " \n", + " \"nerfstudio\"\n", " \n", "

\n", "\n", @@ -276,4 +276,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file diff --git a/docs/developer_guides/config.md b/docs/developer_guides/config.md index bf216278f..acf6914d7 100644 --- a/docs/developer_guides/config.md +++ b/docs/developer_guides/config.md @@ -89,7 +89,7 @@ Often times, you just want to play with the parameters of an existing model with ns-train --help ``` -- List out all exist configurable parameters for `{METHOD_NAME}` +- List out all existing configurable parameters for `{METHOD_NAME}` ```bash ns-train {METHOD_NAME} --help diff --git a/docs/developer_guides/new_methods.md b/docs/developer_guides/new_methods.md index 4b56d8581..dd1ef31e7 100644 --- a/docs/developer_guides/new_methods.md +++ b/docs/developer_guides/new_methods.md @@ -132,7 +132,7 @@ finally run the following to register the dataparser. pip install -e . ``` -Similarly to the method develomement, you can also use environment variables to register dataparsers. +Similarly to the method development, you can also use environment variables to register dataparsers. Use the `NERFSTUDIO_DATAPARSER_CONFIGS` environment variable: ``` diff --git a/docs/developer_guides/pipelines/datamanagers.md b/docs/developer_guides/pipelines/datamanagers.md index 32c55b63e..84722ba78 100644 --- a/docs/developer_guides/pipelines/datamanagers.md +++ b/docs/developer_guides/pipelines/datamanagers.md @@ -62,13 +62,11 @@ class VanillaDataManagerConfig(InstantiateConfig): """number of rays per batch to use per eval iteration""" eval_num_images_to_sample_from: int = -1 """number of images to sample during eval iteration""" - eval_image_indices: Optional[Tuple[int, ...]] = (0,) - """specifies the image indices to use during eval; if None, uses all""" camera_optimizer: CameraOptimizerConfig = CameraOptimizerConfig() """specifies the camera pose optimizer used during training""" ``` -Let's take a quick look at how the `run_train` method is implemented. Here we sample images, then pixels, and then return the RayBundle and RayGT information. +Let's take a quick look at how the `next_train` method is implemented. Here we sample images, then pixels, and then return the RayBundle and RayGT information. ```python def next_train(self, step: int) -> Tuple[RayBundle, Dict]: diff --git a/docs/developer_guides/viewer/custom_gui.md b/docs/developer_guides/viewer/custom_gui.md index 2ae41fa6a..0fd600732 100644 --- a/docs/developer_guides/viewer/custom_gui.md +++ b/docs/developer_guides/viewer/custom_gui.md @@ -9,10 +9,10 @@ We provide support for custom viewer GUI elements that can be defined in any `nn ## Adding an Element -To define a custom element, create an instance of one of the provided classes in `nerfstudio.viewer.server.viewer_elements`, and assign it as a class variable in your `nn.Module`. +To define a custom element, create an instance of one of the provided classes in `nerfstudio.viewer.viewer_elements`, and assign it as a class variable in your `nn.Module`. ```python -from nerfstudio.viewer.server.viewer_elements import ViewerNumber +from nerfstudio.viewer.viewer_elements import ViewerNumber class MyClass(nn.Module):#must inherit from nn.Module def __init__(self): @@ -20,7 +20,7 @@ class MyClass(nn.Module):#must inherit from nn.Module self.custom_value = ViewerNumber(name="My Value", default_value=1.0) ``` **Element Hierarchy** -The viewer recursively searches all `nn.Module` children of the base `Pipeline` object, and arranges parameters into folders based on their variable names. +The viewer recursively searches all `nn.Module` children of the base `Pipeline` object, and arranges parameters into folders based on their variable names. For example, a `ViewerElement` defined in `pipeline.model.field` will be in the "Custom/model/field" folder in the GUI. **Reading the value** @@ -81,7 +81,7 @@ Updating module state while training can have unexpected side effects. It is up This was created with the following elements: ```python -from nerfstudio.viewer.server.viewer_elements import * +from nerfstudio.viewer.viewer_elements import * class MyModel(Model): def __init__(self): diff --git a/docs/developer_guides/viewer/index.md b/docs/developer_guides/viewer/index.md index 93204a203..9df5c60d5 100644 --- a/docs/developer_guides/viewer/index.md +++ b/docs/developer_guides/viewer/index.md @@ -16,7 +16,7 @@ local_viewer We thank the authors and contributors to the following repos, which we've started, used, and modified for our use-cases. -- [Viser](https://github.com/brentyi/viser/tree/main/viser) - made by [Brent Yi](https://github.com/brentyi) +- [Viser](https://github.com/brentyi/viser/) - made by [Brent Yi](https://github.com/brentyi) - [meshcat-python](https://github.com/rdeits/meshcat-python) - made by [Robin Deits](https://github.com/rdeits) - [meshcat](https://github.com/rdeits/meshcat) - made by [Robin Deits](https://github.com/rdeits) - [ThreeJS](https://threejs.org/) diff --git a/docs/developer_guides/viewer/local_viewer.md b/docs/developer_guides/viewer/local_viewer.md index d5912bd89..2931f8c0d 100644 --- a/docs/developer_guides/viewer/local_viewer.md +++ b/docs/developer_guides/viewer/local_viewer.md @@ -1,6 +1,6 @@ # Local Server -If you are unable to connect to `https://viewer.nerf.studio`, want to use Safari, or want develop the viewer codebase, you can launch your own local viewer. +If you are unable to connect to `https://viewer.nerf.studio`, want to use Safari, or want to develop the viewer codebase, you can launch your own local viewer. ## Installing Dependencies diff --git a/docs/developer_guides/viewer/viewer_control.md b/docs/developer_guides/viewer/viewer_control.md index e6a84d3d7..7490a3f67 100644 --- a/docs/developer_guides/viewer/viewer_control.md +++ b/docs/developer_guides/viewer/viewer_control.md @@ -14,7 +14,7 @@ Just like `ViewerElements`, you can create an instance inside any class which in and is contained within the `Pipeline` object (for example the `Model`) ```python -from nerfstudio.viewer.server.viewer_elements import ViewerControl +from nerfstudio.viewer.viewer_elements import ViewerControl class MyModel(nn.Module): # Must inherit from nn.Module def __init__(self): @@ -25,7 +25,7 @@ class MyModel(nn.Module): # Must inherit from nn.Module To get the current camera intrinsics and extrinsics, use the `get_camera` function. This returns a `nerfstudio.cameras.cameras.Cameras` object. This object can be used to generate `RayBundles`, retrieve intrinsics and extrinsics, and more. ```python -from nerfstudio.viewer.server.viewer_elements import ViewerControl, ViewerButton +from nerfstudio.viewer.viewer_elements import ViewerControl, ViewerButton class MyModel(nn.Module): # Must inherit from nn.Module def __init__(self): @@ -47,10 +47,10 @@ class MyModel(nn.Module): # Must inherit from nn.Module ``` ## Set Camera Properties -You can set the viewer camera position and FOV from python. +You can set the viewer camera position and FOV from python. To set position, you must define a new camera position as well as a 3D "look at" point which the camera aims towards. ```python -from nerfstudio.viewer.server.viewer_elements import ViewerControl,ViewerButton +from nerfstudio.viewer.viewer_elements import ViewerControl,ViewerButton class MyModel(nn.Module): # Must inherit from nn.Module def __init__(self): @@ -62,11 +62,11 @@ class MyModel(nn.Module): # Must inherit from nn.Module self.viewer_button = ViewerButton(name="Dummy Button",cb_hook=button_cb) ``` -## Double-click Callbacks -We forward *double* clicks inside the viewer to the ViewerControl object, which you can use to interact with the scene. To do this, register a callback using `register_click_cb()`. The click is defined to be a ray that starts at the camera origin and passes through the click point on the screen, in world coordinates. +## Scene Click Callbacks +We forward *single* clicks inside the viewer to the ViewerControl object, which you can use to interact with the scene. To do this, register a callback using `register_click_cb()`. The click is defined to be a ray that starts at the camera origin and passes through the click point on the screen, in world coordinates. ```python -from nerfstudio.viewer.server.viewer_elements import ViewerControl,ViewerClick +from nerfstudio.viewer.viewer_elements import ViewerControl,ViewerClick class MyModel(nn.Module): # must inherit from nn.Module def __init__(self): @@ -77,6 +77,15 @@ class MyModel(nn.Module): # must inherit from nn.Module self.viewer_control.register_click_cb(click_cb) ``` +You can also use `unregister_click_cb()` to remove callbacks that are no longer needed. A good example is a "Click on Scene" button, that when pressed, would register a callback that would wait for the next click, and then unregister itself. +```python + ... + def button_cb(button: ViewerButton): + def click_cb(click: ViewerClick): + print(f"Click at {click.origin} in direction {click.direction}") + self.viewer_control.unregister_click_cb(click_cb) + self.viewer_control.register_click_cb(click_cb) +``` + ### Thread safety Just like `ViewerElement` callbacks, click callbacks are asynchronous to training and can potentially interrupt a call to `get_outputs()`. - diff --git a/docs/index.md b/docs/index.md index 9e294c3ac..bc3a9ef1b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -135,7 +135,7 @@ This documentation is organized into 3 parts: ### Included Methods -- [**Nerfacto**](nerfology/methods/nerfacto.md): Recommended method, integrates mutiple methods into one. +- [**Nerfacto**](nerfology/methods/nerfacto.md): Recommended method, integrates multiple methods into one. - [Instant-NGP](nerfology/methods/instant_ngp.md): Instant Neural Graphics Primitives with a Multiresolution Hash Encoding - [NeRF](nerfology/methods/nerf.md): OG Neural Radiance Fields - [Mip-NeRF](nerfology/methods/mipnerf.md): A Multiscale Representation for Anti-Aliasing Neural Radiance Fields @@ -151,6 +151,7 @@ This documentation is organized into 3 parts: - [Nerfbusters](nerfology/methods/nerfbusters.md): Removing Ghostly Artifacts from Casually Captured NeRFs - [NeRFPlayer](nerfology/methods/nerfplayer.md): 4D Radiance Fields by Streaming Feature Channels - [Tetra-NeRF](nerfology/methods/tetranerf.md): Representing Neural Radiance Fields Using Tetrahedra +- [Instruct-GS2GS](nerfology/methods/igs2gs.md): Editing 3DGS Scenes with Instructions **Eager to contribute a method?** We'd love to see you use nerfstudio in implementing new (or even existing) methods! Please view our {ref}`guide` for more details about how to add to this list! @@ -160,7 +161,6 @@ This documentation is organized into 3 parts: | ---------------------------------------------------------- | ---------------------- | | [Github](https://github.com/nerfstudio-project/nerfstudio) | Official Github Repo | | [Discord](https://discord.gg/RyVk6w5WWP) | Join Discord Community | -| [Viewer](https://viewer.nerf.studio/) | Web-based Nerf Viewer | | [Feedback Form](https://forms.gle/sqN5phJN7LfQVwnP9) | Provide Nerfstudio Feedback | ## Sponsors diff --git a/docs/nerfology/methods/igs2gs.md b/docs/nerfology/methods/igs2gs.md new file mode 100644 index 000000000..0b60486fd --- /dev/null +++ b/docs/nerfology/methods/igs2gs.md @@ -0,0 +1,103 @@ +# Instruct-GS2GS + +

Editing Gaussian Splatting Scenes with Instructions

+ +```{button-link} https://instruct-gs2gs.github.io/ +:color: primary +:outline: +Paper Website +``` + +```{button-link} https://github.com/cvachha/instruct-gs2gs +:color: primary +:outline: +Code +``` + + + +**Instruct-GS2GS enables instruction-based editing of 3D Gaussian Splatting scenes via a 2D diffusion model** + +## Installation + +First install nerfstudio dependencies. Then run: + +```bash +pip install git+https://github.com/cvachha/instruct-gs2gs +cd instruct-gs2gs +pip install --upgrade pip setuptools +pip install -e . +``` + +## Running Instruct-GS2GS + +Details for running Instruct-GS2GS (built with Nerfstudio!) can be found [here](https://github.com/cvachha/instruct-gs2gs). Once installed, run: + +```bash +ns-train igs2gs --help +``` + +| Method | Description | Memory | +| ------------ | ---------------------------- | ------ | +| `igs2gs` | Full model, used in paper | ~15GB | + +Datasets need to be processed with COLMAP for Gaussian Splatting support. + +Once you have trained your GS scene for 20k iterations, the checkpoints will be saved to the `outputs` directory. Copy the path to the `nerfstudio_models` folder. (Note: We noticed that training for 20k iterations rather than 30k seemed to run more reliably) + +To start training for editing the GS, run the following command: + +```bash +ns-train igs2gs --data {PROCESSED_DATA_DIR} --load-dir {outputs/.../nerfstudio_models} --pipeline.prompt {"prompt"} --pipeline.guidance-scale 12.5 --pipeline.image-guidance-scale 1.5 +``` + +The `{PROCESSED_DATA_DIR}` must be the same path as used in training the original GS. Using the CLI commands, you can choose the prompt and the guidance scales used for InstructPix2Pix. + +## Method + +### Overview + +Instruct-GS2GS is a method for editing 3D Gaussian Splatting (3DGS) scenes with text instructions in a method based on [Instruct-NeRF2NeRF](https://instruct-nerf2nerf.github.io/). Given a 3DGS scene of a scene and the collection of images used to reconstruct it, this method uses an image-conditioned diffusion model ([InstructPix2Pix](https://www.timothybrooks.com/instruct-pix2pix)) to iteratively edit the input images while optimizing the underlying scene, resulting in an optimized 3D scene that respects the edit instruction. The paper demonstrates that our proposed method is able to edit large-scale, real-world scenes, and is able to accomplish realistic and targeted edits. + + +## Pipeline + + + +This section will walk through each component of the Instruct-GS2GS method. + +### How it Works + +Instruct-GS2GS gradually updates a reconstructed Gaussian Splatting scene by iteratively updating the dataset images while training the 3DGS: + +1. Images are rendered from the scene at all training viewpoints. +2. They get edited by InstructPix2Pix given a global text instruction. +3. The training dataset images are replaced with the edited images. +4. The 3DGS continues training as usual for 2.5k iterations. + +### Editing Images with InstructPix2Pix + +To update a dataset image from a given viewpoint, Instruct-GS2GS takes the original, unedited training image as image conditioning and uses the global text instruction as text conditioning. This process mixes the information of the diffusion model, which attempts to edit the image, the current 3D structure of the 3DGS, and view-consistent information from the unedited, ground-truth images. By combining this set of information, the edit is respected while maintaining 3D consistency. + +The code snippet for how an image is edited in the pipeline can be found [here](https://github.com/cvachha/instruct-gs2gs/blob/main/igs2gs/ip2p.py). + +### Iterative Dataset Update and Implementation + +The method takes in a dataset of camera poses and training images, a trained 3DGS scene, and a user-specified text-prompt instruction, e.g. β€œmake him a marble statue”. Instruct-GS2GS constructs the edited GS scene guided by the text-prompt by applying a 2D text and image conditioned diffusion model, in this case Instruct-Pix2Pix, to all training images over the course of training. It performs these edits using an iterative udpate scheme in which all training dataset images are updated using a diffusion model individually, for sequential iterations spanning the size of the training images, every 2.5k training iterations. This process allows the GS to have a holistic edit and maintain 3D consistency. + +The process is similar to Instruct-NeRF2NeRF where for a given training camera view, it sets the original training image as the conditioning image, the noisy image input as the GS rendered from the camera combined with some randomly selected noise, and receives an edited image respecting the text conditioning. With this method, it is able to propagate the edited changes to the GS scene. The method is able to maintain grounded edits by conditioning Instruct-Pix2Pix on the original unedited training image. + +This method uses Nerfstudio’s gsplat library for our underlying gaussian splatting model. We adapt similar parameters for the diffusion model from Instruct-NeRF2NeRF. Among these are the values that define the amount of noise (and therefore the amount signal retained from the original images). We vary the classifier-free guidance scales per edit and scene, using a range of values. We edit the entire dataset and then train the scene for 2.5k iterations. For GS training, we use L1 and LPIPS losses. We train our method for a maximum of 27.5k iterations (starting with a GS scene trained for 20k iterations). However, in practice we stop training once the edit has converged. In many cases, the optimal training length is a subjective decision β€” a user may prefer more subtle or more extreme edits that are best found at different stages of training. + + +## Results + +For results, view the [project page](https://instruct-gs2gs.github.io/)! + + \ No newline at end of file diff --git a/docs/nerfology/methods/index.md b/docs/nerfology/methods/index.md index f1d4bd5e8..0cc68432d 100644 --- a/docs/nerfology/methods/index.md +++ b/docs/nerfology/methods/index.md @@ -38,6 +38,7 @@ The following methods are supported in nerfstudio: Tetra-NeRF TensoRF Generfacto + Instruct-GS2GS ``` (own_method_docs)= @@ -50,7 +51,7 @@ We also welcome additions to the list of methods above. To do this, simply creat 1. Add a markdown file describing the model to the `docs/nerfology/methods` folder 2. Update the above list of implement methods in this file. -3. Add the method to the {ref}`this` list in `docs/index.md`. +3. Add the method to {ref}`this` list in `docs/index.md`. 4. Add a new `ExternalMethod` entry to the `nerfstudio/configs/external_methods.py` file. For the method description, please refer to the [Instruct-NeRF2NeRF](in2n) page as an example of the layout. Please try to include the following information: diff --git a/docs/nerfology/methods/instant_ngp.md b/docs/nerfology/methods/instant_ngp.md index c7589e295..5f5f7e07f 100644 --- a/docs/nerfology/methods/instant_ngp.md +++ b/docs/nerfology/methods/instant_ngp.md @@ -34,7 +34,7 @@ Instant-NGP breaks NeRF training into 3 pillars and proposes improvements to eac The core idea behind the improved sampling technique is that sampling over empty space should be skipped and sampling behind high density areas should also be skipped. This is achieved by maintaining a set of multiscale occupancy grids which coarsely mark empty and non-empty space. Occupancy is stored as a single bit, and a sample on a ray is skipped if its occupancy is too low. These occupancy grids are stored independently of the trainable encoding and are updated throughout training based on the updated density predictions. The authors find they can increase sampling speed by 10-100x compared to naive approaches. -Nerfstudio uses [NerfAcc](https://www.nerfacc.com/en/latest/index.html) as the sampling algorithm implementation. The details on NerfAcc's sampling and occupancy grid is discussed [here](https://www.nerfacc.com/en/stable/methodology/sampling.html#occupancy-grid-estimator). +Nerfstudio uses [NerfAcc](https://www.nerfacc.com/index.html) as the sampling algorithm implementation. The details on NerfAcc's sampling and occupancy grid is discussed [here](https://www.nerfacc.com/en/stable/methodology/sampling.html#occupancy-grid-estimator). Another major bottleneck for NeRF's training speed has been querying the neural network. The authors of this work implement the network such that it runs entirely on a single CUDA kernel. The network is also shrunk down to be just 4 layers with 64 neurons in each layer. They show that their fully-fused neural network is 5-10x faster than a Tensorflow implementation. diff --git a/docs/quickstart/custom_dataset.md b/docs/quickstart/custom_dataset.md index 723a97c91..6444ca445 100644 --- a/docs/quickstart/custom_dataset.md +++ b/docs/quickstart/custom_dataset.md @@ -10,7 +10,7 @@ ns-process-data {video,images,polycam,record3d} --data {DATA_PATH} --output-dir A full set of arguments can be found {doc}`here`. -We Currently support the following custom data types: +We currently support the following custom data types: | Data | Capture Device | Requirements | `ns-process-data` Speed | | ----------------------------- | -------------- | ----------------------------------------------- | ----------------------- | | πŸ“· [Images](images_and_video) | Any | [COLMAP](https://colmap.github.io/install.html) | 🐒 | @@ -19,8 +19,11 @@ We Currently support the following custom data types: | πŸ“± [Polycam](polycam) | IOS with LiDAR | [Polycam App](https://poly.cam/) | πŸ‡ | | πŸ“± [KIRI Engine](kiri) | IOS or Android | [KIRI Engine App](https://www.kiriengine.com/) | πŸ‡ | | πŸ“± [Record3D](record3d) | IOS with LiDAR | [Record3D app](https://record3d.app/) | πŸ‡ | +| πŸ“± [Spectacular AI](spectacularai) | IOS, OAK, others| [App](https://apps.apple.com/us/app/spectacular-rec/id6473188128) / [`sai-cli`](https://www.spectacularai.com/mapping) | πŸ‡ | | πŸ–₯ [Metashape](metashape) | Any | [Metashape](https://www.agisoft.com/) | πŸ‡ | | πŸ–₯ [RealityCapture](realitycapture) | Any | [RealityCapture](https://www.capturingreality.com/realitycapture) | πŸ‡ | +| πŸ–₯ [ODM](odm) | Any | [ODM](https://github.com/OpenDroneMap/ODM) | πŸ‡ | +| πŸ‘“ [Aria](aria) | Aria glasses | [Project Aria](https://projectaria.com/) | πŸ‡ | (images_and_video)= @@ -265,6 +268,44 @@ ns-process-data record3d --data {data directory} --output-dir {output directory} ns-train nerfacto --data {output directory} ``` +(spectacularai)= + +## Spectacular AI + +Spectacular AI SDK and apps can be used to capture data from various devices: + + * iPhones (with LiDAR) + * OAK-D cameras + * RealSense D455/D435i + * Azure Kinect DK + +The SDK also records IMU data, which is fused with camera and (if available) LiDAR/ToF data when computing the camera poses. This approach, VISLAM, is more robust than purely image based methods (e.g., COLMAP) and can work better and faster for difficult data (monotonic environments, fast motions, narrow FoV, etc.). + +Instructions: + +1. Installation. With the Nerfstudio Conda environment active, first install the Spectacular AI Python library + +```bash +pip install spectacularAI[full] +``` + +2. Install FFmpeg. Linux: `apt install ffmpeg` (or similar, if using another package manager). Windows: [see here](https://www.editframe.com/guides/how-to-install-and-start-using-ffmpeg-in-under-10-minutes). FFmpeg must be in your `PATH` so that `ffmpeg` works on the command line. + +3. Data capture. See [here for specific instructions for each supported device](https://github.com/SpectacularAI/sdk-examples/tree/main/python/mapping#recording-data). + +4. Process and export. Once you have recorded a dataset in Spectacular AI format and have it stored in `{data directory}` it can be converted into a Nerfstudio supported format with: + +```bash +sai-cli process {data directory} --preview3d --key_frame_distance=0.05 {output directory} +``` +The optional `--preview3d` flag shows a 3D preview of the point cloud and estimated trajectory live while VISLAM is running. The `--key_frame_distance` argument can be tuned based on the recorded scene size: 0.05 (5cm) is good for small scans and 0.15 for room-sized scans. If the processing gets slow, you can also try adding a --fast flag to `sai-cli process` to trade off quality for speed. + +5. Train. No separate `ns-process-data` step is needed. The data in `{output directory}` can now be trained with Nerfstudio: + +```bash +ns-train nerfacto --data {output directory} +``` + (metashape)= ## Metashape @@ -319,6 +360,59 @@ ns-process-data realitycapture --data {data directory} --csv {csv file} --output ns-train nerfacto --data {output directory} ``` +(odm)= + +## ODM + +All images/videos must be captured with the same camera. + +1. Process a dataset using [ODM](https://github.com/OpenDroneMap/ODM#quickstart) + +```bash +$ ls /path/to/dataset +images +odm_report +odm_orthophoto +... +``` + +2. Convert to nerfstudio format. + +```bash +ns-process-data odm --data /path/to/dataset --output-dir {output directory} +``` + +4. Train! + +```bash +ns-train nerfacto --data {output directory} +``` + +(aria)= + +## Aria + +1. Install projectaria_tools: + +```bash +conda activate nerfstudio +pip install projectaria-tools'[all]' +``` + +2. Download a VRS file from Project Aria glasses, and run Machine Perception Services to extract poses. + +3. Convert to nerfstudio format. + +```bash +ns-process-data aria --vrs-file /path/to/vrs/file --mps-data-dir /path/to/mps/data --output-dir {output directory} +``` + +4. Train! + +```bash +ns-train nerfacto --data {output directory} +``` + (360_data)= ## 360 Data (Equirectangular) @@ -377,7 +471,7 @@ This outputs two 180 deg equirectangular renders horizontally stacked, one for e ### Setup instructions -To render for VR video it is essential to adjust the NeRF to have an approximately true-to-life real world scale (adjustable in the camera path) to ensure that the scene depth and IPD (distance between the eyes) is appropriate for the render to be viewable in VR. You can adjust the scene scale with the [Nerfstudio Blender Add-on](https://docs.nerf.studio/en/latest/extensions/blender_addon.html) by appropriately scaling a point cloud representation of the NeRF. +To render for VR video it is essential to adjust the NeRF to have an approximately true-to-life real world scale (adjustable in the camera path) to ensure that the scene depth and IPD (distance between the eyes) is appropriate for the render to be viewable in VR. You can adjust the scene scale with the [Nerfstudio Blender Add-on](https://docs.nerf.studio/extensions/blender_addon.html) by appropriately scaling a point cloud representation of the NeRF. Results may be unviewable if the scale is not set appropriately. The IPD is set at 64mm by default but only is accurate when the NeRF scene is true to scale. For good quality renders, it is recommended to render at high resolutions (For ODS: 4096x2048 per eye, or 2048x1024 per eye. For VR180: 4096x4096 per eye or 2048x2048 per eye). Render resolutions for a single eye are specified in the camera path. For VR180, resolutions must be in a 1:1 aspect ratio. For ODS, resolutions must be in a 2:1 aspect ratio. The final stacked render output will automatically be constructed (with aspect ratios for VR180 as 2:1 and ODS as 1:1). @@ -388,7 +482,7 @@ If you are rendering an image sequence, it is recommended to render as png inste ::: To render with the VR videos camera: -1. Use the [Nerfstudio Blender Add-on](https://docs.nerf.studio/en/latest/extensions/blender_addon.html) to set the scale of the NeRF scene and create the camera path +1. Use the [Nerfstudio Blender Add-on](https://docs.nerf.studio/extensions/blender_addon.html) to set the scale of the NeRF scene and create the camera path - Export a point cloud representation of the NeRF - Import the point cloud representation in Blender and enable the Nerfstudio Blender Add-on - Create a reference object such as a cube which may be 1x1x1 meter. You could also create a cylinder and scale it to an appropriate height of a viewer. @@ -430,4 +524,4 @@ If the depth of the scene is unviewable and looks too close or expanded when vie - The IPD can be modified in the `cameras.py` script as the variable `vr_ipd` (default is 64 mm). - Compositing with Blender Objects and VR180 or ODS Renders - Configure the Blender camera as panoramic and equirectangular. For the VR180 Blender camera, set the panoramic longitude min and max to -90 and 90. - - Change the Stereoscopy mode to "Parallel" set the Interocular Distance to 0.064 m. \ No newline at end of file + - Change the Stereoscopy mode to "Parallel" set the Interocular Distance to 0.064 m. diff --git a/docs/quickstart/data_conventions.md b/docs/quickstart/data_conventions.md index 34324e30e..a929ff53f 100644 --- a/docs/quickstart/data_conventions.md +++ b/docs/quickstart/data_conventions.md @@ -31,10 +31,10 @@ If all of the images share the same camera intrinsics, the values can be placed "cy": 1000.0, // principal point y "w": 3008, // image width "h": 2000, // image height - "k1": 0.0312, // first radial distorial parameter, used by [OPENCV, OPENCV_FISHEYE] - "k2": 0.0051, // second radial distorial parameter, used by [OPENCV, OPENCV_FISHEYE] - "k3": 0.0006, // third radial distorial parameter, used by [OPENCV_FISHEYE] - "k4": 0.0001, // fourth radial distorial parameter, used by [OPENCV_FISHEYE] + "k1": 0.0312, // first radial distortion parameter, used by [OPENCV, OPENCV_FISHEYE] + "k2": 0.0051, // second radial distortion parameter, used by [OPENCV, OPENCV_FISHEYE] + "k3": 0.0006, // third radial distortion parameter, used by [OPENCV_FISHEYE] + "k4": 0.0001, // fourth radial distortion parameter, used by [OPENCV_FISHEYE] "p1": -6.47e-5, // first tangential distortion parameter, used by [OPENCV] "p2": -1.37e-7, // second tangential distortion parameter, used by [OPENCV] "frames": // ... per-frame intrinsics and extrinsics parameters @@ -82,7 +82,7 @@ For a transform matrix, the first 3 columns are the +X, +Y, and +Z defining the ### Depth images -To train with depth supervision, you can also provide a `depth_file_path` for each frame in your `transforms.json` and use one of the methods that support additional depth losses (e.g., depth-nerfacto). The depths are assumed to be 16-bit or 32-bit and to be in millimeters to remain consistent with [Polyform](https://github.com/PolyCam/polyform). You can adjust this scaling factor using the `depth_unit_scale_factor` parameter in `NerfstudioDataParserConfig`. Note that by default, we resize the depth images to match the shape of the RGB images. +To train with depth supervision, you can also provide a `depth_file_path` for each frame in your `transforms.json` and use one of the methods that support additional depth losses (e.g., depth-nerfacto). The depths are assumed to be 16-bit or 32-bit and to be in millimeters to remain consistent with [Polyform](https://github.com/PolyCam/polyform). Zero-value in the depth image is treated as unknown depth. You can adjust this scaling factor using the `depth_unit_scale_factor` parameter in `NerfstudioDataParserConfig`. Note that by default, we resize the depth images to match the shape of the RGB images. ```json { diff --git a/docs/quickstart/first_nerf.md b/docs/quickstart/first_nerf.md index 137bd0b23..67a84783f 100644 --- a/docs/quickstart/first_nerf.md +++ b/docs/quickstart/first_nerf.md @@ -98,7 +98,7 @@ Each script will have some other minor quirks (like the training script datapars ## Comet / Tensorboard / WandB / Viewer -We support four different methods to track training progress, using the viewer [tensorboard](https://www.tensorflow.org/tensorboard), [Weights and Biases](https://wandb.ai/site), and [Comet](https://comet.com/?utm_source=nerf&utm_medium=referral&utm_content=nerf_docs). You can specify which visualizer to use by appending `--vis {viewer, tensorboard, wandb, viewer+wandb, viewer+tensorboard, viewer+comet}` to the training command. Simultaneously utilizing the viewer alongside wandb or tensorboard may cause stuttering issues during evaluation steps. The viewer only works for methods that are fast (ie. nerfacto, instant-ngp), for slower methods like NeRF, use the other loggers. +We support four different methods to track training progress, using the viewer [tensorboard](https://www.tensorflow.org/tensorboard), [Weights and Biases](https://wandb.ai/site), and [Comet](https://comet.com/?utm_source=nerf&utm_medium=referral&utm_content=nerf_docs). You can specify which visualizer to use by appending `--vis {viewer, tensorboard, wandb, comet, viewer+wandb, viewer+tensorboard, viewer+comet}` to the training command. Simultaneously utilizing the viewer alongside wandb or tensorboard may cause stuttering issues during evaluation steps. The viewer only works for methods that are fast (ie. nerfacto, instant-ngp), for slower methods like NeRF, use the other loggers. ## Evaluating Runs diff --git a/docs/reference/api/viewer.rst b/docs/reference/api/viewer.rst index 4be105c86..06166246e 100644 --- a/docs/reference/api/viewer.rst +++ b/docs/reference/api/viewer.rst @@ -3,6 +3,6 @@ Viewer ============ -.. automodule:: nerfstudio.viewer.server.viewer_elements +.. automodule:: nerfstudio.viewer.viewer_elements :members: :show-inheritance: diff --git a/docs/reference/contributing.md b/docs/reference/contributing.md index 529d154eb..bf0af33db 100644 --- a/docs/reference/contributing.md +++ b/docs/reference/contributing.md @@ -94,7 +94,7 @@ python nerfstudio/scripts/docs/build_docs.py ### Auto build -As you change or add models/components, the auto-generated [Reference API](https://docs.nerf.studio/en/latest/reference/api/index.html) may change. +As you change or add models/components, the auto-generated [Reference API](https://docs.nerf.studio/reference/api/index.html) may change. If you want the code to build on save you can use [sphinx autobuild](https://github.com/executablebooks/sphinx-autobuild). :::{admonition} Tip diff --git a/nerfstudio/cameras/camera_optimizers.py b/nerfstudio/cameras/camera_optimizers.py index df8296ea1..4aa69a69b 100644 --- a/nerfstudio/cameras/camera_optimizers.py +++ b/nerfstudio/cameras/camera_optimizers.py @@ -23,19 +23,17 @@ from dataclasses import dataclass, field from typing import Literal, Optional, Type, Union import torch -import tyro from jaxtyping import Float, Int from torch import Tensor, nn from typing_extensions import assert_never from nerfstudio.cameras.lie_groups import exp_map_SE3, exp_map_SO3xR3 +from nerfstudio.cameras.rays import RayBundle from nerfstudio.configs.base_config import InstantiateConfig -from nerfstudio.engine.optimizers import AdamOptimizerConfig, OptimizerConfig -from nerfstudio.engine.schedulers import ( - ExponentialDecaySchedulerConfig, - SchedulerConfig, -) from nerfstudio.utils import poses as pose_utils +from nerfstudio.engine.optimizers import OptimizerConfig +from nerfstudio.engine.schedulers import SchedulerConfig +from nerfstudio.cameras.cameras import Cameras @dataclass @@ -47,21 +45,38 @@ class CameraOptimizerConfig(InstantiateConfig): mode: Literal["off", "SO3xR3", "SE3"] = "off" """Pose optimization strategy to use. If enabled, we recommend SO3xR3.""" - position_noise_std: float = 0.0 - """Noise to add to initial positions. Useful for debugging.""" + trans_l2_penalty: float = 1e-2 + """L2 penalty on translation parameters.""" - orientation_noise_std: float = 0.0 - """Noise to add to initial orientations. Useful for debugging.""" + rot_l2_penalty: float = 1e-3 + """L2 penalty on rotation parameters.""" - optimizer: OptimizerConfig = field(default_factory=lambda: AdamOptimizerConfig(lr=6e-4, eps=1e-15)) - """ADAM parameters for camera optimization.""" + optimizer: Optional[OptimizerConfig] = field(default=None) + """Deprecated, now specified inside the optimizers dict""" - scheduler: SchedulerConfig = field(default_factory=lambda: ExponentialDecaySchedulerConfig(max_steps=10000)) - """Learning rate scheduler for camera optimizer..""" + scheduler: Optional[SchedulerConfig] = field(default=None) + """Deprecated, now specified inside the optimizers dict""" - param_group: tyro.conf.Suppress[str] = "camera_opt" - """Name of the parameter group used for pose optimization. Can be any string that doesn't conflict with other - groups.""" + def __post_init__(self): + if self.optimizer is not None: + import warnings + from nerfstudio.utils.rich_utils import CONSOLE + + CONSOLE.print( + "\noptimizer is no longer specified in the CameraOptimizerConfig, it is now defined with the rest of the param groups inside the config file under the name 'camera_opt'\n", + style="bold yellow", + ) + warnings.warn("above message coming from", FutureWarning, stacklevel=3) + + if self.scheduler is not None: + import warnings + from nerfstudio.utils.rich_utils import CONSOLE + + CONSOLE.print( + "\nscheduler is no longer specified in the CameraOptimizerConfig, it is now defined with the rest of the param groups inside the config file under the name 'camera_opt'\n", + style="bold yellow", + ) + warnings.warn("above message coming from", FutureWarning, stacklevel=3) class CameraOptimizer(nn.Module): @@ -91,16 +106,6 @@ class CameraOptimizer(nn.Module): else: assert_never(self.config.mode) - # Initialize pose noise; useful for debugging. - if config.position_noise_std != 0.0 or config.orientation_noise_std != 0.0: - assert config.position_noise_std >= 0.0 and config.orientation_noise_std >= 0.0 - std_vector = torch.tensor( - [config.position_noise_std] * 3 + [config.orientation_noise_std] * 3, device=device - ) - self.pose_noise = exp_map_SE3(torch.normal(torch.zeros((num_cameras, 6), device=device), std_vector)) - else: - self.pose_noise = None - def forward( self, indices: Int[Tensor, "camera_indices"], @@ -125,13 +130,56 @@ class CameraOptimizer(nn.Module): assert_never(self.config.mode) # Detach non-trainable indices by setting to identity transform if self.non_trainable_camera_indices is not None: - outputs[0][self.non_trainable_camera_indices] = torch.eye(4, device=self.device)[:3, :4] + if self.non_trainable_camera_indices.device != self.pose_adjustment.device: + self.non_trainable_camera_indices = self.non_trainable_camera_indices.to(self.pose_adjustment.device) + outputs[0][self.non_trainable_camera_indices] = torch.eye(4, device=self.pose_adjustment.device)[:3, :4] - # Apply initial pose noise. - if self.pose_noise is not None: - outputs.append(self.pose_noise[indices, :, :]) # Return: identity if no transforms are needed, otherwise multiply transforms together. if len(outputs) == 0: # Note that using repeat() instead of tile() here would result in unnecessary copies. return torch.eye(4, device=self.device)[None, :3, :4].tile(indices.shape[0], 1, 1) return functools.reduce(pose_utils.multiply, outputs) + + def apply_to_raybundle(self, raybundle: RayBundle) -> None: + """Apply the pose correction to the raybundle""" + if self.config.mode != "off": + correction_matrices = self(raybundle.camera_indices.squeeze()) # type: ignore + raybundle.origins = raybundle.origins + correction_matrices[:, :3, 3] + raybundle.directions = torch.bmm(correction_matrices[:, :3, :3], raybundle.directions[..., None]).squeeze() + + def apply_to_camera(self, camera: Cameras) -> None: + """Apply the pose correction to the raybundle""" + if self.config.mode != "off": + assert camera.metadata is not None, "Must provide id of camera in its metadata" + assert "cam_idx" in camera.metadata, "Must provide id of camera in its metadata" + camera_idx = camera.metadata["cam_idx"] + adj = self([camera_idx]) # type: ignore + adj = torch.cat([adj, torch.Tensor([0, 0, 0, 1])[None, None].to(adj)], dim=1) + camera.camera_to_worlds = torch.bmm(camera.camera_to_worlds, adj) + + def get_loss_dict(self, loss_dict: dict) -> None: + """Add regularization""" + if self.config.mode != "off": + loss_dict["camera_opt_regularizer"] = ( + self.pose_adjustment[:, :3].norm(dim=-1).mean() * self.config.trans_l2_penalty + + self.pose_adjustment[:, 3:].norm(dim=-1).mean() * self.config.rot_l2_penalty + ) + + def get_correction_matrices(self): + """Get optimized pose correction matrices""" + return self(torch.arange(0, self.num_cameras).long()) + + def get_metrics_dict(self, metrics_dict: dict) -> None: + """Get camera optimizer metrics""" + if self.config.mode != "off": + metrics_dict["camera_opt_translation"] = self.pose_adjustment[:, :3].norm() + metrics_dict["camera_opt_rotation"] = self.pose_adjustment[:, 3:].norm() + + def get_param_groups(self, param_groups: dict) -> None: + """Get camera optimizer parameters""" + camera_opt_params = list(self.parameters()) + if self.config.mode != "off": + assert len(camera_opt_params) > 0 + param_groups["camera_opt"] = camera_opt_params + else: + assert len(camera_opt_params) == 0 diff --git a/nerfstudio/cameras/camera_paths.py b/nerfstudio/cameras/camera_paths.py index fef982ae1..f0c5f0a1d 100644 --- a/nerfstudio/cameras/camera_paths.py +++ b/nerfstudio/cameras/camera_paths.py @@ -24,7 +24,7 @@ import nerfstudio.utils.poses as pose_utils from nerfstudio.cameras import camera_utils from nerfstudio.cameras.camera_utils import get_interpolated_poses_many from nerfstudio.cameras.cameras import Cameras, CameraType -from nerfstudio.viewer.server.utils import three_js_perspective_camera_focal_length +from nerfstudio.viewer_legacy.server.utils import three_js_perspective_camera_focal_length def get_interpolated_camera_path(cameras: Cameras, steps: int, order_poses: bool) -> Cameras: diff --git a/nerfstudio/cameras/camera_utils.py b/nerfstudio/cameras/camera_utils.py index 8c527b9db..445656623 100644 --- a/nerfstudio/cameras/camera_utils.py +++ b/nerfstudio/cameras/camera_utils.py @@ -25,8 +25,6 @@ from jaxtyping import Float from numpy.typing import NDArray from torch import Tensor -from nerfstudio.utils.misc import torch_compile - _EPS = np.finfo(float).eps * 4.0 @@ -407,7 +405,7 @@ def _compute_residual_and_jacobian( return fx, fy, fx_x, fx_y, fy_x, fy_y -@torch_compile(dynamic=True, mode="reduce-overhead", backend="eager") +# @torch_compile(dynamic=True, mode="reduce-overhead", backend="eager") def radial_and_tangential_undistort( coords: torch.Tensor, distortion_params: torch.Tensor, @@ -623,3 +621,224 @@ def auto_orient_and_center_poses( raise ValueError(f"Unknown value for method: {method}") return oriented_poses, transform + + +@torch.jit.script +def fisheye624_project(xyz, params): + """ + Batched implementation of the FisheyeRadTanThinPrism (aka Fisheye624) camera + model project() function. + Inputs: + xyz: BxNx3 tensor of 3D points to be projected + params: Bx16 tensor of Fisheye624 parameters formatted like this: + [f_u f_v c_u c_v {k_0 ... k_5} {p_0 p_1} {s_0 s_1 s_2 s_3}] + or Bx15 tensor of Fisheye624 parameters formatted like this: + [f c_u c_v {k_0 ... k_5} {p_0 p_1} {s_0 s_1 s_2 s_3}] + Outputs: + uv: BxNx2 tensor of 2D projections of xyz in image plane + Model for fisheye cameras with radial, tangential, and thin-prism distortion. + This model allows fu != fv. + Specifically, the model is: + uvDistorted = [x_r] + tangentialDistortion + thinPrismDistortion + [y_r] + proj = diag(fu,fv) * uvDistorted + [cu;cv]; + where: + a = x/z, b = y/z, r = (a^2+b^2)^(1/2) + th = atan(r) + cosPhi = a/r, sinPhi = b/r + [x_r] = (th+ k0 * th^3 + k1* th^5 + ...) [cosPhi] + [y_r] [sinPhi] + the number of terms in the series is determined by the template parameter numK. + tangentialDistortion = [(2 x_r^2 + rd^2)*p_0 + 2*x_r*y_r*p_1] + [(2 y_r^2 + rd^2)*p_1 + 2*x_r*y_r*p_0] + where rd^2 = x_r^2 + y_r^2 + thinPrismDistortion = [s0 * rd^2 + s1 rd^4] + [s2 * rd^2 + s3 rd^4] + Author: Daniel DeTone (ddetone@meta.com) + """ + + assert xyz.ndim == 3 + assert params.ndim == 2 + assert params.shape[-1] == 16 or params.shape[-1] == 15, "This model allows fx != fy" + eps = 1e-9 + B, N = xyz.shape[0], xyz.shape[1] + + # Radial correction. + z = xyz[:, :, 2].reshape(B, N, 1) + z = torch.where(torch.abs(z) < eps, eps * torch.sign(z), z) + ab = xyz[:, :, :2] / z + r = torch.norm(ab, dim=-1, p=2, keepdim=True) + th = torch.atan(r) + th_divr = torch.where(r < eps, torch.ones_like(ab), ab / r) + th_k = th.reshape(B, N, 1).clone() + for i in range(6): + th_k = th_k + params[:, -12 + i].reshape(B, 1, 1) * torch.pow(th, 3 + i * 2) + xr_yr = th_k * th_divr + uv_dist = xr_yr + + # Tangential correction. + p0 = params[:, -6].reshape(B, 1) + p1 = params[:, -5].reshape(B, 1) + xr = xr_yr[:, :, 0].reshape(B, N) + yr = xr_yr[:, :, 1].reshape(B, N) + xr_yr_sq = torch.square(xr_yr) + xr_sq = xr_yr_sq[:, :, 0].reshape(B, N) + yr_sq = xr_yr_sq[:, :, 1].reshape(B, N) + rd_sq = xr_sq + yr_sq + uv_dist_tu = uv_dist[:, :, 0] + ((2.0 * xr_sq + rd_sq) * p0 + 2.0 * xr * yr * p1) + uv_dist_tv = uv_dist[:, :, 1] + ((2.0 * yr_sq + rd_sq) * p1 + 2.0 * xr * yr * p0) + uv_dist = torch.stack([uv_dist_tu, uv_dist_tv], dim=-1) # Avoids in-place complaint. + + # Thin Prism correction. + s0 = params[:, -4].reshape(B, 1) + s1 = params[:, -3].reshape(B, 1) + s2 = params[:, -2].reshape(B, 1) + s3 = params[:, -1].reshape(B, 1) + rd_4 = torch.square(rd_sq) + uv_dist[:, :, 0] = uv_dist[:, :, 0] + (s0 * rd_sq + s1 * rd_4) + uv_dist[:, :, 1] = uv_dist[:, :, 1] + (s2 * rd_sq + s3 * rd_4) + + # Finally, apply standard terms: focal length and camera centers. + if params.shape[-1] == 15: + fx_fy = params[:, 0].reshape(B, 1, 1) + cx_cy = params[:, 1:3].reshape(B, 1, 2) + else: + fx_fy = params[:, 0:2].reshape(B, 1, 2) + cx_cy = params[:, 2:4].reshape(B, 1, 2) + result = uv_dist * fx_fy + cx_cy + + return result + + +# Core implementation of fisheye 624 unprojection. More details are documented here: +# https://facebookresearch.github.io/projectaria_tools/docs/tech_insights/camera_intrinsic_models#the-fisheye62-model +@torch.jit.script +def fisheye624_unproject_helper(uv, params, max_iters: int = 5): + """ + Batched implementation of the FisheyeRadTanThinPrism (aka Fisheye624) camera + model. There is no analytical solution for the inverse of the project() + function so this solves an optimization problem using Newton's method to get + the inverse. + Inputs: + uv: BxNx3 tensor of 2D pixels to be projected + params: Bx16 tensor of Fisheye624 parameters formatted like this: + [f_u f_v c_u c_v {k_0 ... k_5} {p_0 p_1} {s_0 s_1 s_2 s_3}] + or Bx15 tensor of Fisheye624 parameters formatted like this: + [f c_u c_v {k_0 ... k_5} {p_0 p_1} {s_0 s_1 s_2 s_3}] + Outputs: + xyz: BxNx3 tensor of 3D rays of uv points with z = 1. + Model for fisheye cameras with radial, tangential, and thin-prism distortion. + This model assumes fu=fv. This unproject function holds that: + X = unproject(project(X)) [for X=(x,y,z) in R^3, z>0] + and + x = project(unproject(s*x)) [for s!=0 and x=(u,v) in R^2] + Author: Daniel DeTone (ddetone@meta.com) + """ + + assert uv.ndim == 3, "Expected batched input shaped BxNx3" + assert params.ndim == 2 + assert params.shape[-1] == 16 or params.shape[-1] == 15, "This model allows fx != fy" + eps = 1e-6 + B, N = uv.shape[0], uv.shape[1] + + if params.shape[-1] == 15: + fx_fy = params[:, 0].reshape(B, 1, 1) + cx_cy = params[:, 1:3].reshape(B, 1, 2) + else: + fx_fy = params[:, 0:2].reshape(B, 1, 2) + cx_cy = params[:, 2:4].reshape(B, 1, 2) + + uv_dist = (uv - cx_cy) / fx_fy + + # Compute xr_yr using Newton's method. + xr_yr = uv_dist.clone() # Initial guess. + for _ in range(max_iters): + uv_dist_est = xr_yr.clone() + # Tangential terms. + p0 = params[:, -6].reshape(B, 1) + p1 = params[:, -5].reshape(B, 1) + xr = xr_yr[:, :, 0].reshape(B, N) + yr = xr_yr[:, :, 1].reshape(B, N) + xr_yr_sq = torch.square(xr_yr) + xr_sq = xr_yr_sq[:, :, 0].reshape(B, N) + yr_sq = xr_yr_sq[:, :, 1].reshape(B, N) + rd_sq = xr_sq + yr_sq + uv_dist_est[:, :, 0] = uv_dist_est[:, :, 0] + ((2.0 * xr_sq + rd_sq) * p0 + 2.0 * xr * yr * p1) + uv_dist_est[:, :, 1] = uv_dist_est[:, :, 1] + ((2.0 * yr_sq + rd_sq) * p1 + 2.0 * xr * yr * p0) + # Thin Prism terms. + s0 = params[:, -4].reshape(B, 1) + s1 = params[:, -3].reshape(B, 1) + s2 = params[:, -2].reshape(B, 1) + s3 = params[:, -1].reshape(B, 1) + rd_4 = torch.square(rd_sq) + uv_dist_est[:, :, 0] = uv_dist_est[:, :, 0] + (s0 * rd_sq + s1 * rd_4) + uv_dist_est[:, :, 1] = uv_dist_est[:, :, 1] + (s2 * rd_sq + s3 * rd_4) + # Compute the derivative of uv_dist w.r.t. xr_yr. + duv_dist_dxr_yr = uv.new_ones(B, N, 2, 2) + duv_dist_dxr_yr[:, :, 0, 0] = 1.0 + 6.0 * xr_yr[:, :, 0] * p0 + 2.0 * xr_yr[:, :, 1] * p1 + offdiag = 2.0 * (xr_yr[:, :, 0] * p1 + xr_yr[:, :, 1] * p0) + duv_dist_dxr_yr[:, :, 0, 1] = offdiag + duv_dist_dxr_yr[:, :, 1, 0] = offdiag + duv_dist_dxr_yr[:, :, 1, 1] = 1.0 + 6.0 * xr_yr[:, :, 1] * p1 + 2.0 * xr_yr[:, :, 0] * p0 + xr_yr_sq_norm = xr_yr_sq[:, :, 0] + xr_yr_sq[:, :, 1] + temp1 = 2.0 * (s0 + 2.0 * s1 * xr_yr_sq_norm) + duv_dist_dxr_yr[:, :, 0, 0] = duv_dist_dxr_yr[:, :, 0, 0] + (xr_yr[:, :, 0] * temp1) + duv_dist_dxr_yr[:, :, 0, 1] = duv_dist_dxr_yr[:, :, 0, 1] + (xr_yr[:, :, 1] * temp1) + temp2 = 2.0 * (s2 + 2.0 * s3 * xr_yr_sq_norm) + duv_dist_dxr_yr[:, :, 1, 0] = duv_dist_dxr_yr[:, :, 1, 0] + (xr_yr[:, :, 0] * temp2) + duv_dist_dxr_yr[:, :, 1, 1] = duv_dist_dxr_yr[:, :, 1, 1] + (xr_yr[:, :, 1] * temp2) + # Compute 2x2 inverse manually here since torch.inverse() is very slow. + # Because this is slow: inv = duv_dist_dxr_yr.inverse() + # About a 10x reduction in speed with above line. + mat = duv_dist_dxr_yr.reshape(-1, 2, 2) + a = mat[:, 0, 0].reshape(-1, 1, 1) + b = mat[:, 0, 1].reshape(-1, 1, 1) + c = mat[:, 1, 0].reshape(-1, 1, 1) + d = mat[:, 1, 1].reshape(-1, 1, 1) + det = 1.0 / ((a * d) - (b * c)) + top = torch.cat([d, -b], dim=2) + bot = torch.cat([-c, a], dim=2) + inv = det * torch.cat([top, bot], dim=1) + inv = inv.reshape(B, N, 2, 2) + # Manually compute 2x2 @ 2x1 matrix multiply. + # Because this is slow: step = (inv @ (uv_dist - uv_dist_est)[..., None])[..., 0] + diff = uv_dist - uv_dist_est + a = inv[:, :, 0, 0] + b = inv[:, :, 0, 1] + c = inv[:, :, 1, 0] + d = inv[:, :, 1, 1] + e = diff[:, :, 0] + f = diff[:, :, 1] + step = torch.stack([a * e + b * f, c * e + d * f], dim=-1) + # Newton step. + xr_yr = xr_yr + step + + # Compute theta using Newton's method. + xr_yr_norm = xr_yr.norm(p=2, dim=2).reshape(B, N, 1) + th = xr_yr_norm.clone() + for _ in range(max_iters): + th_radial = uv.new_ones(B, N, 1) + dthd_th = uv.new_ones(B, N, 1) + for k in range(6): + r_k = params[:, -12 + k].reshape(B, 1, 1) + th_radial = th_radial + (r_k * torch.pow(th, 2 + k * 2)) + dthd_th = dthd_th + ((3.0 + 2.0 * k) * r_k * torch.pow(th, 2 + k * 2)) + th_radial = th_radial * th + step = (xr_yr_norm - th_radial) / dthd_th + # handle dthd_th close to 0. + step = torch.where(dthd_th.abs() > eps, step, torch.sign(step) * eps * 10.0) + th = th + step + # Compute the ray direction using theta and xr_yr. + close_to_zero = torch.logical_and(th.abs() < eps, xr_yr_norm.abs() < eps) + ray_dir = torch.where(close_to_zero, xr_yr, torch.tan(th) / xr_yr_norm * xr_yr) + ray = torch.cat([ray_dir, uv.new_ones(B, N, 1)], dim=2) + return ray + + +# unproject 2D point to 3D with fisheye624 model +def fisheye624_unproject(coords: torch.Tensor, distortion_params: torch.Tensor) -> torch.Tensor: + dirs = fisheye624_unproject_helper(coords.unsqueeze(0), distortion_params[0].unsqueeze(0)) + # correct for camera space differences: + dirs[..., 1] = -dirs[..., 1] + dirs[..., 2] = -dirs[..., 2] + return dirs diff --git a/nerfstudio/cameras/cameras.py b/nerfstudio/cameras/cameras.py index 68f7b186f..3086298e2 100644 --- a/nerfstudio/cameras/cameras.py +++ b/nerfstudio/cameras/cameras.py @@ -32,7 +32,7 @@ import nerfstudio.utils.math import nerfstudio.utils.poses as pose_utils from nerfstudio.cameras import camera_utils from nerfstudio.cameras.rays import RayBundle -from nerfstudio.data.scene_box import SceneBox, OrientedBox +from nerfstudio.data.scene_box import OrientedBox, SceneBox from nerfstudio.utils.tensor_dataclass import TensorDataclass TORCH_DEVICE = Union[torch.device, str] @@ -48,6 +48,8 @@ class CameraType(Enum): OMNIDIRECTIONALSTEREO_R = auto() VR180_L = auto() VR180_R = auto() + ORTHOPHOTO = auto() + FISHEYE624 = auto() CAMERA_MODEL_TO_TYPE = { @@ -62,6 +64,8 @@ CAMERA_MODEL_TO_TYPE = { "OMNIDIRECTIONALSTEREO_R": CameraType.OMNIDIRECTIONALSTEREO_R, "VR180_L": CameraType.VR180_L, "VR180_R": CameraType.VR180_R, + "ORTHOPHOTO": CameraType.ORTHOPHOTO, + "FISHEYE624": CameraType.FISHEYE624, } @@ -79,7 +83,7 @@ class Cameras(TensorDataclass): cy: Principal point y width: Image width height: Image height - distortion_params: OpenCV 6 radial distortion coefficients + distortion_params: distortion coefficients (OpenCV 6 radial or 6-2-4 radial, tangential, thin-prism for Fisheye624) camera_type: Type of camera model. This will be an int corresponding to the CameraType enum. times: Timestamps for each camera metadata: Additional metadata or data needed for interpolation, will mimic shape of the cameras @@ -615,9 +619,9 @@ class Cameras(TensorDataclass): # Get our image coordinates and image coordinates offset by 1 (offsets used for dx, dy calculations) # Also make sure the shapes are correct - coord = torch.stack([(x - cx) / fx, -(y - cy) / fy], -1) # (num_rays, 2) - coord_x_offset = torch.stack([(x - cx + 1) / fx, -(y - cy) / fy], -1) # (num_rays, 2) - coord_y_offset = torch.stack([(x - cx) / fx, -(y - cy + 1) / fy], -1) # (num_rays, 2) + coord = torch.stack([(x - cx) / fx, (y - cy) / fy], -1) # (num_rays, 2) + coord_x_offset = torch.stack([(x - cx + 1) / fx, (y - cy) / fy], -1) # (num_rays, 2) + coord_y_offset = torch.stack([(x - cx) / fx, (y - cy + 1) / fy], -1) # (num_rays, 2) assert ( coord.shape == num_rays_shape + (2,) and coord_x_offset.shape == num_rays_shape + (2,) @@ -629,8 +633,8 @@ class Cameras(TensorDataclass): assert coord_stack.shape == (3,) + num_rays_shape + (2,) # Undistorts our images according to our distortion parameters + distortion_params = None if not disable_distortion: - distortion_params = None if self.distortion_params is not None: distortion_params = self.distortion_params[true_indices] if distortion_params_delta is not None: @@ -648,6 +652,9 @@ class Cameras(TensorDataclass): distortion_params[mask, :], ).reshape(-1, 2) + # Switch from OpenCV to OpenGL + coord_stack[..., 1] *= -1 + # Make sure after we have undistorted our images, the shapes are still correct assert coord_stack.shape == (3,) + num_rays_shape + (2,) @@ -829,6 +836,49 @@ class Cameras(TensorDataclass): # assign final camera origins c2w[..., :3, 3] = vr180_origins + elif CameraType.ORTHOPHOTO.value in cam_types: + # here the focal length determine the imaging area, the smaller fx, the bigger imaging area. + mask = (self.camera_type[true_indices] == CameraType.ORTHOPHOTO.value).squeeze(-1) + dir_mask = torch.stack([mask, mask, mask], dim=0) + # in orthophoto cam, all rays have same direction, dir = R @ [0, 0, 1], R will be applied following. + directions_stack[dir_mask] = torch.tensor( + [0.0, 0.0, -1.0], dtype=directions_stack.dtype, device=directions_stack.device + ) + # in orthophoto cam, ray origins are grids, then transform grids with c2w, c2w @ P. + grids = coord[mask] + grids[..., 1] *= -1.0 # convert to left-hand system. + grids = torch.cat([grids, torch.zeros_like(grids[..., -1:]), torch.ones_like(grids[..., -1:])], dim=-1) + grids = torch.matmul(c2w[mask], grids[..., None]).squeeze(-1) + c2w[..., :3, 3][mask] = grids + + elif CameraType.FISHEYE624.value in cam_types: + mask = (self.camera_type[true_indices] == CameraType.FISHEYE624.value).squeeze(-1) # (num_rays) + coord_mask = torch.stack([mask, mask, mask], dim=0) + + # fisheye624 requires pixel coordinates to unproject, so we need to recomput the offsets in pixel coords. + pcoord = torch.stack([x, y], -1) # (num_rays, 2) + pcoord_x_offset = torch.stack([x + 1, y], -1) # (num_rays, 2) + pcoord_y_offset = torch.stack([x, y + 1], -1) # (num_rays, 2) + + # Stack image coordinates and image coordinates offset by 1, check shapes too + pcoord_stack = torch.stack([pcoord, pcoord_x_offset, pcoord_y_offset], dim=0) # (3, num_rays, 2) + + assert distortion_params is not None + masked_coords = pcoord_stack[coord_mask, :] + # The fisheye unprojection does not rely on planar/pinhold unprojection, thus the method needs + # to access the focal length and principle points directly. + camera_params = torch.cat( + [ + fx[mask].unsqueeze(1), + fy[mask].unsqueeze(1), + cx[mask].unsqueeze(1), + cy[mask].unsqueeze(1), + distortion_params[mask, :], + ], + dim=1, + ) + directions_stack[coord_mask] = camera_utils.fisheye624_unproject(masked_coords, camera_params) + else: raise ValueError(f"Camera type {cam} not supported.") @@ -869,8 +919,6 @@ class Cameras(TensorDataclass): else: metadata = {"directions_norm": directions_norm[0].detach()} - times = self.times[camera_indices, 0] if self.times is not None else None - return RayBundle( origins=origins, directions=directions, diff --git a/nerfstudio/configs/base_config.py b/nerfstudio/configs/base_config.py index fc906fae9..85c205c5c 100644 --- a/nerfstudio/configs/base_config.py +++ b/nerfstudio/configs/base_config.py @@ -17,7 +17,7 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field from pathlib import Path from typing import Any, List, Literal, Optional, Tuple, Type @@ -113,7 +113,7 @@ class LoggingConfig(PrintableConfig): max_buffer_size: int = 20 """maximum history size to keep for computing running averages of stats. e.g. if 20, averages will be computed over past 20 occurrences.""" - local_writer: LocalWriterConfig = LocalWriterConfig(enable=True) + local_writer: LocalWriterConfig = field(default_factory=lambda: LocalWriterConfig(enable=True)) """if provided, will print stats locally. if None, will disable printing""" profiler: Literal["none", "basic", "pytorch"] = "basic" """how to profile the code; @@ -144,7 +144,11 @@ class ViewerConfig(PrintableConfig): """Whether to kill the training job when it has completed. Note this will stop rendering in the viewer.""" image_format: Literal["jpeg", "png"] = "jpeg" """Image format viewer should use; jpeg is lossy compression, while png is lossless.""" - jpeg_quality: int = 90 + jpeg_quality: int = 75 """Quality tradeoff to use for jpeg compression.""" make_share_url: bool = False """Viewer beta feature: print a shareable URL. `vis` must be set to viewer_beta; this flag is otherwise ignored.""" + camera_frustum_scale: float = 0.1 + """Scale for the camera frustums in the viewer.""" + default_composite_depth: bool = True + """The default value for compositing depth. Turn off if you want to see the camera frustums without occlusions.""" diff --git a/nerfstudio/configs/dataparser_configs.py b/nerfstudio/configs/dataparser_configs.py index f60092a46..e3fa66ec5 100644 --- a/nerfstudio/configs/dataparser_configs.py +++ b/nerfstudio/configs/dataparser_configs.py @@ -35,6 +35,7 @@ from nerfstudio.data.dataparsers.phototourism_dataparser import PhototourismData from nerfstudio.data.dataparsers.scannet_dataparser import ScanNetDataParserConfig from nerfstudio.data.dataparsers.sdfstudio_dataparser import SDFStudioDataParserConfig from nerfstudio.data.dataparsers.sitcoms3d_dataparser import Sitcoms3DDataParserConfig +from nerfstudio.data.dataparsers.scannetpp_dataparser import ScanNetppDataParserConfig from nerfstudio.plugins.registry_dataparser import discover_dataparsers dataparsers = { @@ -51,6 +52,7 @@ dataparsers = { "sdfstudio-data": SDFStudioDataParserConfig(), "nerfosr-data": NeRFOSRDataParserConfig(), "sitcoms3d-data": Sitcoms3DDataParserConfig(), + "scannetpp-data": ScanNetppDataParserConfig(), "colmap": ColmapDataParserConfig(), } diff --git a/nerfstudio/configs/experiment_config.py b/nerfstudio/configs/experiment_config.py index 42cc4399e..cd431dca2 100644 --- a/nerfstudio/configs/experiment_config.py +++ b/nerfstudio/configs/experiment_config.py @@ -16,7 +16,7 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field from datetime import datetime from pathlib import Path from typing import Any, Dict, Literal, Optional @@ -51,13 +51,13 @@ class ExperimentConfig(InstantiateConfig): """Project name.""" timestamp: str = "{timestamp}" """Experiment timestamp.""" - machine: MachineConfig = MachineConfig() + machine: MachineConfig = field(default_factory=lambda: MachineConfig()) """Machine configuration""" - logging: LoggingConfig = LoggingConfig() + logging: LoggingConfig = field(default_factory=lambda: LoggingConfig()) """Logging configuration""" - viewer: ViewerConfig = ViewerConfig() + viewer: ViewerConfig = field(default_factory=lambda: ViewerConfig()) """Viewer configuration""" - pipeline: VanillaPipelineConfig = VanillaPipelineConfig() + pipeline: VanillaPipelineConfig = field(default_factory=lambda: VanillaPipelineConfig()) """Pipeline configuration""" optimizers: Dict[str, Any] = to_immutable_dict( { @@ -69,7 +69,7 @@ class ExperimentConfig(InstantiateConfig): ) """Dictionary of optimizer groups and their schedulers""" vis: Literal[ - "viewer", "wandb", "tensorboard", "comet", "viewer+wandb", "viewer+tensorboard", "viewer+comet", "viewer_beta" + "viewer", "wandb", "tensorboard", "comet", "viewer+wandb", "viewer+tensorboard", "viewer+comet", "viewer_legacy" ] = "wandb" """Which visualizer to use.""" data: Optional[Path] = None @@ -81,13 +81,13 @@ class ExperimentConfig(InstantiateConfig): load_scheduler: bool = True """Whether to load the scheduler state_dict to resume training, if it exists.""" - def is_viewer_enabled(self) -> bool: - """Checks if a viewer is enabled.""" - return ("viewer" == self.vis) | ("viewer+wandb" == self.vis) | ("viewer+tensorboard" == self.vis) + def is_viewer_legacy_enabled(self) -> bool: + """Checks if the legacy viewer is enabled.""" + return "viewer_legacy" == self.vis - def is_viewer_beta_enabled(self) -> bool: - """Checks if a viewer beta is enabled.""" - return "viewer_beta" == self.vis + def is_viewer_enabled(self) -> bool: + """Checks if the viewer is enabled.""" + return self.vis in ("viewer", "viewer+wandb", "viewer+tensorboard", "viewer+comet") def is_wandb_enabled(self) -> bool: """Checks if wandb is enabled.""" diff --git a/nerfstudio/configs/external_methods.py b/nerfstudio/configs/external_methods.py index 6a0c78b09..72a679066 100644 --- a/nerfstudio/configs/external_methods.py +++ b/nerfstudio/configs/external_methods.py @@ -45,7 +45,7 @@ external_methods = [] external_methods.append( ExternalMethod( """[bold yellow]Instruct-NeRF2NeRF[/bold yellow] -For more information visit: https://docs.nerf.studio/en/latest/nerfology/methods/in2n.html +For more information visit: https://docs.nerf.studio/nerfology/methods/in2n.html To enable Instruct-NeRF2NeRF, you must install it first by running: [grey]pip install git+https://github.com/ayaanzhaque/instruct-nerf2nerf[/grey]""", @@ -62,7 +62,7 @@ To enable Instruct-NeRF2NeRF, you must install it first by running: external_methods.append( ExternalMethod( """[bold yellow]K-Planes[/bold yellow] -For more information visit https://docs.nerf.studio/en/latest/nerfology/methods/kplanes.html +For more information visit https://docs.nerf.studio/nerfology/methods/kplanes.html To enable K-Planes, you must install it first by running: [grey]pip install kplanes-nerfstudio[/grey]""", @@ -78,7 +78,7 @@ To enable K-Planes, you must install it first by running: external_methods.append( ExternalMethod( """[bold yellow]LERF[/bold yellow] -For more information visit: https://docs.nerf.studio/en/latest/nerfology/methods/lerf.html +For more information visit: https://docs.nerf.studio/nerfology/methods/lerf.html To enable LERF, you must install it first by running: [grey]pip install git+https://github.com/kerrj/lerf[/grey]""", @@ -95,7 +95,7 @@ To enable LERF, you must install it first by running: external_methods.append( ExternalMethod( """[bold yellow]Tetra-NeRF[/bold yellow] -For more information visit: https://docs.nerf.studio/en/latest/nerfology/methods/tetranerf.html +For more information visit: https://docs.nerf.studio/nerfology/methods/tetranerf.html To enable Tetra-NeRF, you must install it first. Please follow the instructions here: https://github.com/jkulhanek/tetra-nerf/blob/master/README.md#installation""", @@ -110,7 +110,7 @@ To enable Tetra-NeRF, you must install it first. Please follow the instructions external_methods.append( ExternalMethod( """[bold yellow]NeRFPlayer[/bold yellow] -For more information visit: https://docs.nerf.studio/en/latest/nerfology/methods/nerfplayer.html +For more information visit: https://docs.nerf.studio/nerfology/methods/nerfplayer.html To enable NeRFPlayer, you must install it first by running: [grey]pip install git+https://github.com/lsongx/nerfplayer-nerfstudio[/grey]""", @@ -125,7 +125,7 @@ To enable NeRFPlayer, you must install it first by running: external_methods.append( ExternalMethod( """[bold yellow]Volinga[/bold yellow] -For more information visit: https://docs.nerf.studio/en/latest/extensions/unreal_engine.html +For more information visit: https://docs.nerf.studio/extensions/unreal_engine.html To enable Volinga, you must install it first by running: [grey]pip install git+https://github.com/Volinga/volinga-model[/grey]""", @@ -139,6 +139,19 @@ To enable Volinga, you must install it first by running: ) ) +# Instruct-GS2GS +external_methods.append( + ExternalMethod( + """[bold yellow]Instruct-GS2GS[/bold yellow] +For more information visit: https://docs.nerf.studio/nerfology/methods/igs2gs.html + +To enable Instruct-GS2GS, you must install it first by running: + [grey]pip install git+https://github.com/cvachha/instruct-gs2gs[/grey]""", + configurations=[("igs2gs", "Instruct-GS2GS. Full model, used in paper")], + pip_package="git+https://github.com/cvachha/instruct-gs2gs", + ) +) + @dataclass class ExternalMethodTrainerConfig(TrainerConfig): diff --git a/nerfstudio/configs/method_configs.py b/nerfstudio/configs/method_configs.py index a92c042bf..1f1a09edb 100644 --- a/nerfstudio/configs/method_configs.py +++ b/nerfstudio/configs/method_configs.py @@ -22,25 +22,33 @@ from collections import OrderedDict from typing import Dict import tyro -from nerfstudio.data.pixel_samplers import PairPixelSamplerConfig from nerfstudio.cameras.camera_optimizers import CameraOptimizerConfig from nerfstudio.configs.base_config import ViewerConfig from nerfstudio.configs.external_methods import get_external_methods - -from nerfstudio.data.datamanagers.random_cameras_datamanager import RandomCamerasDataManagerConfig -from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager, VanillaDataManagerConfig - +from nerfstudio.data.datamanagers.base_datamanager import ( + VanillaDataManager, + VanillaDataManagerConfig, +) +from nerfstudio.data.datamanagers.parallel_datamanager import ParallelDataManagerConfig +from nerfstudio.data.datamanagers.random_cameras_datamanager import ( + RandomCamerasDataManagerConfig, +) from nerfstudio.data.dataparsers.blender_dataparser import BlenderDataParserConfig from nerfstudio.data.dataparsers.dnerf_dataparser import DNeRFDataParserConfig -from nerfstudio.data.dataparsers.instant_ngp_dataparser import InstantNGPDataParserConfig +from nerfstudio.data.dataparsers.instant_ngp_dataparser import ( + InstantNGPDataParserConfig, +) from nerfstudio.data.dataparsers.nerfstudio_dataparser import NerfstudioDataParserConfig -from nerfstudio.data.dataparsers.phototourism_dataparser import PhototourismDataParserConfig +from nerfstudio.data.dataparsers.phototourism_dataparser import ( + PhototourismDataParserConfig, +) from nerfstudio.data.dataparsers.sdfstudio_dataparser import SDFStudioDataParserConfig from nerfstudio.data.dataparsers.sitcoms3d_dataparser import Sitcoms3DDataParserConfig from nerfstudio.data.datasets.depth_dataset import DepthDataset from nerfstudio.data.datasets.sdf_dataset import SDFDataset from nerfstudio.data.datasets.semantic_dataset import SemanticDataset +from nerfstudio.data.pixel_samplers import PairPixelSamplerConfig from nerfstudio.engine.optimizers import AdamOptimizerConfig, RAdamOptimizerConfig from nerfstudio.engine.schedulers import ( CosineDecaySchedulerConfig, @@ -51,8 +59,10 @@ from nerfstudio.engine.trainer import TrainerConfig from nerfstudio.field_components.temporal_distortions import TemporalDistortionKind from nerfstudio.fields.sdf_field import SDFFieldConfig from nerfstudio.models.depth_nerfacto import DepthNerfactoModelConfig +from nerfstudio.models.gaussian_splatting import GaussianSplattingModelConfig from nerfstudio.models.generfacto import GenerfactoModelConfig from nerfstudio.models.instant_ngp import InstantNGPModelConfig +from nerfstudio.data.dataparsers.colmap_dataparser import ColmapDataParserConfig from nerfstudio.models.mipnerf import MipNerfModel from nerfstudio.models.nerfacto import NerfactoModelConfig from nerfstudio.models.neus import NeuSModelConfig @@ -61,6 +71,7 @@ from nerfstudio.models.semantic_nerfw import SemanticNerfWModelConfig from nerfstudio.models.tensorf import TensoRFModelConfig from nerfstudio.models.vanilla_nerf import NeRFModel, VanillaModelConfig from nerfstudio.pipelines.base_pipeline import VanillaPipelineConfig +from nerfstudio.data.datamanagers.full_images_datamanager import FullImageDatamanagerConfig from nerfstudio.pipelines.dynamic_batch import DynamicBatchPipelineConfig from nerfstudio.plugins.registry import discover_methods @@ -79,6 +90,7 @@ descriptions = { "generfacto": "Generative Text to NeRF model", "neus": "Implementation of NeuS. (slow)", "neus-facto": "Implementation of NeuS-Facto. (slow)", + "gaussian-splatting": "Gaussian Splatting model", } method_configs["nerfacto"] = TrainerConfig( @@ -88,17 +100,15 @@ method_configs["nerfacto"] = TrainerConfig( max_num_iterations=30000, mixed_precision=True, pipeline=VanillaPipelineConfig( - datamanager=VanillaDataManagerConfig( + datamanager=ParallelDataManagerConfig( dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=4096, eval_num_rays_per_batch=4096, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", - optimizer=AdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-2), - scheduler=ExponentialDecaySchedulerConfig(lr_final=6e-6, max_steps=200000), - ), ), - model=NerfactoModelConfig(eval_num_rays_per_chunk=1 << 15), + model=NerfactoModelConfig( + eval_num_rays_per_chunk=1 << 15, + camera_optimizer=CameraOptimizerConfig(mode="SO3xR3"), + ), ), optimizers={ "proposal_networks": { @@ -109,10 +119,15 @@ method_configs["nerfacto"] = TrainerConfig( "optimizer": AdamOptimizerConfig(lr=1e-2, eps=1e-15), "scheduler": ExponentialDecaySchedulerConfig(lr_final=0.0001, max_steps=200000), }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", ) + method_configs["nerfacto-big"] = TrainerConfig( method_name="nerfacto", steps_per_eval_batch=500, @@ -120,14 +135,10 @@ method_configs["nerfacto-big"] = TrainerConfig( max_num_iterations=100000, mixed_precision=True, pipeline=VanillaPipelineConfig( - datamanager=VanillaDataManagerConfig( + datamanager=ParallelDataManagerConfig( dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=8192, eval_num_rays_per_batch=4096, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", - optimizer=RAdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-3), - ), ), model=NerfactoModelConfig( eval_num_rays_per_chunk=1 << 15, @@ -139,6 +150,7 @@ method_configs["nerfacto-big"] = TrainerConfig( max_res=4096, proposal_weights_anneal_max_num_iters=5000, log2_hashmap_size=21, + camera_optimizer=CameraOptimizerConfig(mode="SO3xR3"), ), ), optimizers={ @@ -150,10 +162,15 @@ method_configs["nerfacto-big"] = TrainerConfig( "optimizer": RAdamOptimizerConfig(lr=1e-2, eps=1e-15), "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=50000), }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", ) + method_configs["nerfacto-huge"] = TrainerConfig( method_name="nerfacto", steps_per_eval_batch=500, @@ -161,15 +178,10 @@ method_configs["nerfacto-huge"] = TrainerConfig( max_num_iterations=100000, mixed_precision=True, pipeline=VanillaPipelineConfig( - datamanager=VanillaDataManagerConfig( + datamanager=ParallelDataManagerConfig( dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=16384, eval_num_rays_per_batch=4096, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", - optimizer=RAdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-3), - scheduler=ExponentialDecaySchedulerConfig(lr_final=6e-5, max_steps=50000), - ), ), model=NerfactoModelConfig( eval_num_rays_per_chunk=1 << 15, @@ -185,6 +197,7 @@ method_configs["nerfacto-huge"] = TrainerConfig( max_res=8192, proposal_weights_anneal_max_num_iters=5000, log2_hashmap_size=21, + camera_optimizer=CameraOptimizerConfig(mode="SO3xR3"), ), ), optimizers={ @@ -196,6 +209,10 @@ method_configs["nerfacto-huge"] = TrainerConfig( "optimizer": RAdamOptimizerConfig(lr=1e-2, eps=1e-15), "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=50000), }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", @@ -214,11 +231,11 @@ method_configs["depth-nerfacto"] = TrainerConfig( dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=4096, eval_num_rays_per_batch=4096, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", optimizer=AdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-2) - ), ), - model=DepthNerfactoModelConfig(eval_num_rays_per_chunk=1 << 15), + model=DepthNerfactoModelConfig( + eval_num_rays_per_chunk=1 << 15, + camera_optimizer=CameraOptimizerConfig(mode="SO3xR3"), + ), ), optimizers={ "proposal_networks": { @@ -229,6 +246,10 @@ method_configs["depth-nerfacto"] = TrainerConfig( "optimizer": AdamOptimizerConfig(lr=1e-2, eps=1e-15), "scheduler": None, }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", @@ -258,7 +279,6 @@ method_configs["instant-ngp"] = TrainerConfig( vis="viewer", ) - method_configs["instant-ngp-bounded"] = TrainerConfig( method_name="instant-ngp-bounded", steps_per_eval_batch=500, @@ -286,12 +306,12 @@ method_configs["instant-ngp-bounded"] = TrainerConfig( viewer=ViewerConfig(num_rays_per_chunk=1 << 12), vis="viewer", ) - - +# +# method_configs["mipnerf"] = TrainerConfig( method_name="mipnerf", pipeline=VanillaPipelineConfig( - datamanager=VanillaDataManagerConfig(dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=1024), + datamanager=ParallelDataManagerConfig(dataparser=NerfstudioDataParserConfig(), train_num_rays_per_batch=1024), model=VanillaModelConfig( _target=MipNerfModel, loss_coefficients={"rgb_loss_coarse": 0.1, "rgb_loss_fine": 1.0}, @@ -364,13 +384,14 @@ method_configs["tensorf"] = TrainerConfig( max_num_iterations=30000, mixed_precision=False, pipeline=VanillaPipelineConfig( - datamanager=VanillaDataManagerConfig( + datamanager=ParallelDataManagerConfig( dataparser=BlenderDataParserConfig(), train_num_rays_per_batch=4096, eval_num_rays_per_batch=4096, ), model=TensoRFModelConfig( regularization="tv", + camera_optimizer=CameraOptimizerConfig(mode="off"), ), ), optimizers={ @@ -382,6 +403,10 @@ method_configs["tensorf"] = TrainerConfig( "optimizer": AdamOptimizerConfig(lr=0.02), "scheduler": ExponentialDecaySchedulerConfig(lr_final=0.002, max_steps=30000), }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-4, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-5, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", @@ -420,16 +445,16 @@ method_configs["phototourism"] = TrainerConfig( dataparser=PhototourismDataParserConfig(), # NOTE: one of the only differences with nerfacto train_num_rays_per_batch=4096, eval_num_rays_per_batch=4096, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", optimizer=AdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-2) - ), # Large dataset, so using prior values from VariableResDataManager. train_num_images_to_sample_from=40, train_num_times_to_repeat_images=100, eval_num_images_to_sample_from=40, eval_num_times_to_repeat_images=100, ), - model=NerfactoModelConfig(eval_num_rays_per_chunk=1 << 15), + model=NerfactoModelConfig( + eval_num_rays_per_chunk=1 << 15, + camera_optimizer=CameraOptimizerConfig(mode="SO3xR3"), + ), ), optimizers={ "proposal_networks": { @@ -440,6 +465,10 @@ method_configs["phototourism"] = TrainerConfig( "optimizer": AdamOptimizerConfig(lr=1e-2, eps=1e-15), "scheduler": None, }, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=5000), + }, }, viewer=ViewerConfig(num_rays_per_chunk=1 << 15), vis="viewer", @@ -504,9 +533,6 @@ method_configs["neus"] = TrainerConfig( dataparser=SDFStudioDataParserConfig(), train_num_rays_per_batch=1024, eval_num_rays_per_batch=1024, - camera_optimizer=CameraOptimizerConfig( - mode="off", optimizer=AdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-2) - ), ), model=NeuSModelConfig(eval_num_rays_per_chunk=1024), ), @@ -538,9 +564,6 @@ method_configs["neus-facto"] = TrainerConfig( dataparser=SDFStudioDataParserConfig(), train_num_rays_per_batch=2048, eval_num_rays_per_batch=2048, - camera_optimizer=CameraOptimizerConfig( - mode="SO3xR3", optimizer=AdamOptimizerConfig(lr=6e-4, eps=1e-8, weight_decay=1e-2) - ), ), model=NeuSFactoModelConfig( # proposal network allows for significantly smaller sdf/color network @@ -575,6 +598,55 @@ method_configs["neus-facto"] = TrainerConfig( vis="viewer", ) +method_configs["gaussian-splatting"] = TrainerConfig( + method_name="gaussian-splatting", + steps_per_eval_image=100, + steps_per_eval_batch=0, + steps_per_save=2000, + steps_per_eval_all_images=1000, + max_num_iterations=30000, + mixed_precision=False, + gradient_accumulation_steps={"camera_opt": 100}, + pipeline=VanillaPipelineConfig( + datamanager=FullImageDatamanagerConfig( + dataparser=ColmapDataParserConfig(load_3D_points=True), + ), + model=GaussianSplattingModelConfig(), + ), + optimizers={ + "xyz": { + "optimizer": AdamOptimizerConfig(lr=1.6e-4, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig( + lr_final=1.6e-6, + max_steps=30000, + ), + }, + "features_dc": { + "optimizer": AdamOptimizerConfig(lr=0.0025, eps=1e-15), + "scheduler": None, + }, + "features_rest": { + "optimizer": AdamOptimizerConfig(lr=0.0025 / 20, eps=1e-15), + "scheduler": None, + }, + "opacity": { + "optimizer": AdamOptimizerConfig(lr=0.05, eps=1e-15), + "scheduler": None, + }, + "scaling": { + "optimizer": AdamOptimizerConfig(lr=0.005, eps=1e-15), + "scheduler": None, + }, + "rotation": {"optimizer": AdamOptimizerConfig(lr=0.001, eps=1e-15), "scheduler": None}, + "camera_opt": { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=5e-5, max_steps=30000), + }, + }, + viewer=ViewerConfig(num_rays_per_chunk=1 << 15), + vis="viewer", +) + def merge_methods(methods, method_descriptions, new_methods, new_descriptions, overwrite=True): """Merge new methods and descriptions into existing methods and descriptions. diff --git a/nerfstudio/data/datamanagers/base_datamanager.py b/nerfstudio/data/datamanagers/base_datamanager.py index 52fd0af0f..67180bea6 100644 --- a/nerfstudio/data/datamanagers/base_datamanager.py +++ b/nerfstudio/data/datamanagers/base_datamanager.py @@ -21,12 +21,13 @@ from __future__ import annotations from abc import abstractmethod from collections import defaultdict from dataclasses import dataclass, field -from pathlib import Path from functools import cached_property +from pathlib import Path from typing import ( Any, Callable, Dict, + ForwardRef, Generic, List, Literal, @@ -35,9 +36,8 @@ from typing import ( Type, Union, cast, - ForwardRef, - get_origin, get_args, + get_origin, ) import torch @@ -47,7 +47,7 @@ from torch.utils.data.distributed import DistributedSampler from typing_extensions import TypeVar from nerfstudio.cameras.camera_optimizers import CameraOptimizerConfig -from nerfstudio.cameras.cameras import CameraType +from nerfstudio.cameras.cameras import Cameras, CameraType from nerfstudio.cameras.rays import RayBundle from nerfstudio.configs.base_config import InstantiateConfig from nerfstudio.configs.dataparser_configs import AnnotatedDataParserUnion @@ -55,9 +55,9 @@ from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs from nerfstudio.data.dataparsers.blender_dataparser import BlenderDataParserConfig from nerfstudio.data.datasets.base_dataset import InputDataset from nerfstudio.data.pixel_samplers import ( + PatchPixelSamplerConfig, PixelSampler, PixelSamplerConfig, - PatchPixelSamplerConfig, ) from nerfstudio.data.utils.dataloaders import ( CacheDataloader, @@ -67,9 +67,8 @@ from nerfstudio.data.utils.dataloaders import ( from nerfstudio.data.utils.nerfstudio_collate import nerfstudio_collate from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes from nerfstudio.model_components.ray_generators import RayGenerator -from nerfstudio.utils.misc import IterableWrapper +from nerfstudio.utils.misc import IterableWrapper, get_orig_class from nerfstudio.utils.rich_utils import CONSOLE -from nerfstudio.utils.misc import get_orig_class def variable_res_collate(batch: List[Dict]) -> Dict: @@ -113,8 +112,6 @@ class DataManagerConfig(InstantiateConfig): """Target class to instantiate.""" data: Optional[Path] = None """Source of data, may not be used by all models.""" - camera_optimizer: Optional[CameraOptimizerConfig] = None - """Specifies the camera pose optimizer used during training. Helpful if poses are noisy.""" masks_on_gpu: bool = False """Process masks on GPU for speed at the expense of memory, if True.""" images_on_gpu: bool = False @@ -133,7 +130,7 @@ class DataManager(nn.Module): To get data, use the next_train and next_eval functions. This data manager's next_train and next_eval methods will return 2 things: - 1. A Raybundle: This will contain the rays we are sampling, with latents and + 1. 'rays': This will contain the rays or camera we are sampling, with latents and conditionals attached (everything needed at inference) 2. A "batch" of auxiliary information: This will contain the mask, the ground truth pixels, etc needed to actually train, score, etc the model @@ -248,7 +245,7 @@ class DataManager(nn.Module): """Sets up the data manager for evaluation""" @abstractmethod - def next_train(self, step: int) -> Tuple[RayBundle, Dict]: + def next_train(self, step: int) -> Tuple[Union[RayBundle, Cameras], Dict]: """Returns the next batch of data from the train data manager. Args: @@ -260,25 +257,25 @@ class DataManager(nn.Module): raise NotImplementedError @abstractmethod - def next_eval(self, step: int) -> Tuple[RayBundle, Dict]: + def next_eval(self, step: int) -> Tuple[Union[RayBundle, Cameras], Dict]: """Returns the next batch of data from the eval data manager. Args: step: the step number of the eval image to retrieve Returns: - A tuple of the ray bundle for the image, and a dictionary of additional batch information + A tuple of the ray/camera for the image, and a dictionary of additional batch information such as the groundtruth image. """ raise NotImplementedError @abstractmethod - def next_eval_image(self, step: int) -> Tuple[int, RayBundle, Dict]: + def next_eval_image(self, step: int) -> Tuple[Cameras, Dict]: """Retrieve the next eval image. Args: step: the step number of the eval image to retrieve Returns: - A tuple of the step number, the ray bundle for the image, and a dictionary of + A tuple of the step number, the ray/camera for the image, and a dictionary of additional batch information such as the groundtruth image. """ raise NotImplementedError @@ -315,11 +312,11 @@ class DataManager(nn.Module): @dataclass class VanillaDataManagerConfig(DataManagerConfig): - """A basic data manager""" + """A basic data manager for a ray-based model""" _target: Type = field(default_factory=lambda: VanillaDataManager) """Target class to instantiate.""" - dataparser: AnnotatedDataParserUnion = BlenderDataParserConfig() + dataparser: AnnotatedDataParserUnion = field(default_factory=lambda: BlenderDataParserConfig()) """Specifies the dataparser used to unpack the data.""" train_num_rays_per_batch: int = 1024 """Number of rays per batch to use per training iteration.""" @@ -337,9 +334,6 @@ class VanillaDataManagerConfig(DataManagerConfig): new images. If -1, never pick new images.""" eval_image_indices: Optional[Tuple[int, ...]] = (0,) """Specifies the image indices to use during eval; if None, uses all.""" - camera_optimizer: CameraOptimizerConfig = CameraOptimizerConfig() - """Specifies the camera pose optimizer used during training. Helpful if poses are noisy, such as for data from - Record3D.""" collate_fn: Callable[[Any], Any] = cast(Any, staticmethod(nerfstudio_collate)) """Specifies the collate function to use for the train and eval dataloaders.""" camera_res_scale_factor: float = 1.0 @@ -347,10 +341,22 @@ class VanillaDataManagerConfig(DataManagerConfig): along with relevant information about camera intrinsics """ patch_size: int = 1 - """Size of patch to sample from. If >1, patch-based sampling will be used.""" - pixel_sampler: PixelSamplerConfig = PixelSamplerConfig() + """Size of patch to sample from. If > 1, patch-based sampling will be used.""" + camera_optimizer: Optional[CameraOptimizerConfig] = field(default=None) + """Deprecated, has been moved to the model config.""" + pixel_sampler: PixelSamplerConfig = field(default_factory=lambda: PixelSamplerConfig()) """Specifies the pixel sampler used to sample pixels from images.""" + def __post_init__(self): + """Warn user of camera optimizer change.""" + if self.camera_optimizer is not None: + import warnings + + CONSOLE.print( + "\nCameraOptimizerConfig has been moved from the DataManager to the Model.\n", style="bold yellow" + ) + warnings.warn("above message coming from", FutureWarning, stacklevel=3) + TDataset = TypeVar("TDataset", bound=InputDataset, default=InputDataset) @@ -468,8 +474,15 @@ class VanillaDataManager(DataManager, Generic[TDataset]): is_equirectangular = (dataset.cameras.camera_type == CameraType.EQUIRECTANGULAR.value).all() if is_equirectangular.any(): CONSOLE.print("[bold yellow]Warning: Some cameras are equirectangular, but using default pixel sampler.") + + fisheye_crop_radius = None + if dataset.cameras.metadata is not None and "fisheye_crop_radius" in dataset.cameras.metadata: + fisheye_crop_radius = dataset.cameras.metadata["fisheye_crop_radius"] + return self.config.pixel_sampler.setup( - is_equirectangular=is_equirectangular, num_rays_per_batch=num_rays_per_batch + is_equirectangular=is_equirectangular, + num_rays_per_batch=num_rays_per_batch, + fisheye_crop_radius=fisheye_crop_radius, ) def setup_train(self): @@ -488,13 +501,7 @@ class VanillaDataManager(DataManager, Generic[TDataset]): ) self.iter_train_image_dataloader = iter(self.train_image_dataloader) self.train_pixel_sampler = self._get_pixel_sampler(self.train_dataset, self.config.train_num_rays_per_batch) - self.train_camera_optimizer = self.config.camera_optimizer.setup( - num_cameras=self.train_dataset.cameras.size, device=self.device - ) - self.train_ray_generator = RayGenerator( - self.train_dataset.cameras.to(self.device), - self.train_camera_optimizer, - ) + self.train_ray_generator = RayGenerator(self.train_dataset.cameras.to(self.device)) def setup_eval(self): """Sets up the data loader for evaluation""" @@ -512,13 +519,7 @@ class VanillaDataManager(DataManager, Generic[TDataset]): ) self.iter_eval_image_dataloader = iter(self.eval_image_dataloader) self.eval_pixel_sampler = self._get_pixel_sampler(self.eval_dataset, self.config.eval_num_rays_per_batch) - self.eval_camera_optimizer = self.config.camera_optimizer.setup( - num_cameras=self.eval_dataset.cameras.size, device=self.device - ) - self.eval_ray_generator = RayGenerator( - self.eval_dataset.cameras.to(self.device), - self.eval_camera_optimizer, - ) + self.eval_ray_generator = RayGenerator(self.eval_dataset.cameras.to(self.device)) # for loading full images self.fixed_indices_eval_dataloader = FixedIndicesEvalDataloader( input_dataset=self.eval_dataset, @@ -553,17 +554,20 @@ class VanillaDataManager(DataManager, Generic[TDataset]): ray_bundle = self.eval_ray_generator(ray_indices) return ray_bundle, batch - def next_eval_image(self, step: int) -> Tuple[int, RayBundle, Dict]: - for camera_ray_bundle, batch in self.eval_dataloader: - assert camera_ray_bundle.camera_indices is not None - image_idx = int(camera_ray_bundle.camera_indices[0, 0, 0]) - return image_idx, camera_ray_bundle, batch + def next_eval_image(self, step: int) -> Tuple[Cameras, Dict]: + for camera, batch in self.eval_dataloader: + assert camera.shape[0] == 1 + return camera, batch raise ValueError("No more eval images") def get_train_rays_per_batch(self) -> int: + if self.train_pixel_sampler is not None: + return self.train_pixel_sampler.num_rays_per_batch return self.config.train_num_rays_per_batch def get_eval_rays_per_batch(self) -> int: + if self.eval_pixel_sampler is not None: + return self.eval_pixel_sampler.num_rays_per_batch return self.config.eval_num_rays_per_batch def get_datapath(self) -> Path: @@ -574,13 +578,4 @@ class VanillaDataManager(DataManager, Generic[TDataset]): Returns: A list of dictionaries containing the data manager's param groups. """ - param_groups = {} - - camera_opt_params = list(self.train_camera_optimizer.parameters()) - if self.config.camera_optimizer.mode != "off": - assert len(camera_opt_params) > 0 - param_groups[self.config.camera_optimizer.param_group] = camera_opt_params - else: - assert len(camera_opt_params) == 0 - - return param_groups + return {} diff --git a/nerfstudio/data/datamanagers/full_images_datamanager.py b/nerfstudio/data/datamanagers/full_images_datamanager.py new file mode 100644 index 000000000..92d65478c --- /dev/null +++ b/nerfstudio/data/datamanagers/full_images_datamanager.py @@ -0,0 +1,410 @@ +# Copyright 2022 The Nerfstudio Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Data manager that outputs cameras / images instead of raybundles + +Good for things like gaussian splatting which require full cameras instead of the standard ray +paradigm +""" + +from __future__ import annotations + +import random +from dataclasses import dataclass, field +from functools import cached_property +from pathlib import Path +from typing import Dict, ForwardRef, Generic, List, Literal, Optional, Tuple, Type, Union, cast, get_args, get_origin + +import cv2 +import numpy as np +import torch +from copy import deepcopy +from torch.nn import Parameter +from tqdm import tqdm + +from nerfstudio.cameras.cameras import Cameras, CameraType +from nerfstudio.configs.dataparser_configs import AnnotatedDataParserUnion +from nerfstudio.data.datamanagers.base_datamanager import DataManager, DataManagerConfig, TDataset +from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs +from nerfstudio.data.dataparsers.nerfstudio_dataparser import NerfstudioDataParserConfig +from nerfstudio.data.datasets.base_dataset import InputDataset +from nerfstudio.utils.misc import get_orig_class +from nerfstudio.utils.rich_utils import CONSOLE + + +@dataclass +class FullImageDatamanagerConfig(DataManagerConfig): + _target: Type = field(default_factory=lambda: FullImageDatamanager) + dataparser: AnnotatedDataParserUnion = NerfstudioDataParserConfig() + camera_res_scale_factor: float = 1.0 + """The scale factor for scaling spatial data such as images, mask, semantics + along with relevant information about camera intrinsics + """ + eval_num_images_to_sample_from: int = -1 + """Number of images to sample during eval iteration.""" + eval_num_times_to_repeat_images: int = -1 + """When not evaluating on all images, number of iterations before picking + new images. If -1, never pick new images.""" + eval_image_indices: Optional[Tuple[int, ...]] = (0,) + """Specifies the image indices to use during eval; if None, uses all.""" + cache_images: Literal["cpu", "gpu"] = "cpu" + """Whether to cache images in memory. If "cpu", caches on cpu. If "gpu", caches on device.""" + + +class FullImageDatamanager(DataManager, Generic[TDataset]): + """ + A datamanager that outputs full images and cameras instead of raybundles. This makes the + datamanager more lightweight since we don't have to do generate rays. Useful for full-image + training e.g. rasterization pipelines + """ + + config: FullImageDatamanagerConfig + train_dataset: TDataset + eval_dataset: TDataset + + def __init__( + self, + config: FullImageDatamanagerConfig, + device: Union[torch.device, str] = "cpu", + test_mode: Literal["test", "val", "inference"] = "val", + world_size: int = 1, + local_rank: int = 0, + **kwargs, + ): + self.config = config + self.device = device + self.world_size = world_size + self.local_rank = local_rank + self.sampler = None + self.test_mode = test_mode + self.test_split = "test" if test_mode in ["test", "inference"] else "val" + self.dataparser_config = self.config.dataparser + if self.config.data is not None: + self.config.dataparser.data = Path(self.config.data) + else: + self.config.data = self.config.dataparser.data + self.dataparser = self.dataparser_config.setup() + if test_mode == "inference": + self.dataparser.downscale_factor = 1 # Avoid opening images + self.includes_time = self.dataparser.includes_time + + self.train_dataparser_outputs: DataparserOutputs = self.dataparser.get_dataparser_outputs(split="train") + self.train_dataset = self.create_train_dataset() + self.eval_dataset = self.create_eval_dataset() + if len(self.train_dataset) > 500 and self.config.cache_images == "gpu": + CONSOLE.print("Train dataset has over 500 images, overriding cach_images to cpu", style="bold yellow") + self.config.cache_images = "cpu" + self.cached_train, self.cached_eval = self.cache_images(self.config.cache_images) + self.exclude_batch_keys_from_device = self.train_dataset.exclude_batch_keys_from_device + if self.config.masks_on_gpu is True: + self.exclude_batch_keys_from_device.remove("mask") + if self.config.images_on_gpu is True: + self.exclude_batch_keys_from_device.remove("image") + + # Some logic to make sure we sample every camera in equal amounts + self.train_unseen_cameras = [i for i in range(len(self.train_dataset))] + self.eval_unseen_cameras = [i for i in range(len(self.eval_dataset))] + assert len(self.train_unseen_cameras) > 0, "No data found in dataset" + + super().__init__() + + def cache_images(self, cache_images_option): + cached_train = [] + cached_eval = [] + CONSOLE.log("Caching / undistorting train images") + for i in tqdm(range(len(self.train_dataset)), leave=False): + # cv2.undistort the images / cameras + data = self.train_dataset.get_data(i) + camera = self.train_dataset.cameras[i].reshape(()) + K = camera.get_intrinsics_matrices().numpy() + if camera.distortion_params is None: + continue + distortion_params = camera.distortion_params.numpy() + image = data["image"].numpy() + + if camera.camera_type.item() == CameraType.PERSPECTIVE.value: + distortion_params = np.array( + [ + distortion_params[0], + distortion_params[1], + distortion_params[4], + distortion_params[5], + distortion_params[2], + distortion_params[3], + 0, + 0, + ] + ) + newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0) + image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore + # crop the image and update the intrinsics accordingly + x, y, w, h = roi + image = image[y : y + h, x : x + w] + if "mask" in data: + data["mask"] = data["mask"][y : y + h, x : x + w] + if "depth_image" in data: + data["depth_image"] = data["depth_image"][y : y + h, x : x + w] + # update the width, height + self.train_dataset.cameras.width[i] = w + self.train_dataset.cameras.height[i] = h + if "mask" in data: + mask = data["mask"].numpy() + mask = mask.astype(np.uint8) * 255 + mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore + mask = mask[y : y + h, x : x + w] + data["mask"] = torch.from_numpy(mask).bool() + K = newK + + elif camera.camera_type.item() == CameraType.FISHEYE.value: + distortion_params = np.array( + [distortion_params[0], distortion_params[1], distortion_params[2], distortion_params[3]] + ) + newK = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify( + K, distortion_params, (image.shape[1], image.shape[0]), np.eye(3), balance=0 + ) + map1, map2 = cv2.fisheye.initUndistortRectifyMap( + K, distortion_params, np.eye(3), newK, (image.shape[1], image.shape[0]), cv2.CV_32FC1 + ) + # and then remap: + image = cv2.remap(image, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) + if "mask" in data: + mask = data["mask"].numpy() + mask = mask.astype(np.uint8) * 255 + mask = cv2.fisheye.undistortImage(mask, K, distortion_params, None, newK) + data["mask"] = torch.from_numpy(mask).bool() + K = newK + else: + raise NotImplementedError("Only perspective and fisheye cameras are supported") + data["image"] = torch.from_numpy(image) + + cached_train.append(data) + + self.train_dataset.cameras.fx[i] = float(K[0, 0]) + self.train_dataset.cameras.fy[i] = float(K[1, 1]) + self.train_dataset.cameras.cx[i] = float(K[0, 2]) + self.train_dataset.cameras.cy[i] = float(K[1, 2]) + + CONSOLE.log("Caching / undistorting eval images") + for i in tqdm(range(len(self.eval_dataset)), leave=False): + # cv2.undistort the images / cameras + data = self.eval_dataset.get_data(i) + camera = self.eval_dataset.cameras[i].reshape(()) + K = camera.get_intrinsics_matrices().numpy() + if camera.distortion_params is None: + continue + distortion_params = camera.distortion_params.numpy() + image = data["image"].numpy() + + if camera.camera_type.item() == CameraType.PERSPECTIVE.value: + distortion_params = np.array( + [ + distortion_params[0], + distortion_params[1], + distortion_params[4], + distortion_params[5], + distortion_params[2], + distortion_params[3], + 0, + 0, + ] + ) + newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0) + image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore + # crop the image and update the intrinsics accordingly + x, y, w, h = roi + image = image[y : y + h, x : x + w] + # update the width, height + self.eval_dataset.cameras.width[i] = w + self.eval_dataset.cameras.height[i] = h + if "mask" in data: + mask = data["mask"].numpy() + mask = mask.astype(np.uint8) * 255 + mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore + mask = mask[y : y + h, x : x + w] + data["mask"] = torch.from_numpy(mask).bool() + K = newK + + elif camera.camera_type.item() == CameraType.FISHEYE.value: + distortion_params = np.array( + [distortion_params[0], distortion_params[1], distortion_params[2], distortion_params[3]] + ) + newK = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify( + K, distortion_params, (image.shape[1], image.shape[0]), np.eye(3), balance=0 + ) + map1, map2 = cv2.fisheye.initUndistortRectifyMap( + K, distortion_params, np.eye(3), newK, (image.shape[1], image.shape[0]), cv2.CV_32FC1 + ) + # and then remap: + image = cv2.remap(image, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) + if "mask" in data: + mask = data["mask"].numpy() + mask = mask.astype(np.uint8) * 255 + mask = cv2.fisheye.undistortImage(mask, K, distortion_params, None, newK) + data["mask"] = torch.from_numpy(mask).bool() + K = newK + else: + raise NotImplementedError("Only perspective and fisheye cameras are supported") + data["image"] = torch.from_numpy(image) + + cached_eval.append(data) + + self.eval_dataset.cameras.fx[i] = float(K[0, 0]) + self.eval_dataset.cameras.fy[i] = float(K[1, 1]) + self.eval_dataset.cameras.cx[i] = float(K[0, 2]) + self.eval_dataset.cameras.cy[i] = float(K[1, 2]) + + if cache_images_option == "gpu": + for cache in cached_train: + cache["image"] = cache["image"].to(self.device) + if "mask" in cache: + cache["mask"] = cache["mask"].to(self.device) + for cache in cached_eval: + cache["image"] = cache["image"].to(self.device) + if "mask" in cache: + cache["mask"] = cache["mask"].to(self.device) + else: + for cache in cached_train: + cache["image"] = cache["image"].pin_memory() + if "mask" in cache: + cache["mask"] = cache["mask"].pin_memory() + for cache in cached_eval: + cache["image"] = cache["image"].pin_memory() + if "mask" in cache: + cache["mask"] = cache["mask"].pin_memory() + + return cached_train, cached_eval + + def create_train_dataset(self) -> TDataset: + """Sets up the data loaders for training""" + return self.dataset_type( + dataparser_outputs=self.train_dataparser_outputs, + scale_factor=self.config.camera_res_scale_factor, + ) + + def create_eval_dataset(self) -> TDataset: + """Sets up the data loaders for evaluation""" + return self.dataset_type( + dataparser_outputs=self.dataparser.get_dataparser_outputs(split=self.test_split), + scale_factor=self.config.camera_res_scale_factor, + ) + + @cached_property + def dataset_type(self) -> Type[TDataset]: + """Returns the dataset type passed as the generic argument""" + default: Type[TDataset] = cast(TDataset, TDataset.__default__) # type: ignore + orig_class: Type[FullImageDatamanager] = get_orig_class(self, default=None) # type: ignore + if type(self) is FullImageDatamanager and orig_class is None: + return default + if orig_class is not None and get_origin(orig_class) is FullImageDatamanager: + return get_args(orig_class)[0] + + # For inherited classes, we need to find the correct type to instantiate + for base in getattr(self, "__orig_bases__", []): + if get_origin(base) is FullImageDatamanager: + for value in get_args(base): + if isinstance(value, ForwardRef): + if value.__forward_evaluated__: + value = value.__forward_value__ + elif value.__forward_module__ is None: + value.__forward_module__ = type(self).__module__ + value = getattr(value, "_evaluate")(None, None, set()) + assert isinstance(value, type) + if issubclass(value, InputDataset): + return cast(Type[TDataset], value) + return default + + def get_datapath(self) -> Path: + return self.config.dataparser.data + + def setup_train(self): + """Sets up the data loaders for training""" + + def setup_eval(self): + """Sets up the data loader for evaluation""" + + @property + def fixed_indices_eval_dataloader(self) -> List[Tuple[Cameras, Dict]]: + """ + Pretends to be the dataloader for evaluation, it returns a list of (camera, data) tuples + """ + image_indices = [i for i in range(len(self.eval_dataset))] + data = deepcopy(self.cached_eval) + _cameras = deepcopy(self.eval_dataset.cameras).to(self.device) + cameras = [] + for i in image_indices: + data[i]["image"] = data[i]["image"].to(self.device) + cameras.append(_cameras[i : i + 1]) + assert len(self.eval_dataset.cameras.shape) == 1, "Assumes single batch dimension" + return list(zip(cameras, data)) + + def get_param_groups(self) -> Dict[str, List[Parameter]]: + """Get the param groups for the data manager. + Returns: + A list of dictionaries containing the data manager's param groups. + """ + return {} + + def get_train_rays_per_batch(self): + # TODO: fix this to be the resolution of the last image rendered + return 800 * 800 + + def next_train(self, step: int) -> Tuple[Cameras, Dict]: + """Returns the next training batch + + Returns a Camera instead of raybundle""" + image_idx = self.train_unseen_cameras.pop(random.randint(0, len(self.train_unseen_cameras) - 1)) + # Make sure to re-populate the unseen cameras list if we have exhausted it + if len(self.train_unseen_cameras) == 0: + self.train_unseen_cameras = [i for i in range(len(self.train_dataset))] + + data = deepcopy(self.cached_train[image_idx]) + data["image"] = data["image"].to(self.device) + + assert len(self.train_dataset.cameras.shape) == 1, "Assumes single batch dimension" + camera = self.train_dataset.cameras[image_idx : image_idx + 1].to(self.device) + if camera.metadata is None: + camera.metadata = {} + camera.metadata["cam_idx"] = image_idx + return camera, data + + def next_eval(self, step: int) -> Tuple[Cameras, Dict]: + """Returns the next evaluation batch + + Returns a Camera instead of raybundle""" + image_idx = self.eval_unseen_cameras.pop(random.randint(0, len(self.eval_unseen_cameras) - 1)) + # Make sure to re-populate the unseen cameras list if we have exhausted it + if len(self.eval_unseen_cameras) == 0: + self.eval_unseen_cameras = [i for i in range(len(self.eval_dataset))] + data = deepcopy(self.cached_eval[image_idx]) + data["image"] = data["image"].to(self.device) + assert len(self.eval_dataset.cameras.shape) == 1, "Assumes single batch dimension" + camera = self.eval_dataset.cameras[image_idx : image_idx + 1].to(self.device) + return camera, data + + def next_eval_image(self, step: int) -> Tuple[Cameras, Dict]: + """Returns the next evaluation batch + + Returns a Camera instead of raybundle + + TODO: Make sure this logic is consistent with the vanilladatamanager""" + image_idx = self.eval_unseen_cameras.pop(random.randint(0, len(self.eval_unseen_cameras) - 1)) + # Make sure to re-populate the unseen cameras list if we have exhausted it + if len(self.eval_unseen_cameras) == 0: + self.eval_unseen_cameras = [i for i in range(len(self.eval_dataset))] + data = deepcopy(self.cached_eval[image_idx]) + data["image"] = data["image"].to(self.device) + assert len(self.eval_dataset.cameras.shape) == 1, "Assumes single batch dimension" + camera = self.eval_dataset.cameras[image_idx : image_idx + 1].to(self.device) + return camera, data diff --git a/nerfstudio/data/datamanagers/parallel_datamanager.py b/nerfstudio/data/datamanagers/parallel_datamanager.py new file mode 100644 index 000000000..9f36807a6 --- /dev/null +++ b/nerfstudio/data/datamanagers/parallel_datamanager.py @@ -0,0 +1,306 @@ +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Parallel data manager that generates training data in multiple python processes. +""" +from __future__ import annotations + +import concurrent.futures +import queue +import time +from dataclasses import dataclass, field +from pathlib import Path +from typing import Dict, Generic, List, Literal, Optional, Tuple, Type, Union + +import torch +from pathos.helpers import mp +from rich.progress import track +from torch.nn import Parameter + +from nerfstudio.cameras.cameras import Cameras, CameraType +from nerfstudio.cameras.rays import RayBundle +from nerfstudio.data.datamanagers.base_datamanager import ( + DataManager, + TDataset, + VanillaDataManagerConfig, + variable_res_collate, +) +from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs +from nerfstudio.data.pixel_samplers import PatchPixelSamplerConfig, PixelSampler, PixelSamplerConfig +from nerfstudio.data.utils.dataloaders import CacheDataloader, FixedIndicesEvalDataloader, RandIndicesEvalDataloader +from nerfstudio.model_components.ray_generators import RayGenerator +from nerfstudio.utils.rich_utils import CONSOLE + + +@dataclass +class ParallelDataManagerConfig(VanillaDataManagerConfig): + """Config for a `ParallelDataManager` which reads data in multiple processes""" + + _target: Type = field(default_factory=lambda: ParallelDataManager) + """Target class to instantiate.""" + num_processes: int = 1 + """Number of processes to use for train data loading. More than 1 doesn't result in that much better performance""" + queue_size: int = 2 + """Size of shared data queue containing generated ray bundles and batches. + If queue_size <= 0, the queue size is infinite.""" + max_thread_workers: Optional[int] = None + """Maximum number of threads to use in thread pool executor. If None, use ThreadPool default.""" + + +class DataProcessor(mp.Process): # type: ignore + """Parallel dataset batch processor. + + This class is responsible for generating ray bundles from an input dataset + in parallel python processes. + + Args: + out_queue: the output queue for storing the processed data + config: configuration object for the parallel data manager + dataparser_outputs: outputs from the dataparser + dataset: input dataset + pixel_sampler: The pixel sampler for sampling rays + """ + + def __init__( + self, + out_queue: mp.Queue, # type: ignore + config: ParallelDataManagerConfig, + dataparser_outputs: DataparserOutputs, + dataset: TDataset, + pixel_sampler: PixelSampler, + ): + super().__init__() + self.daemon = True + self.out_queue = out_queue + self.config = config + self.dataparser_outputs = dataparser_outputs + self.dataset = dataset + self.exclude_batch_keys_from_device = self.dataset.exclude_batch_keys_from_device + self.pixel_sampler = pixel_sampler + self.ray_generator = RayGenerator(self.dataset.cameras) + self.cache_images() + + def run(self): + """Append out queue in parallel with ray bundles and batches.""" + while True: + batch = self.pixel_sampler.sample(self.img_data) + ray_indices = batch["indices"] + ray_bundle: RayBundle = self.ray_generator(ray_indices) + # check that GPUs are available + if torch.cuda.is_available(): + ray_bundle = ray_bundle.pin_memory() + while True: + try: + self.out_queue.put((ray_bundle, batch)) + break + except queue.Full: + time.sleep(0.0001) + except Exception: + CONSOLE.print_exception() + CONSOLE.print("[bold red]Error: Error occured in parallel datamanager queue.") + + def cache_images(self): + """Caches all input images into a NxHxWx3 tensor.""" + indices = range(len(self.dataset)) + batch_list = [] + results = [] + with concurrent.futures.ThreadPoolExecutor(max_workers=self.config.max_thread_workers) as executor: + for idx in indices: + res = executor.submit(self.dataset.__getitem__, idx) + results.append(res) + for res in track(results, description="Loading data batch", transient=False): + batch_list.append(res.result()) + self.img_data = self.config.collate_fn(batch_list) + + +class ParallelDataManager(DataManager, Generic[TDataset]): + """Data manager implementation for parallel dataloading. + + Args: + config: the DataManagerConfig used to instantiate class + """ + + def __init__( + self, + config: ParallelDataManagerConfig, + device: Union[torch.device, str] = "cpu", + test_mode: Literal["test", "val", "inference"] = "val", + world_size: int = 1, + local_rank: int = 0, + **kwargs, + ): + self.dataset_type: Type[TDataset] = kwargs.get("_dataset_type", getattr(TDataset, "__default__")) + self.config = config + self.device = device + self.world_size = world_size + self.local_rank = local_rank + self.test_mode = test_mode + self.test_split = "test" if test_mode in ["test", "inference"] else "val" + self.dataparser_config = self.config.dataparser + if self.config.data is not None: + self.config.dataparser.data = Path(self.config.data) + else: + self.config.data = self.config.dataparser.data + self.dataparser = self.dataparser_config.setup() + if test_mode == "inference": + self.dataparser.downscale_factor = 1 # Avoid opening images + self.includes_time = self.dataparser.includes_time + self.train_dataparser_outputs: DataparserOutputs = self.dataparser.get_dataparser_outputs(split="train") + self.eval_dataparser_outputs: DataparserOutputs = self.dataparser.get_dataparser_outputs(split=self.test_split) + cameras = self.train_dataparser_outputs.cameras + if len(cameras) > 1: + for i in range(1, len(cameras)): + if cameras[0].width != cameras[i].width or cameras[0].height != cameras[i].height: + CONSOLE.print("Variable resolution, using variable_res_collate") + self.config.collate_fn = variable_res_collate + break + self.train_dataset = self.create_train_dataset() + self.eval_dataset = self.create_eval_dataset() + self.exclude_batch_keys_from_device = self.train_dataset.exclude_batch_keys_from_device + # Spawn is critical for not freezing the program (PyTorch compatability issue) + # check if spawn is already set + if mp.get_start_method(allow_none=True) is None: # type: ignore + mp.set_start_method("spawn") # type: ignore + super().__init__() + + def create_train_dataset(self) -> TDataset: + """Sets up the data loaders for training.""" + return self.dataset_type( + dataparser_outputs=self.train_dataparser_outputs, + scale_factor=self.config.camera_res_scale_factor, + ) + + def create_eval_dataset(self) -> TDataset: + """Sets up the data loaders for evaluation.""" + return self.dataset_type( + dataparser_outputs=self.dataparser.get_dataparser_outputs(split=self.test_split), + scale_factor=self.config.camera_res_scale_factor, + ) + + def _get_pixel_sampler(self, dataset: TDataset, num_rays_per_batch: int) -> PixelSampler: + """Infer pixel sampler to use.""" + if self.config.patch_size > 1 and type(self.config.pixel_sampler) is PixelSamplerConfig: + return PatchPixelSamplerConfig().setup( + patch_size=self.config.patch_size, num_rays_per_batch=num_rays_per_batch + ) + is_equirectangular = (dataset.cameras.camera_type == CameraType.EQUIRECTANGULAR.value).all() + if is_equirectangular.any(): + CONSOLE.print("[bold yellow]Warning: Some cameras are equirectangular, but using default pixel sampler.") + return self.config.pixel_sampler.setup( + is_equirectangular=is_equirectangular, num_rays_per_batch=num_rays_per_batch + ) + + def setup_train(self): + """Sets up parallel python data processes for training.""" + assert self.train_dataset is not None + self.train_pixel_sampler = self._get_pixel_sampler(self.train_dataset, self.config.train_num_rays_per_batch) # type: ignore + self.data_queue = mp.Queue(maxsize=self.config.queue_size) # type: ignore + self.data_procs = [ + DataProcessor( + out_queue=self.data_queue, # type: ignore + config=self.config, + dataparser_outputs=self.train_dataparser_outputs, + dataset=self.train_dataset, + pixel_sampler=self.train_pixel_sampler, + ) + for i in range(self.config.num_processes) + ] + for proc in self.data_procs: + proc.start() + print("Started threads") + + def setup_eval(self): + """Sets up the data loader for evaluation.""" + assert self.eval_dataset is not None + CONSOLE.print("Setting up evaluation dataset...") + self.eval_image_dataloader = CacheDataloader( + self.eval_dataset, + num_images_to_sample_from=self.config.eval_num_images_to_sample_from, + num_times_to_repeat_images=self.config.eval_num_times_to_repeat_images, + device=self.device, + num_workers=self.world_size * 4, + pin_memory=True, + collate_fn=self.config.collate_fn, + exclude_batch_keys_from_device=self.exclude_batch_keys_from_device, + ) + self.iter_eval_image_dataloader = iter(self.eval_image_dataloader) + self.eval_pixel_sampler = self._get_pixel_sampler(self.eval_dataset, self.config.eval_num_rays_per_batch) # type: ignore + self.eval_ray_generator = RayGenerator(self.eval_dataset.cameras.to(self.device)) + # for loading full images + self.fixed_indices_eval_dataloader = FixedIndicesEvalDataloader( + input_dataset=self.eval_dataset, + device=self.device, + num_workers=self.world_size * 4, + ) + self.eval_dataloader = RandIndicesEvalDataloader( + input_dataset=self.eval_dataset, + device=self.device, + num_workers=self.world_size * 4, + ) + + def next_train(self, step: int) -> Tuple[RayBundle, Dict]: + """Returns the next batch of data from the parallel training processes.""" + self.train_count += 1 + bundle, batch = self.data_queue.get() + ray_bundle = bundle.to(self.device) + return ray_bundle, batch + + def next_eval(self, step: int) -> Tuple[RayBundle, Dict]: + """Returns the next batch of data from the eval dataloader.""" + self.eval_count += 1 + image_batch = next(self.iter_eval_image_dataloader) + assert self.eval_pixel_sampler is not None + assert isinstance(image_batch, dict) + batch = self.eval_pixel_sampler.sample(image_batch) + ray_indices = batch["indices"] + ray_bundle = self.eval_ray_generator(ray_indices) + return ray_bundle, batch + + def next_eval_image(self, step: int) -> Tuple[Cameras, Dict]: + """Retrieve the next eval image.""" + for camera, batch in self.eval_dataloader: + assert camera.shape[0] == 1 + return camera, batch + raise ValueError("No more eval images") + + def get_train_rays_per_batch(self) -> int: + """Returns the number of rays per batch for training.""" + if self.train_pixel_sampler is not None: + return self.train_pixel_sampler.num_rays_per_batch + return self.config.train_num_rays_per_batch + + def get_eval_rays_per_batch(self) -> int: + """Returns the number of rays per batch for evaluation.""" + if self.eval_pixel_sampler is not None: + return self.eval_pixel_sampler.num_rays_per_batch + return self.config.eval_num_rays_per_batch + + def get_datapath(self) -> Path: + """Returns the path to the data. This is used to determine where to save camera paths.""" + return self.config.dataparser.data + + def get_param_groups(self) -> Dict[str, List[Parameter]]: + """Get the param groups for the data manager. + Returns: + A list of dictionaries containing the data manager's param groups. + """ + return {} + + def __del__(self): + """Clean up the parallel data processes.""" + if hasattr(self, "data_procs"): + for proc in self.data_procs: + proc.terminate() + proc.join() diff --git a/nerfstudio/data/datamanagers/random_cameras_datamanager.py b/nerfstudio/data/datamanagers/random_cameras_datamanager.py index 4269b428a..7704f2f16 100644 --- a/nerfstudio/data/datamanagers/random_cameras_datamanager.py +++ b/nerfstudio/data/datamanagers/random_cameras_datamanager.py @@ -290,11 +290,10 @@ class RandomCamerasDataManager(DataManager): # pylint: disable=abstract-method return ray_bundle, {"vertical": vertical_rotation, "central": central_rotation} - def next_eval_image(self, step: int) -> Tuple[int, RayBundle, Dict]: - for camera_ray_bundle, batch in self.eval_dataloader: - assert camera_ray_bundle.camera_indices is not None - image_idx = int(camera_ray_bundle.camera_indices[0, 0, 0]) - return image_idx, camera_ray_bundle, batch + def next_eval_image(self, step: int) -> Tuple[Cameras, Dict]: + for camera, batch in self.eval_dataloader: + assert camera.shape[0] == 1 + return camera, batch raise ValueError("No more eval images") def get_train_rays_per_batch(self) -> int: diff --git a/nerfstudio/data/dataparsers/base_dataparser.py b/nerfstudio/data/dataparsers/base_dataparser.py index 80fab739f..5cf1e6bdb 100644 --- a/nerfstudio/data/dataparsers/base_dataparser.py +++ b/nerfstudio/data/dataparsers/base_dataparser.py @@ -57,7 +57,7 @@ class DataparserOutputs: """Camera object storing collection of camera information in dataset.""" alpha_color: Optional[Float[Tensor, "3"]] = None """Color of dataset background.""" - scene_box: SceneBox = SceneBox(aabb=torch.tensor([[-1, -1, -1], [1, 1, 1]])) + scene_box: SceneBox = field(default_factory=lambda: SceneBox(aabb=torch.tensor([[-1, -1, -1], [1, 1, 1]]))) """Scene box of dataset. Used to bound the scene or provide the scene scale depending on model.""" mask_filenames: Optional[List[Path]] = None """Filenames for any masks that are required""" diff --git a/nerfstudio/data/dataparsers/colmap_dataparser.py b/nerfstudio/data/dataparsers/colmap_dataparser.py index 071a11e1e..c478a318f 100644 --- a/nerfstudio/data/dataparsers/colmap_dataparser.py +++ b/nerfstudio/data/dataparsers/colmap_dataparser.py @@ -15,7 +15,6 @@ from __future__ import annotations -import math import sys from dataclasses import dataclass, field from pathlib import Path @@ -35,6 +34,12 @@ from nerfstudio.data.utils import colmap_parsing_utils as colmap_utils from nerfstudio.process_data.colmap_utils import parse_colmap_camera_params from nerfstudio.utils.scripts import run_command from nerfstudio.utils.rich_utils import CONSOLE, status +from nerfstudio.data.utils.dataparsers_utils import ( + get_train_eval_split_filename, + get_train_eval_split_fraction, + get_train_eval_split_interval, + get_train_eval_split_all, +) MAX_AUTO_RESOLUTION = 1600 @@ -59,8 +64,18 @@ class ColmapDataParserConfig(DataParserConfig): """The method to use to center the poses.""" auto_scale_poses: bool = True """Whether to automatically scale the poses to fit in +/- 1 bounding box.""" + eval_mode: Literal["fraction", "filename", "interval", "all"] = "interval" + """ + The method to use for splitting the dataset into train and eval. + Fraction splits based on a percentage for train and the remaining for eval. + Filename splits based on filenames containing train/eval. + Interval uses every nth frame for eval (used by most academic papers, e.g. MipNerf360, GSplat). + All uses all the images for any split. + """ train_split_fraction: float = 0.9 """The fraction of images to use for training. The remaining images are for eval.""" + eval_interval: int = 8 + """The interval between frames to use for eval. Only used when eval_mode is eval-interval.""" depth_unit_scale_factor: float = 1e-3 """Scales the depth values to meters. Default value is 0.001 for a millimeter to meter conversion.""" @@ -70,11 +85,11 @@ class ColmapDataParserConfig(DataParserConfig): """Path to masks directory. If not set, masks are not loaded.""" depths_path: Optional[Path] = None """Path to depth maps directory. If not set, depths are not loaded.""" - colmap_path: Path = Path("sparse/0") + colmap_path: Path = Path("colmap/sparse/0") """Path to the colmap reconstruction directory relative to the data path.""" load_3D_points: bool = False """Whether to load the 3D points from the colmap reconstruction.""" - max_2D_matches_per_3D_point: int = -1 + max_2D_matches_per_3D_point: int = 0 """Maximum number of 2D matches per 3D point. If set to -1, all 2D matches are loaded. If set to 0, no 2D matches are loaded.""" @@ -124,7 +139,10 @@ class ColmapDataParser(DataParser): cameras[cam_id] = parse_colmap_camera_params(cam_data) # Parse frames - for im_id, im_data in im_id_to_image.items(): + # we want to sort all images based on im_id + ordered_im_id = sorted(im_id_to_image.keys()) + for im_id in ordered_im_id: + im_data = im_id_to_image[im_id] # NB: COLMAP uses Eigen / scalar-first quaternions # * https://colmap.github.io/format.html # * https://github.com/colmap/colmap/blob/bf3e19140f491c3042bfd85b7192ef7d249808ec/src/base/pose.cc#L75 @@ -136,6 +154,8 @@ class ColmapDataParser(DataParser): c2w = np.linalg.inv(w2c) # Convert from COLMAP's camera coordinate system (OpenCV) to ours (OpenGL) c2w[0:3, 1:3] *= -1 + # Why do we want to flip Z with a handedness transform? + # See https://github.com/nerfstudio-project/nerfstudio/issues/1504 c2w = c2w[np.array([1, 0, 2, 3]), :] c2w[2, :] *= -1 @@ -161,6 +181,8 @@ class ColmapDataParser(DataParser): out = {} out["frames"] = frames + # Why do we want to flip Z with a handedness transform? + # See https://github.com/nerfstudio-project/nerfstudio/issues/1504 applied_transform = np.eye(4)[:3, :] applied_transform = applied_transform[np.array([1, 0, 2]), :] applied_transform[2, :] *= -1 @@ -193,16 +215,21 @@ class ColmapDataParser(DataParser): elif has_split_files_spec: raise RuntimeError(f"The dataset's list of filenames for split {split} is missing.") else: - # filter image_filenames and poses based on train/eval split percentage - num_images = len(image_filenames) - num_train_images = math.ceil(num_images * self.config.train_split_fraction) - num_eval_images = num_images - num_train_images - i_all = np.arange(num_images) - i_train = np.linspace( - 0, num_images - 1, num_train_images, dtype=int - ) # equally spaced training images starting and ending at 0 and num_images-1 - i_eval = np.setdiff1d(i_all, i_train) # eval images are the remaining images - assert len(i_eval) == num_eval_images + # find train and eval indices based on the eval_mode specified + if self.config.eval_mode == "fraction": + i_train, i_eval = get_train_eval_split_fraction(image_filenames, self.config.train_split_fraction) + elif self.config.eval_mode == "filename": + i_train, i_eval = get_train_eval_split_filename(image_filenames) + elif self.config.eval_mode == "interval": + i_train, i_eval = get_train_eval_split_interval(image_filenames, self.config.eval_interval) + elif self.config.eval_mode == "all": + CONSOLE.log( + "[yellow] Be careful with '--eval-mode=all'. If using camera optimization, the cameras may diverge in the current implementation, giving unpredictable results." + ) + i_train, i_eval = get_train_eval_split_all(image_filenames) + else: + raise ValueError(f"Unknown eval mode {self.config.eval_mode}") + if split == "train": indices = i_train elif split in ["val", "test"]: diff --git a/nerfstudio/data/dataparsers/dycheck_dataparser.py b/nerfstudio/data/dataparsers/dycheck_dataparser.py index 50a581f28..9af4f5800 100644 --- a/nerfstudio/data/dataparsers/dycheck_dataparser.py +++ b/nerfstudio/data/dataparsers/dycheck_dataparser.py @@ -173,7 +173,10 @@ def _rescale_depth(depth_raw: np.ndarray, cam: Dict) -> np.ndarray: viewdirs /= np.linalg.norm(viewdirs, axis=-1, keepdims=True) viewdirs = viewdirs.reshape((*batch_shape, 3)) cosa = viewdirs @ (cam["camera_to_worlds"][:, 2]) - depth = depth_raw / cosa[..., None] + if depth_raw.ndim == cosa.ndim: + depth = depth_raw[..., None] / cosa[..., None] + else: + depth = depth_raw / cosa[..., None] return depth @@ -322,7 +325,7 @@ class Dycheck(DataParser): for frame in frame_names: cv2.imwrite( str(self.data / f"rgb/{d}x/{frame}.png"), - cv2.resize(cv2.imread(str(self.data / f"rgb/1x/{frame}.png")), (h, w)), + cv2.resize(cv2.imread(str(self.data / f"rgb/1x/{frame}.png")), (w, h)), ) CONSOLE.print("finished") diff --git a/nerfstudio/data/dataparsers/nerfstudio_dataparser.py b/nerfstudio/data/dataparsers/nerfstudio_dataparser.py index eaeda0bce..7f6f21b88 100644 --- a/nerfstudio/data/dataparsers/nerfstudio_dataparser.py +++ b/nerfstudio/data/dataparsers/nerfstudio_dataparser.py @@ -20,6 +20,7 @@ from pathlib import Path from typing import Literal, Optional, Type import numpy as np +import open3d as o3d import torch from PIL import Image @@ -104,10 +105,11 @@ class Nerfstudio(DataParser): height_fixed = "h" in meta width_fixed = "w" in meta distort_fixed = False - for distort_key in ["k1", "k2", "k3", "p1", "p2"]: + for distort_key in ["k1", "k2", "k3", "p1", "p2", "distortion_params"]: if distort_key in meta: distort_fixed = True break + fisheye_crop_radius = meta.get("fisheye_crop_radius", None) fx = [] fy = [] cx = [] @@ -149,7 +151,9 @@ class Nerfstudio(DataParser): width.append(int(frame["w"])) if not distort_fixed: distort.append( - camera_utils.get_distortion_params( + torch.tensor(frame["distortion_params"], dtype=torch.float32) + if "distortion_params" in frame + else camera_utils.get_distortion_params( k1=float(frame["k1"]) if "k1" in frame else 0.0, k2=float(frame["k2"]) if "k2" in frame else 0.0, k3=float(frame["k3"]) if "k3" in frame else 0.0, @@ -274,17 +278,22 @@ class Nerfstudio(DataParser): height = int(meta["h"]) if height_fixed else torch.tensor(height, dtype=torch.int32)[idx_tensor] width = int(meta["w"]) if width_fixed else torch.tensor(width, dtype=torch.int32)[idx_tensor] if distort_fixed: - distortion_params = camera_utils.get_distortion_params( - k1=float(meta["k1"]) if "k1" in meta else 0.0, - k2=float(meta["k2"]) if "k2" in meta else 0.0, - k3=float(meta["k3"]) if "k3" in meta else 0.0, - k4=float(meta["k4"]) if "k4" in meta else 0.0, - p1=float(meta["p1"]) if "p1" in meta else 0.0, - p2=float(meta["p2"]) if "p2" in meta else 0.0, + distortion_params = ( + torch.tensor(meta["distortion_params"], dtype=torch.float32) + if "distortion_params" in meta + else camera_utils.get_distortion_params( + k1=float(meta["k1"]) if "k1" in meta else 0.0, + k2=float(meta["k2"]) if "k2" in meta else 0.0, + k3=float(meta["k3"]) if "k3" in meta else 0.0, + k4=float(meta["k4"]) if "k4" in meta else 0.0, + p1=float(meta["p1"]) if "p1" in meta else 0.0, + p2=float(meta["p2"]) if "p2" in meta else 0.0, + ) ) else: distortion_params = torch.stack(distort, dim=0)[idx_tensor] + metadata = {"fisheye_crop_radius": fisheye_crop_radius} if fisheye_crop_radius is not None else None cameras = Cameras( fx=fx, fy=fy, @@ -295,6 +304,7 @@ class Nerfstudio(DataParser): width=width, camera_to_worlds=poses[:, :3, :4], camera_type=camera_type, + metadata=metadata, ) assert self.downscale_factor is not None @@ -309,6 +319,12 @@ class Nerfstudio(DataParser): applied_scale = float(meta["applied_scale"]) scale_factor *= applied_scale + # Load 3D points + metadata = {} + if "ply_file_path" in meta: + ply_file_path = data_dir / meta["ply_file_path"] + metadata.update(self._load_3D_points(ply_file_path, transform_matrix, scale_factor)) + dataparser_outputs = DataparserOutputs( image_filenames=image_filenames, cameras=cameras, @@ -319,10 +335,34 @@ class Nerfstudio(DataParser): metadata={ "depth_filenames": depth_filenames if len(depth_filenames) > 0 else None, "depth_unit_scale_factor": self.config.depth_unit_scale_factor, + **metadata, }, ) return dataparser_outputs + def _load_3D_points(self, ply_file_path: Path, transform_matrix: torch.Tensor, scale_factor: float): + pcd = o3d.io.read_point_cloud(str(ply_file_path)) + + points3D = torch.from_numpy(np.asarray(pcd.points, dtype=np.float32)) + points3D = ( + torch.cat( + ( + points3D, + torch.ones_like(points3D[..., :1]), + ), + -1, + ) + @ transform_matrix.T + ) + points3D *= scale_factor + points3D_rgb = torch.from_numpy((np.asarray(pcd.colors) * 255).astype(np.uint8)) + + out = { + "points3D_xyz": points3D, + "points3D_rgb": points3D_rgb, + } + return out + def _get_fname(self, filepath: Path, data_dir: Path, downsample_folder_prefix="images_") -> Path: """Get the filename of the image file. downsample_folder_prefix can be used to point to auxiliary image data, e.g. masks diff --git a/nerfstudio/data/dataparsers/nuscenes_dataparser.py b/nerfstudio/data/dataparsers/nuscenes_dataparser.py index 317717e86..0e899a68c 100644 --- a/nerfstudio/data/dataparsers/nuscenes_dataparser.py +++ b/nerfstudio/data/dataparsers/nuscenes_dataparser.py @@ -200,10 +200,10 @@ class NuScenes(DataParser): ) cameras = Cameras( - fx=intrinsics[:, 0, 0], - fy=intrinsics[:, 1, 1], - cx=intrinsics[:, 0, 2], - cy=intrinsics[:, 1, 2], + fx=intrinsics[:, 0, 0].detach().clone(), + fy=intrinsics[:, 1, 1].detach().clone(), + cx=intrinsics[:, 0, 2].detach().clone(), + cy=intrinsics[:, 1, 2].detach().clone(), height=900, width=1600, camera_to_worlds=poses[:, :3, :4], diff --git a/nerfstudio/data/dataparsers/scannetpp_dataparser.py b/nerfstudio/data/dataparsers/scannetpp_dataparser.py new file mode 100644 index 000000000..bbd260730 --- /dev/null +++ b/nerfstudio/data/dataparsers/scannetpp_dataparser.py @@ -0,0 +1,207 @@ +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" Data parser for ScanNet++ datasets. """ + +from __future__ import annotations + +from dataclasses import dataclass, field +from pathlib import Path +from typing import Literal, Type + +import numpy as np +import torch +from nerfstudio.cameras import camera_utils +from nerfstudio.cameras.cameras import CAMERA_MODEL_TO_TYPE, Cameras, CameraType +from nerfstudio.data.dataparsers.base_dataparser import DataParser, DataParserConfig, DataparserOutputs +from nerfstudio.data.scene_box import SceneBox +from nerfstudio.utils.io import load_from_json +from nerfstudio.utils.rich_utils import CONSOLE + + +@dataclass +class ScanNetppDataParserConfig(DataParserConfig): + """ScanNet++ dataset config. + ScanNet++ dataset (https://kaldir.vc.in.tum.de/scannetpp/) is a real-world 3D indoor dataset for semantics understanding and novel view synthesis. + This dataparser follow the file structure of the dataset. + Expected structure of the directory: + + .. code-block:: text + + root/ + β”œβ”€β”€ SCENE_ID0 + β”œβ”€β”€ dslr + β”œβ”€β”€ resized_images + β”œβ”€β”€ resized_anon_masks + β”œβ”€β”€ nerfstudio/transforms.json + β”œβ”€β”€ SCENE_ID1/ + ... + """ + + _target: Type = field(default_factory=lambda: ScanNetpp) + """target class to instantiate""" + data: Path = Path("scannetpp/410c470782") + """Directory to the root of the data.""" + scale_factor: float = 1.0 + """How much to scale the camera origins by.""" + scene_scale: float = 1.5 + """How much to scale the region of interest by. Default is 1.5 since the cameras are inside the rooms.""" + orientation_method: Literal["pca", "up", "vertical", "none"] = "up" + """The method to use for orientation.""" + center_method: Literal["poses", "focus", "none"] = "poses" + """The method to use to center the poses.""" + auto_scale_poses: bool = True + """Whether to automatically scale the poses to fit in +/- 1 bounding box.""" + images_dir: Path = Path("dslr/resized_images") + """Relative path to the images directory (default: resized_images)""" + masks_dir: Path = Path("dslr/resized_anon_masks") + """Relative path to the masks directory (default: resized_anon_masks)""" + transforms_path: Path = Path("dslr/nerfstudio/transforms.json") + """Relative path to the transforms.json file""" + + +@dataclass +class ScanNetpp(DataParser): + """ScanNet++ DatasetParser""" + + config: ScanNetppDataParserConfig + + def _generate_dataparser_outputs(self, split="train"): + assert self.config.data.exists(), f"Data directory {self.config.data} does not exist." + meta = load_from_json(self.config.data / self.config.transforms_path) + data_dir = self.config.data / self.config.images_dir + mask_dir = self.config.data / self.config.masks_dir + + image_filenames = [] + mask_filenames = [] + poses = [] + i_train = [] + i_eval = [] + # sort the frames by fname + frames = meta["frames"] + meta["test_frames"] + test_frames = [f["file_path"] for f in meta["test_frames"]] + frames.sort(key=lambda x: x["file_path"]) + + for idx, frame in enumerate(frames): + filepath = Path(frame["file_path"]) + fname = data_dir / filepath + + image_filenames.append(fname) + poses.append(np.array(frame["transform_matrix"])) + if meta.get("has_mask", True) and "mask_path" in frame: + mask_filepath = Path(frame["mask_path"]) + mask_fname = mask_dir / mask_filepath + mask_filenames.append(mask_fname) + + if frame["file_path"] in test_frames: + i_eval.append(idx) + else: + i_train.append(idx) + + assert len(mask_filenames) == 0 or ( + len(mask_filenames) == len(image_filenames) + ), """ + Different number of image and mask filenames. + You should check that mask_path is specified for every frame (or zero frames) in transforms.json. + """ + + if split == "train": + indices = i_train + elif split in ["val", "test"]: + indices = i_eval + else: + raise ValueError(f"Unknown dataparser split {split}") + + if "orientation_override" in meta: + orientation_method = meta["orientation_override"] + CONSOLE.log(f"[yellow] Dataset is overriding orientation method to {orientation_method}") + else: + orientation_method = self.config.orientation_method + + poses = torch.from_numpy(np.array(poses).astype(np.float32)) + poses, transform_matrix = camera_utils.auto_orient_and_center_poses( + poses, + method=orientation_method, + center_method=self.config.center_method, + ) + + # Scale poses + scale_factor = 1.0 + if self.config.auto_scale_poses: + scale_factor /= float(torch.max(torch.abs(poses[:, :3, 3]))) + scale_factor *= self.config.scale_factor + + poses[:, :3, 3] *= scale_factor + + # Choose image_filenames and poses based on split, but after auto orient and scaling the poses. + image_filenames = [image_filenames[i] for i in indices] + mask_filenames = [mask_filenames[i] for i in indices] if len(mask_filenames) > 0 else [] + + idx_tensor = torch.tensor(indices, dtype=torch.long) + poses = poses[idx_tensor] + + # in x,y,z order + # assumes that the scene is centered at the origin + if not self.config.auto_scale_poses: + # Set aabb_scale to scene_scale * the max of the absolute values of the poses + aabb_scale = self.config.scene_scale * float(torch.max(torch.abs(poses[:, :3, 3]))) + else: + aabb_scale = self.config.scene_scale + scene_box = SceneBox( + aabb=torch.tensor( + [[-aabb_scale, -aabb_scale, -aabb_scale], [aabb_scale, aabb_scale, aabb_scale]], dtype=torch.float32 + ) + ) + + if "camera_model" in meta: + camera_type = CAMERA_MODEL_TO_TYPE[meta["camera_model"]] + else: + camera_type = CameraType.PERSPECTIVE + + fx = float(meta["fl_x"]) + fy = float(meta["fl_y"]) + cx = float(meta["cx"]) + cy = float(meta["cy"]) + height = int(meta["h"]) + width = int(meta["w"]) + distortion_params = camera_utils.get_distortion_params( + k1=float(meta["k1"]) if "k1" in meta else 0.0, + k2=float(meta["k2"]) if "k2" in meta else 0.0, + k3=float(meta["k3"]) if "k3" in meta else 0.0, + k4=float(meta["k4"]) if "k4" in meta else 0.0, + p1=float(meta["p1"]) if "p1" in meta else 0.0, + p2=float(meta["p2"]) if "p2" in meta else 0.0, + ) + + cameras = Cameras( + fx=fx, + fy=fy, + cx=cx, + cy=cy, + distortion_params=distortion_params, + height=height, + width=width, + camera_to_worlds=poses[:, :3, :4], + camera_type=camera_type, + ) + + dataparser_outputs = DataparserOutputs( + image_filenames=image_filenames, + cameras=cameras, + scene_box=scene_box, + mask_filenames=mask_filenames if len(mask_filenames) > 0 else None, + dataparser_scale=scale_factor, + dataparser_transform=transform_matrix, + metadata={}, + ) + return dataparser_outputs diff --git a/nerfstudio/data/pixel_samplers.py b/nerfstudio/data/pixel_samplers.py index 463d08294..c7ce9e073 100644 --- a/nerfstudio/data/pixel_samplers.py +++ b/nerfstudio/data/pixel_samplers.py @@ -17,13 +17,7 @@ Code for sampling pixels. """ import random - -import torch -from jaxtyping import Int -from torch import Tensor - from dataclasses import dataclass, field -from nerfstudio.data.utils.pixel_sampling_utils import erode_mask from typing import ( Dict, Optional, @@ -31,9 +25,14 @@ from typing import ( Union, ) +import torch +from jaxtyping import Int +from torch import Tensor + from nerfstudio.configs.base_config import ( InstantiateConfig, ) +from nerfstudio.data.utils.pixel_sampling_utils import erode_mask @dataclass @@ -48,6 +47,8 @@ class PixelSamplerConfig(InstantiateConfig): """Whether or not to include a reference to the full image in returned batch.""" is_equirectangular: bool = False """List of whether or not camera i is equirectangular.""" + fisheye_crop_radius: Optional[float] = None + """Set to the radius (in pixels) for fisheye cameras.""" class PixelSampler: @@ -66,6 +67,7 @@ class PixelSampler: self.config.num_rays_per_batch = self.kwargs.get("num_rays_per_batch", self.config.num_rays_per_batch) self.config.keep_full_image = self.kwargs.get("keep_full_image", self.config.keep_full_image) self.config.is_equirectangular = self.kwargs.get("is_equirectangular", self.config.is_equirectangular) + self.config.fisheye_crop_radius = self.kwargs.get("fisheye_crop_radius", self.config.fisheye_crop_radius) self.set_num_rays_per_batch(self.config.num_rays_per_batch) def set_num_rays_per_batch(self, num_rays_per_batch: int): @@ -98,7 +100,7 @@ class PixelSampler: chosen_indices = random.sample(range(len(nonzero_indices)), k=batch_size) indices = nonzero_indices[chosen_indices] else: - indices = torch.floor( + indices = ( torch.rand((batch_size, 3), device=device) * torch.tensor([num_images, image_height, image_width], device=device) ).long() @@ -135,6 +137,36 @@ class PixelSampler: return indices + def sample_method_fisheye( + self, + batch_size: int, + num_images: int, + image_height: int, + image_width: int, + mask: Optional[Tensor] = None, + device: Union[torch.device, str] = "cpu", + ) -> Int[Tensor, "batch_size 3"]: + if isinstance(mask, torch.Tensor): + indices = self.sample_method(batch_size, num_images, image_height, image_width, mask=mask, device=device) + else: + rand_samples = torch.rand((batch_size, 3), device=device) + # convert random samples tto radius and theta + radii = self.config.fisheye_crop_radius * torch.sqrt(rand_samples[:, 1]) + theta = 2.0 * torch.pi * rand_samples[:, 2] + + # convert radius and theta to x and y between -radii and radii + x = radii * torch.cos(theta) + y = radii * torch.sin(theta) + + # Multiply by the batch size and height/width to get pixel indices. + indices = torch.floor( + torch.stack([rand_samples[:, 0], y, x], dim=1) + * torch.tensor([num_images, image_height // 2, image_width // 2], device=device) + + torch.tensor([0, image_height // 2, image_width // 2], device=device) + ).long() + + return indices + def collate_image_dataset_batch(self, batch: Dict, num_rays_per_batch: int, keep_full_image: bool = False): """ Operates on a batch of images and samples pixels to use for generating rays. @@ -155,6 +187,10 @@ class PixelSampler: indices = self.sample_method_equirectangular( num_rays_per_batch, num_images, image_height, image_width, mask=batch["mask"], device=device ) + elif self.config.fisheye_crop_radius is not None: + indices = self.sample_method_fisheye( + num_rays_per_batch, num_images, image_height, image_width, mask=batch["mask"], device=device + ) else: indices = self.sample_method( num_rays_per_batch, num_images, image_height, image_width, mask=batch["mask"], device=device @@ -164,6 +200,10 @@ class PixelSampler: indices = self.sample_method_equirectangular( num_rays_per_batch, num_images, image_height, image_width, device=device ) + elif self.config.fisheye_crop_radius is not None: + indices = self.sample_method_fisheye( + num_rays_per_batch, num_images, image_height, image_width, device=device + ) else: indices = self.sample_method(num_rays_per_batch, num_images, image_height, image_width, device=device) @@ -172,13 +212,11 @@ class PixelSampler: collated_batch = { key: value[c, y, x] for key, value in batch.items() if key != "image_idx" and value is not None } - assert collated_batch["image"].shape[0] == num_rays_per_batch # Needed to correct the random indices to their actual camera idx locations. indices[:, 0] = batch["image_idx"][c] collated_batch["indices"] = indices # with the abs camera indices - if keep_full_image: collated_batch["full_image"] = batch["image"] @@ -399,31 +437,31 @@ class PairPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods mask: Optional[Tensor] = None, device: Union[torch.device, str] = "cpu", ) -> Int[Tensor, "batch_size 3"]: + rays_to_sample = self.rays_to_sample + if batch_size is not None: + assert ( + int(batch_size) % 2 == 0 + ), f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})" + rays_to_sample = batch_size // 2 + if isinstance(mask, Tensor): m = erode_mask(mask.permute(0, 3, 1, 2).float(), pixel_radius=self.radius) nonzero_indices = torch.nonzero(m[:, 0], as_tuple=False).to(device) - chosen_indices = random.sample(range(len(nonzero_indices)), k=self.rays_to_sample) + chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample) indices = nonzero_indices[chosen_indices] else: - rays_to_sample = self.rays_to_sample - if batch_size is not None: - assert ( - int(batch_size) % 2 == 0 - ), f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})" - rays_to_sample = batch_size // 2 - s = (rays_to_sample, 1) ns = torch.randint(0, num_images, s, dtype=torch.long, device=device) hs = torch.randint(self.radius, image_height - self.radius, s, dtype=torch.long, device=device) ws = torch.randint(self.radius, image_width - self.radius, s, dtype=torch.long, device=device) indices = torch.concat((ns, hs, ws), dim=1) - pair_indices = torch.hstack( - ( - torch.zeros(rays_to_sample, 1, device=device, dtype=torch.long), - torch.randint(-self.radius, self.radius, (rays_to_sample, 2), device=device, dtype=torch.long), - ) + pair_indices = torch.hstack( + ( + torch.zeros(rays_to_sample, 1, device=device, dtype=torch.long), + torch.randint(-self.radius, self.radius, (rays_to_sample, 2), device=device, dtype=torch.long), ) - pair_indices += indices - indices = torch.hstack((indices, pair_indices)).view(rays_to_sample * 2, 3) + ) + pair_indices += indices + indices = torch.hstack((indices, pair_indices)).view(rays_to_sample * 2, 3) return indices diff --git a/nerfstudio/data/utils/dataloaders.py b/nerfstudio/data/utils/dataloaders.py index e546a130b..6a64ba738 100644 --- a/nerfstudio/data/utils/dataloaders.py +++ b/nerfstudio/data/utils/dataloaders.py @@ -175,13 +175,17 @@ class EvalDataloader(DataLoader): def __next__(self) -> Tuple[RayBundle, Dict]: """Returns the next batch of data""" - def get_camera(self, image_idx: int = 0) -> Cameras: + def get_camera(self, image_idx: int = 0) -> Tuple[Cameras, Dict]: """Get camera for the given image index Args: image_idx: Camera image index """ - return self.cameras[image_idx] + camera = self.cameras[image_idx : image_idx + 1] + batch = self.input_dataset[image_idx] + batch = get_dict_to_torch(batch, device=self.device, exclude=["image"]) + assert isinstance(batch, dict) + return camera, batch def get_data_from_image_idx(self, image_idx: int) -> Tuple[RayBundle, Dict]: """Returns the data for a specific image index. @@ -226,9 +230,9 @@ class FixedIndicesEvalDataloader(EvalDataloader): def __next__(self): if self.count < len(self.image_indices): image_idx = self.image_indices[self.count] - ray_bundle, batch = self.get_data_from_image_idx(image_idx) + camera, batch = self.get_camera(image_idx) self.count += 1 - return ray_bundle, batch + return camera, batch raise StopIteration @@ -245,5 +249,5 @@ class RandIndicesEvalDataloader(EvalDataloader): def __next__(self): # choose a random image index image_idx = random.randint(0, len(self.cameras) - 1) - ray_bundle, batch = self.get_data_from_image_idx(image_idx) - return ray_bundle, batch + camera, batch = self.get_camera(image_idx) + return camera, batch diff --git a/nerfstudio/data/utils/nerfstudio_collate.py b/nerfstudio/data/utils/nerfstudio_collate.py index 8d61c695e..c259c4658 100644 --- a/nerfstudio/data/utils/nerfstudio_collate.py +++ b/nerfstudio/data/utils/nerfstudio_collate.py @@ -102,7 +102,7 @@ def nerfstudio_collate(batch: Any, extra_mappings: Union[Dict[type, Callable], N out = elem.new(storage).resize_(len(batch), *list(elem.size())) return torch.stack(batch, 0, out=out) elif elem_type.__module__ == "numpy" and elem_type.__name__ != "str_" and elem_type.__name__ != "string_": - if elem_type.__name__ == "ndarray" or elem_type.__name__ == "memmap": + if elem_type.__name__ in ("ndarray", "memmap"): # array of string classes and object if np_str_obj_array_pattern.search(elem.dtype.str) is not None: raise TypeError(NERFSTUDIO_COLLATE_ERR_MSG_FORMAT.format(elem.dtype)) diff --git a/nerfstudio/engine/optimizers.py b/nerfstudio/engine/optimizers.py index b80b77bcb..0a8794786 100644 --- a/nerfstudio/engine/optimizers.py +++ b/nerfstudio/engine/optimizers.py @@ -84,6 +84,25 @@ class Optimizers: self.schedulers = {} self.parameters = {} for param_group_name, params in param_groups.items(): + # For deprecation, catch the camera_opt param group and fix it nicely + if param_group_name == "camera_opt" and "camera_opt" not in config: + from nerfstudio.engine.schedulers import ExponentialDecaySchedulerConfig + from nerfstudio.utils.rich_utils import CONSOLE + + CONSOLE.print( + "\nThe 'camera_opt' param group should be assigned an optimizer in the config. Assigning default optimizers for now. This will be removed in a future release.\n", + style="bold yellow", + ) + + config["camera_opt"] = { + "optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15), + "scheduler": ExponentialDecaySchedulerConfig(lr_final=1e-4, max_steps=30000), + } + # Print some nice warning messages if the user forgot to specify an optimizer + if param_group_name not in config: + raise RuntimeError( + f"""Optimizer config for '{param_group_name}' not found in config file. Make sure you specify an optimizer for each parameter group. Provided configs were: {config.keys()}""" + ) lr_init = config[param_group_name]["optimizer"].lr self.optimizers[param_group_name] = config[param_group_name]["optimizer"].setup(params=params) self.parameters[param_group_name] = params @@ -116,6 +135,12 @@ class Optimizers: for _, optimizer in self.optimizers.items(): optimizer.zero_grad() + def zero_grad_some(self, param_groups: List[str]) -> None: + """Zero the gradients for the given parameter groups.""" + for param_group in param_groups: + optimizer = self.optimizers[param_group] + optimizer.zero_grad() + def optimizer_scaler_step_all(self, grad_scaler: GradScaler) -> None: """Take an optimizer step using a grad scaler. @@ -130,6 +155,21 @@ class Optimizers: if any(any(p.grad is not None for p in g["params"]) for g in optimizer.param_groups): grad_scaler.step(optimizer) + def optimizer_scaler_step_some(self, grad_scaler: GradScaler, param_groups: List[str]) -> None: + """Take an optimizer step using a grad scaler ONLY on the specified param groups. + + Args: + grad_scaler: GradScaler to use + """ + for param_group in param_groups: + optimizer = self.optimizers[param_group] + max_norm = self.config[param_group]["optimizer"].max_norm + if max_norm is not None: + grad_scaler.unscale_(optimizer) + torch.nn.utils.clip_grad_norm_(self.parameters[param_group], max_norm) + if any(any(p.grad is not None for p in g["params"]) for g in optimizer.param_groups): + grad_scaler.step(optimizer) + def optimizer_step_all(self) -> None: """Run step for all optimizers.""" for param_group, optimizer in self.optimizers.items(): diff --git a/nerfstudio/engine/trainer.py b/nerfstudio/engine/trainer.py index b4ccba9ee..b51f5b556 100644 --- a/nerfstudio/engine/trainer.py +++ b/nerfstudio/engine/trainer.py @@ -24,11 +24,10 @@ import time from dataclasses import dataclass, field from pathlib import Path from threading import Lock -from typing import Dict, List, Literal, Optional, Tuple, Type, cast - +from typing import Dict, List, Literal, Optional, Tuple, Type, cast, DefaultDict +from collections import defaultdict import torch from nerfstudio.configs.experiment_config import ExperimentConfig -from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes, TrainingCallbackLocation from nerfstudio.engine.optimizers import Optimizers from nerfstudio.pipelines.base_pipeline import VanillaPipeline @@ -37,8 +36,8 @@ from nerfstudio.utils.decorators import check_eval_enabled, check_main_thread, c from nerfstudio.utils.misc import step_check from nerfstudio.utils.rich_utils import CONSOLE from nerfstudio.utils.writer import EventName, TimeWriter -from nerfstudio.viewer.server.viewer_state import ViewerState -from nerfstudio.viewer_beta.viewer import Viewer as ViewerBetaState +from nerfstudio.viewer_legacy.server.viewer_state import ViewerLegacyState +from nerfstudio.viewer.viewer import Viewer as ViewerState from rich import box, style from rich.panel import Panel from rich.table import Table @@ -81,8 +80,8 @@ class TrainerConfig(ExperimentConfig): """Path to checkpoint file.""" log_gradients: bool = False """Optionally log gradients during training""" - gradient_accumulation_steps: int = 1 - """Number of steps to accumulate gradients over.""" + gradient_accumulation_steps: Dict = field(default_factory=lambda: {}) + """Number of steps to accumulate gradients over. Contains a mapping of {param_group:num}""" class Trainer: @@ -119,7 +118,8 @@ class Trainer: self.mixed_precision: bool = self.config.mixed_precision self.use_grad_scaler: bool = self.mixed_precision or self.config.use_grad_scaler self.training_state: Literal["training", "paused", "completed"] = "training" - self.gradient_accumulation_steps: int = self.config.gradient_accumulation_steps + self.gradient_accumulation_steps: DefaultDict = defaultdict(lambda: 1) + self.gradient_accumulation_steps.update(self.config.gradient_accumulation_steps) if self.device == "cpu": self.mixed_precision = False @@ -156,6 +156,19 @@ class Trainer: # set up viewer if enabled viewer_log_path = self.base_dir / self.config.viewer.relative_log_filename self.viewer_state, banner_messages = None, None + if self.config.is_viewer_legacy_enabled() and self.local_rank == 0: + datapath = self.config.data + if datapath is None: + datapath = self.base_dir + self.viewer_state = ViewerLegacyState( + self.config.viewer, + log_filename=viewer_log_path, + datapath=datapath, + pipeline=self.pipeline, + trainer=self, + train_lock=self.train_lock, + ) + banner_messages = [f"Legacy viewer at: {self.viewer_state.viewer_url}"] if self.config.is_viewer_enabled() and self.local_rank == 0: datapath = self.config.data if datapath is None: @@ -167,22 +180,9 @@ class Trainer: pipeline=self.pipeline, trainer=self, train_lock=self.train_lock, - ) - banner_messages = [f"Viewer at: {self.viewer_state.viewer_url}"] - if self.config.is_viewer_beta_enabled() and self.local_rank == 0: - datapath = self.config.data - if datapath is None: - datapath = self.base_dir - self.viewer_state = ViewerBetaState( - self.config.viewer, - log_filename=viewer_log_path, - datapath=datapath, - pipeline=self.pipeline, - trainer=self, - train_lock=self.train_lock, share=self.config.viewer.make_share_url, ) - banner_messages = [f"Viewer Beta at: {self.viewer_state.viewer_url}"] + banner_messages = [f"Viewer at: {self.viewer_state.viewer_url}"] self._check_viewer_warnings() self._load_checkpoint() @@ -219,24 +219,15 @@ class Trainer: """ optimizer_config = self.config.optimizers.copy() param_groups = self.pipeline.get_param_groups() - camera_optimizer_config = self.config.pipeline.datamanager.camera_optimizer - if camera_optimizer_config is not None and camera_optimizer_config.mode != "off": - assert camera_optimizer_config.param_group not in optimizer_config - optimizer_config[camera_optimizer_config.param_group] = { - "optimizer": camera_optimizer_config.optimizer, - "scheduler": camera_optimizer_config.scheduler, - } return Optimizers(optimizer_config, param_groups) def train(self) -> None: """Train the model.""" assert self.pipeline.datamanager.train_dataset is not None, "Missing DatsetInputs" - # don't want to call save_dataparser_transform if pipeline's datamanager does not have a dataparser - if isinstance(self.pipeline.datamanager, VanillaDataManager): - self.pipeline.datamanager.train_dataparser_outputs.save_dataparser_transform( - self.base_dir / "dataparser_transforms.json" - ) + self.pipeline.datamanager.train_dataparser_outputs.save_dataparser_transform( + self.base_dir / "dataparser_transforms.json" + ) self._init_viewer_state() with TimeWriter(writer, EventName.TOTAL_TRAIN_TIME): @@ -327,7 +318,7 @@ class Trainer: def _check_viewer_warnings(self) -> None: """Helper to print out any warnings regarding the way the viewer/loggers are enabled""" if ( - (self.config.is_viewer_enabled() or self.config.is_viewer_beta_enabled()) + (self.config.is_viewer_legacy_enabled() or self.config.is_viewer_enabled()) and not self.config.is_tensorboard_enabled() and not self.config.is_wandb_enabled() and not self.config.is_comet_enabled() @@ -469,18 +460,23 @@ class Trainer: step: Current training step. """ - self.optimizers.zero_grad_all() + needs_zero = [ + group for group in self.optimizers.parameters.keys() if step % self.gradient_accumulation_steps[group] == 0 + ] + self.optimizers.zero_grad_some(needs_zero) cpu_or_cuda_str: str = self.device.split(":")[0] - assert ( - self.gradient_accumulation_steps > 0 - ), f"gradient_accumulation_steps must be > 0, not {self.gradient_accumulation_steps}" - for _ in range(self.gradient_accumulation_steps): - with torch.autocast(device_type=cpu_or_cuda_str, enabled=self.mixed_precision): - _, loss_dict, metrics_dict = self.pipeline.get_train_loss_dict(step=step) - loss = functools.reduce(torch.add, loss_dict.values()) - loss /= self.gradient_accumulation_steps - self.grad_scaler.scale(loss).backward() # type: ignore - self.optimizers.optimizer_scaler_step_all(self.grad_scaler) + cpu_or_cuda_str = "cpu" if cpu_or_cuda_str == "mps" else cpu_or_cuda_str + + with torch.autocast(device_type=cpu_or_cuda_str, enabled=self.mixed_precision): + _, loss_dict, metrics_dict = self.pipeline.get_train_loss_dict(step=step) + loss = functools.reduce(torch.add, loss_dict.values()) + self.grad_scaler.scale(loss).backward() # type: ignore + needs_step = [ + group + for group in self.optimizers.parameters.keys() + if step % self.gradient_accumulation_steps[group] == self.gradient_accumulation_steps[group] - 1 + ] + self.optimizers.optimizer_scaler_step_some(self.grad_scaler, needs_step) if self.config.log_gradients: total_grad = 0 diff --git a/nerfstudio/exporter/exporter_utils.py b/nerfstudio/exporter/exporter_utils.py index 66d658690..56b4b6445 100644 --- a/nerfstudio/exporter/exporter_utils.py +++ b/nerfstudio/exporter/exporter_utils.py @@ -28,6 +28,7 @@ import open3d as o3d import pymeshlab import torch from jaxtyping import Float +from nerfstudio.cameras.rays import RayBundle from rich.progress import BarColumn, Progress, TaskProgressColumn, TextColumn, TimeRemainingColumn from torch import Tensor @@ -130,6 +131,7 @@ def generate_point_cloud( with torch.no_grad(): ray_bundle, _ = pipeline.datamanager.next_train(0) + assert isinstance(ray_bundle, RayBundle) outputs = pipeline.model(ray_bundle) if rgb_output_name not in outputs: CONSOLE.rule("Error", style="red") diff --git a/nerfstudio/exporter/marching_cubes.py b/nerfstudio/exporter/marching_cubes.py index a83307501..ee13422b1 100644 --- a/nerfstudio/exporter/marching_cubes.py +++ b/nerfstudio/exporter/marching_cubes.py @@ -249,7 +249,7 @@ def generate_mesh_with_multires_marching_cubes( ) verts = verts + np.array([x_min, y_min, z_min]) - meshcrop = trimesh.Trimesh(verts, faces, normals) + meshcrop = trimesh.Trimesh(verts, faces, normals) # type: ignore meshes.append(meshcrop) combined_mesh: trimesh.Trimesh = trimesh.util.concatenate(meshes) # type: ignore diff --git a/nerfstudio/field_components/encodings.py b/nerfstudio/field_components/encodings.py index 437ee805f..dc410673b 100644 --- a/nerfstudio/field_components/encodings.py +++ b/nerfstudio/field_components/encodings.py @@ -27,9 +27,13 @@ from jaxtyping import Float, Int, Shaped from torch import Tensor, nn from nerfstudio.field_components.base_field_component import FieldComponent -from nerfstudio.utils.math import components_from_spherical_harmonics, expected_sin +from nerfstudio.utils.external import TCNN_EXISTS, tcnn +from nerfstudio.utils.math import ( + components_from_spherical_harmonics, + expected_sin, + generate_polyhedron_basis, +) from nerfstudio.utils.printing import print_tcnn_speed_warning -from nerfstudio.utils.external import tcnn, TCNN_EXISTS class Encoding(FieldComponent): @@ -44,6 +48,11 @@ class Encoding(FieldComponent): raise ValueError("Input dimension should be greater than zero") super().__init__(in_dim=in_dim) + @classmethod + def get_tcnn_encoding_config(cls) -> dict: + """Get the encoding configuration for tcnn if implemented""" + raise NotImplementedError("Encoding does not have a TCNN implementation") + @abstractmethod def forward(self, in_tensor: Shaped[Tensor, "*bs input_dim"]) -> Shaped[Tensor, "*bs output_dim"]: """Call forward and returns and processed tensor @@ -122,14 +131,20 @@ class NeRFEncoding(Encoding): if implementation == "tcnn" and not TCNN_EXISTS: print_tcnn_speed_warning("NeRFEncoding") elif implementation == "tcnn": - encoding_config = {"otype": "Frequency", "n_frequencies": num_frequencies} assert min_freq_exp == 0, "tcnn only supports min_freq_exp = 0" assert max_freq_exp == num_frequencies - 1, "tcnn only supports max_freq_exp = num_frequencies - 1" + encoding_config = self.get_tcnn_encoding_config(num_frequencies=self.num_frequencies) self.tcnn_encoding = tcnn.Encoding( n_input_dims=in_dim, encoding_config=encoding_config, ) + @classmethod + def get_tcnn_encoding_config(cls, num_frequencies) -> dict: + """Get the encoding configuration for tcnn if implemented""" + encoding_config = {"otype": "Frequency", "n_frequencies": num_frequencies} + return encoding_config + def get_out_dim(self) -> int: if self.in_dim is None: raise ValueError("Input dimension has not been set") @@ -153,7 +168,7 @@ class NeRFEncoding(Encoding): Output values will be between -1 and 1 """ scaled_in_tensor = 2 * torch.pi * in_tensor # scale to [0, 2pi] - freqs = 2 ** torch.linspace(self.min_freq, self.max_freq, self.num_frequencies).to(in_tensor.device) + freqs = 2 ** torch.linspace(self.min_freq, self.max_freq, self.num_frequencies, device=in_tensor.device) scaled_inputs = scaled_in_tensor[..., None] * freqs # [..., "input_dim", "num_scales"] scaled_inputs = scaled_inputs.view(*scaled_inputs.shape[:-2], -1) # [..., "input_dim" * "num_scales"] @@ -165,47 +180,54 @@ class NeRFEncoding(Encoding): encoded_inputs = expected_sin( torch.cat([scaled_inputs, scaled_inputs + torch.pi / 2.0], dim=-1), torch.cat(2 * [input_var], dim=-1) ) - - if self.include_input: - encoded_inputs = torch.cat([encoded_inputs, in_tensor], dim=-1) return encoded_inputs def forward( self, in_tensor: Float[Tensor, "*bs input_dim"], covs: Optional[Float[Tensor, "*bs input_dim input_dim"]] = None ) -> Float[Tensor, "*bs output_dim"]: if self.tcnn_encoding is not None: - return self.tcnn_encoding(in_tensor) - return self.pytorch_fwd(in_tensor, covs) + encoded_inputs = self.tcnn_encoding(in_tensor) + else: + encoded_inputs = self.pytorch_fwd(in_tensor, covs) + if self.include_input: + encoded_inputs = torch.cat([encoded_inputs, in_tensor], dim=-1) + return encoded_inputs -class RFFEncoding(Encoding): - """Random Fourier Feature encoding. Supports integrated encodings. +class FFEncoding(Encoding): + """Fourier Feature encoding. Supports integrated encodings. Args: in_dim: Input dimension of tensor - num_frequencies: Number of encoding frequencies - scale: Std of Gaussian to sample frequencies. Must be greater than zero + basis: Basis matrix from which to construct the Fourier features. + num_frequencies: Number of encoded frequencies per axis + min_freq_exp: Minimum frequency exponent + max_freq_exp: Maximum frequency exponent include_input: Append the input coordinate to the encoding """ - def __init__(self, in_dim: int, num_frequencies: int, scale: float, include_input: bool = False) -> None: + def __init__( + self, + in_dim: int, + basis: Float[Tensor, "M N"], + num_frequencies: int, + min_freq_exp: float, + max_freq_exp: float, + include_input: bool = False, + ) -> None: super().__init__(in_dim) - self.num_frequencies = num_frequencies - if not scale > 0: - raise ValueError("RFF encoding scale should be greater than zero") - self.scale = scale - if self.in_dim is None: - raise ValueError("Input dimension has not been set") - b_matrix = torch.normal(mean=0, std=self.scale, size=(self.in_dim, self.num_frequencies)) - self.register_buffer(name="b_matrix", tensor=b_matrix) + self.min_freq = min_freq_exp + self.max_freq = max_freq_exp + self.register_buffer(name="b_matrix", tensor=basis) self.include_input = include_input def get_out_dim(self) -> int: - out_dim = self.num_frequencies * 2 + if self.in_dim is None: + raise ValueError("Input dimension has not been set") + assert isinstance(self.b_matrix, Tensor) + out_dim = self.b_matrix.shape[1] * self.num_frequencies * 2 if self.include_input: - if self.in_dim is None: - raise ValueError("Input dimension has not been set") out_dim += self.in_dim return out_dim @@ -214,7 +236,7 @@ class RFFEncoding(Encoding): in_tensor: Float[Tensor, "*bs input_dim"], covs: Optional[Float[Tensor, "*bs input_dim input_dim"]] = None, ) -> Float[Tensor, "*bs output_dim"]: - """Calculates RFF encoding. If covariances are provided the encodings will be integrated as proposed + """Calculates FF encoding. If covariances are provided the encodings will be integrated as proposed in mip-NeRF. Args: @@ -226,11 +248,16 @@ class RFFEncoding(Encoding): """ scaled_in_tensor = 2 * torch.pi * in_tensor # scale to [0, 2pi] scaled_inputs = scaled_in_tensor @ self.b_matrix # [..., "num_frequencies"] + freqs = 2 ** torch.linspace(self.min_freq, self.max_freq, self.num_frequencies, device=in_tensor.device) + scaled_inputs = scaled_inputs[..., None] * freqs # [..., "input_dim", "num_scales"] + scaled_inputs = scaled_inputs.view(*scaled_inputs.shape[:-2], -1) # [..., "input_dim" * "num_scales"] if covs is None: encoded_inputs = torch.sin(torch.cat([scaled_inputs, scaled_inputs + torch.pi / 2.0], dim=-1)) else: input_var = torch.sum((covs @ self.b_matrix) * self.b_matrix, -2) + input_var = input_var[..., :, None] * freqs[None, :] ** 2 + input_var = input_var.reshape((*input_var.shape[:-2], -1)) encoded_inputs = expected_sin( torch.cat([scaled_inputs, scaled_inputs + torch.pi / 2.0], dim=-1), torch.cat(2 * [input_var], dim=-1) ) @@ -241,6 +268,49 @@ class RFFEncoding(Encoding): return encoded_inputs +class RFFEncoding(FFEncoding): + """Random Fourier Feature encoding. Supports integrated encodings. + + Args: + in_dim: Input dimension of tensor + num_frequencies: Number of encoding frequencies + scale: Std of Gaussian to sample frequencies. Must be greater than zero + include_input: Append the input coordinate to the encoding + """ + + def __init__(self, in_dim: int, num_frequencies: int, scale: float, include_input: bool = False) -> None: + if not scale > 0: + raise ValueError("RFF encoding scale should be greater than zero") + + b_matrix = torch.normal(mean=0, std=scale, size=(in_dim, num_frequencies)) + super().__init__(in_dim, b_matrix, 1, 0.0, 0.0, include_input) + + +class PolyhedronFFEncoding(FFEncoding): + """Fourier Feature encoding using polyhedron basis as proposed by mip-NeRF360. Supports integrated encodings. + + Args: + num_frequencies: Number of encoded frequencies per axis + min_freq_exp: Minimum frequency exponent + max_freq_exp: Maximum frequency exponent + basis_shape: Shape of polyhedron basis. Either "octahedron" or "icosahedron" + basis_subdivisions: Number of times to tesselate the polyhedron. + include_input: Append the input coordinate to the encoding + """ + + def __init__( + self, + num_frequencies: int, + min_freq_exp: float, + max_freq_exp: float, + basis_shape: Literal["octahedron", "icosahedron"] = "octahedron", + basis_subdivisions: int = 1, + include_input: bool = False, + ) -> None: + basis_t = generate_polyhedron_basis(basis_shape, basis_subdivisions).T + super().__init__(3, basis_t, num_frequencies, min_freq_exp, max_freq_exp, include_input) + + class HashEncoding(Encoding): """Hash encoding @@ -268,48 +338,67 @@ class HashEncoding(Encoding): ) -> None: super().__init__(in_dim=3) self.num_levels = num_levels + self.min_res = min_res self.features_per_level = features_per_level + self.hash_init_scale = hash_init_scale self.log2_hashmap_size = log2_hashmap_size self.hash_table_size = 2**log2_hashmap_size levels = torch.arange(num_levels) - growth_factor = np.exp((np.log(max_res) - np.log(min_res)) / (num_levels - 1)) if num_levels > 1 else 1 - self.scalings = torch.floor(min_res * growth_factor**levels) + self.growth_factor = np.exp((np.log(max_res) - np.log(min_res)) / (num_levels - 1)) if num_levels > 1 else 1 + self.scalings = torch.floor(min_res * self.growth_factor**levels) self.hash_offset = levels * self.hash_table_size self.tcnn_encoding = None self.hash_table = torch.empty(0) - if implementation == "tcnn" and not TCNN_EXISTS: + if implementation == "torch": + self.build_nn_modules() + elif implementation == "tcnn" and not TCNN_EXISTS: print_tcnn_speed_warning("HashEncoding") - implementation = "torch" - - if implementation == "tcnn": - encoding_config = { - "otype": "HashGrid", - "n_levels": self.num_levels, - "n_features_per_level": self.features_per_level, - "log2_hashmap_size": self.log2_hashmap_size, - "base_resolution": min_res, - "per_level_scale": growth_factor, - } - if interpolation is not None: - encoding_config["interpolation"] = interpolation - + self.build_nn_modules() + elif implementation == "tcnn": + encoding_config = self.get_tcnn_encoding_config( + num_levels=self.num_levels, + features_per_level=self.features_per_level, + log2_hashmap_size=self.log2_hashmap_size, + min_res=self.min_res, + growth_factor=self.growth_factor, + interpolation=interpolation, + ) self.tcnn_encoding = tcnn.Encoding( n_input_dims=3, encoding_config=encoding_config, ) - elif implementation == "torch": - self.hash_table = torch.rand(size=(self.hash_table_size * num_levels, features_per_level)) * 2 - 1 - self.hash_table *= hash_init_scale - self.hash_table = nn.Parameter(self.hash_table) if self.tcnn_encoding is None: assert ( interpolation is None or interpolation == "Linear" ), f"interpolation '{interpolation}' is not supported for torch encoding backend" + def build_nn_modules(self) -> None: + """Initialize the torch version of the hash encoding.""" + self.hash_table = torch.rand(size=(self.hash_table_size * self.num_levels, self.features_per_level)) * 2 - 1 + self.hash_table *= self.hash_init_scale + self.hash_table = nn.Parameter(self.hash_table) + + @classmethod + def get_tcnn_encoding_config( + cls, num_levels, features_per_level, log2_hashmap_size, min_res, growth_factor, interpolation=None + ) -> dict: + """Get the encoding configuration for tcnn if implemented""" + encoding_config = { + "otype": "HashGrid", + "n_levels": num_levels, + "n_features_per_level": features_per_level, + "log2_hashmap_size": log2_hashmap_size, + "base_resolution": min_res, + "per_level_scale": growth_factor, + } + if interpolation is not None: + encoding_config["interpolation"] = interpolation + return encoding_config + def get_out_dim(self) -> int: return self.num_levels * self.features_per_level @@ -424,9 +513,8 @@ class TensorCPEncoding(Encoding): resolution: Target resolution. """ - self.line_coef.data = F.interpolate( - self.line_coef.data, size=(resolution, 1), mode="bilinear", align_corners=True - ) + line_coef = F.interpolate(self.line_coef.data, size=(resolution, 1), mode="bilinear", align_corners=True) + self.line_coef = torch.nn.Parameter(line_coef) self.resolution = resolution @@ -687,15 +775,21 @@ class SHEncoding(Encoding): if implementation == "tcnn" and not TCNN_EXISTS: print_tcnn_speed_warning("SHEncoding") elif implementation == "tcnn": - encoding_config = { - "otype": "SphericalHarmonics", - "degree": levels, - } + encoding_config = self.get_tcnn_encoding_config(levels=self.levels) self.tcnn_encoding = tcnn.Encoding( n_input_dims=3, encoding_config=encoding_config, ) + @classmethod + def get_tcnn_encoding_config(cls, levels) -> dict: + """Get the encoding configuration for tcnn if implemented""" + encoding_config = { + "otype": "SphericalHarmonics", + "degree": levels, + } + return encoding_config + def get_out_dim(self) -> int: return self.levels**2 diff --git a/nerfstudio/field_components/mlp.py b/nerfstudio/field_components/mlp.py index 2585a49e4..94e6b1eb4 100644 --- a/nerfstudio/field_components/mlp.py +++ b/nerfstudio/field_components/mlp.py @@ -17,12 +17,14 @@ Multi Layer Perceptron """ from typing import Literal, Optional, Set, Tuple, Union +import numpy as np import torch from jaxtyping import Float from torch import Tensor, nn from nerfstudio.field_components.base_field_component import FieldComponent from nerfstudio.utils.printing import print_tcnn_speed_warning +from nerfstudio.field_components.encodings import HashEncoding from nerfstudio.utils.rich_utils import CONSOLE from nerfstudio.utils.external import TCNN_EXISTS, tcnn @@ -66,6 +68,7 @@ class MLP(FieldComponent): out_dim: Output layer dimension. Uses layer_width if None. activation: intermediate layer activation function. out_activation: output activation function. + implementation: Implementation of hash encoding. Fallback to torch if tcnn not available. """ def __init__( @@ -98,39 +101,47 @@ class MLP(FieldComponent): print_tcnn_speed_warning("MLP") self.build_nn_modules() elif implementation == "tcnn": - activation_str = activation_to_tcnn_string(activation) - output_activation_str = activation_to_tcnn_string(out_activation) - if layer_width in [16, 32, 64, 128]: - network_config = { - "otype": "FullyFusedMLP", - "activation": activation_str, - "output_activation": output_activation_str, - "n_neurons": layer_width, - "n_hidden_layers": num_layers - 1, - } - else: - CONSOLE.line() - CONSOLE.print("[bold yellow]WARNING: Using slower TCNN CutlassMLP instead of TCNN FullyFusedMLP") - CONSOLE.print( - "[bold yellow]Use layer width of 16, 32, 64, or 128 to use the faster TCNN FullyFusedMLP." - ) - CONSOLE.line() - network_config = { - "otype": "CutlassMLP", - "activation": activation_str, - "output_activation": output_activation_str, - "n_neurons": layer_width, - "n_hidden_layers": num_layers - 1, - } - + network_config = self.get_tcnn_network_config( + activation=self.activation, + out_activation=self.out_activation, + layer_width=self.layer_width, + num_layers=self.num_layers, + ) self.tcnn_encoding = tcnn.Network( n_input_dims=in_dim, - n_output_dims=out_dim, + n_output_dims=self.out_dim, network_config=network_config, ) + @classmethod + def get_tcnn_network_config(cls, activation, out_activation, layer_width, num_layers) -> dict: + """Get the network configuration for tcnn if implemented""" + activation_str = activation_to_tcnn_string(activation) + output_activation_str = activation_to_tcnn_string(out_activation) + if layer_width in [16, 32, 64, 128]: + network_config = { + "otype": "FullyFusedMLP", + "activation": activation_str, + "output_activation": output_activation_str, + "n_neurons": layer_width, + "n_hidden_layers": num_layers - 1, + } + else: + CONSOLE.line() + CONSOLE.print("[bold yellow]WARNING: Using slower TCNN CutlassMLP instead of TCNN FullyFusedMLP") + CONSOLE.print("[bold yellow]Use layer width of 16, 32, 64, or 128 to use the faster TCNN FullyFusedMLP.") + CONSOLE.line() + network_config = { + "otype": "CutlassMLP", + "activation": activation_str, + "output_activation": output_activation_str, + "n_neurons": layer_width, + "n_hidden_layers": num_layers - 1, + } + return network_config + def build_nn_modules(self) -> None: - """Initialize multi-layer perceptron.""" + """Initialize the torch version of the multi-layer perceptron.""" layers = [] if self.num_layers == 1: layers.append(nn.Linear(self.in_dim, self.out_dim)) @@ -171,3 +182,114 @@ class MLP(FieldComponent): if self.tcnn_encoding is not None: return self.tcnn_encoding(in_tensor) return self.pytorch_fwd(in_tensor) + + +class MLPWithHashEncoding(FieldComponent): + """Multilayer perceptron with hash encoding + + Args: + num_levels: Number of feature grids. + min_res: Resolution of smallest feature grid. + max_res: Resolution of largest feature grid. + log2_hashmap_size: Size of hash map is 2^log2_hashmap_size. + features_per_level: Number of features per level. + hash_init_scale: Value to initialize hash grid. + interpolation: Interpolation override for tcnn hashgrid. Not supported for torch unless linear. + num_layers: Number of network layers + layer_width: Width of each MLP layer + out_dim: Output layer dimension. Uses layer_width if None. + activation: intermediate layer activation function. + out_activation: output activation function. + implementation: Implementation of hash encoding. Fallback to torch if tcnn not available. + """ + + def __init__( + self, + num_levels: int = 16, + min_res: int = 16, + max_res: int = 1024, + log2_hashmap_size: int = 19, + features_per_level: int = 2, + hash_init_scale: float = 0.001, + interpolation: Optional[Literal["Nearest", "Linear", "Smoothstep"]] = None, + num_layers: int = 2, + layer_width: int = 64, + out_dim: Optional[int] = None, + skip_connections: Optional[Tuple[int]] = None, + activation: Optional[nn.Module] = nn.ReLU(), + out_activation: Optional[nn.Module] = None, + implementation: Literal["tcnn", "torch"] = "torch", + ) -> None: + super().__init__() + self.in_dim = 3 + + self.num_levels = num_levels + self.min_res = min_res + self.max_res = max_res + self.features_per_level = features_per_level + self.hash_init_scale = hash_init_scale + self.log2_hashmap_size = log2_hashmap_size + self.hash_table_size = 2**log2_hashmap_size + + self.growth_factor = np.exp((np.log(max_res) - np.log(min_res)) / (num_levels - 1)) if num_levels > 1 else 1 + + self.out_dim = out_dim if out_dim is not None else layer_width + self.num_layers = num_layers + self.layer_width = layer_width + self.skip_connections = skip_connections + self._skip_connections: Set[int] = set(skip_connections) if skip_connections else set() + self.activation = activation + self.out_activation = out_activation + self.net = None + + self.tcnn_encoding = None + if implementation == "torch": + self.build_nn_modules() + elif implementation == "tcnn" and not TCNN_EXISTS: + print_tcnn_speed_warning("MLPWithHashEncoding") + self.build_nn_modules() + elif implementation == "tcnn": + self.model = tcnn.NetworkWithInputEncoding( + n_input_dims=self.in_dim, + n_output_dims=self.out_dim, + encoding_config=HashEncoding.get_tcnn_encoding_config( + num_levels=self.num_levels, + features_per_level=self.features_per_level, + log2_hashmap_size=self.log2_hashmap_size, + min_res=self.min_res, + growth_factor=self.growth_factor, + interpolation=interpolation, + ), + network_config=MLP.get_tcnn_network_config( + activation=self.activation, + out_activation=self.out_activation, + layer_width=self.layer_width, + num_layers=self.num_layers, + ), + ) + + def build_nn_modules(self) -> None: + """Initialize the torch version of the MLP with hash encoding.""" + encoder = HashEncoding( + num_levels=self.num_levels, + min_res=self.min_res, + max_res=self.max_res, + log2_hashmap_size=self.log2_hashmap_size, + features_per_level=self.features_per_level, + hash_init_scale=self.hash_init_scale, + implementation="torch", + ) + mlp = MLP( + in_dim=encoder.get_out_dim(), + num_layers=self.num_layers, + layer_width=self.layer_width, + out_dim=self.out_dim, + skip_connections=self.skip_connections, + activation=self.activation, + out_activation=self.out_activation, + implementation="torch", + ) + self.model = torch.nn.Sequential(encoder, mlp) + + def forward(self, in_tensor: Float[Tensor, "*bs in_dim"]) -> Float[Tensor, "*bs out_dim"]: + return self.model(in_tensor) diff --git a/nerfstudio/fields/nerfacto_field.py b/nerfstudio/fields/nerfacto_field.py index 34c6346fc..910bba846 100644 --- a/nerfstudio/fields/nerfacto_field.py +++ b/nerfstudio/fields/nerfacto_field.py @@ -26,7 +26,7 @@ from nerfstudio.cameras.rays import RaySamples from nerfstudio.data.scene_box import SceneBox from nerfstudio.field_components.activations import trunc_exp from nerfstudio.field_components.embedding import Embedding -from nerfstudio.field_components.encodings import HashEncoding, NeRFEncoding, SHEncoding +from nerfstudio.field_components.encodings import NeRFEncoding, SHEncoding from nerfstudio.field_components.field_heads import ( FieldHeadNames, PredNormalsFieldHead, @@ -35,13 +35,13 @@ from nerfstudio.field_components.field_heads import ( TransientRGBFieldHead, UncertaintyFieldHead, ) -from nerfstudio.field_components.mlp import MLP +from nerfstudio.field_components.mlp import MLP, MLPWithHashEncoding from nerfstudio.field_components.spatial_distortions import SpatialDistortion from nerfstudio.fields.base_field import Field, get_normalized_directions class NerfactoField(Field): - """Compound Field that uses TCNN + """Compound Field Args: aabb: parameters of scene aabb bounds @@ -116,6 +116,7 @@ class NerfactoField(Field): self.use_pred_normals = use_pred_normals self.pass_semantic_gradients = pass_semantic_gradients self.base_res = base_res + self.step = 0 self.direction_encoding = SHEncoding( levels=4, @@ -126,16 +127,12 @@ class NerfactoField(Field): in_dim=3, num_frequencies=2, min_freq_exp=0, max_freq_exp=2 - 1, implementation=implementation ) - self.mlp_base_grid = HashEncoding( + self.mlp_base = MLPWithHashEncoding( num_levels=num_levels, min_res=base_res, max_res=max_res, log2_hashmap_size=log2_hashmap_size, features_per_level=features_per_level, - implementation=implementation, - ) - self.mlp_base_mlp = MLP( - in_dim=self.mlp_base_grid.get_out_dim(), num_layers=num_layers, layer_width=hidden_dim, out_dim=1 + self.geo_feat_dim, @@ -143,7 +140,6 @@ class NerfactoField(Field): out_activation=None, implementation=implementation, ) - self.mlp_base = torch.nn.Sequential(self.mlp_base_grid, self.mlp_base_mlp) # transients if self.use_transient_embedding: diff --git a/nerfstudio/model_components/losses.py b/nerfstudio/model_components/losses.py index e90ec8e61..d46466989 100644 --- a/nerfstudio/model_components/losses.py +++ b/nerfstudio/model_components/losses.py @@ -575,6 +575,10 @@ def depth_ranking_loss(rendered_depth, gt_depth): and rendered_depth are from pixels with a radius of each other """ m = 1e-4 + if rendered_depth.shape[0] % 2 != 0: + # chop off one index + rendered_depth = rendered_depth[:-1, :] + gt_depth = gt_depth[:-1, :] dpt_diff = gt_depth[::2, :] - gt_depth[1::2, :] out_diff = rendered_depth[::2, :] - rendered_depth[1::2, :] + m differing_signs = torch.sign(dpt_diff) != torch.sign(out_diff) diff --git a/nerfstudio/model_components/ray_generators.py b/nerfstudio/model_components/ray_generators.py index 49754548d..fab9e39bb 100644 --- a/nerfstudio/model_components/ray_generators.py +++ b/nerfstudio/model_components/ray_generators.py @@ -18,7 +18,6 @@ Ray generator. from jaxtyping import Int from torch import Tensor, nn -from nerfstudio.cameras.camera_optimizers import CameraOptimizer from nerfstudio.cameras.cameras import Cameras from nerfstudio.cameras.rays import RayBundle @@ -29,15 +28,13 @@ class RayGenerator(nn.Module): Args: cameras: Camera objects containing camera info. - pose_optimizer: pose optimization module, for optimizing noisy camera intrinsics/extrinsics. """ image_coords: Tensor - def __init__(self, cameras: Cameras, pose_optimizer: CameraOptimizer) -> None: + def __init__(self, cameras: Cameras) -> None: super().__init__() self.cameras = cameras - self.pose_optimizer = pose_optimizer self.register_buffer("image_coords", cameras.get_image_coords(), persistent=False) def forward(self, ray_indices: Int[Tensor, "num_rays 3"]) -> RayBundle: @@ -51,11 +48,8 @@ class RayGenerator(nn.Module): x = ray_indices[:, 2] # col indices coords = self.image_coords[y, x] - camera_opt_to_camera = self.pose_optimizer(c) - ray_bundle = self.cameras.generate_rays( camera_indices=c.unsqueeze(-1), coords=coords, - camera_opt_to_camera=camera_opt_to_camera, ) return ray_bundle diff --git a/nerfstudio/model_components/scene_colliders.py b/nerfstudio/model_components/scene_colliders.py index 69c073b3b..025a46265 100644 --- a/nerfstudio/model_components/scene_colliders.py +++ b/nerfstudio/model_components/scene_colliders.py @@ -172,16 +172,20 @@ class NearFarCollider(SceneCollider): Args: near_plane: distance to near plane far_plane: distance to far plane + reset_near_plane: whether to reset the near plane to 0.0 during inference. The near plane can be + helpful for reducing floaters during training, but it can cause clipping artifacts during + inference when an evaluation or viewer camera moves closer to the object. """ - def __init__(self, near_plane: float, far_plane: float, **kwargs) -> None: + def __init__(self, near_plane: float, far_plane: float, reset_near_plane: bool = True, **kwargs) -> None: self.near_plane = near_plane self.far_plane = far_plane + self.reset_near_plane = reset_near_plane super().__init__(**kwargs) def set_nears_and_fars(self, ray_bundle: RayBundle) -> RayBundle: ones = torch.ones_like(ray_bundle.origins[..., 0:1]) - near_plane = self.near_plane if self.training else 0 + near_plane = self.near_plane if (self.training or not self.reset_near_plane) else 0 ray_bundle.nears = ones * near_plane ray_bundle.fars = ones * self.far_plane return ray_bundle diff --git a/nerfstudio/models/base_model.py b/nerfstudio/models/base_model.py index ebda710b3..2a434a85e 100644 --- a/nerfstudio/models/base_model.py +++ b/nerfstudio/models/base_model.py @@ -13,7 +13,7 @@ # limitations under the License. """ -Base Model implementation which takes in RayBundles +Base Model implementation which takes in RayBundles or Cameras """ from __future__ import annotations @@ -27,10 +27,11 @@ import torch from torch import nn from torch.nn import Parameter +from nerfstudio.cameras.cameras import Cameras from nerfstudio.cameras.rays import RayBundle from nerfstudio.configs.base_config import InstantiateConfig from nerfstudio.configs.config_utils import to_immutable_dict -from nerfstudio.data.scene_box import SceneBox +from nerfstudio.data.scene_box import SceneBox, OrientedBox from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes from nerfstudio.model_components.scene_colliders import NearFarCollider @@ -117,7 +118,7 @@ class Model(nn.Module): """ @abstractmethod - def get_outputs(self, ray_bundle: RayBundle) -> Dict[str, Union[torch.Tensor, List]]: + def get_outputs(self, ray_bundle: Union[RayBundle, Cameras]) -> Dict[str, Union[torch.Tensor, List]]: """Takes in a Ray Bundle and returns a dictionary of outputs. Args: @@ -128,7 +129,7 @@ class Model(nn.Module): Outputs of model. (ie. rendered colors) """ - def forward(self, ray_bundle: RayBundle) -> Dict[str, Union[torch.Tensor, List]]: + def forward(self, ray_bundle: Union[RayBundle, Cameras]) -> Dict[str, Union[torch.Tensor, List]]: """Run forward starting with a ray bundle. This outputs different things depending on the configuration of the model and whether or not the batch is provided (whether or not we are training basically) @@ -161,6 +162,18 @@ class Model(nn.Module): metrics_dict: dictionary of metrics, some of which we can use for loss """ + @torch.no_grad() + def get_outputs_for_camera(self, camera: Cameras, obb_box: Optional[OrientedBox] = None) -> Dict[str, torch.Tensor]: + """Takes in a camera, generates the raybundle, and computes the output of the model. + Assumes a ray-based model. + + Args: + camera: generates raybundle + """ + return self.get_outputs_for_camera_ray_bundle( + camera.generate_rays(camera_indices=0, keep_shape=True, obb_box=obb_box) + ) + @torch.no_grad() def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle) -> Dict[str, torch.Tensor]: """Takes in camera parameters and computes the output of the model. @@ -168,6 +181,7 @@ class Model(nn.Module): Args: camera_ray_bundle: ray bundle to calculate outputs over """ + input_device = camera_ray_bundle.directions.device num_rays_per_chunk = self.config.eval_num_rays_per_chunk image_height, image_width = camera_ray_bundle.origins.shape[:2] num_rays = len(camera_ray_bundle) @@ -176,12 +190,15 @@ class Model(nn.Module): start_idx = i end_idx = i + num_rays_per_chunk ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(start_idx, end_idx) + # move the chunk inputs to the model device + ray_bundle = ray_bundle.to(self.device) outputs = self.forward(ray_bundle=ray_bundle) for output_name, output in outputs.items(): # type: ignore - if not torch.is_tensor(output): + if not isinstance(output, torch.Tensor): # TODO: handle lists of tensors as well continue - outputs_lists[output_name].append(output) + # move the chunk outputs from the model device back to the device of the inputs. + outputs_lists[output_name].append(output.to(input_device)) outputs = {} for output_name, outputs_list in outputs_lists.items(): outputs[output_name] = torch.cat(outputs_list).view(image_height, image_width, -1) # type: ignore diff --git a/nerfstudio/models/base_surface_model.py b/nerfstudio/models/base_surface_model.py index 638aa2713..97bde9d9f 100644 --- a/nerfstudio/models/base_surface_model.py +++ b/nerfstudio/models/base_surface_model.py @@ -79,7 +79,7 @@ class SurfaceModelConfig(ModelConfig): """Monocular normal consistency loss multiplier.""" mono_depth_loss_mult: float = 0.0 """Monocular depth consistency loss multiplier.""" - sdf_field: SDFFieldConfig = SDFFieldConfig() + sdf_field: SDFFieldConfig = field(default_factory=lambda: SDFFieldConfig()) """Config for SDF Field""" background_model: Literal["grid", "mlp", "none"] = "mlp" """background models""" diff --git a/nerfstudio/models/gaussian_splatting.py b/nerfstudio/models/gaussian_splatting.py new file mode 100644 index 000000000..e5f560772 --- /dev/null +++ b/nerfstudio/models/gaussian_splatting.py @@ -0,0 +1,839 @@ +# ruff: noqa: E741 +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +NeRF implementation that combines many recent advancements. +""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Dict, List, Optional, Tuple, Type, Union +from nerfstudio.data.scene_box import OrientedBox + +import torch +from torch.nn import Parameter +from torchmetrics.image import PeakSignalNoiseRatio +from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity +import torchvision.transforms.functional as TF + +from nerfstudio.cameras.cameras import Cameras +from gsplat._torch_impl import quat_to_rotmat +from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes, TrainingCallbackLocation +from nerfstudio.engine.optimizers import Optimizers +from nerfstudio.models.base_model import Model, ModelConfig +import math +import numpy as np +from sklearn.neighbors import NearestNeighbors +from nerfstudio.cameras.camera_optimizers import CameraOptimizer, CameraOptimizerConfig + +from gsplat.rasterize import RasterizeGaussians +from gsplat.project_gaussians import ProjectGaussians +from gsplat.sh import SphericalHarmonics, num_sh_bases + +from gsplat.compute_cumulative_intersects import compute_cumulative_intersects +from pytorch_msssim import SSIM + +# need following import for background color override +from nerfstudio.model_components import renderers +from nerfstudio.utils.rich_utils import CONSOLE + + +def random_quat_tensor(N): + """ + Defines a random quaternion tensor of shape (N, 4) + """ + u = torch.rand(N) + v = torch.rand(N) + w = torch.rand(N) + return torch.stack( + [ + torch.sqrt(1 - u) * torch.sin(2 * math.pi * v), + torch.sqrt(1 - u) * torch.cos(2 * math.pi * v), + torch.sqrt(u) * torch.sin(2 * math.pi * w), + torch.sqrt(u) * torch.cos(2 * math.pi * w), + ], + dim=-1, + ) + + +def RGB2SH(rgb): + """ + Converts from RGB values [0,1] to the 0th spherical harmonic coefficient + """ + C0 = 0.28209479177387814 + return (rgb - 0.5) / C0 + + +def SH2RGB(sh): + """ + Converts from the 0th spherical harmonic coefficient to RGB values [0,1] + """ + C0 = 0.28209479177387814 + return sh * C0 + 0.5 + + +def projection_matrix(znear, zfar, fovx, fovy, device: Union[str, torch.device] = "cpu"): + """ + Constructs an OpenGL-style perspective projection matrix. + """ + t = znear * math.tan(0.5 * fovy) + b = -t + r = znear * math.tan(0.5 * fovx) + l = -r + n = znear + f = zfar + return torch.tensor( + [ + [2 * n / (r - l), 0.0, (r + l) / (r - l), 0.0], + [0.0, 2 * n / (t - b), (t + b) / (t - b), 0.0], + [0.0, 0.0, (f + n) / (f - n), -1.0 * f * n / (f - n)], + [0.0, 0.0, 1.0, 0.0], + ], + device=device, + ) + + +@dataclass +class GaussianSplattingModelConfig(ModelConfig): + """Gaussian Splatting Model Config""" + + _target: Type = field(default_factory=lambda: GaussianSplattingModel) + warmup_length: int = 500 + """period of steps where refinement is turned off""" + refine_every: int = 100 + """period of steps where gaussians are culled and densified""" + resolution_schedule: int = 250 + """training starts at 1/d resolution, every n steps this is doubled""" + num_downscales: int = 0 + """at the beginning, resolution is 1/2^d, where d is this number""" + cull_alpha_thresh: float = 0.1 + """threshold of opacity for culling gaussians. One can set it to a lower value (e.g. 0.005) for higher quality.""" + cull_scale_thresh: float = 0.5 + """threshold of scale for culling huge gaussians""" + continue_cull_post_densification: bool = True + """If True, continue to cull gaussians post refinement""" + reset_alpha_every: int = 30 + """Every this many refinement steps, reset the alpha""" + densify_grad_thresh: float = 0.0002 + """threshold of positional gradient norm for densifying gaussians""" + densify_size_thresh: float = 0.01 + """below this size, gaussians are *duplicated*, otherwise split""" + n_split_samples: int = 2 + """number of samples to split gaussians into""" + sh_degree_interval: int = 1000 + """every n intervals turn on another sh degree""" + cull_screen_size: float = 0.15 + """if a gaussian is more than this percent of screen space, cull it""" + split_screen_size: float = 0.05 + """if a gaussian is more than this percent of screen space, split it""" + stop_screen_size_at: int = 4000 + """stop culling/splitting at this step WRT screen size of gaussians""" + random_init: bool = False + """whether to initialize the positions uniformly randomly (not SFM points)""" + ssim_lambda: float = 0.2 + """weight of ssim loss""" + stop_split_at: int = 15000 + """stop splitting at this step""" + sh_degree: int = 3 + """maximum degree of spherical harmonics to use""" + camera_optimizer: CameraOptimizerConfig = CameraOptimizerConfig(mode="off") + """camera optimizer config""" + use_scale_regularization: bool = False + """If enabled, a scale regularization introduced in PhysGauss (https://xpandora.github.io/PhysGaussian/) is used for reducing huge spikey gaussians.""" + max_gauss_ratio: float = 10.0 + """threshold of ratio of gaussian max to min scale before applying regularization + loss from the PhysGaussian paper + """ + + +class GaussianSplattingModel(Model): + """Gaussian Splatting model + + Args: + config: Gaussian Splatting configuration to instantiate model + """ + + config: GaussianSplattingModelConfig + + def __init__(self, *args, **kwargs): + if "seed_points" in kwargs: + self.seed_pts = kwargs["seed_points"] + else: + self.seed_pts = None + super().__init__(*args, **kwargs) + + def populate_modules(self): + if self.seed_pts is not None and not self.config.random_init: + self.means = torch.nn.Parameter(self.seed_pts[0]) # (Location, Color) + else: + self.means = torch.nn.Parameter((torch.rand((500000, 3)) - 0.5) * 10) + self.xys_grad_norm = None + self.max_2Dsize = None + distances, _ = self.k_nearest_sklearn(self.means.data, 3) + distances = torch.from_numpy(distances) + # find the average of the three nearest neighbors for each point and use that as the scale + avg_dist = distances.mean(dim=-1, keepdim=True) + self.scales = torch.nn.Parameter(torch.log(avg_dist.repeat(1, 3))) + self.quats = torch.nn.Parameter(random_quat_tensor(self.num_points)) + dim_sh = num_sh_bases(self.config.sh_degree) + + if self.seed_pts is not None and not self.config.random_init: + shs = torch.zeros((self.seed_pts[1].shape[0], dim_sh, 3)).float().cuda() + if self.config.sh_degree > 0: + shs[:, 0, :3] = RGB2SH(self.seed_pts[1] / 255) + shs[:, 1:, 3:] = 0.0 + else: + CONSOLE.log("use color only optimization with sigmoid activation") + shs[:, 0, :3] = torch.logit(self.seed_pts[1] / 255, eps=1e-10) + self.features_dc = torch.nn.Parameter(shs[:, 0, :]) + self.features_rest = torch.nn.Parameter(shs[:, 1:, :]) + else: + self.features_dc = torch.nn.Parameter(torch.rand(self.num_points, 3)) + self.features_rest = torch.nn.Parameter(torch.zeros((self.num_points, dim_sh - 1, 3))) + + self.opacities = torch.nn.Parameter(torch.logit(0.1 * torch.ones(self.num_points, 1))) + + # metrics + self.psnr = PeakSignalNoiseRatio(data_range=1.0) + self.ssim = SSIM(data_range=1.0, size_average=True, channel=3) + self.lpips = LearnedPerceptualImagePatchSimilarity(normalize=True) + self.step = 0 + + self.crop_box: Optional[OrientedBox] = None + self.back_color = torch.zeros(3) + + self.camera_optimizer: CameraOptimizer = self.config.camera_optimizer.setup( + num_cameras=self.num_train_data, device="cpu" + ) + + @property + def colors(self): + if self.config.sh_degree > 0: + return SH2RGB(self.features_dc) + else: + return torch.sigmoid(self.features_dc) + + @property + def shs_0(self): + return self.features_dc + + @property + def shs_rest(self): + return self.features_rest + + def load_state_dict(self, dict, **kwargs): # type: ignore + # resize the parameters to match the new number of points + self.step = 30000 + newp = dict["means"].shape[0] + self.means = torch.nn.Parameter(torch.zeros(newp, 3, device=self.device)) + self.scales = torch.nn.Parameter(torch.zeros(newp, 3, device=self.device)) + self.quats = torch.nn.Parameter(torch.zeros(newp, 4, device=self.device)) + self.opacities = torch.nn.Parameter(torch.zeros(newp, 1, device=self.device)) + self.features_dc = torch.nn.Parameter(torch.zeros(newp, 3, device=self.device)) + self.features_rest = torch.nn.Parameter( + torch.zeros(newp, num_sh_bases(self.config.sh_degree) - 1, 3, device=self.device) + ) + super().load_state_dict(dict, **kwargs) + + def k_nearest_sklearn(self, x: torch.Tensor, k: int): + """ + Find k-nearest neighbors using sklearn's NearestNeighbors. + x: The data tensor of shape [num_samples, num_features] + k: The number of neighbors to retrieve + """ + # Convert tensor to numpy array + x_np = x.cpu().numpy() + + # Build the nearest neighbors model + nn_model = NearestNeighbors(n_neighbors=k + 1, algorithm="auto", metric="euclidean").fit(x_np) + + # Find the k-nearest neighbors + distances, indices = nn_model.kneighbors(x_np) + + # Exclude the point itself from the result and return + return distances[:, 1:].astype(np.float32), indices[:, 1:].astype(np.float32) + + def remove_from_optim(self, optimizer, deleted_mask, new_params): + """removes the deleted_mask from the optimizer provided""" + assert len(new_params) == 1 + # assert isinstance(optimizer, torch.optim.Adam), "Only works with Adam" + + param = optimizer.param_groups[0]["params"][0] + param_state = optimizer.state[param] + del optimizer.state[param] + + # Modify the state directly without deleting and reassigning. + param_state["exp_avg"] = param_state["exp_avg"][~deleted_mask] + param_state["exp_avg_sq"] = param_state["exp_avg_sq"][~deleted_mask] + + # Update the parameter in the optimizer's param group. + del optimizer.param_groups[0]["params"][0] + del optimizer.param_groups[0]["params"] + optimizer.param_groups[0]["params"] = new_params + optimizer.state[new_params[0]] = param_state + + def remove_from_all_optim(self, optimizers, deleted_mask): + param_groups = self.get_gaussian_param_groups() + for group, param in param_groups.items(): + self.remove_from_optim(optimizers.optimizers[group], deleted_mask, param) + torch.cuda.empty_cache() + + def dup_in_optim(self, optimizer, dup_mask, new_params, n=2): + """adds the parameters to the optimizer""" + param = optimizer.param_groups[0]["params"][0] + param_state = optimizer.state[param] + repeat_dims = (n,) + tuple(1 for _ in range(param_state["exp_avg"].dim() - 1)) + param_state["exp_avg"] = torch.cat( + [param_state["exp_avg"], torch.zeros_like(param_state["exp_avg"][dup_mask.squeeze()]).repeat(*repeat_dims)], + dim=0, + ) + param_state["exp_avg_sq"] = torch.cat( + [ + param_state["exp_avg_sq"], + torch.zeros_like(param_state["exp_avg_sq"][dup_mask.squeeze()]).repeat(*repeat_dims), + ], + dim=0, + ) + del optimizer.state[param] + optimizer.state[new_params[0]] = param_state + optimizer.param_groups[0]["params"] = new_params + del param + + def dup_in_all_optim(self, optimizers, dup_mask, n): + param_groups = self.get_gaussian_param_groups() + for group, param in param_groups.items(): + self.dup_in_optim(optimizers.optimizers[group], dup_mask, param, n) + + def after_train(self, step: int): + assert step == self.step + # to save some training time, we no longer need to update those stats post refinement + if self.step >= self.config.stop_split_at: + return + with torch.no_grad(): + # keep track of a moving average of grad norms + visible_mask = (self.radii > 0).flatten() + grads = self.xys.grad.detach().norm(dim=-1) # TODO fill in + # print(f"grad norm min {grads.min().item()} max {grads.max().item()} mean {grads.mean().item()} size {grads.shape}") + if self.xys_grad_norm is None: + self.xys_grad_norm = grads + self.vis_counts = torch.ones_like(self.xys_grad_norm) + else: + assert self.vis_counts is not None + self.vis_counts[visible_mask] = self.vis_counts[visible_mask] + 1 + self.xys_grad_norm[visible_mask] = grads[visible_mask] + self.xys_grad_norm[visible_mask] + + # update the max screen size, as a ratio of number of pixels + if self.max_2Dsize is None: + self.max_2Dsize = torch.zeros_like(self.radii, dtype=torch.float32) + newradii = self.radii.detach()[visible_mask] + self.max_2Dsize[visible_mask] = torch.maximum( + self.max_2Dsize[visible_mask], newradii / float(max(self.last_size[0], self.last_size[1])) + ) + + def set_crop(self, crop_box: Optional[OrientedBox]): + self.crop_box = crop_box + + def set_background(self, back_color: torch.Tensor): + assert back_color.shape == (3,) + self.back_color = back_color + + def refinement_after(self, optimizers: Optimizers, step): + assert step == self.step + if self.step <= self.config.warmup_length: + return + with torch.no_grad(): + # Offset all the opacity reset logic by refine_every so that we don't + # save checkpoints right when the opacity is reset (saves every 2k) + # then cull + # only split/cull if we've seen every image since opacity reset + reset_interval = self.config.reset_alpha_every * self.config.refine_every + do_densification = ( + self.step < self.config.stop_split_at + and self.step % reset_interval > self.num_train_data + self.config.refine_every + ) + if do_densification: + # then we densify + assert self.xys_grad_norm is not None and self.vis_counts is not None and self.max_2Dsize is not None + avg_grad_norm = (self.xys_grad_norm / self.vis_counts) * 0.5 * max(self.last_size[0], self.last_size[1]) + high_grads = (avg_grad_norm > self.config.densify_grad_thresh).squeeze() + splits = (self.scales.exp().max(dim=-1).values > self.config.densify_size_thresh).squeeze() + if self.step < self.config.stop_screen_size_at: + splits |= (self.max_2Dsize > self.config.split_screen_size).squeeze() + splits &= high_grads + nsamps = self.config.n_split_samples + ( + split_means, + split_features_dc, + split_features_rest, + split_opacities, + split_scales, + split_quats, + ) = self.split_gaussians(splits, nsamps) + + dups = (self.scales.exp().max(dim=-1).values <= self.config.densify_size_thresh).squeeze() + dups &= high_grads + ( + dup_means, + dup_features_dc, + dup_features_rest, + dup_opacities, + dup_scales, + dup_quats, + ) = self.dup_gaussians(dups) + self.means = Parameter(torch.cat([self.means.detach(), split_means, dup_means], dim=0)) + self.features_dc = Parameter( + torch.cat([self.features_dc.detach(), split_features_dc, dup_features_dc], dim=0) + ) + self.features_rest = Parameter( + torch.cat([self.features_rest.detach(), split_features_rest, dup_features_rest], dim=0) + ) + self.opacities = Parameter(torch.cat([self.opacities.detach(), split_opacities, dup_opacities], dim=0)) + self.scales = Parameter(torch.cat([self.scales.detach(), split_scales, dup_scales], dim=0)) + self.quats = Parameter(torch.cat([self.quats.detach(), split_quats, dup_quats], dim=0)) + # append zeros to the max_2Dsize tensor + self.max_2Dsize = torch.cat( + [self.max_2Dsize, torch.zeros_like(split_scales[:, 0]), torch.zeros_like(dup_scales[:, 0])], + dim=0, + ) + + split_idcs = torch.where(splits)[0] + self.dup_in_all_optim(optimizers, split_idcs, nsamps) + + dup_idcs = torch.where(dups)[0] + self.dup_in_all_optim(optimizers, dup_idcs, 1) + + # After a guassian is split into two new gaussians, the original one should also be pruned. + splits_mask = torch.cat( + (splits, torch.zeros(nsamps * splits.sum() + dups.sum(), device=self.device, dtype=torch.bool)) + ) + + deleted_mask = self.cull_gaussians(splits_mask) + elif self.step >= self.config.stop_split_at and self.config.continue_cull_post_densification: + deleted_mask = self.cull_gaussians() + else: + # if we donot allow culling post refinement, no more gaussians will be pruned. + deleted_mask = None + + if deleted_mask is not None: + self.remove_from_all_optim(optimizers, deleted_mask) + + if self.step < self.config.stop_split_at and self.step % reset_interval == self.config.refine_every: + # Reset value is set to be twice of the cull_alpha_thresh + reset_value = self.config.cull_alpha_thresh * 2.0 + self.opacities.data = torch.clamp( + self.opacities.data, max=torch.logit(torch.tensor(reset_value, device=self.device)).item() + ) + # reset the exp of optimizer + optim = optimizers.optimizers["opacity"] + param = optim.param_groups[0]["params"][0] + param_state = optim.state[param] + param_state["exp_avg"] = torch.zeros_like(param_state["exp_avg"]) + param_state["exp_avg_sq"] = torch.zeros_like(param_state["exp_avg_sq"]) + + self.xys_grad_norm = None + self.vis_counts = None + self.max_2Dsize = None + + def cull_gaussians(self, extra_cull_mask: Optional[torch.Tensor] = None): + """ + This function deletes gaussians with under a certain opacity threshold + extra_cull_mask: a mask indicates extra gaussians to cull besides existing culling criterion + """ + n_bef = self.num_points + # cull transparent ones + culls = (torch.sigmoid(self.opacities) < self.config.cull_alpha_thresh).squeeze() + below_alpha_count = torch.sum(culls).item() + toobigs_count = 0 + if extra_cull_mask is not None: + culls = culls | extra_cull_mask + if self.step > self.config.refine_every * self.config.reset_alpha_every: + # cull huge ones + toobigs = (torch.exp(self.scales).max(dim=-1).values > self.config.cull_scale_thresh).squeeze() + if self.step < self.config.stop_screen_size_at: + # cull big screen space + assert self.max_2Dsize is not None + toobigs = toobigs | (self.max_2Dsize > self.config.cull_screen_size).squeeze() + culls = culls | toobigs + toobigs_count = torch.sum(toobigs).item() + self.means = Parameter(self.means[~culls].detach()) + self.scales = Parameter(self.scales[~culls].detach()) + self.quats = Parameter(self.quats[~culls].detach()) + self.features_dc = Parameter(self.features_dc[~culls].detach()) + self.features_rest = Parameter(self.features_rest[~culls].detach()) + self.opacities = Parameter(self.opacities[~culls].detach()) + + CONSOLE.log( + f"Culled {n_bef - self.num_points} gaussians " + f"({below_alpha_count} below alpha thresh, {toobigs_count} too bigs, {self.num_points} remaining)" + ) + + return culls + + def split_gaussians(self, split_mask, samps): + """ + This function splits gaussians that are too large + """ + + n_splits = split_mask.sum().item() + CONSOLE.log(f"Splitting {split_mask.sum().item()/self.num_points} gaussians: {n_splits}/{self.num_points}") + centered_samples = torch.randn((samps * n_splits, 3), device=self.device) # Nx3 of axis-aligned scales + scaled_samples = ( + torch.exp(self.scales[split_mask].repeat(samps, 1)) * centered_samples + ) # how these scales are rotated + quats = self.quats[split_mask] / self.quats[split_mask].norm(dim=-1, keepdim=True) # normalize them first + rots = quat_to_rotmat(quats.repeat(samps, 1)) # how these scales are rotated + rotated_samples = torch.bmm(rots, scaled_samples[..., None]).squeeze() + new_means = rotated_samples + self.means[split_mask].repeat(samps, 1) + # step 2, sample new colors + new_features_dc = self.features_dc[split_mask].repeat(samps, 1) + new_features_rest = self.features_rest[split_mask].repeat(samps, 1, 1) + # step 3, sample new opacities + new_opacities = self.opacities[split_mask].repeat(samps, 1) + # step 4, sample new scales + size_fac = 1.6 + new_scales = torch.log(torch.exp(self.scales[split_mask]) / size_fac).repeat(samps, 1) + self.scales[split_mask] = torch.log(torch.exp(self.scales[split_mask]) / size_fac) + # step 5, sample new quats + new_quats = self.quats[split_mask].repeat(samps, 1) + return new_means, new_features_dc, new_features_rest, new_opacities, new_scales, new_quats + + def dup_gaussians(self, dup_mask): + """ + This function duplicates gaussians that are too small + """ + n_dups = dup_mask.sum().item() + CONSOLE.log(f"Duplicating {dup_mask.sum().item()/self.num_points} gaussians: {n_dups}/{self.num_points}") + dup_means = self.means[dup_mask] + dup_features_dc = self.features_dc[dup_mask] + dup_features_rest = self.features_rest[dup_mask] + dup_opacities = self.opacities[dup_mask] + dup_scales = self.scales[dup_mask] + dup_quats = self.quats[dup_mask] + return dup_means, dup_features_dc, dup_features_rest, dup_opacities, dup_scales, dup_quats + + @property + def num_points(self): + return self.means.shape[0] + + def get_training_callbacks( + self, training_callback_attributes: TrainingCallbackAttributes + ) -> List[TrainingCallback]: + cbs = [] + cbs.append(TrainingCallback([TrainingCallbackLocation.BEFORE_TRAIN_ITERATION], self.step_cb)) + # The order of these matters + cbs.append( + TrainingCallback( + [TrainingCallbackLocation.AFTER_TRAIN_ITERATION], + self.after_train, + ) + ) + cbs.append( + TrainingCallback( + [TrainingCallbackLocation.AFTER_TRAIN_ITERATION], + self.refinement_after, + update_every_num_iters=self.config.refine_every, + args=[training_callback_attributes.optimizers], + ) + ) + return cbs + + def step_cb(self, step): + self.step = step + + def get_gaussian_param_groups(self) -> Dict[str, List[Parameter]]: + return { + "xyz": [self.means], + "features_dc": [self.features_dc], + "features_rest": [self.features_rest], + "opacity": [self.opacities], + "scaling": [self.scales], + "rotation": [self.quats], + } + + def get_param_groups(self) -> Dict[str, List[Parameter]]: + """Obtain the parameter groups for the optimizers + + Returns: + Mapping of different parameter groups + """ + gps = self.get_gaussian_param_groups() + # add camera optimizer param groups + self.camera_optimizer.get_param_groups(gps) + return gps + + def _get_downscale_factor(self): + if self.training: + return 2 ** max((self.config.num_downscales - self.step // self.config.resolution_schedule), 0) + else: + return 1 + + def get_outputs(self, camera: Cameras) -> Dict[str, Union[torch.Tensor, List]]: + """Takes in a Ray Bundle and returns a dictionary of outputs. + + Args: + ray_bundle: Input bundle of rays. This raybundle should have all the + needed information to compute the outputs. + + Returns: + Outputs of model. (ie. rendered colors) + """ + if not isinstance(camera, Cameras): + print("Called get_outputs with not a camera") + return {} + assert camera.shape[0] == 1, "Only one camera at a time" + if self.training: + # currently relies on the branch vickie/camera-grads + self.camera_optimizer.apply_to_camera(camera) + if self.training: + background = torch.rand(3, device=self.device) + else: + # logic for setting the background of the scene + if renderers.BACKGROUND_COLOR_OVERRIDE is not None: + background = renderers.BACKGROUND_COLOR_OVERRIDE + else: + background = self.back_color.to(self.device) + if self.crop_box is not None and not self.training: + crop_ids = self.crop_box.within(self.means).squeeze() + if crop_ids.sum() == 0: + return {"rgb": background.repeat(int(camera.height.item()), int(camera.width.item()), 1)} + else: + crop_ids = None + camera_downscale = self._get_downscale_factor() + camera.rescale_output_resolution(1 / camera_downscale) + # shift the camera to center of scene looking at center + R = camera.camera_to_worlds[0, :3, :3] # 3 x 3 + T = camera.camera_to_worlds[0, :3, 3:4] # 3 x 1 + # flip the z and y axes to align with gsplat conventions + R_edit = torch.diag(torch.tensor([1, -1, -1], device=self.device, dtype=R.dtype)) + R = R @ R_edit + # analytic matrix inverse to get world2camera matrix + R_inv = R.T + T_inv = -R_inv @ T + viewmat = torch.eye(4, device=R.device, dtype=R.dtype) + viewmat[:3, :3] = R_inv + viewmat[:3, 3:4] = T_inv + # calculate the FOV of the camera given fx and fy, width and height + cx = camera.cx.item() + cy = camera.cy.item() + fovx = 2 * math.atan(camera.width / (2 * camera.fx)) + fovy = 2 * math.atan(camera.height / (2 * camera.fy)) + W, H = camera.width.item(), camera.height.item() + self.last_size = (H, W) + projmat = projection_matrix(0.001, 1000, fovx, fovy, device=self.device) + BLOCK_X, BLOCK_Y = 16, 16 + tile_bounds = ( + (W + BLOCK_X - 1) // BLOCK_X, + (H + BLOCK_Y - 1) // BLOCK_Y, + 1, + ) + + if crop_ids is not None: + opacities_crop = self.opacities[crop_ids] + means_crop = self.means[crop_ids] + features_dc_crop = self.features_dc[crop_ids] + features_rest_crop = self.features_rest[crop_ids] + scales_crop = self.scales[crop_ids] + quats_crop = self.quats[crop_ids] + else: + opacities_crop = self.opacities + means_crop = self.means + features_dc_crop = self.features_dc + features_rest_crop = self.features_rest + scales_crop = self.scales + quats_crop = self.quats + + colors_crop = torch.cat((features_dc_crop[:, None, :], features_rest_crop), dim=1) + + self.xys, depths, self.radii, conics, num_tiles_hit, cov3d = ProjectGaussians.apply( + means_crop, + torch.exp(scales_crop), + 1, + quats_crop / quats_crop.norm(dim=-1, keepdim=True), + viewmat.squeeze()[:3, :], + projmat.squeeze() @ viewmat.squeeze(), + camera.fx.item(), + camera.fy.item(), + cx, + cy, + H, + W, + tile_bounds, + ) # type: ignore + if (self.radii).sum() == 0: + return {"rgb": background.repeat(int(camera.height.item()), int(camera.width.item()), 1)} + + # Important to allow xys grads to populate properly + if self.training: + self.xys.retain_grad() + + if self.config.sh_degree > 0: + viewdirs = means_crop.detach() - camera.camera_to_worlds.detach()[..., :3, 3] # (N, 3) + viewdirs = viewdirs / viewdirs.norm(dim=-1, keepdim=True) + n = min(self.step // self.config.sh_degree_interval, self.config.sh_degree) + rgbs = SphericalHarmonics.apply(n, viewdirs, colors_crop) + rgbs = torch.clamp(rgbs + 0.5, min=0.0) # type: ignore + else: + rgbs = torch.sigmoid(colors_crop[:, 0, :]) + + # rescale the camera back to original dimensions + camera.rescale_output_resolution(camera_downscale) + + # avoid empty rasterization + num_intersects, _ = compute_cumulative_intersects(self.xys.size(0), num_tiles_hit) + assert num_intersects > 0 + + rgb = RasterizeGaussians.apply( + self.xys, + depths, + self.radii, + conics, + num_tiles_hit, + rgbs, + torch.sigmoid(opacities_crop), + H, + W, + background, + ) # type: ignore + rgb = torch.clamp(rgb, max=1.0) # type: ignore + depth_im = None + if not self.training: + depth_im = RasterizeGaussians.apply( # type: ignore + self.xys, + depths, + self.radii, + conics, + num_tiles_hit, + depths[:, None].repeat(1, 3), + torch.sigmoid(opacities_crop), + H, + W, + torch.ones(3, device=self.device) * 10, + )[ + ..., 0:1 + ] # type: ignore + + return {"rgb": rgb, "depth": depth_im} # type: ignore + + def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]: + """Compute and returns metrics. + + Args: + outputs: the output to compute loss dict to + batch: ground truth batch corresponding to outputs + """ + d = self._get_downscale_factor() + if d > 1: + newsize = [batch["image"].shape[0] // d, batch["image"].shape[1] // d] + gt_img = TF.resize(batch["image"].permute(2, 0, 1), newsize, antialias=None).permute(1, 2, 0) + else: + gt_img = batch["image"] + metrics_dict = {} + gt_rgb = gt_img.to(self.device) # RGB or RGBA image + predicted_rgb = outputs["rgb"] + metrics_dict["psnr"] = self.psnr(predicted_rgb, gt_rgb) + + self.camera_optimizer.get_metrics_dict(metrics_dict) + metrics_dict["gaussian_count"] = self.num_points + return metrics_dict + + def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]: + """Computes and returns the losses dict. + + Args: + outputs: the output to compute loss dict to + batch: ground truth batch corresponding to outputs + metrics_dict: dictionary of metrics, some of which we can use for loss + """ + d = self._get_downscale_factor() + if d > 1: + newsize = [batch["image"].shape[0] // d, batch["image"].shape[1] // d] + gt_img = TF.resize(batch["image"].permute(2, 0, 1), newsize, antialias=None).permute(1, 2, 0) + else: + gt_img = batch["image"] + Ll1 = torch.abs(gt_img - outputs["rgb"]).mean() + simloss = 1 - self.ssim(gt_img.permute(2, 0, 1)[None, ...], outputs["rgb"].permute(2, 0, 1)[None, ...]) + if self.config.use_scale_regularization and self.step % 10 == 0: + scale_exp = torch.exp(self.scales) + scale_reg = ( + torch.maximum( + scale_exp.amax(dim=-1) / scale_exp.amin(dim=-1), torch.tensor(self.config.max_gauss_ratio) + ) + - self.config.max_gauss_ratio + ) + scale_reg = 0.1 * scale_reg.mean() + else: + scale_reg = torch.tensor(0.0).to(self.device) + + return { + "main_loss": (1 - self.config.ssim_lambda) * Ll1 + self.config.ssim_lambda * simloss, + "scale_reg": scale_reg, + } + + @torch.no_grad() + def get_outputs_for_camera(self, camera: Cameras, obb_box: Optional[OrientedBox] = None) -> Dict[str, torch.Tensor]: + """Takes in a camera, generates the raybundle, and computes the output of the model. + Overridden for a camera-based gaussian model. + + Args: + camera: generates raybundle + """ + assert camera is not None, "must provide camera to gaussian model" + self.set_crop(obb_box) + outs = self.get_outputs(camera.to(self.device)) + return outs # type: ignore + + def get_image_metrics_and_images( + self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor] + ) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]: + """Writes the test image outputs. + + Args: + image_idx: Index of the image. + step: Current step. + batch: Batch of data. + outputs: Outputs of the model. + + Returns: + A dictionary of metrics. + """ + d = self._get_downscale_factor() + if d > 1: + newsize = [batch["image"].shape[0] // d, batch["image"].shape[1] // d] + gt_img = TF.resize(batch["image"].permute(2, 0, 1), newsize, antialias=None).permute(1, 2, 0) + predicted_rgb = TF.resize(outputs["rgb"].permute(2, 0, 1), newsize, antialias=None).permute(1, 2, 0) + else: + gt_img = batch["image"] + predicted_rgb = outputs["rgb"] + + gt_rgb = gt_img.to(self.device) + + combined_rgb = torch.cat([gt_rgb, predicted_rgb], dim=1) + + # Switch images from [H, W, C] to [1, C, H, W] for metrics computations + gt_rgb = torch.moveaxis(gt_rgb, -1, 0)[None, ...] + predicted_rgb = torch.moveaxis(predicted_rgb, -1, 0)[None, ...] + + psnr = self.psnr(gt_rgb, predicted_rgb) + ssim = self.ssim(gt_rgb, predicted_rgb) + lpips = self.lpips(gt_rgb, predicted_rgb) + + # all of these metrics will be logged as scalars + metrics_dict = {"psnr": float(psnr.item()), "ssim": float(ssim)} # type: ignore + metrics_dict["lpips"] = float(lpips) + + images_dict = {"img": combined_rgb} + + return metrics_dict, images_dict diff --git a/nerfstudio/models/instant_ngp.py b/nerfstudio/models/instant_ngp.py index c67d45db5..48cf1a95a 100644 --- a/nerfstudio/models/instant_ngp.py +++ b/nerfstudio/models/instant_ngp.py @@ -37,7 +37,7 @@ from nerfstudio.engine.callbacks import ( from nerfstudio.field_components.field_heads import FieldHeadNames from nerfstudio.field_components.spatial_distortions import SceneContraction from nerfstudio.fields.nerfacto_field import NerfactoField -from nerfstudio.model_components.losses import MSELoss +from nerfstudio.model_components.losses import MSELoss, scale_gradients_by_distance_squared from nerfstudio.model_components.ray_samplers import VolumetricSampler from nerfstudio.model_components.renderers import ( AccumulationRenderer, @@ -78,6 +78,8 @@ class InstantNGPModelConfig(ModelConfig): """How far along ray to start sampling.""" far_plane: float = 1e3 """How far along ray to stop sampling.""" + use_gradient_scaling: bool = False + """Use gradient scaler where the gradients are lower for points closer to the camera.""" use_appearance_embedding: bool = False """Whether to use an appearance embedding.""" background_color: Literal["random", "black", "white"] = "random" @@ -187,6 +189,8 @@ class NGPModel(Model): ) field_outputs = self.field(ray_samples) + if self.config.use_gradient_scaling: + field_outputs = scale_gradients_by_distance_squared(field_outputs, ray_samples) # accumulation packed_info = nerfacc.pack_info(ray_indices, num_rays) diff --git a/nerfstudio/models/mipnerf.py b/nerfstudio/models/mipnerf.py index 67ca63251..cf48b08a7 100644 --- a/nerfstudio/models/mipnerf.py +++ b/nerfstudio/models/mipnerf.py @@ -29,7 +29,7 @@ from nerfstudio.cameras.rays import RayBundle from nerfstudio.field_components.encodings import NeRFEncoding from nerfstudio.field_components.field_heads import FieldHeadNames from nerfstudio.fields.vanilla_nerf_field import NeRFField -from nerfstudio.model_components.losses import MSELoss +from nerfstudio.model_components.losses import MSELoss, scale_gradients_by_distance_squared from nerfstudio.model_components.ray_samplers import PDFSampler, UniformSampler from nerfstudio.model_components.renderers import ( AccumulationRenderer, @@ -109,6 +109,8 @@ class MipNerfModel(Model): # First pass: field_outputs_coarse = self.field.forward(ray_samples_uniform) + if self.config.use_gradient_scaling: + field_outputs_coarse = scale_gradients_by_distance_squared(field_outputs_coarse, ray_samples_uniform) weights_coarse = ray_samples_uniform.get_weights(field_outputs_coarse[FieldHeadNames.DENSITY]) rgb_coarse = self.renderer_rgb( rgb=field_outputs_coarse[FieldHeadNames.RGB], @@ -122,6 +124,8 @@ class MipNerfModel(Model): # Second pass: field_outputs_fine = self.field.forward(ray_samples_pdf) + if self.config.use_gradient_scaling: + field_outputs_fine = scale_gradients_by_distance_squared(field_outputs_fine, ray_samples_pdf) weights_fine = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.DENSITY]) rgb_fine = self.renderer_rgb( rgb=field_outputs_fine[FieldHeadNames.RGB], diff --git a/nerfstudio/models/nerfacto.py b/nerfstudio/models/nerfacto.py index 1122d5c20..667d23eb8 100644 --- a/nerfstudio/models/nerfacto.py +++ b/nerfstudio/models/nerfacto.py @@ -28,6 +28,7 @@ from torchmetrics.functional import structural_similarity_index_measure from torchmetrics.image import PeakSignalNoiseRatio from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity +from nerfstudio.cameras.camera_optimizers import CameraOptimizer, CameraOptimizerConfig from nerfstudio.cameras.rays import RayBundle, RaySamples from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes, TrainingCallbackLocation from nerfstudio.field_components.field_heads import FieldHeadNames @@ -126,6 +127,8 @@ class NerfactoModelConfig(ModelConfig): """Which implementation to use for the model.""" appearance_embed_dim: int = 32 """Dimension of the appearance embedding.""" + camera_optimizer: CameraOptimizerConfig = field(default_factory=lambda: CameraOptimizerConfig(mode="SO3xR3")) + """Config of the camera optimizer to use""" class NerfactoModel(Model): @@ -165,6 +168,9 @@ class NerfactoModel(Model): implementation=self.config.implementation, ) + self.camera_optimizer: CameraOptimizer = self.config.camera_optimizer.setup( + num_cameras=self.num_train_data, device="cpu" + ) self.density_fns = [] num_prop_nets = self.config.num_proposal_iterations # Build the proposal network(s) @@ -229,7 +235,7 @@ class NerfactoModel(Model): # losses self.rgb_loss = MSELoss() - + self.step = 0 # metrics self.psnr = PeakSignalNoiseRatio(data_range=1.0) self.ssim = structural_similarity_index_measure @@ -240,6 +246,7 @@ class NerfactoModel(Model): param_groups = {} param_groups["proposal_networks"] = list(self.proposal_networks.parameters()) param_groups["fields"] = list(self.field.parameters()) + self.camera_optimizer.get_param_groups(param_groups=param_groups) return param_groups def get_training_callbacks( @@ -252,6 +259,7 @@ class NerfactoModel(Model): def set_anneal(step): # https://arxiv.org/pdf/2111.12077.pdf eq. 18 + self.step = step train_frac = np.clip(step / N, 0, 1) self.step = step @@ -278,6 +286,9 @@ class NerfactoModel(Model): return callbacks def get_outputs(self, ray_bundle: RayBundle): + # apply the camera optimizer pose tweaks + if self.training: + self.camera_optimizer.apply_to_raybundle(ray_bundle) ray_samples: RaySamples ray_samples, weights_list, ray_samples_list = self.proposal_sampler(ray_bundle, density_fns=self.density_fns) field_outputs = self.field.forward(ray_samples, compute_normals=self.config.predict_normals) @@ -324,7 +335,6 @@ class NerfactoModel(Model): for i in range(self.config.num_proposal_iterations): outputs[f"prop_depth_{i}"] = self.renderer_depth(weights=weights_list[i], ray_samples=ray_samples_list[i]) - return outputs def get_metrics_dict(self, outputs, batch): @@ -336,6 +346,8 @@ class NerfactoModel(Model): if self.training: metrics_dict["distortion"] = distortion_loss(outputs["weights_list"], outputs["ray_samples_list"]) + + self.camera_optimizer.get_metrics_dict(metrics_dict) return metrics_dict def get_loss_dict(self, outputs, batch, metrics_dict=None): @@ -364,6 +376,8 @@ class NerfactoModel(Model): loss_dict["pred_normal_loss"] = self.config.pred_normal_loss_mult * torch.mean( outputs["rendered_pred_normal_loss"] ) + # Add loss from camera optimizer + self.camera_optimizer.get_loss_dict(loss_dict) return loss_dict def get_image_metrics_and_images( diff --git a/nerfstudio/models/tensorf.py b/nerfstudio/models/tensorf.py index a0380d42e..0ca56f10d 100644 --- a/nerfstudio/models/tensorf.py +++ b/nerfstudio/models/tensorf.py @@ -43,7 +43,7 @@ from nerfstudio.field_components.encodings import ( ) from nerfstudio.field_components.field_heads import FieldHeadNames from nerfstudio.fields.tensorf_field import TensoRFField -from nerfstudio.model_components.losses import MSELoss, tv_loss +from nerfstudio.model_components.losses import MSELoss, tv_loss, scale_gradients_by_distance_squared from nerfstudio.model_components.ray_samplers import PDFSampler, UniformSampler from nerfstudio.model_components.renderers import ( AccumulationRenderer, @@ -53,6 +53,7 @@ from nerfstudio.model_components.renderers import ( from nerfstudio.model_components.scene_colliders import AABBBoxCollider from nerfstudio.models.base_model import Model, ModelConfig from nerfstudio.utils import colormaps, colors, misc +from nerfstudio.cameras.camera_optimizers import CameraOptimizer, CameraOptimizerConfig @dataclass @@ -89,6 +90,10 @@ class TensoRFModelConfig(ModelConfig): tensorf_encoding: Literal["triplane", "vm", "cp"] = "vm" regularization: Literal["none", "l1", "tv"] = "l1" """Regularization method used in tensorf paper""" + camera_optimizer: CameraOptimizerConfig = field(default_factory=lambda: CameraOptimizerConfig(mode="SO3xR3")) + """Config of the camera optimizer to use""" + use_gradient_scaling: bool = False + """Use gradient scaler where the gradients are lower for points closer to the camera.""" background_color: Literal["random", "last_sample", "black", "white"] = "white" """Whether to randomize the background color.""" @@ -256,6 +261,11 @@ class TensoRFModel(Model): if self.config.tensorf_encoding == "cp" and self.config.regularization == "tv": raise RuntimeError("TV reg not supported for CP decomposition") + # (optional) camera optimizer + self.camera_optimizer: CameraOptimizer = self.config.camera_optimizer.setup( + num_cameras=self.num_train_data, device="cpu" + ) + def get_param_groups(self) -> Dict[str, List[Parameter]]: param_groups = {} @@ -267,11 +277,14 @@ class TensoRFModel(Model): param_groups["encodings"] = list(self.field.color_encoding.parameters()) + list( self.field.density_encoding.parameters() ) + self.camera_optimizer.get_param_groups(param_groups=param_groups) return param_groups def get_outputs(self, ray_bundle: RayBundle): # uniform sampling + if self.training: + self.camera_optimizer.apply_to_raybundle(ray_bundle) ray_samples_uniform = self.sampler_uniform(ray_bundle) dens = self.field.get_density(ray_samples_uniform) weights = ray_samples_uniform.get_weights(dens) @@ -285,6 +298,8 @@ class TensoRFModel(Model): field_outputs_fine = self.field.forward( ray_samples_pdf, mask=acc_mask, bg_color=colors.WHITE.to(weights.device) ) + if self.config.use_gradient_scaling: + field_outputs_fine = scale_gradients_by_distance_squared(field_outputs_fine, ray_samples_pdf) weights_fine = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.DENSITY]) @@ -334,6 +349,8 @@ class TensoRFModel(Model): else: raise ValueError(f"Regularization {self.config.regularization} not supported") + self.camera_optimizer.get_loss_dict(loss_dict) + loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients) return loss_dict @@ -367,5 +384,7 @@ class TensoRFModel(Model): "ssim": float(ssim.item()), "lpips": float(lpips.item()), } + self.camera_optimizer.get_metrics_dict(metrics_dict) + images_dict = {"img": combined_rgb, "accumulation": acc, "depth": depth} return metrics_dict, images_dict diff --git a/nerfstudio/models/vanilla_nerf.py b/nerfstudio/models/vanilla_nerf.py index e95c8dfa4..4f4dbd8f6 100644 --- a/nerfstudio/models/vanilla_nerf.py +++ b/nerfstudio/models/vanilla_nerf.py @@ -33,7 +33,7 @@ from nerfstudio.field_components.encodings import NeRFEncoding from nerfstudio.field_components.field_heads import FieldHeadNames from nerfstudio.field_components.temporal_distortions import TemporalDistortionKind from nerfstudio.fields.vanilla_nerf_field import NeRFField -from nerfstudio.model_components.losses import MSELoss +from nerfstudio.model_components.losses import MSELoss, scale_gradients_by_distance_squared from nerfstudio.model_components.ray_samplers import PDFSampler, UniformSampler from nerfstudio.model_components.renderers import ( AccumulationRenderer, @@ -58,6 +58,8 @@ class VanillaModelConfig(ModelConfig): """Specifies whether or not to include ray warping based on time.""" temporal_distortion_params: Dict[str, Any] = to_immutable_dict({"kind": TemporalDistortionKind.DNERF}) """Parameters to instantiate temporal distortion with""" + use_gradient_scaling: bool = False + """Use gradient scaler where the gradients are lower for points closer to the camera.""" background_color: Literal["random", "last_sample", "black", "white"] = "white" """Whether to randomize the background color.""" @@ -154,6 +156,8 @@ class NeRFModel(Model): # coarse field: field_outputs_coarse = self.field_coarse.forward(ray_samples_uniform) + if self.config.use_gradient_scaling: + field_outputs_coarse = scale_gradients_by_distance_squared(field_outputs_coarse, ray_samples_uniform) weights_coarse = ray_samples_uniform.get_weights(field_outputs_coarse[FieldHeadNames.DENSITY]) rgb_coarse = self.renderer_rgb( rgb=field_outputs_coarse[FieldHeadNames.RGB], @@ -172,6 +176,8 @@ class NeRFModel(Model): # fine field: field_outputs_fine = self.field_fine.forward(ray_samples_pdf) + if self.config.use_gradient_scaling: + field_outputs_fine = scale_gradients_by_distance_squared(field_outputs_fine, ray_samples_pdf) weights_fine = ray_samples_pdf.get_weights(field_outputs_fine[FieldHeadNames.DENSITY]) rgb_fine = self.renderer_rgb( rgb=field_outputs_fine[FieldHeadNames.RGB], diff --git a/nerfstudio/pipelines/base_pipeline.py b/nerfstudio/pipelines/base_pipeline.py index 5e202b06c..b75adc6ad 100644 --- a/nerfstudio/pipelines/base_pipeline.py +++ b/nerfstudio/pipelines/base_pipeline.py @@ -26,18 +26,11 @@ from typing import Any, Dict, List, Literal, Mapping, Optional, Tuple, Type, Uni import torch import torch.distributed as dist -from PIL import Image -from rich.progress import ( - BarColumn, - MofNCompleteColumn, - Progress, - TextColumn, - TimeElapsedColumn, -) +from rich.progress import BarColumn, MofNCompleteColumn, Progress, TextColumn, TimeElapsedColumn from torch import nn +from torch.cuda.amp.grad_scaler import GradScaler from torch.nn import Parameter from torch.nn.parallel import DistributedDataParallel as DDP -from torch.cuda.amp.grad_scaler import GradScaler from nerfstudio.configs import base_config as cfg from nerfstudio.data.datamanagers.base_datamanager import ( @@ -45,6 +38,8 @@ from nerfstudio.data.datamanagers.base_datamanager import ( DataManagerConfig, VanillaDataManager, ) +from nerfstudio.data.datamanagers.parallel_datamanager import ParallelDataManager +from nerfstudio.data.datamanagers.full_images_datamanager import FullImageDatamanager from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes from nerfstudio.models.base_model import Model, ModelConfig from nerfstudio.utils import profiler @@ -223,9 +218,9 @@ class VanillaPipelineConfig(cfg.InstantiateConfig): _target: Type = field(default_factory=lambda: VanillaPipeline) """target class to instantiate""" - datamanager: DataManagerConfig = DataManagerConfig() + datamanager: DataManagerConfig = field(default_factory=lambda: DataManagerConfig()) """specifies the datamanager config""" - model: ModelConfig = ModelConfig() + model: ModelConfig = field(default_factory=lambda: ModelConfig()) """specifies the model config""" @@ -263,6 +258,15 @@ class VanillaPipeline(Pipeline): self.datamanager: DataManager = config.datamanager.setup( device=device, test_mode=test_mode, world_size=world_size, local_rank=local_rank ) + # TODO make cleaner + seed_pts = None + if ( + hasattr(self.datamanager, "train_dataparser_outputs") + and "points3D_xyz" in self.datamanager.train_dataparser_outputs.metadata + ): + pts = self.datamanager.train_dataparser_outputs.metadata["points3D_xyz"] + pts_rgb = self.datamanager.train_dataparser_outputs.metadata["points3D_rgb"] + seed_pts = (pts, pts_rgb) self.datamanager.to(device) # TODO(ethan): get rid of scene_bounds from the model assert self.datamanager.train_dataset is not None, "Missing input dataset" @@ -273,6 +277,7 @@ class VanillaPipeline(Pipeline): metadata=self.datamanager.train_dataset.metadata, device=device, grad_scaler=grad_scaler, + seed_points=seed_pts, ) self.model.to(device) @@ -298,18 +303,6 @@ class VanillaPipeline(Pipeline): ray_bundle, batch = self.datamanager.next_train(step) model_outputs = self._model(ray_bundle) # train distributed data parallel model if world_size > 1 metrics_dict = self.model.get_metrics_dict(model_outputs, batch) - - if self.config.datamanager.camera_optimizer is not None: - camera_opt_param_group = self.config.datamanager.camera_optimizer.param_group - if camera_opt_param_group in self.datamanager.get_param_groups(): - # Report the camera optimization metrics - metrics_dict["camera_opt_translation"] = ( - self.datamanager.get_param_groups()[camera_opt_param_group][0].data[:, :3].norm() - ) - metrics_dict["camera_opt_rotation"] = ( - self.datamanager.get_param_groups()[camera_opt_param_group][0].data[:, 3:].norm() - ) - loss_dict = self.model.get_loss_dict(model_outputs, batch, metrics_dict) return model_outputs, loss_dict, metrics_dict @@ -346,13 +339,11 @@ class VanillaPipeline(Pipeline): step: current iteration step """ self.eval() - image_idx, camera_ray_bundle, batch = self.datamanager.next_eval_image(step) - outputs = self.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle) + camera, batch = self.datamanager.next_eval_image(step) + outputs = self.model.get_outputs_for_camera(camera) metrics_dict, images_dict = self.model.get_image_metrics_and_images(outputs, batch) - assert "image_idx" not in metrics_dict - metrics_dict["image_idx"] = image_idx assert "num_rays" not in metrics_dict - metrics_dict["num_rays"] = len(camera_ray_bundle) + metrics_dict["num_rays"] = (camera.height * camera.width * camera.size).item() self.train() return metrics_dict, images_dict @@ -372,7 +363,7 @@ class VanillaPipeline(Pipeline): """ self.eval() metrics_dict_list = [] - assert isinstance(self.datamanager, VanillaDataManager) + assert isinstance(self.datamanager, (VanillaDataManager, ParallelDataManager, FullImageDatamanager)) num_images = len(self.datamanager.fixed_indices_eval_dataloader) with Progress( TextColumn("[progress.description]{task.description}"), @@ -382,26 +373,21 @@ class VanillaPipeline(Pipeline): transient=True, ) as progress: task = progress.add_task("[green]Evaluating all eval images...", total=num_images) - for camera_ray_bundle, batch in self.datamanager.fixed_indices_eval_dataloader: + for camera, batch in self.datamanager.fixed_indices_eval_dataloader: # time this the following line inner_start = time() - height, width = camera_ray_bundle.shape + outputs = self.model.get_outputs_for_camera(camera=camera) + height, width = camera.height, camera.width num_rays = height * width - outputs = self.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle) - metrics_dict, images_dict = self.model.get_image_metrics_and_images(outputs, batch) - + metrics_dict, _ = self.model.get_image_metrics_and_images(outputs, batch) if output_path is not None: - camera_indices = camera_ray_bundle.camera_indices - assert camera_indices is not None - for key, val in images_dict.items(): - Image.fromarray((val * 255).byte().cpu().numpy()).save( - output_path / "{0:06d}-{1}.jpg".format(int(camera_indices[0, 0, 0]), key) - ) + raise NotImplementedError("Saving images is not implemented yet") + assert "num_rays_per_sec" not in metrics_dict - metrics_dict["num_rays_per_sec"] = num_rays / (time() - inner_start) + metrics_dict["num_rays_per_sec"] = (num_rays / (time() - inner_start)).item() fps_str = "fps" assert fps_str not in metrics_dict - metrics_dict[fps_str] = metrics_dict["num_rays_per_sec"] / (height * width) + metrics_dict[fps_str] = (metrics_dict["num_rays_per_sec"] / (height * width)).item() metrics_dict_list.append(metrics_dict) progress.advance(task) # average the metrics list diff --git a/nerfstudio/process_data/colmap_converter_to_nerfstudio_dataset.py b/nerfstudio/process_data/colmap_converter_to_nerfstudio_dataset.py index 7ab711b46..924c4b752 100644 --- a/nerfstudio/process_data/colmap_converter_to_nerfstudio_dataset.py +++ b/nerfstudio/process_data/colmap_converter_to_nerfstudio_dataset.py @@ -41,6 +41,9 @@ class ColmapConverterToNerfstudioDataset(BaseConverterToNerfstudioDataset): refine_pixsfm: bool = False """If True, runs refinement using Pixel Perfect SFM. Only works with hloc sfm_tool""" + refine_intrinsics: bool = True + """If True, do bundle adjustment to refine intrinsics. + Only works with colmap sfm_tool""" feature_type: Literal[ "any", "sift", @@ -63,6 +66,8 @@ class ColmapConverterToNerfstudioDataset(BaseConverterToNerfstudioDataset): "NN-ratio", "NN-mutual", "adalam", + "disk+lightglue", + "superpoint+lightglue", ] = "any" """Matching algorithm.""" num_downscales: int = 3 @@ -204,6 +209,7 @@ class ColmapConverterToNerfstudioDataset(BaseConverterToNerfstudioDataset): gpu=self.gpu, verbose=self.verbose, matching_method=self.matching_method, + refine_intrinsics=self.refine_intrinsics, colmap_cmd=self.colmap_cmd, ) elif sfm_tool == "hloc": diff --git a/nerfstudio/process_data/colmap_utils.py b/nerfstudio/process_data/colmap_utils.py index 2322075b5..27d6e8615 100644 --- a/nerfstudio/process_data/colmap_utils.py +++ b/nerfstudio/process_data/colmap_utils.py @@ -55,7 +55,9 @@ def get_colmap_version(colmap_cmd: str, default_version=3.8) -> float: assert output is not None for line in output.split("\n"): if line.startswith("COLMAP"): - return float(line.split(" ")[1]) + version = line.split(" ")[1] + version = "".join([c for c in version if c.isdigit() or c == "."]) + return float(version) CONSOLE.print(f"[bold red]Could not find COLMAP version. Using default {default_version}") return default_version @@ -93,6 +95,7 @@ def run_colmap( gpu: bool = True, verbose: bool = False, matching_method: Literal["vocab_tree", "exhaustive", "sequential"] = "vocab_tree", + refine_intrinsics: bool = True, colmap_cmd: str = "colmap", ) -> None: """Runs COLMAP on the images. @@ -105,6 +108,7 @@ def run_colmap( gpu: If True, use GPU. verbose: If True, logs the output of the command. matching_method: Matching method to use. + refine_intrinsics: If True, refine intrinsics. colmap_cmd: Path to the COLMAP executable. """ @@ -154,7 +158,7 @@ def run_colmap( f"--output_path {sparse_dir}", ] if colmap_version >= 3.7: - mapper_cmd.append("--Mapper.ba_global_function_tolerance 1e-6") + mapper_cmd.append("--Mapper.ba_global_function_tolerance=1e-6") mapper_cmd = " ".join(mapper_cmd) @@ -165,15 +169,17 @@ def run_colmap( ): run_command(mapper_cmd, verbose=verbose) CONSOLE.log("[bold green]:tada: Done COLMAP bundle adjustment.") - with status(msg="[bold yellow]Refine intrinsics...", spinner="dqpb", verbose=verbose): - bundle_adjuster_cmd = [ - f"{colmap_cmd} bundle_adjuster", - f"--input_path {sparse_dir}/0", - f"--output_path {sparse_dir}/0", - "--BundleAdjustment.refine_principal_point 1", - ] - run_command(" ".join(bundle_adjuster_cmd), verbose=verbose) - CONSOLE.log("[bold green]:tada: Done refining intrinsics.") + + if refine_intrinsics: + with status(msg="[bold yellow]Refine intrinsics...", spinner="dqpb", verbose=verbose): + bundle_adjuster_cmd = [ + f"{colmap_cmd} bundle_adjuster", + f"--input_path {sparse_dir}/0", + f"--output_path {sparse_dir}/0", + "--BundleAdjustment.refine_principal_point 1", + ] + run_command(" ".join(bundle_adjuster_cmd), verbose=verbose) + CONSOLE.log("[bold green]:tada: Done refining intrinsics.") def parse_colmap_camera_params(camera) -> Dict[str, Any]: diff --git a/nerfstudio/process_data/equirect_utils.py b/nerfstudio/process_data/equirect_utils.py index 497cb3233..232d96733 100644 --- a/nerfstudio/process_data/equirect_utils.py +++ b/nerfstudio/process_data/equirect_utils.py @@ -304,7 +304,7 @@ def generate_planar_projections_from_equirectangular( with progress: for i in progress.track(os.listdir(frame_dir), description="", total=num_ims): - if i.lower().endswith((".jpg", ".png", ".jpeg")): + if i.lower().endswith((".jpg", ".png", ".jpeg", ".tiff", ".tif")): im = np.array(cv2.imread(os.path.join(frame_dir, i))) im = torch.tensor(im, dtype=torch.float32, device=device) im = torch.permute(im, (2, 0, 1)).unsqueeze(0) / 255.0 @@ -331,7 +331,7 @@ def compute_resolution_from_equirect(image_dir: Path, num_images: int) -> Tuple[ """ for i in os.listdir(image_dir): - if i.lower().endswith((".jpg", ".png", ".jpeg")): + if i.lower().endswith((".jpg", ".png", ".jpeg", ".tiff", ".tif")): im = np.array(cv2.imread(os.path.join(image_dir, i))) res_squared = (im.shape[0] * im.shape[1]) / num_images return (int(np.sqrt(res_squared)), int(np.sqrt(res_squared))) diff --git a/nerfstudio/process_data/hloc_utils.py b/nerfstudio/process_data/hloc_utils.py index 85049b5bf..b7167efd8 100644 --- a/nerfstudio/process_data/hloc_utils.py +++ b/nerfstudio/process_data/hloc_utils.py @@ -29,7 +29,7 @@ from nerfstudio.utils.rich_utils import CONSOLE try: # TODO(1480) un-hide pycolmap import import pycolmap - from hloc import ( + from hloc import ( # type: ignore extract_features, match_features, pairs_from_exhaustive, @@ -38,6 +38,7 @@ try: ) except ImportError: _HAS_HLOC = False + else: _HAS_HLOC = True @@ -59,7 +60,14 @@ def run_hloc( "sift", "superpoint_aachen", "superpoint_max", "superpoint_inloc", "r2d2", "d2net-ss", "sosnet", "disk" ] = "superpoint_aachen", matcher_type: Literal[ - "superglue", "superglue-fast", "NN-superpoint", "NN-ratio", "NN-mutual", "adalam" + "superglue", + "superglue-fast", + "NN-superpoint", + "NN-ratio", + "NN-mutual", + "adalam", + "disk+lightglue", + "superpoint+lightglue", ] = "superglue", num_matched: int = 50, refine_pixsfm: bool = False, @@ -95,24 +103,24 @@ def run_hloc( features = outputs / "features.h5" matches = outputs / "matches.h5" - retrieval_conf = extract_features.confs["netvlad"] - feature_conf = extract_features.confs[feature_type] - matcher_conf = match_features.confs[matcher_type] + retrieval_conf = extract_features.confs["netvlad"] # type: ignore + feature_conf = extract_features.confs[feature_type] # type: ignore + matcher_conf = match_features.confs[matcher_type] # type: ignore references = [p.relative_to(image_dir).as_posix() for p in image_dir.iterdir()] - extract_features.main(feature_conf, image_dir, image_list=references, feature_path=features) + extract_features.main(feature_conf, image_dir, image_list=references, feature_path=features) # type: ignore if matching_method == "exhaustive": - pairs_from_exhaustive.main(sfm_pairs, image_list=references) + pairs_from_exhaustive.main(sfm_pairs, image_list=references) # type: ignore else: - retrieval_path = extract_features.main(retrieval_conf, image_dir, outputs) + retrieval_path = extract_features.main(retrieval_conf, image_dir, outputs) # type: ignore if num_matched >= len(references): num_matched = len(references) - pairs_from_retrieval.main(retrieval_path, sfm_pairs, num_matched=num_matched) - match_features.main(matcher_conf, sfm_pairs, features=features, matches=matches) + pairs_from_retrieval.main(retrieval_path, sfm_pairs, num_matched=num_matched) # type: ignore + match_features.main(matcher_conf, sfm_pairs, features=features, matches=matches) # type: ignore - image_options = pycolmap.ImageReaderOptions(camera_model=camera_model.value) + image_options = pycolmap.ImageReaderOptions(camera_model=camera_model.value) # type: ignore if refine_pixsfm: - sfm = PixSfM( + sfm = PixSfM( # type: ignore conf={ "dense_features": {"use_cache": True}, "KA": {"dense_features": {"use_cache": True}, "max_kps_per_problem": 1000}, @@ -126,20 +134,20 @@ def run_hloc( features, matches, image_list=references, - camera_mode=pycolmap.CameraMode.SINGLE, + camera_mode=pycolmap.CameraMode.SINGLE, # type: ignore image_options=image_options, verbose=verbose, ) print("Refined", refined.summary()) else: - reconstruction.main( + reconstruction.main( # type: ignore sfm_dir, image_dir, sfm_pairs, features, matches, - camera_mode=pycolmap.CameraMode.SINGLE, + camera_mode=pycolmap.CameraMode.SINGLE, # type: ignore image_options=image_options, verbose=verbose, ) diff --git a/nerfstudio/process_data/odm_utils.py b/nerfstudio/process_data/odm_utils.py new file mode 100644 index 000000000..aa9b69541 --- /dev/null +++ b/nerfstudio/process_data/odm_utils.py @@ -0,0 +1,148 @@ +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Helper utils for processing ODM data into the nerfstudio format.""" + +import json +from pathlib import Path +from typing import Dict, List +import os +import sys +import math + +import numpy as np + +from nerfstudio.process_data.process_data_utils import CAMERA_MODELS + + +def rodrigues_vec_to_rotation_mat(rodrigues_vec: np.ndarray) -> np.ndarray: + theta = np.linalg.norm(rodrigues_vec) + if theta < sys.float_info.epsilon: + rotation_mat = np.eye(3, dtype=float) + else: + r = rodrigues_vec / theta + ident = np.eye(3, dtype=float) + r_rT = np.array( + [ + [r[0] * r[0], r[0] * r[1], r[0] * r[2]], + [r[1] * r[0], r[1] * r[1], r[1] * r[2]], + [r[2] * r[0], r[2] * r[1], r[2] * r[2]], + ] + ) + r_cross = np.array([[0, -r[2], r[1]], [r[2], 0, -r[0]], [-r[1], r[0], 0]], dtype=float) + rotation_mat = math.cos(theta) * ident + (1 - math.cos(theta)) * r_rT + math.sin(theta) * r_cross + return rotation_mat + + +def cameras2nerfds( + image_filename_map: Dict[str, Path], + cameras_file: Path, + shots_file: Path, + output_dir: Path, + verbose: bool = False, +) -> List[str]: + """Convert ODM cameras into a nerfstudio dataset. + + Args: + image_filename_map: Mapping of original image filenames to their saved locations. + shots_file: Path to ODM's shots.geojson + output_dir: Path to the output directory. + verbose: Whether to print verbose output. + + Returns: + Summary of the conversion. + """ + + with open(cameras_file, "r", encoding="utf-8") as f: + cameras = json.loads(f.read()) + with open(shots_file, "r", encoding="utf-8") as f: + shots = json.loads(f.read()) + + camera_ids = list(cameras.keys()) + if len(camera_ids) > 1: + raise ValueError("Only one camera is supported") + camera_id = camera_ids[0] + camera = cameras[camera_id] + data = {} + if camera["projection_type"] in ["brown", "perspective"]: + data["camera_model"] = CAMERA_MODELS["perspective"].value + elif camera["projection_type"] in ["fisheye", "fisheye_opencv"]: + data["camera_model"] = CAMERA_MODELS["fisheye"].value + elif camera["projection_type"] in ["spherical", "equirectangular"]: + data["camera_model"] = CAMERA_MODELS["equirectangular"].value + else: + raise ValueError("Unsupported ODM camera model: " + data["camera_model"]) + + sensor_dict = {} + s = {"w": int(camera["width"]), "h": int(camera["height"])} + + s["fl_x"] = camera.get("focal_x", camera.get("focal")) * max(s["w"], s["h"]) + s["fl_y"] = camera.get("focal_y", camera.get("focal")) * max(s["w"], s["h"]) + + s["cx"] = camera["c_x"] + (s["w"] - 1.0) / 2.0 + s["cy"] = camera["c_y"] + (s["h"] - 1.0) / 2.0 + + for p in ["k1", "k2", "p1", "p2", "k3"]: + if p in camera: + s[p] = camera[p] + + sensor_dict[camera_id] = s + + shots = shots["features"] + shots_dict = {} + for shot in shots: + props = shot["properties"] + filename = props["filename"] + rotation = rodrigues_vec_to_rotation_mat(np.array(props["rotation"]) * -1) + translation = np.array(props["translation"]) + + m = np.eye(4) + m[:3, :3] = rotation + m[:3, 3] = translation + + name, ext = os.path.splitext(filename) + shots_dict[name] = m + + frames = [] + num_skipped = 0 + + for fname in shots_dict: + transform = shots_dict[fname] + if fname not in image_filename_map: + num_skipped += 1 + continue + + frame = {} + frame["file_path"] = image_filename_map[fname].as_posix() + frame.update(sensor_dict[camera_id]) + + transform = transform[[2, 0, 1, 3], :] + transform[:, 1:3] *= -1 + frame["transform_matrix"] = transform.tolist() + frames.append(frame) + + data["frames"] = frames + + with open(output_dir / "transforms.json", "w", encoding="utf-8") as f: + json.dump(data, f, indent=4) + + summary = [] + if num_skipped == 1: + summary.append(f"{num_skipped} image skipped because it was missing its camera pose.") + if num_skipped > 1: + summary.append(f"{num_skipped} images were skipped because they were missing camera poses.") + + summary.append(f"Final dataset is {len(data['frames'])} frames.") + + return summary diff --git a/nerfstudio/process_data/process_data_utils.py b/nerfstudio/process_data/process_data_utils.py index 334dab4de..c1946305b 100644 --- a/nerfstudio/process_data/process_data_utils.py +++ b/nerfstudio/process_data/process_data_utils.py @@ -24,9 +24,13 @@ from typing import List, Literal, Optional, OrderedDict, Tuple, Union import cv2 import imageio -import numpy as np -import rawpy +try: + import rawpy +except ImportError: + import newrawpy as rawpy # type: ignore + +import numpy as np from nerfstudio.utils.rich_utils import CONSOLE, status from nerfstudio.utils.scripts import run_command @@ -352,7 +356,11 @@ def copy_and_upscale_polycam_depth_maps_list( depth_dir.mkdir(parents=True, exist_ok=True) # copy and upscale them to new directory - with status(msg="[bold yellow] Upscaling depth maps...", spinner="growVertical", verbose=verbose): + with status( + msg="[bold yellow] Upscaling depth maps...", + spinner="growVertical", + verbose=verbose, + ): upscale_factor = 2**POLYCAM_UPSCALING_TIMES assert upscale_factor > 1 assert isinstance(upscale_factor, int) @@ -437,7 +445,11 @@ def downscale_images( if num_downscales == 0: return "No downscaling performed." - with status(msg="[bold yellow]Downscaling images...", spinner="growVertical", verbose=verbose): + with status( + msg="[bold yellow]Downscaling images...", + spinner="growVertical", + verbose=verbose, + ): downscale_factors = [2**i for i in range(num_downscales + 1)[1:]] for downscale_factor in downscale_factors: assert downscale_factor > 1 @@ -477,7 +489,16 @@ def find_tool_feature_matcher_combination( "disk", ], matcher_type: Literal[ - "any", "NN", "superglue", "superglue-fast", "NN-superpoint", "NN-ratio", "NN-mutual", "adalam" + "any", + "NN", + "superglue", + "superglue-fast", + "NN-superpoint", + "NN-ratio", + "NN-mutual", + "adalam", + "disk+lightglue", + "superpoint+lightglue", ], ) -> Union[ Tuple[None, None, None], @@ -493,7 +514,17 @@ def find_tool_feature_matcher_combination( "sosnet", "disk", ], - Literal["NN", "superglue", "superglue-fast", "NN-superpoint", "NN-ratio", "NN-mutual", "adalam"], + Literal[ + "NN", + "superglue", + "superglue-fast", + "NN-superpoint", + "NN-ratio", + "NN-mutual", + "adalam", + "disk+lightglue", + "superpoint+lightglue", + ], ], ]: """Find a valid combination of sfm tool, feature type, and matcher type. @@ -581,7 +612,10 @@ def generate_crop_mask(height: int, width: int, crop_factor: Tuple[float, float, def generate_mask( - height: int, width: int, crop_factor: Tuple[float, float, float, float], percent_radius: float + height: int, + width: int, + crop_factor: Tuple[float, float, float, float], + percent_radius: float, ) -> Optional[np.ndarray]: """generate a mask of the given size. diff --git a/nerfstudio/scripts/datasets/process_project_aria.py b/nerfstudio/scripts/datasets/process_project_aria.py new file mode 100644 index 000000000..10f26653f --- /dev/null +++ b/nerfstudio/scripts/datasets/process_project_aria.py @@ -0,0 +1,231 @@ +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import sys +import threading +from dataclasses import dataclass +from pathlib import Path +from typing import Dict, List + +import numpy as np +import tyro +from PIL import Image + +try: + from projectaria_tools.core import mps + from projectaria_tools.core.data_provider import VrsDataProvider, create_vrs_data_provider + from projectaria_tools.core.sophus import SE3 +except ImportError: + print("projectaria_tools import failed, please install with pip3 install projectaria-tools'[all]'") + sys.exit(1) + +ARIA_CAMERA_MODEL = "FISHEYE624" + +# The Aria coordinate system is different than the Blender/NerfStudio coordinate system. +# Blender / Nerfstudio: +Z = back, +Y = up, +X = right +# Surreal: +Z = forward, +Y = down, +X = right +T_ARIA_NERFSTUDIO = SE3.from_matrix( + np.array( + [ + [1.0, 0.0, 0.0, 0.0], + [0.0, -1.0, 0.0, 0.0], + [0.0, 0.0, -1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + ) +) + + +@dataclass +class AriaCameraCalibration: + fx: float + fy: float + cx: float + cy: float + distortion_params: np.ndarray + width: int + height: int + t_device_camera: SE3 + + +@dataclass +class AriaImageFrame: + camera: AriaCameraCalibration + file_path: str + t_world_camera: SE3 + timestamp_ns: float + + +@dataclass +class TimedPoses: + timestamps_ns: np.ndarray + t_world_devices: List[SE3] + + +def get_camera_calibs(provider: VrsDataProvider) -> Dict[str, AriaCameraCalibration]: + """Retrieve the per-camera factory calibration from within the VRS.""" + + factory_calib = {} + name = "camera-rgb" + device_calib = provider.get_device_calibration() + assert device_calib is not None, "Could not find device calibration" + sensor_calib = device_calib.get_camera_calib(name) + assert sensor_calib is not None, f"Could not find sensor calibration for {name}" + + width = sensor_calib.get_image_size()[0].item() + height = sensor_calib.get_image_size()[1].item() + intrinsics = sensor_calib.projection_params() + + factory_calib[name] = AriaCameraCalibration( + fx=intrinsics[0], + fy=intrinsics[0], + cx=intrinsics[1], + cy=intrinsics[2], + distortion_params=intrinsics[3:15], + width=width, + height=height, + t_device_camera=sensor_calib.get_transform_device_camera(), + ) + + return factory_calib + + +def read_trajectory_csv_to_dict(file_iterable_csv: str) -> TimedPoses: + closed_loop_traj = mps.read_closed_loop_trajectory(file_iterable_csv) # type: ignore + + timestamps_secs, poses = zip( + *[(it.tracking_timestamp.total_seconds(), it.transform_world_device) for it in closed_loop_traj] + ) + + SEC_TO_NANOSEC = 1e9 + return TimedPoses( + timestamps_ns=(np.array(timestamps_secs) * SEC_TO_NANOSEC).astype(int), + t_world_devices=poses, + ) + + +def to_aria_image_frame( + provider: VrsDataProvider, + index: int, + name_to_camera: Dict[str, AriaCameraCalibration], + t_world_devices: TimedPoses, + output_dir: Path, +) -> AriaImageFrame: + name = "camera-rgb" + + camera_calibration = name_to_camera[name] + stream_id = provider.get_stream_id_from_label(name) + assert stream_id is not None, f"Could not find stream {name}" + + # Get the image corresponding to this index + image_data = provider.get_image_data_by_index(stream_id, index) + img = Image.fromarray(image_data[0].to_numpy_array()) + capture_time_ns = image_data[1].capture_timestamp_ns + + file_path = f"{output_dir}/{name}_{capture_time_ns}.jpg" + threading.Thread(target=lambda: img.save(file_path)).start() + + # Find the nearest neighbor pose with the closest timestamp to the capture time. + nearest_pose_idx = np.searchsorted(t_world_devices.timestamps_ns, capture_time_ns) + nearest_pose_idx = np.minimum(nearest_pose_idx, len(t_world_devices.timestamps_ns) - 1) + assert nearest_pose_idx != -1, f"Could not find pose for {capture_time_ns}" + t_world_device = t_world_devices.t_world_devices[nearest_pose_idx] + + # Compute the world to camera transform. + t_world_camera = t_world_device @ camera_calibration.t_device_camera @ T_ARIA_NERFSTUDIO + + return AriaImageFrame( + camera=camera_calibration, + file_path=file_path, + t_world_camera=t_world_camera, + timestamp_ns=capture_time_ns, + ) + + +def to_nerfstudio_frame(frame: AriaImageFrame) -> Dict: + return { + "fl_x": frame.camera.fx, + "fl_y": frame.camera.fy, + "cx": frame.camera.cx, + "cy": frame.camera.cy, + "distortion_params": frame.camera.distortion_params.tolist(), + "w": frame.camera.width, + "h": frame.camera.height, + "file_path": frame.file_path, + "transform_matrix": frame.t_world_camera.to_matrix().tolist(), + "timestamp": frame.timestamp_ns, + } + + +@dataclass +class ProcessProjectAria: + """Processes Project Aria data i.e. a VRS of the raw recording streams and the MPS attachments + that provide poses, calibration, and 3d points. More information on MPS data can be found at: + https://facebookresearch.github.io/projectaria_tools/docs/ARK/mps. + """ + + vrs_file: Path + """Path to the VRS file.""" + mps_data_dir: Path + """Path to Project Aria Machine Perception Services (MPS) attachments.""" + output_dir: Path + """Path to the output directory.""" + + def main(self) -> None: + """Generate a nerfstudio dataset from ProjectAria data (VRS) and MPS attachments.""" + # Create output directory if it doesn't exist. + self.output_dir = self.output_dir.absolute() + self.output_dir.mkdir(parents=True, exist_ok=True) + + provider = create_vrs_data_provider(str(self.vrs_file.absolute())) + assert provider is not None, "Cannot open file" + + name_to_camera = get_camera_calibs(provider) + + print("Getting poses from closed loop trajectory CSV...") + trajectory_csv = self.mps_data_dir / "closed_loop_trajectory.csv" + t_world_devices = read_trajectory_csv_to_dict(str(trajectory_csv.absolute())) + + name = "camera-rgb" + stream_id = provider.get_stream_id_from_label(name) + + # create an AriaImageFrame for each image in the VRS. + print("Creating Aria frames...") + aria_frames = [ + to_aria_image_frame(provider, index, name_to_camera, t_world_devices, self.output_dir) + for index in range(0, provider.get_num_data(stream_id)) + ] + + # create the NerfStudio frames from the AriaImageFrames. + print("Creating NerfStudio frames...") + CANONICAL_RGB_VALID_RADIUS = 707.5 + CANONICAL_RGB_WIDTH = 1408 + rgb_valid_radius = CANONICAL_RGB_VALID_RADIUS * (aria_frames[0].camera.width / CANONICAL_RGB_WIDTH) + nerfstudio_frames = { + "camera_model": ARIA_CAMERA_MODEL, + "frames": [to_nerfstudio_frame(frame) for frame in aria_frames], + "fisheye_crop_radius": rgb_valid_radius, + } + + # write the json out to disk as transforms.json + print("Writing transforms.json") + transform_file = self.output_dir / "transforms.json" + with open(transform_file, "w", encoding="UTF-8"): + transform_file.write_text(json.dumps(nerfstudio_frames)) + + +if __name__ == "__main__": + tyro.extras.set_accent_color("bright_yellow") + tyro.cli(ProcessProjectAria).main() diff --git a/nerfstudio/scripts/eval.py b/nerfstudio/scripts/eval.py index 751b318a2..103467bbd 100644 --- a/nerfstudio/scripts/eval.py +++ b/nerfstudio/scripts/eval.py @@ -45,7 +45,7 @@ class ComputePSNR: config, pipeline, checkpoint_path, _ = eval_setup(self.load_config) assert self.output_path.suffix == ".json" if self.render_output_path is not None: - self.render_output_path.mkdir(parents=True) + self.render_output_path.mkdir(parents=True, exist_ok=True) metrics_dict = pipeline.get_average_eval_image_metrics(output_path=self.render_output_path, get_std=True) self.output_path.parent.mkdir(parents=True, exist_ok=True) # Get the output and define the names to save to diff --git a/nerfstudio/scripts/exporter.py b/nerfstudio/scripts/exporter.py index 2e4fc68ca..1eef27dbf 100644 --- a/nerfstudio/scripts/exporter.py +++ b/nerfstudio/scripts/exporter.py @@ -34,17 +34,13 @@ from typing_extensions import Annotated, Literal from nerfstudio.cameras.rays import RayBundle from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager +from nerfstudio.data.datamanagers.parallel_datamanager import ParallelDataManager from nerfstudio.data.scene_box import OrientedBox from nerfstudio.exporter import texture_utils, tsdf_utils -from nerfstudio.exporter.exporter_utils import ( - collect_camera_poses, - generate_point_cloud, - get_mesh_from_filename, -) -from nerfstudio.exporter.marching_cubes import ( - generate_mesh_with_multires_marching_cubes, -) -from nerfstudio.fields.sdf_field import SDFField +from nerfstudio.exporter.exporter_utils import collect_camera_poses, generate_point_cloud, get_mesh_from_filename +from nerfstudio.exporter.marching_cubes import generate_mesh_with_multires_marching_cubes +from nerfstudio.fields.sdf_field import SDFField # noqa +from nerfstudio.models.gaussian_splatting import GaussianSplattingModel from nerfstudio.pipelines.base_pipeline import Pipeline, VanillaPipeline from nerfstudio.utils.eval_utils import eval_setup from nerfstudio.utils.rich_utils import CONSOLE @@ -125,6 +121,9 @@ class ExportPointCloud(Exporter): """Number of rays to evaluate per batch. Decrease if you run out of memory.""" std_ratio: float = 10.0 """Threshold based on STD of the average distances across the point cloud to remove outliers.""" + save_world_frame: bool = True + """If true, saves in the frame of the transform.json file, if false saves in the frame of the scaled + dataparser transform""" def main(self) -> None: """Export point cloud.""" @@ -137,7 +136,7 @@ class ExportPointCloud(Exporter): validate_pipeline(self.normal_method, self.normal_output_name, pipeline) # Increase the batchsize to speed up the evaluation. - assert isinstance(pipeline.datamanager, VanillaDataManager) + assert isinstance(pipeline.datamanager, (VanillaDataManager, ParallelDataManager)) assert pipeline.datamanager.train_pixel_sampler is not None pipeline.datamanager.train_pixel_sampler.num_rays_per_batch = self.num_rays_per_batch @@ -161,6 +160,17 @@ class ExportPointCloud(Exporter): crop_obb=crop_obb, std_ratio=self.std_ratio, ) + if self.save_world_frame: + # apply the inverse dataparser transform to the point cloud + points = np.asarray(pcd.points) + poses = np.eye(4, dtype=np.float32)[None, ...].repeat(points.shape[0], axis=0)[:, :3, :] + poses[:, :3, 3] = points + poses = pipeline.datamanager.train_dataparser_outputs.transform_poses_to_original_space( + torch.from_numpy(poses) + ) + points = poses[:, :3, 3].numpy() + pcd.points = o3d.utility.Vector3dVector(points) + torch.cuda.empty_cache() CONSOLE.print(f"[bold green]:white_check_mark: Generated {pcd}") @@ -307,7 +317,7 @@ class ExportPoissonMesh(Exporter): validate_pipeline(self.normal_method, self.normal_output_name, pipeline) # Increase the batchsize to speed up the evaluation. - assert isinstance(pipeline.datamanager, VanillaDataManager) + assert isinstance(pipeline.datamanager, (VanillaDataManager, ParallelDataManager)) assert pipeline.datamanager.train_pixel_sampler is not None pipeline.datamanager.train_pixel_sampler.num_rays_per_batch = self.num_rays_per_batch @@ -468,6 +478,76 @@ class ExportCameraPoses(Exporter): CONSOLE.print(f"[bold green]:white_check_mark: Saved poses to {output_file_path}") +@dataclass +class ExportGaussianSplat(Exporter): + """ + Export 3D Gaussian Splatting model to a .ply + """ + + def main(self) -> None: + if not self.output_dir.exists(): + self.output_dir.mkdir(parents=True) + + _, pipeline, _, _ = eval_setup(self.load_config) + + assert isinstance(pipeline.model, GaussianSplattingModel) + + model: GaussianSplattingModel = pipeline.model + + filename = self.output_dir / "point_cloud.ply" + + map_to_tensors = {} + + with torch.no_grad(): + positions = model.means.cpu().numpy() + n = positions.shape[0] + map_to_tensors["positions"] = positions + map_to_tensors["normals"] = np.zeros_like(positions, dtype=np.float32) + + if model.config.sh_degree > 0: + shs_0 = model.shs_0.contiguous().cpu().numpy() + for i in range(shs_0.shape[1]): + map_to_tensors[f"f_dc_{i}"] = shs_0[:, i, None] + + # transpose(1, 2) was needed to match the sh order in Inria version + shs_rest = model.shs_rest.transpose(1, 2).contiguous().cpu().numpy() + shs_rest = shs_rest.reshape((n, -1)) + for i in range(shs_rest.shape[-1]): + map_to_tensors[f"f_rest_{i}"] = shs_rest[:, i, None] + else: + colors = torch.clamp(model.colors.clone(), 0.0, 1.0).data.cpu().numpy() + map_to_tensors["colors"] = (colors * 255).astype(np.uint8) + + map_to_tensors["opacity"] = model.opacities.data.cpu().numpy() + + scales = model.scales.data.cpu().numpy() + for i in range(3): + map_to_tensors[f"scale_{i}"] = scales[:, i, None] + + quats = model.quats.data.cpu().numpy() + for i in range(4): + map_to_tensors[f"rot_{i}"] = quats[:, i, None] + + # post optimization, it is possible have NaN/Inf values in some attributes + # to ensure the exported ply file has finite values, we enforce finite filters. + select = np.ones(n, dtype=bool) + for k, t in map_to_tensors.items(): + n_before = np.sum(select) + select = np.logical_and(select, np.isfinite(t).all(axis=1)) + n_after = np.sum(select) + if n_after < n_before: + CONSOLE.print(f"{n_before - n_after} NaN/Inf elements in {k}") + + if np.sum(select) < n: + CONSOLE.print(f"values have NaN/Inf in map_to_tensors, only export {np.sum(select)}/{n}") + for k, t in map_to_tensors.items(): + map_to_tensors[k] = map_to_tensors[k][select, :] + + pcd = o3d.t.geometry.PointCloud(map_to_tensors) + + o3d.t.io.write_point_cloud(str(filename), pcd) + + Commands = tyro.conf.FlagConversionOff[ Union[ Annotated[ExportPointCloud, tyro.conf.subcommand(name="pointcloud")], @@ -475,6 +555,7 @@ Commands = tyro.conf.FlagConversionOff[ Annotated[ExportPoissonMesh, tyro.conf.subcommand(name="poisson")], Annotated[ExportMarchingCubesMesh, tyro.conf.subcommand(name="marching-cubes")], Annotated[ExportCameraPoses, tyro.conf.subcommand(name="cameras")], + Annotated[ExportGaussianSplat, tyro.conf.subcommand(name="gaussian-splat")], ] ] diff --git a/nerfstudio/scripts/process_data.py b/nerfstudio/scripts/process_data.py index 8b4151f75..2c2cd7a38 100644 --- a/nerfstudio/scripts/process_data.py +++ b/nerfstudio/scripts/process_data.py @@ -32,6 +32,7 @@ from nerfstudio.process_data import ( process_data_utils, realitycapture_utils, record3d_utils, + odm_utils, ) from nerfstudio.process_data.colmap_converter_to_nerfstudio_dataset import BaseConverterToNerfstudioDataset from nerfstudio.process_data.images_to_nerfstudio_dataset import ImagesToNerfstudioDataset @@ -387,6 +388,99 @@ class ProcessRealityCapture(BaseConverterToNerfstudioDataset, _NoDefaultProcessR CONSOLE.rule() +@dataclass +class ProcessODM(BaseConverterToNerfstudioDataset): + """Process ODM data into a nerfstudio dataset. + + This script does the following: + + 1. Scales images to a specified size. + 2. Converts ODM poses into the nerfstudio format. + """ + + num_downscales: int = 3 + """Number of times to downscale the images. Downscales by 2 each time. For example a value of 3 + will downscale the images by 2x, 4x, and 8x.""" + max_dataset_size: int = 600 + """Max number of images to train on. If the dataset has more, images will be sampled approximately evenly. If -1, + use all images.""" + + def main(self) -> None: + """Process images into a nerfstudio dataset.""" + + orig_images_dir = self.data / "images" + cameras_file = self.data / "cameras.json" + shots_file = self.data / "odm_report" / "shots.geojson" + + if not shots_file.exists: + raise ValueError(f"shots file {shots_file} doesn't exist") + if not shots_file.exists: + raise ValueError(f"cameras file {cameras_file} doesn't exist") + + if not orig_images_dir.exists: + raise ValueError(f"Images dir {orig_images_dir} doesn't exist") + + if self.eval_data is not None: + raise ValueError("Cannot use eval_data since cameras were already aligned with ODM.") + + self.output_dir.mkdir(parents=True, exist_ok=True) + image_dir = self.output_dir / "images" + image_dir.mkdir(parents=True, exist_ok=True) + + summary_log = [] + + # Copy images to output directory + image_filenames, num_orig_images = process_data_utils.get_image_filenames( + orig_images_dir, self.max_dataset_size + ) + copied_image_paths = process_data_utils.copy_images_list( + image_filenames, + image_dir=image_dir, + verbose=self.verbose, + num_downscales=self.num_downscales, + ) + num_frames = len(copied_image_paths) + + copied_image_paths = [Path("images/" + copied_image_path.name) for copied_image_path in copied_image_paths] + original_names = [image_path.stem for image_path in image_filenames] + image_filename_map = dict(zip(original_names, copied_image_paths)) + + if self.max_dataset_size > 0 and num_frames != num_orig_images: + summary_log.append(f"Started with {num_frames} images out of {num_orig_images} total") + summary_log.append( + "To change the size of the dataset add the argument [yellow]--max_dataset_size[/yellow] to " + f"larger than the current value ({self.max_dataset_size}), or -1 to use all images." + ) + else: + summary_log.append(f"Started with {num_frames} images") + + # Save json + if num_frames == 0: + CONSOLE.print("[bold red]No images found, exiting") + sys.exit(1) + summary_log.extend( + odm_utils.cameras2nerfds( + image_filename_map=image_filename_map, + cameras_file=cameras_file, + shots_file=shots_file, + output_dir=self.output_dir, + verbose=self.verbose, + ) + ) + + CONSOLE.rule("[bold green]:tada: :tada: :tada: All DONE :tada: :tada: :tada:") + + for summary in summary_log: + CONSOLE.print(summary, justify="center") + CONSOLE.rule() + + +@dataclass +class NotInstalled: + def main(self) -> None: + ... + + Commands = Union[ Annotated[ImagesToNerfstudioDataset, tyro.conf.subcommand(name="images")], Annotated[VideoToNerfstudioDataset, tyro.conf.subcommand(name="video")], @@ -394,8 +488,32 @@ Commands = Union[ Annotated[ProcessMetashape, tyro.conf.subcommand(name="metashape")], Annotated[ProcessRealityCapture, tyro.conf.subcommand(name="realitycapture")], Annotated[ProcessRecord3D, tyro.conf.subcommand(name="record3d")], + Annotated[ProcessODM, tyro.conf.subcommand(name="odm")], ] +# Add aria subcommand if projectaria_tools is installed. +try: + import projectaria_tools +except ImportError: + projectaria_tools = None + +if projectaria_tools is not None: + from nerfstudio.scripts.datasets.process_project_aria import ProcessProjectAria + + # Note that Union[A, Union[B, C]] == Union[A, B, C]. + Commands = Union[Commands, Annotated[ProcessProjectAria, tyro.conf.subcommand(name="aria")]] +else: + Commands = Union[ + Commands, + Annotated[ + NotInstalled, + tyro.conf.subcommand( + name="aria", + description="**Not installed.** Processing Project Aria data requires `pip install projectaria_tools'[all]'`.", + ), + ], + ] + def entrypoint(): """Entrypoint for use with pyproject scripts.""" diff --git a/nerfstudio/scripts/render.py b/nerfstudio/scripts/render.py index 99a7cae01..114e4195e 100644 --- a/nerfstudio/scripts/render.py +++ b/nerfstudio/scripts/render.py @@ -18,12 +18,13 @@ render.py """ from __future__ import annotations +import gzip import json import os -import struct import shutil +import struct import sys -from contextlib import ExitStack +from contextlib import ExitStack, contextmanager from dataclasses import dataclass, field from pathlib import Path from typing import Any, Dict, List, Literal, Optional, Union @@ -47,14 +48,27 @@ from rich.table import Table from torch import Tensor from typing_extensions import Annotated +import viser.transforms as tf + from nerfstudio.cameras.camera_paths import ( get_interpolated_camera_path, get_path_from_json, get_spiral_path, ) -from nerfstudio.cameras.cameras import Cameras, CameraType -from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManager + +from nerfstudio.cameras.cameras import Cameras, CameraType, RayBundle +from nerfstudio.data.datamanagers.base_datamanager import ( + VanillaDataManager, + VanillaDataManagerConfig, +) +from nerfstudio.data.datamanagers.parallel_datamanager import ParallelDataManager +from nerfstudio.data.datamanagers.random_cameras_datamanager import ( + RandomCamerasDataManager, +) +from nerfstudio.data.datasets.base_dataset import Dataset from nerfstudio.data.scene_box import OrientedBox +from nerfstudio.data.utils.dataloaders import FixedIndicesEvalDataloader +from nerfstudio.engine.trainer import TrainerConfig from nerfstudio.model_components import renderers from nerfstudio.pipelines.base_pipeline import Pipeline from nerfstudio.utils import colormaps, install_checks @@ -77,6 +91,8 @@ def _render_trajectory_video( depth_near_plane: Optional[float] = None, depth_far_plane: Optional[float] = None, colormap_options: colormaps.ColormapOptions = colormaps.ColormapOptions(), + render_nearest_camera=False, + check_occlusions: bool = False, ) -> None: """Helper function to create a video of the spiral trajectory. @@ -92,6 +108,8 @@ def _render_trajectory_video( depth_near_plane: Closest depth to consider when using the colormap for depth. If None, use min value. depth_far_plane: Furthest depth to consider when using the colormap for depth. If None, use max value. colormap_options: Options for colormap. + render_nearest_camera: Whether to render the nearest training camera to the rendered camera. + check_occlusions: If true, checks line-of-sight occlusions when computing camera distance and rejects cameras not visible to each other """ CONSOLE.print("[bold green]Creating trajectory " + output_format) cameras.rescale_output_resolution(rendered_resolution_scaling_factor) @@ -125,21 +143,76 @@ def _render_trajectory_video( with ExitStack() as stack: writer = None + if render_nearest_camera: + assert pipeline.datamanager.train_dataset is not None + train_dataset = pipeline.datamanager.train_dataset + train_cameras = train_dataset.cameras.to(pipeline.device) + else: + train_dataset = None + train_cameras = None + with progress: for camera_idx in progress.track(range(cameras.size), description=""): obb_box = None if crop_data is not None: obb_box = crop_data.obb - camera_ray_bundle = cameras.generate_rays(camera_indices=camera_idx, obb_box=obb_box) + + max_dist, max_idx = -1, -1 + true_max_dist, true_max_idx = -1, -1 + + if render_nearest_camera: + assert pipeline.datamanager.train_dataset is not None + assert train_dataset is not None + assert train_cameras is not None + cam_pos = cameras[camera_idx].camera_to_worlds[:, 3].cpu() + cam_quat = tf.SO3.from_matrix(cameras[camera_idx].camera_to_worlds[:3, :3].numpy(force=True)).wxyz + + for i in range(len(train_cameras)): + train_cam_pos = train_cameras[i].camera_to_worlds[:, 3].cpu() + # Make sure the line of sight from rendered cam to training cam is not blocked by any object + bundle = RayBundle( + origins=cam_pos.view(1, 3), + directions=((cam_pos - train_cam_pos) / (cam_pos - train_cam_pos).norm()).view(1, 3), + pixel_area=torch.tensor(1).view(1, 1), + nears=torch.tensor(0.05).view(1, 1), + fars=torch.tensor(100).view(1, 1), + camera_indices=torch.tensor(0).view(1, 1), + metadata={}, + ).to(pipeline.device) + outputs = pipeline.model.get_outputs(bundle) + + q = tf.SO3.from_matrix(train_cameras[i].camera_to_worlds[:3, :3].numpy(force=True)).wxyz + # calculate distance between two quaternions + rot_dist = 1 - np.dot(q, cam_quat) ** 2 + pos_dist = torch.norm(train_cam_pos - cam_pos) + dist = 0.3 * rot_dist + 0.7 * pos_dist + + if true_max_dist == -1 or dist < true_max_dist: + true_max_dist = dist + true_max_idx = i + + if outputs["depth"][0] < torch.norm(cam_pos - train_cam_pos).item(): + continue + + if check_occlusions and (max_dist == -1 or dist < max_dist): + max_dist = dist + max_idx = i + + if max_idx == -1: + max_idx = true_max_idx if crop_data is not None: with renderers.background_color_override_context( crop_data.background_color.to(pipeline.device) ), torch.no_grad(): - outputs = pipeline.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle) + outputs = pipeline.model.get_outputs_for_camera( + cameras[camera_idx : camera_idx + 1], obb_box=obb_box + ) else: with torch.no_grad(): - outputs = pipeline.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle) + outputs = pipeline.model.get_outputs_for_camera( + cameras[camera_idx : camera_idx + 1], obb_box=obb_box + ) render_image = [] for rendered_output_name in rendered_output_names: @@ -174,6 +247,28 @@ def _render_trajectory_video( .numpy() ) render_image.append(output_image) + + # Add closest training image to the right of the rendered image + if render_nearest_camera: + assert train_dataset is not None + assert train_cameras is not None + img = train_dataset.get_image(max_idx) + height = cameras.image_height[0] + # maintain the resolution of the img to calculate the width from the height + width = int(img.shape[1] * (height / img.shape[0])) + resized_image = torch.nn.functional.interpolate( + img.permute(2, 0, 1)[None], size=(int(height), int(width)) + )[0].permute(1, 2, 0) + resized_image = ( + colormaps.apply_colormap( + image=resized_image, + colormap_options=colormap_options, + ) + .cpu() + .numpy() + ) + render_image.append(resized_image) + render_image = np.concatenate(render_image, axis=1) if output_format == "images": if image_format == "png": @@ -314,6 +409,9 @@ def get_crop_from_json(camera_json: Dict[str, Any]) -> Optional[CropData]: center = camera_json["crop"]["crop_center"] scale = camera_json["crop"]["crop_scale"] rot = (0.0, 0.0, 0.0) if "crop_rot" not in camera_json["crop"] else tuple(camera_json["crop"]["crop_rot"]) + assert len(center) == 3 + assert len(scale) == 3 + assert len(rot) == 3 return CropData( background_color=torch.Tensor([bg_color["r"] / 255.0, bg_color["g"] / 255.0, bg_color["b"] / 255.0]), obb=OrientedBox.from_params(center, rot, scale), @@ -344,6 +442,10 @@ class BaseRender: """Furthest depth to consider when using the colormap for depth. If None, use max value.""" colormap_options: colormaps.ColormapOptions = colormaps.ColormapOptions() """Colormap options.""" + render_nearest_camera: bool = False + """Whether to render the nearest training camera to the rendered camera.""" + check_occlusions: bool = False + """If true, checks line-of-sight occlusions when computing camera distance and rejects cameras not visible to each other""" @dataclass @@ -408,6 +510,8 @@ class RenderCameraPath(BaseRender): depth_near_plane=self.depth_near_plane, depth_far_plane=self.depth_far_plane, colormap_options=self.colormap_options, + render_nearest_camera=self.render_nearest_camera, + check_occlusions=self.check_occlusions, ) if ( @@ -441,6 +545,8 @@ class RenderCameraPath(BaseRender): depth_near_plane=self.depth_near_plane, depth_far_plane=self.depth_far_plane, colormap_options=self.colormap_options, + render_nearest_camera=self.render_nearest_camera, + check_occlusions=self.check_occlusions, ) self.output_path = Path(str(left_eye_path.parent)[:-5] + ".mp4") @@ -539,6 +645,8 @@ class RenderInterpolated(BaseRender): depth_near_plane=self.depth_near_plane, depth_far_plane=self.depth_far_plane, colormap_options=self.colormap_options, + render_nearest_camera=self.render_nearest_camera, + check_occlusions=self.check_occlusions, ) @@ -565,9 +673,16 @@ class SpiralRender(BaseRender): install_checks.check_ffmpeg_installed() - assert isinstance(pipeline.datamanager, VanillaDataManager) + assert isinstance( + pipeline.datamanager, + ( + VanillaDataManager, + ParallelDataManager, + RandomCamerasDataManager, + ), + ) steps = int(self.frame_rate * self.seconds) - camera_start = pipeline.datamanager.eval_dataloader.get_camera(image_idx=0).flatten() + camera_start, _ = pipeline.datamanager.eval_dataloader.get_camera(image_idx=0) camera_path = get_spiral_path(camera_start, steps=steps, radius=self.radius) _render_trajectory_video( @@ -582,14 +697,213 @@ class SpiralRender(BaseRender): depth_near_plane=self.depth_near_plane, depth_far_plane=self.depth_far_plane, colormap_options=self.colormap_options, + render_nearest_camera=self.render_nearest_camera, + check_occlusions=self.check_occlusions, ) +@contextmanager +def _disable_datamanager_setup(cls): + """ + Disables setup_train or setup_eval for faster initialization. + """ + old_setup_train = getattr(cls, "setup_train") + old_setup_eval = getattr(cls, "setup_eval") + setattr(cls, "setup_train", lambda *args, **kwargs: None) + setattr(cls, "setup_eval", lambda *args, **kwargs: None) + yield cls + setattr(cls, "setup_train", old_setup_train) + setattr(cls, "setup_eval", old_setup_eval) + + +@dataclass +class DatasetRender(BaseRender): + """Render all images in the dataset.""" + + output_path: Path = Path("renders") + """Path to output video file.""" + data: Optional[Path] = None + """Override path to the dataset.""" + downscale_factor: Optional[float] = None + """Scaling factor to apply to the camera image resolution.""" + split: Literal["train", "val", "test", "train+test"] = "test" + """Split to render.""" + rendered_output_names: Optional[List[str]] = field(default_factory=lambda: None) + """Name of the renderer outputs to use. rgb, depth, raw-depth, gt-rgb etc. By default all outputs are rendered.""" + + def main(self): + config: TrainerConfig + + def update_config(config: TrainerConfig) -> TrainerConfig: + data_manager_config = config.pipeline.datamanager + assert isinstance(data_manager_config, VanillaDataManagerConfig) + data_manager_config.eval_num_images_to_sample_from = -1 + data_manager_config.eval_num_times_to_repeat_images = -1 + data_manager_config.train_num_images_to_sample_from = -1 + data_manager_config.train_num_times_to_repeat_images = -1 + if self.data is not None: + data_manager_config.data = self.data + if self.downscale_factor is not None: + assert hasattr(data_manager_config.dataparser, "downscale_factor") + setattr(data_manager_config.dataparser, "downscale_factor", self.downscale_factor) + return config + + config, pipeline, _, _ = eval_setup( + self.load_config, + eval_num_rays_per_chunk=self.eval_num_rays_per_chunk, + test_mode="inference", + update_config_callback=update_config, + ) + data_manager_config = config.pipeline.datamanager + assert isinstance(data_manager_config, VanillaDataManagerConfig) + + for split in self.split.split("+"): + datamanager: VanillaDataManager + dataset: Dataset + if split == "train": + with _disable_datamanager_setup(data_manager_config._target): # pylint: disable=protected-access + datamanager = data_manager_config.setup(test_mode="test", device=pipeline.device) + + dataset = datamanager.train_dataset + dataparser_outputs = getattr(dataset, "_dataparser_outputs", datamanager.train_dataparser_outputs) + else: + with _disable_datamanager_setup(data_manager_config._target): # pylint: disable=protected-access + datamanager = data_manager_config.setup(test_mode=split, device=pipeline.device) + + dataset = datamanager.eval_dataset + dataparser_outputs = getattr(dataset, "_dataparser_outputs", None) + if dataparser_outputs is None: + dataparser_outputs = datamanager.dataparser.get_dataparser_outputs(split=datamanager.test_split) + dataloader = FixedIndicesEvalDataloader( + input_dataset=dataset, + device=datamanager.device, + num_workers=datamanager.world_size * 4, + ) + images_root = Path(os.path.commonpath(dataparser_outputs.image_filenames)) + with Progress( + TextColumn(f":movie_camera: Rendering split {split} :movie_camera:"), + BarColumn(), + TaskProgressColumn( + text_format="[progress.percentage]{task.completed}/{task.total:>.0f}({task.percentage:>3.1f}%)", + show_speed=True, + ), + ItersPerSecColumn(suffix="fps"), + TimeRemainingColumn(elapsed_when_finished=False, compact=False), + TimeElapsedColumn(), + ) as progress: + for camera_idx, (camera, batch) in enumerate(progress.track(dataloader, total=len(dataset))): + with torch.no_grad(): + outputs = pipeline.model.get_outputs_for_camera(camera) + + gt_batch = batch.copy() + gt_batch["rgb"] = gt_batch.pop("image") + all_outputs = ( + list(outputs.keys()) + + [f"raw-{x}" for x in outputs.keys()] + + [f"gt-{x}" for x in gt_batch.keys()] + + [f"raw-gt-{x}" for x in gt_batch.keys()] + ) + rendered_output_names = self.rendered_output_names + if rendered_output_names is None: + rendered_output_names = ["gt-rgb"] + list(outputs.keys()) + for rendered_output_name in rendered_output_names: + if rendered_output_name not in all_outputs: + CONSOLE.rule("Error", style="red") + CONSOLE.print( + f"Could not find {rendered_output_name} in the model outputs", justify="center" + ) + CONSOLE.print( + f"Please set --rendered-output-name to one of: {all_outputs}", justify="center" + ) + sys.exit(1) + + is_raw = False + is_depth = rendered_output_name.find("depth") != -1 + image_name = f"{camera_idx:05d}" + + # Try to get the original filename + image_name = ( + dataparser_outputs.image_filenames[camera_idx].with_suffix("").relative_to(images_root) + ) + + output_path = self.output_path / split / rendered_output_name / image_name + output_path.parent.mkdir(exist_ok=True, parents=True) + + output_name = rendered_output_name + if output_name.startswith("raw-"): + output_name = output_name[4:] + is_raw = True + if output_name.startswith("gt-"): + output_name = output_name[3:] + output_image = gt_batch[output_name] + else: + output_image = outputs[output_name] + if is_depth: + # Divide by the dataparser scale factor + output_image.div_(dataparser_outputs.dataparser_scale) + else: + if output_name.startswith("gt-"): + output_name = output_name[3:] + output_image = gt_batch[output_name] + else: + output_image = outputs[output_name] + del output_name + + # Map to color spaces / numpy + if is_raw: + output_image = output_image.cpu().numpy() + elif is_depth: + output_image = ( + colormaps.apply_depth_colormap( + output_image, + accumulation=outputs["accumulation"], + near_plane=self.depth_near_plane, + far_plane=self.depth_far_plane, + colormap_options=self.colormap_options, + ) + .cpu() + .numpy() + ) + else: + output_image = ( + colormaps.apply_colormap( + image=output_image, + colormap_options=self.colormap_options, + ) + .cpu() + .numpy() + ) + + # Save to file + if is_raw: + with gzip.open(output_path.with_suffix(".npy.gz"), "wb") as f: + np.save(f, output_image) + elif self.image_format == "png": + media.write_image(output_path.with_suffix(".png"), output_image, fmt="png") + elif self.image_format == "jpeg": + media.write_image( + output_path.with_suffix(".jpg"), output_image, fmt="jpeg", quality=self.jpeg_quality + ) + else: + raise ValueError(f"Unknown image format {self.image_format}") + + table = Table( + title=None, + show_header=False, + box=box.MINIMAL, + title_style=style.Style(bold=True), + ) + for split in self.split.split("+"): + table.add_row(f"Outputs {split}", str(self.output_path / split)) + CONSOLE.print(Panel(table, title="[bold][green]:tada: Render on split {} Complete :tada:[/bold]", expand=False)) + + Commands = tyro.conf.FlagConversionOff[ Union[ Annotated[RenderCameraPath, tyro.conf.subcommand(name="camera-path")], Annotated[RenderInterpolated, tyro.conf.subcommand(name="interpolate")], Annotated[SpiralRender, tyro.conf.subcommand(name="spiral")], + Annotated[DatasetRender, tyro.conf.subcommand(name="dataset")], ] ] diff --git a/nerfstudio/scripts/viewer/run_viewer.py b/nerfstudio/scripts/viewer/run_viewer.py index de210a30d..8d679fb22 100644 --- a/nerfstudio/scripts/viewer/run_viewer.py +++ b/nerfstudio/scripts/viewer/run_viewer.py @@ -30,8 +30,8 @@ from nerfstudio.engine.trainer import TrainerConfig from nerfstudio.pipelines.base_pipeline import Pipeline from nerfstudio.utils import writer from nerfstudio.utils.eval_utils import eval_setup -from nerfstudio.viewer.server.viewer_state import ViewerState -from nerfstudio.viewer_beta.viewer import Viewer as ViewerBetaState +from nerfstudio.viewer_legacy.server.viewer_state import ViewerLegacyState +from nerfstudio.viewer.viewer import Viewer as ViewerState @dataclass @@ -53,10 +53,10 @@ class RunViewer: """Path to config YAML file.""" viewer: ViewerConfigWithoutNumRays = field(default_factory=ViewerConfigWithoutNumRays) """Viewer configuration""" - vis: Literal["viewer", "viewer_beta"] = "viewer" + vis: Literal["viewer", "viewer_legacy"] = "viewer" """Type of viewer""" - make_share_url: bool = False - """Viewer beta feature: print a shareable URL. `vis` must be set to viewer_beta; this flag is otherwise ignored.""" + make_share_url: bool = True + """Print a shareable URL""" def main(self) -> None: """Main function.""" @@ -92,23 +92,23 @@ def _start_viewer(config: TrainerConfig, pipeline: Pipeline, step: int): viewer_log_path = base_dir / config.viewer.relative_log_filename banner_messages = None viewer_state = None + if config.vis == "viewer_legacy": + viewer_state = ViewerLegacyState( + config.viewer, + log_filename=viewer_log_path, + datapath=pipeline.datamanager.get_datapath(), + pipeline=pipeline, + ) + banner_messages = [f"Legaccy viewer at: {viewer_state.viewer_url}"] if config.vis == "viewer": viewer_state = ViewerState( config.viewer, log_filename=viewer_log_path, datapath=pipeline.datamanager.get_datapath(), pipeline=pipeline, - ) - banner_messages = [f"Viewer at: {viewer_state.viewer_url}"] - if config.vis == "viewer_beta": - viewer_state = ViewerBetaState( - config.viewer, - log_filename=viewer_log_path, - datapath=base_dir, - pipeline=pipeline, share=config.viewer.make_share_url, ) - banner_messages = [f"Viewer Beta at: {viewer_state.viewer_url}"] + banner_messages = [f"Viewer at: {viewer_state.viewer_url}"] # We don't need logging, but writer.GLOBAL_BUFFER needs to be populated config.logging.local_writer.enable = False @@ -120,7 +120,7 @@ def _start_viewer(config: TrainerConfig, pipeline: Pipeline, step: int): train_state="completed", eval_dataset=pipeline.datamanager.eval_dataset, ) - if isinstance(viewer_state, ViewerState): + if isinstance(viewer_state, ViewerLegacyState): viewer_state.viser_server.set_training_state("completed") viewer_state.update_scene(step=step) while True: diff --git a/nerfstudio/scripts/viewer/sync_viser_message_defs.py b/nerfstudio/scripts/viewer/sync_viser_message_defs.py index 5053a82ea..a178aae4c 100644 --- a/nerfstudio/scripts/viewer/sync_viser_message_defs.py +++ b/nerfstudio/scripts/viewer/sync_viser_message_defs.py @@ -20,7 +20,7 @@ from datetime import datetime import tyro from viser.infra import generate_typescript_interfaces -from nerfstudio.viewer.viser import NerfstudioMessage +from nerfstudio.viewer_legacy.viser import NerfstudioMessage def main() -> None: diff --git a/nerfstudio/utils/decorators.py b/nerfstudio/utils/decorators.py index d6fd3f417..cc78be8a5 100644 --- a/nerfstudio/utils/decorators.py +++ b/nerfstudio/utils/decorators.py @@ -50,11 +50,11 @@ def check_profiler_enabled(func: Callable) -> Callable: def check_viewer_enabled(func: Callable) -> Callable: - """Decorator: check if the viewer or beta viewer is enabled and only run on main process""" + """Decorator: check if the viewer or legacy viewer is enabled and only run on main process""" def wrapper(self, *args, **kwargs): ret = None - if (self.config.is_viewer_enabled() or self.config.is_viewer_beta_enabled()) and comms.is_main_process(): + if (self.config.is_viewer_enabled() or self.config.is_viewer_legacy_enabled()) and comms.is_main_process(): ret = func(self, *args, **kwargs) return ret diff --git a/nerfstudio/utils/eval_utils.py b/nerfstudio/utils/eval_utils.py index 13948678b..0b7306791 100644 --- a/nerfstudio/utils/eval_utils.py +++ b/nerfstudio/utils/eval_utils.py @@ -20,13 +20,12 @@ from __future__ import annotations import os import sys from pathlib import Path -from typing import Literal, Optional, Tuple +from typing import Literal, Optional, Tuple, Callable import torch import yaml from nerfstudio.configs.method_configs import all_methods -from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManagerConfig from nerfstudio.engine.trainer import TrainerConfig from nerfstudio.pipelines.base_pipeline import Pipeline from nerfstudio.utils.rich_utils import CONSOLE @@ -69,6 +68,7 @@ def eval_setup( config_path: Path, eval_num_rays_per_chunk: Optional[int] = None, test_mode: Literal["test", "val", "inference"] = "test", + update_config_callback: Optional[Callable[[TrainerConfig], TrainerConfig]] = None, ) -> Tuple[TrainerConfig, Pipeline, Path, int]: """Shared setup for loading a saved pipeline for evaluation. @@ -79,6 +79,7 @@ def eval_setup( 'val': loads train/val datasets into memory 'test': loads train/test dataset into memory 'inference': does not load any dataset into memory + update_config_callback: Callback to update the config before loading the pipeline Returns: @@ -92,11 +93,12 @@ def eval_setup( if eval_num_rays_per_chunk: config.pipeline.model.eval_num_rays_per_chunk = eval_num_rays_per_chunk + if update_config_callback is not None: + config = update_config_callback(config) + # load checkpoints from wherever they were saved # TODO: expose the ability to choose an arbitrary checkpoint config.load_dir = config.get_checkpoint_dir() - if isinstance(config.pipeline.datamanager, VanillaDataManagerConfig): - config.pipeline.datamanager.eval_image_indices = None # setup pipeline (which includes the DataManager) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") diff --git a/nerfstudio/utils/math.py b/nerfstudio/utils/math.py index 8567a8b28..0ba9e6a51 100644 --- a/nerfstudio/utils/math.py +++ b/nerfstudio/utils/math.py @@ -14,6 +14,8 @@ """ Math Helper Functions """ +import itertools +import math from dataclasses import dataclass from typing import Literal, Tuple @@ -195,7 +197,6 @@ def expected_sin(x_means: torch.Tensor, x_vars: torch.Tensor) -> torch.Tensor: Returns: torch.Tensor: The expected value of sin. """ - return torch.exp(-0.5 * x_vars) * torch.sin(x_means) @@ -360,4 +361,160 @@ def normalized_depth_scale_and_shift( shift[valid] = (-a_01[valid] * b_0[valid] + a_00[valid] * b_1[valid]) / det[valid] return scale, shift - return scale, shift + + +def columnwise_squared_l2_distance( + x: Float[Tensor, "*M N"], + y: Float[Tensor, "*M N"], +) -> Float[Tensor, "N N"]: + """Compute the squared Euclidean distance between all pairs of columns. + Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py + + Args: + x: tensor of floats, with shape [M, N]. + y: tensor of floats, with shape [M, N]. + Returns: + sq_dist: tensor of floats, with shape [N, N]. + """ + # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. + sq_norm_x = torch.sum(x**2, 0) + sq_norm_y = torch.sum(y**2, 0) + sq_dist = sq_norm_x[:, None] + sq_norm_y[None, :] - 2 * x.T @ y + return sq_dist + + +def _compute_tesselation_weights(v: int) -> Tensor: + """Tesselate the vertices of a triangle by a factor of `v`. + Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py + + Args: + v: int, the factor of the tesselation (v==1 is a no-op to the triangle). + + Returns: + weights: tesselated weights. + """ + if v < 1: + raise ValueError(f"v {v} must be >= 1") + int_weights = [] + for i in range(v + 1): + for j in range(v + 1 - i): + int_weights.append((i, j, v - (i + j))) + int_weights = torch.FloatTensor(int_weights) + weights = int_weights / v # Barycentric weights. + return weights + + +def _tesselate_geodesic( + vertices: Float[Tensor, "N 3"], faces: Float[Tensor, "M 3"], v: int, eps: float = 1e-4 +) -> Tensor: + """Tesselate the vertices of a geodesic polyhedron. + + Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py + + Args: + vertices: tensor of floats, the vertex coordinates of the geodesic. + faces: tensor of ints, the indices of the vertices of base_verts that + constitute eachface of the polyhedra. + v: int, the factor of the tesselation (v==1 is a no-op). + eps: float, a small value used to determine if two vertices are the same. + + Returns: + verts: a tensor of floats, the coordinates of the tesselated vertices. + """ + tri_weights = _compute_tesselation_weights(v) + + verts = [] + for face in faces: + new_verts = torch.matmul(tri_weights, vertices[face, :]) + new_verts /= torch.sqrt(torch.sum(new_verts**2, 1, keepdim=True)) + verts.append(new_verts) + verts = torch.concatenate(verts, 0) + + sq_dist = columnwise_squared_l2_distance(verts.T, verts.T) + assignment = torch.tensor([torch.min(torch.argwhere(d <= eps)) for d in sq_dist]) + unique = torch.unique(assignment) + verts = verts[unique, :] + return verts + + +def generate_polyhedron_basis( + basis_shape: Literal["icosahedron", "octahedron"], + angular_tesselation: int, + remove_symmetries: bool = True, + eps: float = 1e-4, +) -> Tensor: + """Generates a 3D basis by tesselating a geometric polyhedron. + Basis is used to construct Fourier features for positional encoding. + See Mip-Nerf360 paper: https://arxiv.org/abs/2111.12077 + Adapted from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/internal/geopoly.py + + Args: + base_shape: string, the name of the starting polyhedron, must be either + 'icosahedron' or 'octahedron'. + angular_tesselation: int, the number of times to tesselate the polyhedron, + must be >= 1 (a value of 1 is a no-op to the polyhedron). + remove_symmetries: bool, if True then remove the symmetric basis columns, + which is usually a good idea because otherwise projections onto the basis + will have redundant negative copies of each other. + eps: float, a small number used to determine symmetries. + + Returns: + basis: a matrix with shape [3, n]. + """ + if basis_shape == "icosahedron": + a = (math.sqrt(5) + 1) / 2 + verts = torch.FloatTensor( + [ + (-1, 0, a), + (1, 0, a), + (-1, 0, -a), + (1, 0, -a), + (0, a, 1), + (0, a, -1), + (0, -a, 1), + (0, -a, -1), + (a, 1, 0), + (-a, 1, 0), + (a, -1, 0), + (-a, -1, 0), + ] + ) / math.sqrt(a + 2) + faces = torch.tensor( + [ + (0, 4, 1), + (0, 9, 4), + (9, 5, 4), + (4, 5, 8), + (4, 8, 1), + (8, 10, 1), + (8, 3, 10), + (5, 3, 8), + (5, 2, 3), + (2, 7, 3), + (7, 10, 3), + (7, 6, 10), + (7, 11, 6), + (11, 0, 6), + (0, 1, 6), + (6, 1, 10), + (9, 0, 11), + (9, 11, 2), + (9, 2, 5), + (7, 2, 11), + ] + ) + verts = _tesselate_geodesic(verts, faces, angular_tesselation) + elif basis_shape == "octahedron": + verts = torch.FloatTensor([(0, 0, -1), (0, 0, 1), (0, -1, 0), (0, 1, 0), (-1, 0, 0), (1, 0, 0)]) + corners = torch.FloatTensor(list(itertools.product([-1, 1], repeat=3))) + pairs = torch.argwhere(columnwise_squared_l2_distance(corners.T, verts.T) == 2) + faces, _ = torch.sort(torch.reshape(pairs[:, 1], [3, -1]).T, 1) + verts = _tesselate_geodesic(verts, faces, angular_tesselation) + + if remove_symmetries: + # Remove elements of `verts` that are reflections of each other. + match = columnwise_squared_l2_distance(verts.T, -verts.T) < eps + verts = verts[torch.any(torch.triu(match), 1), :] + + basis = verts.flip(-1) + return basis diff --git a/nerfstudio/utils/plotly_utils.py b/nerfstudio/utils/plotly_utils.py index 00cab4760..1ab7da1fb 100644 --- a/nerfstudio/utils/plotly_utils.py +++ b/nerfstudio/utils/plotly_utils.py @@ -239,6 +239,7 @@ def get_cube( if isinstance(side_length, float): pts *= side_length / 2.0 else: + assert isinstance(side_length, torch.Tensor) pts[0] *= side_length[0].item() / 2.0 pts[1] *= side_length[1].item() / 2.0 pts[2] *= side_length[2].item() / 2.0 diff --git a/nerfstudio/utils/tensor_dataclass.py b/nerfstudio/utils/tensor_dataclass.py index 0ebe7b5ce..a2b8d1dad 100644 --- a/nerfstudio/utils/tensor_dataclass.py +++ b/nerfstudio/utils/tensor_dataclass.py @@ -261,6 +261,14 @@ class TensorDataclass: """ return self._apply_fn_to_fields(lambda x: x.to(device)) + def pin_memory(self: TensorDataclassT) -> TensorDataclassT: + """Pins the tensor dataclass memory + + Returns: + TensorDataclass: A new TensorDataclass with the same data but pinned. + """ + return self._apply_fn_to_fields(lambda x: x.pin_memory()) + def _apply_fn_to_fields( self: TensorDataclassT, fn: Callable, diff --git a/nerfstudio/viewer_beta/control_panel.py b/nerfstudio/viewer/control_panel.py similarity index 74% rename from nerfstudio/viewer_beta/control_panel.py rename to nerfstudio/viewer/control_panel.py index 07f556dd4..7eba58f9e 100644 --- a/nerfstudio/viewer_beta/control_panel.py +++ b/nerfstudio/viewer/control_panel.py @@ -18,11 +18,11 @@ from typing import Callable, DefaultDict, List, Tuple, get_args import numpy as np import torch +import viser import viser.transforms as vtf from nerfstudio.data.scene_box import OrientedBox from nerfstudio.utils.colormaps import ColormapOptions, Colormaps -from nerfstudio.viewer_beta.viewer_elements import ( # ViewerButtonGroup, - ViewerButton, +from nerfstudio.viewer.viewer_elements import ( # ViewerButtonGroup, ViewerButtonGroup, ViewerCheckbox, ViewerDropdown, @@ -42,8 +42,8 @@ class ControlPanel: time_enabled: whether or not the time slider should be enabled rerender_cb: a callback that will be called when the user changes a parameter that requires a rerender (eg train speed, max res, etc) - crop_update_cb: a callback that will be called when the user changes the crop parameters update_output_cb: a callback that will be called when the user changes the output render + default_composite_depth: whether to default to compositing depth or not """ def __init__( @@ -51,17 +51,16 @@ class ControlPanel: viser_server: ViserServer, time_enabled: bool, scale_ratio: float, - rerender_cb: Callable, - crop_update_cb: Callable, + rerender_cb: Callable[[], None], update_output_cb: Callable, update_split_output_cb: Callable, - toggle_training_state_cb: Callable, - camera_vis: Callable, + default_composite_depth: bool = True, ): self.viser_scale_ratio = scale_ratio # elements holds a mapping from tag: [elements] self.viser_server = viser_server self._elements_by_tag: DefaultDict[str, List[ViewerElement]] = defaultdict(lambda: []) + self.default_composite_depth = default_composite_depth self._train_speed = ViewerButtonGroup( name="Train Speed", @@ -69,56 +68,54 @@ class ControlPanel: options=["Slow", "Mid", "Fast"], cb_hook=lambda han: self._train_speed_cb(), ) - self._reset_camera = ViewerButton( - name="Reset Up Direction", - cb_hook=lambda han: self._reset_camera_cb(), - ) self._output_render = ViewerDropdown( - "Output Render", + "Output type", "not set", ["not set"], - cb_hook=lambda han: [self.update_control_panel(), update_output_cb(han), rerender_cb(han)], + cb_hook=lambda han: [self.update_control_panel(), update_output_cb(han), rerender_cb()], hint="The output to render", ) self._colormap = ViewerDropdown[Colormaps]( - "Colormap", "default", ["default"], cb_hook=rerender_cb, hint="The colormap to use" + "Colormap", "default", ["default"], cb_hook=lambda _: rerender_cb(), hint="The colormap to use" ) - self._invert = ViewerCheckbox("Invert", False, cb_hook=rerender_cb, hint="Invert the colormap") - self._normalize = ViewerCheckbox("Normalize", True, cb_hook=rerender_cb, hint="Normalize the colormap") - self._min = ViewerNumber("Min", 0.0, cb_hook=rerender_cb, hint="Min value of the colormap") - self._max = ViewerNumber("Max", 1.0, cb_hook=rerender_cb, hint="Max value of the colormap") + self._invert = ViewerCheckbox("Invert", False, cb_hook=lambda _: rerender_cb(), hint="Invert the colormap") + self._normalize = ViewerCheckbox( + "Normalize", True, cb_hook=lambda _: rerender_cb(), hint="Normalize the colormap" + ) + self._min = ViewerNumber("Min", 0.0, cb_hook=lambda _: rerender_cb(), hint="Min value of the colormap") + self._max = ViewerNumber("Max", 1.0, cb_hook=lambda _: rerender_cb(), hint="Max value of the colormap") self._split = ViewerCheckbox( "Enable", False, - cb_hook=lambda han: [self.update_control_panel(), rerender_cb(han)], + cb_hook=lambda han: [self.update_control_panel(), rerender_cb()], hint="Render two outputs", ) self._split_percentage = ViewerSlider( - "Split Percentage", 0.5, 0.0, 1.0, 0.01, cb_hook=rerender_cb, hint="Where to split" + "Split percentage", 0.5, 0.0, 1.0, 0.01, cb_hook=lambda _: rerender_cb(), hint="Where to split" ) self._split_output_render = ViewerDropdown( - "Output Render Split", + "Output render split", "not set", ["not set"], - cb_hook=lambda han: [self.update_control_panel(), update_split_output_cb(han), rerender_cb(han)], + cb_hook=lambda han: [self.update_control_panel(), update_split_output_cb(han), rerender_cb()], hint="The second output", ) # Hack: spaces are after at the end of the names to make them unique self._split_colormap = ViewerDropdown[Colormaps]( - "Colormap ", "default", ["default"], cb_hook=rerender_cb, hint="Colormap of the second output" + "Colormap ", "default", ["default"], cb_hook=lambda _: rerender_cb(), hint="Colormap of the second output" ) self._split_invert = ViewerCheckbox( - "Invert ", False, cb_hook=rerender_cb, hint="Invert the colormap of the second output" + "Invert ", False, cb_hook=lambda _: rerender_cb(), hint="Invert the colormap of the second output" ) self._split_normalize = ViewerCheckbox( - "Normalize ", True, cb_hook=rerender_cb, hint="Normalize the colormap of the second output" + "Normalize ", True, cb_hook=lambda _: rerender_cb(), hint="Normalize the colormap of the second output" ) self._split_min = ViewerNumber( - "Min ", 0.0, cb_hook=rerender_cb, hint="Min value of the colormap of the second output" + "Min ", 0.0, cb_hook=lambda _: rerender_cb(), hint="Min value of the colormap of the second output" ) self._split_max = ViewerNumber( - "Max ", 1.0, cb_hook=rerender_cb, hint="Max value of the colormap of the second output" + "Max ", 1.0, cb_hook=lambda _: rerender_cb(), hint="Max value of the colormap of the second output" ) self._train_util = ViewerSlider( @@ -130,30 +127,39 @@ class ControlPanel: hint="Target training utilization, 0.0 is slow, 1.0 is fast. Doesn't affect final render quality", ) self._layer_depth = ViewerCheckbox( - "Composite Depth", True, cb_hook=rerender_cb, hint="Allow NeRF to occlude 3D browser objects" + "Composite depth", + self.default_composite_depth, + cb_hook=lambda _: rerender_cb(), + hint="Allow NeRF to occlude 3D browser objects", ) self._max_res = ViewerSlider( - "Max Res", 512, 64, 2048, 100, cb_hook=rerender_cb, hint="Maximum resolution to render in viewport" + "Max res", + 512, + 64, + 2048, + 100, + cb_hook=lambda _: rerender_cb(), + hint="Maximum resolution to render in viewport", ) self._crop_viewport = ViewerCheckbox( "Enable ", False, - cb_hook=lambda han: [self.update_control_panel(), crop_update_cb(han), rerender_cb(han)], + cb_hook=lambda han: [self.update_control_panel(), rerender_cb()], hint="Crop the scene to a specified box", ) self._background_color = ViewerRGB( - "Background color", (38, 42, 55), cb_hook=crop_update_cb, hint="Color of the background" + "Background color", (38, 42, 55), cb_hook=lambda _: rerender_cb(), hint="Color of the background" ) self._crop_handle = self.viser_server.add_transform_controls("Crop", depth_test=False, line_width=4.0) def update_center(han): - self._crop_handle.position = tuple(p * self.viser_scale_ratio for p in han.value) + self._crop_handle.position = tuple(p * self.viser_scale_ratio for p in han.value) # type: ignore self._crop_center = ViewerVec3( - "Crop Center", + "Crop center", (0.0, 0.0, 0.0), step=0.01, - cb_hook=lambda e: [crop_update_cb(e), update_center(e)], + cb_hook=lambda e: [rerender_cb(), update_center(e)], hint="Center of the crop box", ) @@ -161,49 +167,30 @@ class ControlPanel: self._crop_handle.wxyz = vtf.SO3.from_rpy_radians(*han.value).wxyz self._crop_rot = ViewerVec3( - "Crop Rotation", + "Crop rotation", (0.0, 0.0, 0.0), step=0.01, - cb_hook=lambda e: [crop_update_cb(e), update_rot(e)], + cb_hook=lambda e: [rerender_cb(), update_rot(e)], hint="Rotation of the crop box", ) self._crop_scale = ViewerVec3( - "Crop Scale", (1.0, 1.0, 1.0), step=0.01, cb_hook=crop_update_cb, hint="Scale of the crop box" + "Crop scale", (1.0, 1.0, 1.0), step=0.01, cb_hook=lambda _: rerender_cb(), hint="Size of the crop box." ) @self._crop_handle.on_update def _update_crop_handle(han): pos = self._crop_handle.position - self._crop_center.value = tuple(p / self.viser_scale_ratio for p in pos) + self._crop_center.value = tuple(p / self.viser_scale_ratio for p in pos) # type: ignore rpy = vtf.SO3(self._crop_handle.wxyz).as_rpy_radians() self._crop_rot.value = (float(rpy.roll), float(rpy.pitch), float(rpy.yaw)) - self._time = ViewerSlider("Time", 0.0, 0.0, 1.0, 0.01, cb_hook=rerender_cb, hint="Time to render") + self._time = ViewerSlider("Time", 0.0, 0.0, 1.0, 0.01, cb_hook=lambda _: rerender_cb(), hint="Time to render") self._time_enabled = time_enabled - self.stat_folder = self.viser_server.add_gui_folder("Stats") - with self.stat_folder: - self.markdown = self.viser_server.add_gui_markdown("Step: 0") - self.pause_train = viser_server.add_gui_button(label="Pause Training", disabled=False) - self.pause_train.on_click(lambda _: self.toggle_pause_button()) - self.pause_train.on_click(lambda han: toggle_training_state_cb(han)) - self.resume_train = viser_server.add_gui_button(label="Resume Training", disabled=False) - self.resume_train.on_click(lambda _: self.toggle_pause_button()) - self.resume_train.on_click(lambda han: toggle_training_state_cb(han)) - self.resume_train.visible = False - # Add buttons to toggle training image visibility - self.hide_images = viser_server.add_gui_button(label="Hide Train Cams", disabled=False) - self.hide_images.on_click(lambda _: camera_vis(False)) - self.hide_images.on_click(lambda _: self.toggle_cameravis_button()) - self.show_images = viser_server.add_gui_button(label="Show Train Cams", disabled=False) - self.show_images.on_click(lambda _: camera_vis(True)) - self.show_images.on_click(lambda _: self.toggle_cameravis_button()) - self.show_images.visible = False - self.add_element(self._train_speed) self.add_element(self._train_util) - self.add_element(self._reset_camera) + with self.viser_server.add_gui_folder("Render Options"): self.add_element(self._max_res) self.add_element(self._output_render) @@ -237,6 +224,13 @@ class ControlPanel: self.add_element(self._crop_rot, additional_tags=("crop",)) self.add_element(self._time, additional_tags=("time",)) + self._reset_camera = viser_server.add_gui_button( + label="Reset Up Direction", + icon=viser.Icon.ARROW_BIG_UP_LINES, + color="gray", + hint="Set the up direction of the camera orbit controls to the camera's current up direction.", + ) + self._reset_camera.on_click(self._reset_camera_cb) def _train_speed_cb(self) -> None: pass @@ -252,29 +246,10 @@ class ControlPanel: self._train_util.value = 0.5 self._max_res.value = 1024 - def _reset_camera_cb(self) -> None: + def _reset_camera_cb(self, _) -> None: for client in self.viser_server.get_clients().values(): client.camera.up_direction = vtf.SO3(client.camera.wxyz) @ np.array([0.0, -1.0, 0.0]) - def toggle_pause_button(self) -> None: - self.pause_train.visible = not self.pause_train.visible - self.resume_train.visible = not self.resume_train.visible - - def toggle_cameravis_button(self) -> None: - self.hide_images.visible = not self.hide_images.visible - self.show_images.visible = not self.show_images.visible - - def update_step(self, step): - """ - Args: - step: the train step to set the model to - """ - with self.viser_server.atomic(), self.stat_folder: - # TODO change to a .value call instead of remove() and add, this makes it jittery - with self.viser_server.atomic(): - self.markdown.remove() - self.markdown = self.viser_server.add_gui_markdown(f"Step: {step}") - def update_output_options(self, new_options: List[str]): """ Args: @@ -284,7 +259,7 @@ class ControlPanel: self._split_output_render.set_options(new_options) self._split_output_render.value = new_options[-1] - def add_element(self, e: ViewerElement, additional_tags: Tuple[str] = tuple()) -> None: + def add_element(self, e: ViewerElement, additional_tags: Tuple[str, ...] = tuple()) -> None: """Adds an element to the control panel Args: @@ -442,7 +417,7 @@ def _get_colormap_options(dimensions: int, dtype: type) -> List[Colormaps]: if dimensions == 3: colormap_options = ["default"] if dimensions == 1 and dtype in [torch.float64, torch.float32, torch.float16, torch.bfloat16]: - colormap_options = [c for c in list(get_args(Colormaps)) if c != "default"] + colormap_options = [c for c in list(get_args(Colormaps)) if c not in ("default", "pca")] if dimensions > 3: colormap_options = ["pca"] return colormap_options diff --git a/nerfstudio/viewer_beta/export_panel.py b/nerfstudio/viewer/export_panel.py similarity index 92% rename from nerfstudio/viewer_beta/export_panel.py rename to nerfstudio/viewer/export_panel.py index 425e10a19..ceff4e41f 100644 --- a/nerfstudio/viewer_beta/export_panel.py +++ b/nerfstudio/viewer/export_panel.py @@ -19,7 +19,7 @@ from pathlib import Path import viser import viser.transforms as vtf from nerfstudio.data.scene_box import OrientedBox -from nerfstudio.viewer_beta.control_panel import ControlPanel +from nerfstudio.viewer.control_panel import ControlPanel from typing_extensions import Literal @@ -68,9 +68,9 @@ def get_crop_string(obb: OrientedBox): rpy = vtf.SO3.from_matrix(obb.R.numpy(force=True)).as_rpy_radians() pos = obb.T.squeeze().tolist() scale = obb.S.squeeze().tolist() - rpystring = " ".join([str(x) for x in rpy]) - posstring = " ".join([str(x) for x in pos]) - scalestring = " ".join([str(x) for x in scale]) + rpystring = " ".join([f"{x:.10f}" for x in rpy]) + posstring = " ".join([f"{x:.10f}" for x in pos]) + scalestring = " ".join([f"{x:.10f}" for x in scale]) return f"--obb_center {posstring} --obb_rotation {rpystring} --obb_scale {scalestring}" @@ -81,6 +81,11 @@ def populate_point_cloud_tab( ) -> None: server.add_gui_markdown("Render depth, project to an oriented point cloud, and filter. ") num_points = server.add_gui_number("# Points", initial_value=1_000_000, min=1, max=None, step=1) + world_frame = server.add_gui_checkbox( + "Save in world frame", + True, + hint="Save the point cloud in the transforms.json frame, rather than scaled scene frame", + ) remove_outliers = server.add_gui_checkbox("Remove outliers", True) normals = server.add_gui_dropdown( "Normals", @@ -104,6 +109,7 @@ def populate_point_cloud_tab( f"--remove-outliers {remove_outliers.value}", f"--normal-method {normals.value}", f"--use_bounding_box {control_panel.crop_viewport}", + f"--save-world-frame {world_frame.value}", get_crop_string(control_panel.crop_obb), ] ) diff --git a/nerfstudio/viewer/render_panel.py b/nerfstudio/viewer/render_panel.py new file mode 100644 index 000000000..735499bd1 --- /dev/null +++ b/nerfstudio/viewer/render_panel.py @@ -0,0 +1,1079 @@ +# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import annotations + +import colorsys +import dataclasses +import datetime +import json +import threading +import time +from typing import Dict, List, Optional, Tuple, Literal +from pathlib import Path +from nerfstudio.viewer.control_panel import ControlPanel +import splines +import splines.quaternion +import viser +import viser.transforms as tf +import numpy as np +import scipy + + +@dataclasses.dataclass +class Keyframe: + position: np.ndarray + wxyz: np.ndarray + override_fov_enabled: bool + override_fov_rad: float + aspect: float + override_transition_enabled: bool + override_transition_sec: Optional[float] + + @staticmethod + def from_camera(camera: viser.CameraHandle, aspect: float) -> Keyframe: + return Keyframe( + camera.position, + camera.wxyz, + override_fov_enabled=False, + override_fov_rad=camera.fov, + aspect=aspect, + override_transition_enabled=False, + override_transition_sec=None, + ) + + +class CameraPath: + def __init__(self, server: viser.ViserServer, duration_element: viser.GuiInputHandle[float]): + self._server = server + self._keyframes: Dict[int, Tuple[Keyframe, viser.CameraFrustumHandle]] = {} + self._keyframe_counter: int = 0 + self._spline_nodes: List[viser.SceneNodeHandle] = [] + self._camera_edit_panel: Optional[viser.Gui3dContainerHandle] = None + + self._orientation_spline: Optional[splines.quaternion.KochanekBartels] = None + self._position_spline: Optional[splines.KochanekBartels] = None + self._fov_spline: Optional[splines.KochanekBartels] = None + self._keyframes_visible: bool = True + + self._duration_element = duration_element + + # These parameters should be overridden externally. + self.loop: bool = False + self.framerate: float = 30.0 + self.tension: float = 0.5 # Tension / alpha term. + self.default_fov: float = 0.0 + self.default_transition_sec: float = 0.0 + self.show_spline: bool = True + + def set_keyframes_visible(self, visible: bool) -> None: + self._keyframes_visible = visible + for keyframe in self._keyframes.values(): + keyframe[1].visible = visible + + def add_camera(self, keyframe: Keyframe, keyframe_index: Optional[int] = None) -> None: + """Add a new camera, or replace an old one if `keyframe_index` is passed in.""" + server = self._server + + # Add a keyframe if we aren't replacing an existing one. + if keyframe_index is None: + keyframe_index = self._keyframe_counter + self._keyframe_counter += 1 + + frustum_handle = server.add_camera_frustum( + f"/render_cameras/{keyframe_index}", + fov=keyframe.override_fov_rad if keyframe.override_fov_enabled else self.default_fov, + aspect=keyframe.aspect, + scale=0.1, + color=(200, 10, 30), + wxyz=keyframe.wxyz, + position=keyframe.position, + visible=self._keyframes_visible, + ) + self._server.add_icosphere( + f"/render_cameras/{keyframe_index}/sphere", + radius=0.03, + color=(200, 10, 30), + ) + + @frustum_handle.on_click + def _(_) -> None: + if self._camera_edit_panel is not None: + self._camera_edit_panel.remove() + self._camera_edit_panel = None + + with server.add_3d_gui_container( + "/camera_edit_panel", + position=keyframe.position, + ) as camera_edit_panel: + self._camera_edit_panel = camera_edit_panel + override_fov = server.add_gui_checkbox("Override FOV", initial_value=keyframe.override_fov_enabled) + override_fov_degrees = server.add_gui_slider( + "Override FOV (degrees)", + 5.0, + 175.0, + step=0.1, + initial_value=keyframe.override_fov_rad * 180.0 / np.pi, + disabled=not keyframe.override_fov_enabled, + ) + delete_button = server.add_gui_button("Delete", color="red", icon=viser.Icon.TRASH) + go_to_button = server.add_gui_button("Go to") + close_button = server.add_gui_button("Close") + + @override_fov.on_update + def _(_) -> None: + keyframe.override_fov_enabled = override_fov.value + override_fov_degrees.disabled = not override_fov.value + self.add_camera(keyframe, keyframe_index) + + @override_fov_degrees.on_update + def _(_) -> None: + keyframe.override_fov_rad = override_fov_degrees.value / 180.0 * np.pi + self.add_camera(keyframe, keyframe_index) + + @delete_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client is not None + with event.client.add_gui_modal("Confirm") as modal: + event.client.add_gui_markdown("Delete keyframe?") + confirm_button = event.client.add_gui_button("Yes", color="red", icon=viser.Icon.TRASH) + exit_button = event.client.add_gui_button("Cancel") + + @confirm_button.on_click + def _(_) -> None: + assert camera_edit_panel is not None + + keyframe_id = None + for i, keyframe_tuple in self._keyframes.items(): + if keyframe_tuple[1] is frustum_handle: + keyframe_id = i + break + assert keyframe_id is not None + + self._keyframes.pop(keyframe_id) + frustum_handle.remove() + camera_edit_panel.remove() + self._camera_edit_panel = None + modal.close() + self.update_spline() + + @exit_button.on_click + def _(_) -> None: + modal.close() + + @go_to_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client is not None + client = event.client + T_world_current = tf.SE3.from_rotation_and_translation( + tf.SO3(client.camera.wxyz), client.camera.position + ) + T_world_target = tf.SE3.from_rotation_and_translation( + tf.SO3(keyframe.wxyz), keyframe.position + ) @ tf.SE3.from_translation(np.array([0.0, 0.0, -0.5])) + + T_current_target = T_world_current.inverse() @ T_world_target + + for j in range(10): + T_world_set = T_world_current @ tf.SE3.exp(T_current_target.log() * j / 9.0) + + # Important bit: we atomically set both the orientation and the position + # of the camera. + with client.atomic(): + client.camera.wxyz = T_world_set.rotation().wxyz + client.camera.position = T_world_set.translation() + time.sleep(1.0 / 30.0) + + @close_button.on_click + def _(_) -> None: + assert camera_edit_panel is not None + camera_edit_panel.remove() + self._camera_edit_panel = None + + self._keyframes[keyframe_index] = (keyframe, frustum_handle) + + def update_aspect(self, aspect: float) -> None: + for keyframe_index, frame in self._keyframes.items(): + frame = dataclasses.replace(frame[0], aspect=aspect) + self.add_camera(frame, keyframe_index=keyframe_index) + + def get_aspect(self) -> float: + """Get W/H aspect ratio, which is shared across all keyframes.""" + assert len(self._keyframes) > 0 + return next(iter(self._keyframes.values()))[0].aspect + + def reset(self) -> None: + for frame in self._keyframes.values(): + frame[1].remove() + self._keyframes.clear() + self.update_spline() + + def spline_t_from_t_sec(self, time: np.ndarray) -> np.ndarray: + """From a time value in seconds, compute a t value for our geometric + spline interpolation. An increment of 1 for the latter will move the + camera forward by one keyframe. + + We use a PCHIP spline here to guarantee monotonicity. + """ + transition_times_cumsum = self.compute_transition_times_cumsum() + spline_indices = np.arange(transition_times_cumsum.shape[0]) + + if self.loop: + # In the case of a loop, we pad the spline to match the start/end + # slopes. + interpolator = scipy.interpolate.PchipInterpolator( + x=np.concatenate( + [ + [-(transition_times_cumsum[-1] - transition_times_cumsum[-2])], + transition_times_cumsum, + transition_times_cumsum[-1:] + transition_times_cumsum[1:2], + ], + axis=0, + ), + y=np.concatenate([[-1], spline_indices, [spline_indices[-1] + 1]], axis=0), + ) + else: + interpolator = scipy.interpolate.PchipInterpolator(x=transition_times_cumsum, y=spline_indices) + + # Clip to account for floating point error. + return np.clip(interpolator(time), 0, spline_indices[-1]) + + def interpolate_pose_and_fov_rad(self, normalized_t: float) -> Optional[Tuple[tf.SE3, float]]: + if len(self._keyframes) < 2: + return None + + self._fov_spline = splines.KochanekBartels( + [ + keyframe[0].override_fov_rad if keyframe[0].override_fov_enabled else self.default_fov + for keyframe in self._keyframes.values() + ], + tcb=(self.tension, 0.0, 0.0), + endconditions="closed" if self.loop else "natural", + ) + + assert self._orientation_spline is not None + assert self._position_spline is not None + assert self._fov_spline is not None + max_t = self.compute_duration() + t = max_t * normalized_t + spline_t = float(self.spline_t_from_t_sec(np.array(t))) + + quat = self._orientation_spline.evaluate(spline_t) + assert isinstance(quat, splines.quaternion.UnitQuaternion) + return ( + tf.SE3.from_rotation_and_translation( + tf.SO3(np.array([quat.scalar, *quat.vector])), + self._position_spline.evaluate(spline_t), + ), + float(self._fov_spline.evaluate(spline_t)), + ) + + def update_spline(self) -> None: + num_frames = int(self.compute_duration() * self.framerate) + keyframes = list(self._keyframes.values()) + + if num_frames <= 0 or not self.show_spline or len(keyframes) < 2: + for node in self._spline_nodes: + node.remove() + self._spline_nodes.clear() + return + + transition_times_cumsum = self.compute_transition_times_cumsum() + + self._orientation_spline = splines.quaternion.KochanekBartels( + [ + splines.quaternion.UnitQuaternion.from_unit_xyzw(np.roll(keyframe[0].wxyz, shift=-1)) + for keyframe in keyframes + ], + tcb=(self.tension, 0.0, 0.0), + endconditions="closed" if self.loop else "natural", + ) + self._position_spline = splines.KochanekBartels( + [keyframe[0].position for keyframe in keyframes], + tcb=(self.tension, 0.0, 0.0), + endconditions="closed" if self.loop else "natural", + ) + + # Update visualized spline. + points_array = self._position_spline.evaluate( + self.spline_t_from_t_sec(np.linspace(0, transition_times_cumsum[-1], num_frames)) + ) + colors_array = np.array([colorsys.hls_to_rgb(h, 0.5, 1.0) for h in np.linspace(0.0, 1.0, len(points_array))]) + + # Clear prior spline nodes. + for node in self._spline_nodes: + node.remove() + self._spline_nodes.clear() + + self._spline_nodes.append( + self._server.add_spline_catmull_rom( + "/render_camera_spline", + positions=points_array, + color=(220, 220, 220), + closed=self.loop, + line_width=1.0, + segments=points_array.shape[0] + 1, + ) + ) + self._spline_nodes.append( + self._server.add_point_cloud( + "/render_camera_spline/points", + points=points_array, + colors=colors_array, + point_size=0.04, + ) + ) + + def make_transition_handle(i: int) -> None: + assert self._position_spline is not None + transition_pos = self._position_spline.evaluate( + float( + self.spline_t_from_t_sec( + (transition_times_cumsum[i] + transition_times_cumsum[i + 1]) / 2.0, + ) + ) + ) + transition_sphere = self._server.add_icosphere( + f"/render_camera_spline/transition_{i}", + radius=0.04, + color=(255, 0, 0), + position=transition_pos, + ) + self._spline_nodes.append(transition_sphere) + + @transition_sphere.on_click + def _(_) -> None: + server = self._server + + if self._camera_edit_panel is not None: + self._camera_edit_panel.remove() + self._camera_edit_panel = None + + keyframe_index = (i + 1) % len(self._keyframes) + keyframe = keyframes[keyframe_index][0] + + with server.add_3d_gui_container( + "/camera_edit_panel", + position=transition_pos, + ) as camera_edit_panel: + self._camera_edit_panel = camera_edit_panel + override_transition_enabled = server.add_gui_checkbox( + "Override transition", + initial_value=keyframe.override_transition_enabled, + ) + override_transition_sec = server.add_gui_number( + "Override transition (sec)", + initial_value=keyframe.override_transition_sec + if keyframe.override_transition_sec is not None + else self.default_transition_sec, + min=0.001, + max=30.0, + step=0.001, + disabled=not override_transition_enabled.value, + ) + close_button = server.add_gui_button("Close") + + @override_transition_enabled.on_update + def _(_) -> None: + keyframe.override_transition_enabled = override_transition_enabled.value + override_transition_sec.disabled = not override_transition_enabled.value + self._duration_element.value = self.compute_duration() + + @override_transition_sec.on_update + def _(_) -> None: + keyframe.override_transition_sec = override_transition_sec.value + self._duration_element.value = self.compute_duration() + + @close_button.on_click + def _(_) -> None: + assert camera_edit_panel is not None + camera_edit_panel.remove() + self._camera_edit_panel = None + + (num_transitions_plus_1,) = transition_times_cumsum.shape + for i in range(num_transitions_plus_1 - 1): + make_transition_handle(i) + + # for i in range(transition_times.shape[0]) + + def compute_duration(self) -> float: + """Compute the total duration of the trajectory.""" + total = 0.0 + for i, (keyframe, frustum) in enumerate(self._keyframes.values()): + if i == 0 and not self.loop: + continue + del frustum + total += ( + keyframe.override_transition_sec + if keyframe.override_transition_enabled and keyframe.override_transition_sec is not None + else self.default_transition_sec + ) + return total + + def compute_transition_times_cumsum(self) -> np.ndarray: + """Compute the total duration of the trajectory.""" + total = 0.0 + out = [0.0] + for i, (keyframe, frustum) in enumerate(self._keyframes.values()): + if i == 0: + continue + del frustum + total += ( + keyframe.override_transition_sec + if keyframe.override_transition_enabled and keyframe.override_transition_sec is not None + else self.default_transition_sec + ) + out.append(total) + + if self.loop: + keyframe = next(iter(self._keyframes.values()))[0] + total += ( + keyframe.override_transition_sec + if keyframe.override_transition_enabled and keyframe.override_transition_sec is not None + else self.default_transition_sec + ) + out.append(total) + + return np.array(out) + + +@dataclasses.dataclass +class RenderTabState: + """Useful GUI handles exposed by the render tab.""" + + preview_render: bool + preview_fov: float + preview_aspect: float + preview_camera_type: Literal["Perspective", "Fisheye", "Equirectangular"] + + +def populate_render_tab( + server: viser.ViserServer, + config_path: Path, + datapath: Path, + control_panel: Optional[ControlPanel] = None, +) -> RenderTabState: + from nerfstudio.viewer.viewer import VISER_NERFSTUDIO_SCALE_RATIO + + render_tab_state = RenderTabState( + preview_render=False, + preview_fov=0.0, + preview_aspect=1.0, + preview_camera_type="Perspective", + ) + + fov_degrees = server.add_gui_slider( + "Default FOV", + initial_value=75.0, + min=0.1, + max=175.0, + step=0.01, + hint="Field-of-view for rendering, which can also be overridden on a per-keyframe basis.", + ) + + @fov_degrees.on_update + def _(_) -> None: + fov_radians = fov_degrees.value / 180.0 * np.pi + for client in server.get_clients().values(): + client.camera.fov = fov_radians + camera_path.default_fov = fov_radians + + # Updating the aspect ratio will also re-render the camera frustums. + # Could rethink this. + camera_path.update_aspect(resolution.value[0] / resolution.value[1]) + compute_and_update_preview_camera_state() + + resolution = server.add_gui_vector2( + "Resolution", + initial_value=(1920, 1080), + min=(50, 50), + max=(10_000, 10_000), + step=1, + hint="Render output resolution in pixels.", + ) + + @resolution.on_update + def _(_) -> None: + camera_path.update_aspect(resolution.value[0] / resolution.value[1]) + compute_and_update_preview_camera_state() + + camera_type = server.add_gui_dropdown( + "Camera type", + ("Perspective", "Fisheye", "Equirectangular"), + initial_value="Perspective", + hint="Camera model to render with. This is applied to all keyframes.", + ) + add_button = server.add_gui_button( + "Add Keyframe", + icon=viser.Icon.PLUS, + hint="Add a new keyframe at the current pose.", + ) + + @add_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client_id is not None + camera = server.get_clients()[event.client_id].camera + + # Add this camera to the path. + camera_path.add_camera( + Keyframe.from_camera( + camera, + aspect=resolution.value[0] / resolution.value[1], + ), + ) + duration_number.value = camera_path.compute_duration() + camera_path.update_spline() + + clear_keyframes_button = server.add_gui_button( + "Clear Keyframes", + icon=viser.Icon.TRASH, + hint="Remove all keyframes from the render path.", + ) + + @clear_keyframes_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client_id is not None + client = server.get_clients()[event.client_id] + with client.atomic(), client.add_gui_modal("Confirm") as modal: + client.add_gui_markdown("Clear all keyframes?") + confirm_button = client.add_gui_button("Yes", color="red", icon=viser.Icon.TRASH) + exit_button = client.add_gui_button("Cancel") + + @confirm_button.on_click + def _(_) -> None: + camera_path.reset() + modal.close() + + duration_number.value = camera_path.compute_duration() + + # Clear move handles. + if len(transform_controls) > 0: + for t in transform_controls: + t.remove() + transform_controls.clear() + return + + @exit_button.on_click + def _(_) -> None: + modal.close() + + loop = server.add_gui_checkbox("Loop", False, hint="Add a segment between the first and last keyframes.") + + @loop.on_update + def _(_) -> None: + camera_path.loop = loop.value + duration_number.value = camera_path.compute_duration() + + tension_slider = server.add_gui_slider( + "Spline tension", + min=0.0, + max=1.0, + initial_value=0.0, + step=0.01, + hint="Tension parameter for adjusting smoothness of spline interpolation.", + ) + + @tension_slider.on_update + def _(_) -> None: + camera_path.tension = tension_slider.value + camera_path.update_spline() + + move_checkbox = server.add_gui_checkbox( + "Move keyframes", + initial_value=False, + hint="Toggle move handles for keyframes in the scene.", + ) + + transform_controls: List[viser.SceneNodeHandle] = [] + + @move_checkbox.on_update + def _(event: viser.GuiEvent) -> None: + # Clear move handles when toggled off. + if move_checkbox.value is False: + for t in transform_controls: + t.remove() + transform_controls.clear() + return + + def _make_transform_controls_callback( + keyframe: Tuple[Keyframe, viser.SceneNodeHandle], + controls: viser.TransformControlsHandle, + ) -> None: + @controls.on_update + def _(_) -> None: + keyframe[0].wxyz = controls.wxyz + keyframe[0].position = controls.position + + keyframe[1].wxyz = controls.wxyz + keyframe[1].position = controls.position + + camera_path.update_spline() + + # Show move handles. + assert event.client is not None + for keyframe_index, keyframe in camera_path._keyframes.items(): + controls = event.client.add_transform_controls( + f"/keyframe_move/{keyframe_index}", + scale=0.4, + wxyz=keyframe[0].wxyz, + position=keyframe[0].position, + ) + transform_controls.append(controls) + _make_transform_controls_callback(keyframe, controls) + + show_keyframe_checkbox = server.add_gui_checkbox( + "Show keyframes", + initial_value=True, + hint="Show keyframes in the scene.", + ) + + @show_keyframe_checkbox.on_update + def _(_: viser.GuiEvent) -> None: + camera_path.set_keyframes_visible(show_keyframe_checkbox.value) + + show_spline_checkbox = server.add_gui_checkbox( + "Show spline", + initial_value=True, + hint="Show camera path spline in the scene.", + ) + + @show_spline_checkbox.on_update + def _(_) -> None: + camera_path.show_spline = show_spline_checkbox.value + camera_path.update_spline() + + playback_folder = server.add_gui_folder("Playback") + with playback_folder: + play_button = server.add_gui_button("Play", icon=viser.Icon.PLAYER_PLAY) + pause_button = server.add_gui_button("Pause", icon=viser.Icon.PLAYER_PAUSE, visible=False) + preview_render_button = server.add_gui_button( + "Preview Render", hint="Show a preview of the render in the viewport." + ) + preview_render_stop_button = server.add_gui_button("Exit Render Preview", color="red", visible=False) + + transition_sec_number = server.add_gui_number( + "Transition (sec)", + min=0.001, + max=30.0, + step=0.001, + initial_value=2.0, + hint="Time in seconds between each keyframe, which can also be overridden on a per-transition basis.", + ) + framerate_number = server.add_gui_number("FPS", min=0.1, max=240.0, step=1e-2, initial_value=30.0) + framerate_buttons = server.add_gui_button_group("", ("24", "30", "60")) + duration_number = server.add_gui_number( + "Duration (sec)", + min=0.0, + max=1e8, + step=0.001, + initial_value=0.0, + disabled=True, + ) + + @framerate_buttons.on_click + def _(_) -> None: + framerate_number.value = float(framerate_buttons.value) + + @transition_sec_number.on_update + def _(_) -> None: + camera_path.default_transition_sec = transition_sec_number.value + duration_number.value = camera_path.compute_duration() + + def get_max_frame_index() -> int: + return max(1, int(framerate_number.value * duration_number.value) - 1) + + preview_camera_handle: Optional[viser.SceneNodeHandle] = None + + def remove_preview_camera() -> None: + nonlocal preview_camera_handle + if preview_camera_handle is not None: + preview_camera_handle.remove() + preview_camera_handle = None + + def compute_and_update_preview_camera_state() -> Optional[Tuple[tf.SE3, float]]: + """Update the render tab state with the current preview camera pose. + Returns current camera pose + FOV if available.""" + + if preview_frame_slider is None: + return + maybe_pose_and_fov_rad = camera_path.interpolate_pose_and_fov_rad( + preview_frame_slider.value / get_max_frame_index() + ) + if maybe_pose_and_fov_rad is None: + remove_preview_camera() + return + pose, fov_rad = maybe_pose_and_fov_rad + render_tab_state.preview_fov = fov_rad + render_tab_state.preview_aspect = camera_path.get_aspect() + render_tab_state.preview_camera_type = camera_type.value + return pose, fov_rad + + def add_preview_frame_slider() -> Optional[viser.GuiInputHandle[int]]: + """Helper for creating the current frame # slider. This is removed and + re-added anytime the `max` value changes.""" + + with playback_folder: + preview_frame_slider = server.add_gui_slider( + "Preview frame", + min=0, + max=get_max_frame_index(), + step=1, + initial_value=0, + # Place right after the pause button. + order=preview_render_stop_button.order + 0.01, + disabled=get_max_frame_index() == 1, + ) + play_button.disabled = preview_frame_slider.disabled + preview_render_button.disabled = preview_frame_slider.disabled + + @preview_frame_slider.on_update + def _(_) -> None: + nonlocal preview_camera_handle + maybe_pose_and_fov_rad = compute_and_update_preview_camera_state() + if maybe_pose_and_fov_rad is None: + return + pose, fov_rad = maybe_pose_and_fov_rad + + preview_camera_handle = server.add_camera_frustum( + "/preview_camera", + fov=fov_rad, + aspect=resolution.value[0] / resolution.value[1], + scale=0.35, + wxyz=pose.rotation().wxyz, + position=pose.translation(), + color=(10, 200, 30), + ) + if render_tab_state.preview_render: + for client in server.get_clients().values(): + client.camera.wxyz = pose.rotation().wxyz + client.camera.position = pose.translation() + + return preview_frame_slider + + # We back up the camera poses before and after we start previewing renders. + camera_pose_backup_from_id: Dict[int, tuple] = {} + + @preview_render_button.on_click + def _(_) -> None: + render_tab_state.preview_render = True + preview_render_button.visible = False + preview_render_stop_button.visible = True + + maybe_pose_and_fov_rad = compute_and_update_preview_camera_state() + if maybe_pose_and_fov_rad is None: + remove_preview_camera() + return + pose, fov = maybe_pose_and_fov_rad + del fov + + # Hide all scene nodes when we're previewing the render. + server.set_global_scene_node_visibility(False) + + # Back up and then set camera poses. + for client in server.get_clients().values(): + camera_pose_backup_from_id[client.client_id] = ( + client.camera.position, + client.camera.look_at, + client.camera.up_direction, + ) + client.camera.wxyz = pose.rotation().wxyz + client.camera.position = pose.translation() + + @preview_render_stop_button.on_click + def _(_) -> None: + render_tab_state.preview_render = False + preview_render_button.visible = True + preview_render_stop_button.visible = False + + # Revert camera poses. + for client in server.get_clients().values(): + if client.client_id not in camera_pose_backup_from_id: + continue + cam_position, cam_look_at, cam_up = camera_pose_backup_from_id.pop(client.client_id) + client.camera.position = cam_position + client.camera.look_at = cam_look_at + client.camera.up_direction = cam_up + client.flush() + + # Un-hide scene nodes. + server.set_global_scene_node_visibility(True) + + preview_frame_slider = add_preview_frame_slider() + + # Update the # of frames. + @duration_number.on_update + @framerate_number.on_update + def _(_) -> None: + remove_preview_camera() # Will be re-added when slider is updated. + + nonlocal preview_frame_slider + old = preview_frame_slider + assert old is not None + + preview_frame_slider = add_preview_frame_slider() + if preview_frame_slider is not None: + old.remove() + else: + preview_frame_slider = old + + camera_path.framerate = framerate_number.value + camera_path.update_spline() + + # Play the camera trajectory when the play button is pressed. + @play_button.on_click + def _(_) -> None: + play_button.visible = False + pause_button.visible = True + + def play() -> None: + while not play_button.visible: + max_frame = int(framerate_number.value * duration_number.value) + if max_frame > 0: + assert preview_frame_slider is not None + preview_frame_slider.value = (preview_frame_slider.value + 1) % max_frame + time.sleep(1.0 / framerate_number.value) + + threading.Thread(target=play).start() + + # Play the camera trajectory when the play button is pressed. + @pause_button.on_click + def _(_) -> None: + play_button.visible = True + pause_button.visible = False + + # add button for loading existing path + load_camera_path_button = server.add_gui_button( + "Load Path", icon=viser.Icon.FOLDER_OPEN, hint="Load an existing camera path." + ) + + @load_camera_path_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client is not None + camera_path_dir = datapath / "camera_paths" + camera_path_dir.mkdir(parents=True, exist_ok=True) + preexisting_camera_paths = list(camera_path_dir.glob("*.json")) + preexisting_camera_filenames = [p.name for p in preexisting_camera_paths] + + with event.client.add_gui_modal("Load Path") as modal: + if len(preexisting_camera_filenames) == 0: + event.client.add_gui_markdown("No existing paths found") + else: + event.client.add_gui_markdown("Select existing camera path:") + camera_path_dropdown = event.client.add_gui_dropdown( + label="Camera Path", + options=[str(p) for p in preexisting_camera_filenames], + initial_value=str(preexisting_camera_filenames[0]), + ) + load_button = event.client.add_gui_button("Load") + + @load_button.on_click + def _(_) -> None: + # load the json file + json_path = datapath / "camera_paths" / camera_path_dropdown.value + with open(json_path, "r") as f: + json_data = json.load(f) + + keyframes = json_data["keyframes"] + camera_path.reset() + for i in range(len(keyframes)): + frame = keyframes[i] + pose = tf.SE3.from_matrix(np.array(frame["matrix"]).reshape(4, 4)) + # apply the x rotation by 180 deg + pose = tf.SE3.from_rotation_and_translation( + pose.rotation() @ tf.SO3.from_x_radians(np.pi), + pose.translation(), + ) + camera_path.add_camera( + Keyframe( + position=pose.translation() * VISER_NERFSTUDIO_SCALE_RATIO, + wxyz=pose.rotation().wxyz, + # There are some floating point conversions between degrees and radians, so the fov and + # default_Fov values will not be exactly matched. + override_fov_enabled=abs(frame["fov"] - json_data.get("default_fov", 0.0)) < 1e-3, + override_fov_rad=frame["fov"] / 180.0 * np.pi, + aspect=frame["aspect"], + override_transition_enabled=frame.get("override_transition_enabled", None), + override_transition_sec=frame.get("override_transition_sec", None), + ), + ) + + transition_sec_number.value = json_data.get("default_transition_sec", 0.5) + + # update the render name + render_name_text.value = json_path.stem + camera_path.update_spline() + modal.close() + + cancel_button = event.client.add_gui_button("Cancel") + + @cancel_button.on_click + def _(_) -> None: + modal.close() + + # set the initial value to the current date-time string + now = datetime.datetime.now() + render_name_text = server.add_gui_text( + "Render name", + initial_value=now.strftime("%Y-%m-%d-%H-%M-%S"), + hint="Name of the render", + ) + render_button = server.add_gui_button( + "Generate Command", + color="green", + icon=viser.Icon.FILE_EXPORT, + hint="Generate the ns-render command for rendering the camera path.", + ) + + reset_up_button = server.add_gui_button( + "Reset Up Direction", + icon=viser.Icon.ARROW_BIG_UP_LINES, + color="gray", + hint="Set the up direction of the camera orbit controls to the camera's current up direction.", + ) + + @reset_up_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client is not None + event.client.camera.up_direction = tf.SO3(event.client.camera.wxyz) @ np.array([0.0, -1.0, 0.0]) + + @render_button.on_click + def _(event: viser.GuiEvent) -> None: + assert event.client is not None + num_frames = int(framerate_number.value * duration_number.value) + json_data = {} + # json data has the properties: + # keyframes: list of keyframes with + # matrix : flattened 4x4 matrix + # fov: float in degrees + # aspect: float + # camera_type: string of camera type + # render_height: int + # render_width: int + # fps: int + # seconds: float + # is_cycle: bool + # smoothness_value: float + # camera_path: list of frames with properties + # camera_to_world: flattened 4x4 matrix + # fov: float in degrees + # aspect: float + # first populate the keyframes: + keyframes = [] + for keyframe, dummy in camera_path._keyframes.values(): + pose = tf.SE3.from_rotation_and_translation( + tf.SO3(keyframe.wxyz) @ tf.SO3.from_x_radians(np.pi), + keyframe.position / VISER_NERFSTUDIO_SCALE_RATIO, + ) + keyframes.append( + { + "matrix": pose.as_matrix().flatten().tolist(), + "fov": np.rad2deg(keyframe.override_fov_rad) + if keyframe.override_fov_enabled + else fov_degrees.value, + "aspect": keyframe.aspect, + "override_transition_enabled": keyframe.override_transition_enabled, + "override_transition_sec": keyframe.override_transition_sec, + } + ) + json_data["default_fov"] = fov_degrees.value + json_data["default_transition_sec"] = transition_sec_number.value + json_data["keyframes"] = keyframes + json_data["camera_type"] = camera_type.value.lower() + json_data["render_height"] = resolution.value[1] + json_data["render_width"] = resolution.value[0] + json_data["fps"] = framerate_number.value + json_data["seconds"] = duration_number.value + json_data["is_cycle"] = loop.value + json_data["smoothness_value"] = tension_slider.value + # now populate the camera path: + camera_path_list = [] + for i in range(num_frames): + maybe_pose_and_fov = camera_path.interpolate_pose_and_fov_rad(i / num_frames) + if maybe_pose_and_fov is None: + return + pose, fov = maybe_pose_and_fov + # rotate the axis of the camera 180 about x axis + pose = tf.SE3.from_rotation_and_translation( + pose.rotation() @ tf.SO3.from_x_radians(np.pi), + pose.translation() / VISER_NERFSTUDIO_SCALE_RATIO, + ) + camera_path_list.append( + { + "camera_to_world": pose.as_matrix().flatten().tolist(), + "fov": np.rad2deg(fov), + "aspect": resolution.value[0] / resolution.value[1], + } + ) + json_data["camera_path"] = camera_path_list + # finally add crop data if crop is enabled + if control_panel is not None: + if control_panel.crop_viewport: + obb = control_panel.crop_obb + rpy = tf.SO3.from_matrix(obb.R.numpy()).as_rpy_radians() + color = control_panel.background_color + json_data["crop"] = { + "crop_center": obb.T.tolist(), + "crop_scale": obb.S.tolist(), + "crop_rot": [rpy.roll, rpy.pitch, rpy.yaw], + "crop_bg_color": {"r": color[0], "g": color[1], "b": color[2]}, + } + + # now write the json file + json_outfile = datapath / "camera_paths" / f"{render_name_text.value}.json" + json_outfile.parent.mkdir(parents=True, exist_ok=True) + with open(json_outfile.absolute(), "w") as outfile: + json.dump(json_data, outfile) + # now show the command + with event.client.add_gui_modal("Render Command") as modal: + dataname = datapath.name + command = " ".join( + [ + "ns-render camera-path", + f"--load-config {config_path}", + f"--camera-path-filename {json_outfile.absolute()}", + f"--output-path renders/{dataname}/{render_name_text.value}.mp4", + ] + ) + event.client.add_gui_markdown( + "\n".join( + [ + "To render the trajectory, run the following from the command line:", + "", + "```", + command, + "```", + ] + ) + ) + close_button = event.client.add_gui_button("Close") + + @close_button.on_click + def _(_) -> None: + modal.close() + + camera_path = CameraPath(server, duration_number) + camera_path.default_fov = fov_degrees.value / 180.0 * np.pi + camera_path.default_transition_sec = transition_sec_number.value + + return render_tab_state + + +if __name__ == "__main__": + populate_render_tab( + server=viser.ViserServer(), + config_path=Path("."), + datapath=Path("."), + ) + while True: + time.sleep(10.0) diff --git a/nerfstudio/viewer_beta/render_state_machine.py b/nerfstudio/viewer/render_state_machine.py similarity index 75% rename from nerfstudio/viewer_beta/render_state_machine.py rename to nerfstudio/viewer/render_state_machine.py index 6355a5ba6..b9d43a782 100644 --- a/nerfstudio/viewer_beta/render_state_machine.py +++ b/nerfstudio/viewer/render_state_machine.py @@ -20,15 +20,19 @@ import threading from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Tuple, get_args +import numpy as np import torch +from nerfstudio.cameras.cameras import Cameras from nerfstudio.model_components.renderers import background_color_override_context +from nerfstudio.models.gaussian_splatting import GaussianSplattingModel from nerfstudio.utils import colormaps, writer from nerfstudio.utils.writer import GLOBAL_BUFFER, EventName, TimeWriter -from nerfstudio.viewer.server import viewer_utils -from nerfstudio.viewer_beta.utils import CameraState, get_camera +from nerfstudio.viewer_legacy.server import viewer_utils +from nerfstudio.viewer.utils import CameraState, get_camera +from viser import ClientHandle if TYPE_CHECKING: - from nerfstudio.viewer_beta.viewer import Viewer + from nerfstudio.viewer.viewer import Viewer RenderStates = Literal["low_move", "low_static", "high"] RenderActions = Literal["rerender", "move", "static", "step"] @@ -52,7 +56,7 @@ class RenderStateMachine(threading.Thread): viewer: the viewer state """ - def __init__(self, viewer: Viewer, viser_scale_ratio: float): + def __init__(self, viewer: Viewer, viser_scale_ratio: float, client: ClientHandle): threading.Thread.__init__(self) self.transitions: Dict[RenderStates, Dict[RenderActions, RenderStates]] = { s: {} for s in get_args(RenderStates) @@ -77,6 +81,8 @@ class RenderStateMachine(threading.Thread): self.daemon = True self.output_keys = {} self.viser_scale_ratio = viser_scale_ratio + self.client = client + self.running = True def action(self, action: RenderAction): """Takes an action and updates the state machine @@ -91,7 +97,7 @@ class RenderStateMachine(threading.Thread): # 1. we are in low_moving state # 2. the current next_action is move, static, or rerender return - elif self.next_action == "rerender": + elif self.next_action.action == "rerender": # never overwrite rerenders pass elif action.action == "static" and self.next_action.action == "move": @@ -107,7 +113,7 @@ class RenderStateMachine(threading.Thread): self.render_trigger.set() def _render_img(self, camera_state: CameraState): - """Takes the current camera, generates rays, and renders the iamge + """Takes the current camera, generates rays, and renders the image Args: camera_state: the current camera state @@ -122,11 +128,18 @@ class RenderStateMachine(threading.Thread): camera = get_camera(camera_state, image_height, image_width) camera = camera.to(self.viewer.get_model().device) + assert isinstance(camera, Cameras) assert camera is not None, "render called before viewer connected" with TimeWriter(None, None, write=False) as vis_t: with self.viewer.train_lock if self.viewer.train_lock is not None else contextlib.nullcontext(): - camera_ray_bundle = camera.generate_rays(camera_indices=0, obb_box=obb) + if isinstance(self.viewer.get_model(), GaussianSplattingModel): + color = self.viewer.control_panel.background_color + background_color = torch.tensor( + [color[0] / 255.0, color[1] / 255.0, color[2] / 255.0], + device=self.viewer.get_model().device, + ) + self.viewer.get_model().set_background(background_color) self.viewer.get_model().eval() step = self.viewer.step try: @@ -142,23 +155,29 @@ class RenderStateMachine(threading.Thread): with background_color_override_context( background_color ), torch.no_grad(), viewer_utils.SetTrace(self.check_interrupt): - outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) + outputs = self.viewer.get_model().get_outputs_for_camera(camera, obb_box=obb) else: with torch.no_grad(), viewer_utils.SetTrace(self.check_interrupt): - outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) + outputs = self.viewer.get_model().get_outputs_for_camera(camera, obb_box=obb) except viewer_utils.IOChangeException: self.viewer.get_model().train() raise self.viewer.get_model().train() - num_rays = len(camera_ray_bundle) + num_rays = (camera.height * camera.width).item() if self.viewer.control_panel.layer_depth: - # convert to z_depth if depth compositing is enabled - R = camera.camera_to_worlds[0:3, 0:3].T - pts = camera_ray_bundle.directions * outputs["depth"] - pts = (R @ (pts.view(-1, 3).T)).T.view(*camera_ray_bundle.directions.shape) - outputs["gl_z_buf_depth"] = -pts[..., 2:3] # negative z axis is the coordinate convention + if isinstance(self.viewer.get_model(), GaussianSplattingModel): + # TODO: sending depth at high resolution lags the network a lot, figure out how to do this more efficiently + # outputs["gl_z_buf_depth"] = outputs["depth"] + pass + else: + # convert to z_depth if depth compositing is enabled + R = camera.camera_to_worlds[0, 0:3, 0:3].T + camera_ray_bundle = camera.generate_rays(camera_indices=0, obb_box=obb) + pts = camera_ray_bundle.directions * outputs["depth"] + pts = (R @ (pts.view(-1, 3).T)).T.view(*camera_ray_bundle.directions.shape) + outputs["gl_z_buf_depth"] = -pts[..., 2:3] # negative z axis is the coordinate convention render_time = vis_t.duration - if writer.is_initialized(): + if writer.is_initialized() and render_time != 0: writer.put_time( name=EventName.VIS_RAYS_PER_SEC, duration=num_rays / render_time, step=step, avg_over_steps=True ) @@ -166,11 +185,10 @@ class RenderStateMachine(threading.Thread): def run(self): """Main loop for the render thread""" - while True: + while self.running: if not self.render_trigger.wait(0.2): # if we haven't received a trigger in a while, send a static action - if self.viewer.camera_state is not None: - self.action(RenderAction(action="static", camera_state=self.viewer.camera_state)) + self.action(RenderAction(action="static", camera_state=self.viewer.get_camera_state(self.client))) action = self.next_action self.render_trigger.clear() if action is None: @@ -185,7 +203,7 @@ class RenderStateMachine(threading.Thread): except viewer_utils.IOChangeException: # if we got interrupted, don't send the output to the viewer continue - self._send_output_to_viewer(outputs) + self._send_output_to_viewer(outputs, static_render=(action.action in ["static", "step"])) def check_interrupt(self, frame, event, arg): """Raises interrupt when flag has been set and not already on lowest resolution. @@ -197,7 +215,7 @@ class RenderStateMachine(threading.Thread): raise viewer_utils.IOChangeException return self.check_interrupt - def _send_output_to_viewer(self, outputs: Dict[str, Any]): + def _send_output_to_viewer(self, outputs: Dict[str, Any], static_render: bool = True): """Chooses the correct output and sends it to the viewer Args: @@ -237,12 +255,39 @@ class RenderStateMachine(threading.Thread): depth = ( outputs["gl_z_buf_depth"].cpu().numpy() * self.viser_scale_ratio if "gl_z_buf_depth" in outputs else None ) - self.viewer.viser_server.set_background_image( - selected_output.cpu().numpy(), + + # Convert to numpy. + selected_output = selected_output.cpu().numpy() + assert selected_output.shape[-1] == 3 + + # Pad image if the aspect ratio (W/H) doesn't match the client! + current_h, current_w = selected_output.shape[:2] + desired_aspect = self.client.camera.aspect + pad_width = int(max(0, (desired_aspect * current_h - current_w) // 2)) + pad_height = int(max(0, (current_w / desired_aspect - current_h) // 2)) + if pad_width > 5 or pad_height > 5: + selected_output = np.pad( + selected_output, + ((pad_height, pad_height), (pad_width, pad_width), (0, 0)), + mode="constant", + constant_values=0, + ) + + jpg_quality = ( + self.viewer.config.jpeg_quality + if static_render + else 75 + if self.viewer.render_tab_state.preview_render + else 40 + ) + self.client.set_background_image( + selected_output, format=self.viewer.config.image_format, - jpeg_quality=self.viewer.config.jpeg_quality, + jpeg_quality=jpg_quality, depth=depth, ) + res = f"{selected_output.shape[0]}x{selected_output.shape[1]}px" + self.viewer.stats_markdown.content = self.viewer.make_stats_markdown(None, res) def _calculate_image_res(self, aspect_ratio: float) -> Tuple[int, int]: """Calculate the maximum image height that can be rendered in the time budget diff --git a/nerfstudio/viewer_beta/utils.py b/nerfstudio/viewer/utils.py similarity index 87% rename from nerfstudio/viewer_beta/utils.py rename to nerfstudio/viewer/utils.py index a3dd7b1c2..8b9e23793 100644 --- a/nerfstudio/viewer_beta/utils.py +++ b/nerfstudio/viewer/utils.py @@ -15,7 +15,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Any, List, Optional, Tuple, Union +from typing import Any, List, Literal, Optional, Tuple, Union import numpy as np import torch @@ -31,11 +31,13 @@ class CameraState: """A dataclass for storing the camera state.""" fov: float - """ The field of view of the camera. """ + """The field of view of the camera.""" aspect: float - """ The aspect ratio of the image. """ + """The aspect ratio of the image. """ c2w: Float[torch.Tensor, "3 4"] - """ The camera matrix. """ + """The camera matrix.""" + camera_type: Literal[CameraType.PERSPECTIVE, CameraType.EQUIRECTANGULAR, CameraType.FISHEYE] + """Type of camera to render.""" def get_camera( @@ -57,15 +59,20 @@ def get_camera( focal_length = pp_h / np.tan(fov / 2.0) intrinsics_matrix = torch.tensor([[focal_length, 0, pp_w], [0, focal_length, pp_h], [0, 0, 1]], dtype=torch.float32) - camera_type = CameraType.PERSPECTIVE + if camera_state.camera_type is CameraType.EQUIRECTANGULAR: + fx = float(image_width / 2) + fy = float(image_height) + else: + fx = intrinsics_matrix[0, 0] + fy = intrinsics_matrix[1, 1] camera = Cameras( - fx=intrinsics_matrix[0, 0], - fy=intrinsics_matrix[1, 1], + fx=fx, + fy=fy, cx=pp_w, cy=pp_h, - camera_type=camera_type, - camera_to_worlds=camera_state.c2w.to(torch.float32), + camera_type=camera_state.camera_type, + camera_to_worlds=camera_state.c2w.to(torch.float32)[None, ...], times=torch.tensor([0.0], dtype=torch.float32), ) return camera diff --git a/nerfstudio/viewer_beta/viewer.py b/nerfstudio/viewer/viewer.py similarity index 64% rename from nerfstudio/viewer_beta/viewer.py rename to nerfstudio/viewer/viewer.py index d4fbf973b..d17b93e06 100644 --- a/nerfstudio/viewer_beta/viewer.py +++ b/nerfstudio/viewer/viewer.py @@ -26,21 +26,22 @@ import torchvision import viser import viser.theme import viser.transforms as vtf - from nerfstudio.cameras.camera_optimizers import CameraOptimizer +from nerfstudio.cameras.cameras import CameraType from nerfstudio.configs import base_config as cfg from nerfstudio.data.datasets.base_dataset import InputDataset from nerfstudio.models.base_model import Model from nerfstudio.pipelines.base_pipeline import Pipeline from nerfstudio.utils.decorators import check_main_thread, decorate_all from nerfstudio.utils.writer import GLOBAL_BUFFER, EventName -from nerfstudio.viewer.server import viewer_utils -from nerfstudio.viewer_beta.control_panel import ControlPanel -from nerfstudio.viewer_beta.export_panel import populate_export_tab -from nerfstudio.viewer_beta.render_panel import populate_render_tab -from nerfstudio.viewer_beta.render_state_machine import RenderAction, RenderStateMachine -from nerfstudio.viewer_beta.utils import CameraState, parse_object -from nerfstudio.viewer_beta.viewer_elements import ViewerControl, ViewerElement +from nerfstudio.viewer_legacy.server import viewer_utils +from nerfstudio.viewer.control_panel import ControlPanel +from nerfstudio.viewer.export_panel import populate_export_tab +from nerfstudio.viewer.render_panel import populate_render_tab +from nerfstudio.viewer.render_state_machine import RenderAction, RenderStateMachine +from nerfstudio.viewer.utils import CameraState, parse_object +from nerfstudio.viewer.viewer_elements import ViewerControl, ViewerElement +from typing_extensions import assert_never if TYPE_CHECKING: from nerfstudio.engine.trainer import Trainer @@ -80,6 +81,7 @@ class Viewer: train_lock: Optional[threading.Lock] = None, share: bool = False, ): + self.ready = False # Set to True at end of constructor. self.config = config self.trainer = trainer self.last_step = 0 @@ -95,17 +97,25 @@ class Viewer: websocket_port = self.config.websocket_port self.log_filename.parent.mkdir(exist_ok=True) - self.viewer_url = viewer_utils.get_viewer_url(websocket_port) - # viewer specific variables self.output_type_changed = True self.output_split_type_changed = True self.step = 0 self.train_btn_state: Literal["training", "paused", "completed"] = "training" self._prev_train_state: Literal["training", "paused", "completed"] = "training" + self.last_move_time = 0 - self.client: Optional[viser.ClientHandle] = None - self.viser_server = viser.ViserServer(host=config.websocket_host, port=websocket_port, share=share) + self.viser_server = viser.ViserServer(host=config.websocket_host, port=websocket_port) + # Set the name of the URL either to the share link if available, or the localhost + if share: + url = self.viser_server.request_share_url() + if url is not None: + print("Couldn't make share URL") + self.viewer_url = url + else: + self.viewer_url = f"http://{config.websocket_host}:{websocket_port}" + else: + self.viewer_url = f"http://{config.websocket_host}:{websocket_port}" buttons = ( viser.theme.TitlebarButton( text="Getting Started", @@ -124,8 +134,8 @@ class Viewer: ), ) image = viser.theme.TitlebarImage( - image_url_light="https://docs.nerf.studio/en/latest/_static/imgs/logo.png", - image_url_dark="https://docs.nerf.studio/en/latest/_static/imgs/logo-dark.png", + image_url_light="https://docs.nerf.studio/_static/imgs/logo.png", + image_url_dark="https://docs.nerf.studio/_static/imgs/logo-dark.png", image_alt="NerfStudio Logo", href="https://docs.nerf.studio/", ) @@ -137,9 +147,36 @@ class Viewer: brand_color=(255, 211, 105), ) - self.render_statemachine = RenderStateMachine(self, VISER_NERFSTUDIO_SCALE_RATIO) + self.render_statemachines: Dict[int, RenderStateMachine] = {} + self.viser_server.on_client_disconnect(self.handle_disconnect) self.viser_server.on_client_connect(self.handle_new_client) + # Populate the header, which includes the pause button, train cam button, and stats + self.pause_train = self.viser_server.add_gui_button( + label="Pause Training", disabled=False, icon=viser.Icon.PLAYER_PAUSE_FILLED + ) + self.pause_train.on_click(lambda _: self.toggle_pause_button()) + self.pause_train.on_click(lambda han: self._toggle_training_state(han)) + self.resume_train = self.viser_server.add_gui_button( + label="Resume Training", disabled=False, icon=viser.Icon.PLAYER_PLAY_FILLED + ) + self.resume_train.on_click(lambda _: self.toggle_pause_button()) + self.resume_train.on_click(lambda han: self._toggle_training_state(han)) + self.resume_train.visible = False + # Add buttons to toggle training image visibility + self.hide_images = self.viser_server.add_gui_button( + label="Hide Train Cams", disabled=False, icon=viser.Icon.EYE_OFF, color=None + ) + self.hide_images.on_click(lambda _: self.set_camera_visibility(False)) + self.hide_images.on_click(lambda _: self.toggle_cameravis_button()) + self.show_images = self.viser_server.add_gui_button( + label="Show Train Cams", disabled=False, icon=viser.Icon.EYE, color=None + ) + self.show_images.on_click(lambda _: self.set_camera_visibility(True)) + self.show_images.on_click(lambda _: self.toggle_cameravis_button()) + self.show_images.visible = False + mkdown = self.make_stats_markdown(0, "0x0px") + self.stats_markdown = self.viser_server.add_gui_markdown(mkdown) tabs = self.viser_server.add_gui_tab_group() control_tab = tabs.add_tab("Control", viser.Icon.SETTINGS) with control_tab: @@ -147,16 +184,16 @@ class Viewer: self.viser_server, self.include_time, VISER_NERFSTUDIO_SCALE_RATIO, - self._interrupt_render, - self._crop_params_update, + self._trigger_rerender, self._output_type_change, self._output_split_type_change, - self._toggle_training_state, - self.set_camera_visibility, + default_composite_depth=self.config.default_composite_depth, ) config_path = self.log_filename.parents[0] / "config.yml" with tabs.add_tab("Render", viser.Icon.CAMERA): - populate_render_tab(self.viser_server, config_path, self.datapath, self.control_panel) + self.render_tab_state = populate_render_tab( + self.viser_server, config_path, self.datapath, self.control_panel + ) with tabs.add_tab("Export", viser.Icon.PACKAGE_EXPORT): populate_export_tab(self.viser_server, self.control_panel, config_path) @@ -169,7 +206,7 @@ class Viewer: element.install(self.viser_server) # also rewire the hook to rerender prev_cb = element.cb_hook - element.cb_hook = lambda element: [prev_cb(element), self._interrupt_render(element)] + element.cb_hook = lambda element: [prev_cb(element), self._trigger_rerender()] else: # recursively create folders # If the folder name is "Custom Elements/a/b", then: @@ -198,28 +235,81 @@ class Viewer: # scrape the trainer/pipeline for any ViewerControl objects to initialize them self.viewer_controls: List[ViewerControl] = [ - e for (_, e) in parse_object(self.trainer, ViewerControl, "Custom Elements") + e for (_, e) in parse_object(pipeline, ViewerControl, "Custom Elements") ] for c in self.viewer_controls: c._setup(self) - self.render_statemachine.start() + + self.ready = True + + def toggle_pause_button(self) -> None: + self.pause_train.visible = not self.pause_train.visible + self.resume_train.visible = not self.resume_train.visible + + def toggle_cameravis_button(self) -> None: + self.hide_images.visible = not self.hide_images.visible + self.show_images.visible = not self.show_images.visible + + def make_stats_markdown(self, step: Optional[int], res: Optional[str]) -> str: + # if either are None, read it from the current stats_markdown content + if step is None: + step = int(self.stats_markdown.content.split("\n")[0].split(": ")[1]) + if res is None: + res = (self.stats_markdown.content.split("\n")[1].split(": ")[1]).strip() + return f"Step: {step} \nResolution: {res}" + + def update_step(self, step): + """ + Args: + step: the train step to set the model to + """ + self.stats_markdown.content = self.make_stats_markdown(step, None) + + def get_camera_state(self, client: viser.ClientHandle) -> CameraState: + R = vtf.SO3(wxyz=client.camera.wxyz) + R = R @ vtf.SO3.from_x_radians(np.pi) + R = torch.tensor(R.as_matrix()) + pos = torch.tensor(client.camera.position, dtype=torch.float64) / VISER_NERFSTUDIO_SCALE_RATIO + c2w = torch.concatenate([R, pos[:, None]], dim=1) + if self.ready and self.render_tab_state.preview_render: + camera_type = self.render_tab_state.preview_camera_type + camera_state = CameraState( + fov=self.render_tab_state.preview_fov, + aspect=self.render_tab_state.preview_aspect, + c2w=c2w, + camera_type=CameraType.PERSPECTIVE + if camera_type == "Perspective" + else CameraType.FISHEYE + if camera_type == "Fisheye" + else CameraType.EQUIRECTANGULAR + if camera_type == "Equirectangular" + else assert_never(camera_type), + ) + else: + camera_state = CameraState( + fov=client.camera.fov, + aspect=client.camera.aspect, + c2w=c2w, + camera_type=CameraType.PERSPECTIVE, + ) + return camera_state + + def handle_disconnect(self, client: viser.ClientHandle) -> None: + self.render_statemachines[client.client_id].running = False + self.render_statemachines.pop(client.client_id) def handle_new_client(self, client: viser.ClientHandle) -> None: - self.client = client - self.last_move_time = 0 + self.render_statemachines[client.client_id] = RenderStateMachine(self, VISER_NERFSTUDIO_SCALE_RATIO, client) + self.render_statemachines[client.client_id].start() @client.camera.on_update - def _(cam: viser.CameraHandle) -> None: - assert self.client is not None - with client.atomic(): - self.last_move_time = time.time() - R = vtf.SO3(wxyz=self.client.camera.wxyz) - R = R @ vtf.SO3.from_x_radians(np.pi) - R = torch.tensor(R.as_matrix()) - pos = torch.tensor(self.client.camera.position, dtype=torch.float64) / VISER_NERFSTUDIO_SCALE_RATIO - c2w = torch.concatenate([R, pos[:, None]], dim=1) - self.camera_state = CameraState(fov=self.client.camera.fov, aspect=self.client.camera.aspect, c2w=c2w) - self.render_statemachine.action(RenderAction("move", self.camera_state)) + def _(_: viser.CameraHandle) -> None: + if not self.ready: + return + self.last_move_time = time.time() + with self.viser_server.atomic(): + camera_state = self.get_camera_state(client) + self.render_statemachines[client.client_id].action(RenderAction("move", camera_state)) def set_camera_visibility(self, visible: bool) -> None: """Toggle the visibility of the training cameras.""" @@ -228,30 +318,37 @@ class Viewer: self.camera_handles[idx].visible = visible def update_camera_poses(self): + # TODO this fn accounts for like ~5% of total train time # Update the train camera locations based on optimization assert self.camera_handles is not None - idxs = list(self.camera_handles.keys()) if hasattr(self.pipeline.datamanager, "train_camera_optimizer"): camera_optimizer = self.pipeline.datamanager.train_camera_optimizer - else: + elif hasattr(self.pipeline.model, "camera_optimizer"): camera_optimizer = self.pipeline.model.camera_optimizer + else: + return + idxs = list(self.camera_handles.keys()) with torch.no_grad(): assert isinstance(camera_optimizer, CameraOptimizer) c2ws_delta = camera_optimizer(torch.tensor(idxs, device=camera_optimizer.device)).cpu().numpy() - for idx in idxs: + for i, key in enumerate(idxs): # both are numpy arrays - c2w_orig = self.original_c2w[idx] - c2w_delta = c2ws_delta[idx, ...] + c2w_orig = self.original_c2w[key] + c2w_delta = c2ws_delta[i, ...] c2w = c2w_orig @ np.concatenate((c2w_delta, np.array([[0, 0, 0, 1]])), axis=0) R = vtf.SO3.from_matrix(c2w[:3, :3]) # type: ignore R = R @ vtf.SO3.from_x_radians(np.pi) - self.camera_handles[idx].position = c2w[:3, 3] * VISER_NERFSTUDIO_SCALE_RATIO - self.camera_handles[idx].wxyz = R.wxyz + self.camera_handles[key].position = c2w[:3, 3] * VISER_NERFSTUDIO_SCALE_RATIO + self.camera_handles[key].wxyz = R.wxyz - def _interrupt_render(self, _) -> None: + def _trigger_rerender(self) -> None: """Interrupt current render.""" - if self.camera_state is not None: - self.render_statemachine.action(RenderAction("rerender", self.camera_state)) + if not self.ready: + return + clients = self.viser_server.get_clients() + for id in clients: + camera_state = self.get_camera_state(clients[id]) + self.render_statemachines[id].action(RenderAction("move", camera_state)) def _toggle_training_state(self, _) -> None: """Toggle the trainer's training state.""" @@ -261,11 +358,6 @@ class Viewer: elif self.trainer.training_state == "paused": self.trainer.training_state = "training" - def _crop_params_update(self, _) -> None: - """Update crop parameters""" - if self.camera_state is not None: - self.render_statemachine.action(RenderAction("move", self.camera_state)) - def _output_type_change(self, _): self.output_type_changed = True @@ -309,7 +401,7 @@ class Viewer: camera = train_dataset.cameras[idx] image_uint8 = (image * 255).detach().type(torch.uint8) image_uint8 = image_uint8.permute(2, 0, 1) - image_uint8 = torchvision.transforms.functional.resize(image_uint8, 100) # type: ignore + image_uint8 = torchvision.transforms.functional.resize(image_uint8, 100, antialias=None) # type: ignore image_uint8 = image_uint8.permute(1, 2, 0) image_uint8 = image_uint8.cpu().numpy() c2w = camera.camera_to_worlds.cpu().numpy() @@ -318,7 +410,7 @@ class Viewer: camera_handle = self.viser_server.add_camera_frustum( name=f"/cameras/camera_{idx:05d}", fov=float(2 * np.arctan(camera.cx / camera.fx[0])), - scale=0.1, + scale=self.config.camera_frustum_scale, aspect=float(camera.cx[0] / camera.cy[0]), image=image_uint8, wxyz=R.wxyz, @@ -326,11 +418,10 @@ class Viewer: ) @camera_handle.on_click - def _(event: viser.ClickEvent[viser.CameraFrustumHandle]) -> None: - assert self.client is not None - with self.client.atomic(): - self.client.camera.position = event.target.position - self.client.camera.wxyz = event.target.wxyz + def _(event: viser.SceneNodePointerEvent[viser.CameraFrustumHandle]) -> None: + with event.client.atomic(): + event.client.camera.position = event.target.position + event.client.camera.wxyz = event.target.wxyz self.camera_handles[idx] = camera_handle self.original_c2w[idx] = c2w @@ -347,12 +438,11 @@ class Viewer: """ self.step = step - if self.camera_state is None: + if len(self.render_statemachines) == 0: return # this stops training while moving to make the response smoother while time.time() - self.last_move_time < 0.1: time.sleep(0.05) - # self.render_statemachine.action(RenderAction("static", self.camera_state)) if self.trainer is not None and self.trainer.training_state == "training" and self.train_util != 1: if ( EventName.TRAIN_RAYS_PER_SEC.value in GLOBAL_BUFFER["events"] @@ -371,9 +461,13 @@ class Viewer: render_freq = 30 if step > self.last_step + render_freq: self.last_step = step - self.render_statemachine.action(RenderAction("step", self.camera_state)) + clients = self.viser_server.get_clients() + for id in clients: + camera_state = self.get_camera_state(clients[id]) + if camera_state is not None: + self.render_statemachines[id].action(RenderAction("step", camera_state)) self.update_camera_poses() - self.control_panel.update_step(step) + self.update_step(step) def update_colormap_options(self, dimensions: int, dtype: type) -> None: """update the colormap options based on the current render diff --git a/nerfstudio/viewer_beta/viewer_elements.py b/nerfstudio/viewer/viewer_elements.py similarity index 79% rename from nerfstudio/viewer_beta/viewer_elements.py rename to nerfstudio/viewer/viewer_elements.py index 916566d5d..2a8b64efe 100644 --- a/nerfstudio/viewer_beta/viewer_elements.py +++ b/nerfstudio/viewer/viewer_elements.py @@ -31,14 +31,15 @@ from viser import ( GuiButtonHandle, GuiDropdownHandle, GuiInputHandle, + ScenePointerEvent, ViserServer, ) -from nerfstudio.cameras.cameras import Cameras -from nerfstudio.viewer_beta.utils import CameraState, get_camera +from nerfstudio.viewer.utils import CameraState, get_camera +from nerfstudio.cameras.cameras import Cameras, CameraType if TYPE_CHECKING: - from nerfstudio.viewer_beta.viewer import Viewer + from nerfstudio.viewer.viewer import Viewer TValue = TypeVar("TValue") TString = TypeVar("TString", default=str, bound=str) @@ -68,7 +69,7 @@ class ViewerControl: def __init__(self): # this should be a user-facing constructor, since it will be used inside the model/pipeline class - self.click_cbs = [] + self._click_cbs = {} def _setup(self, viewer: Viewer): """ @@ -117,7 +118,7 @@ class ViewerControl: """ raise NotImplementedError() - def get_camera(self, img_height: int, img_width: int) -> Optional[Cameras]: + def get_camera(self, img_height: int, img_width: int, client_id: Optional[int] = None) -> Optional[Cameras]: """ Returns the Cameras object representing the current camera for the viewer, or None if the viewer is not connected yet @@ -126,16 +127,23 @@ class ViewerControl: img_height: The height of the image to get camera intrinsics for img_width: The width of the image to get camera intrinsics for """ - assert self.viewer.client is not None + clients = self.viser_server.get_clients() + if len(clients) == 0: + return None + if not client_id: + client_id = list(clients.keys())[0] - from nerfstudio.viewer_beta.viewer import VISER_NERFSTUDIO_SCALE_RATIO + from nerfstudio.viewer.viewer import VISER_NERFSTUDIO_SCALE_RATIO - R = vtf.SO3(wxyz=self.viewer.client.camera.wxyz) + client = clients[client_id] + R = vtf.SO3(wxyz=client.camera.wxyz) R = R @ vtf.SO3.from_x_radians(np.pi) R = torch.tensor(R.as_matrix()) - pos = torch.tensor(self.viewer.client.camera.position, dtype=torch.float64) / VISER_NERFSTUDIO_SCALE_RATIO + pos = torch.tensor(client.camera.position, dtype=torch.float64) / VISER_NERFSTUDIO_SCALE_RATIO c2w = torch.concatenate([R, pos[:, None]], dim=1) - camera_state = CameraState(fov=self.viewer.client.camera.fov, aspect=self.viewer.client.camera.aspect, c2w=c2w) + camera_state = CameraState( + fov=client.camera.fov, aspect=client.camera.aspect, c2w=c2w, camera_type=CameraType.PERSPECTIVE + ) return get_camera(camera_state, img_height, img_width) def register_click_cb(self, cb: Callable): @@ -146,13 +154,35 @@ class ViewerControl: cb: The callback to call when a click is detected. The callback should take a ViewerClick object as an argument """ - self.click_cbs.append(cb) + from nerfstudio.viewer.viewer import VISER_NERFSTUDIO_SCALE_RATIO - def on_click(self, msg): + def wrapped_cb(scene_pointer_msg: ScenePointerEvent): + # only call the callback if the event is a click + if scene_pointer_msg.event != "click": + return + origin = scene_pointer_msg.ray_origin + direction = scene_pointer_msg.ray_direction + + origin = tuple([x / VISER_NERFSTUDIO_SCALE_RATIO for x in origin]) + assert len(origin) == 3 + + click = ViewerClick(origin, direction) + cb(click) + + self._click_cbs[cb] = wrapped_cb + self.viser_server.on_scene_click(wrapped_cb) + + def unregister_click_cb(self, cb: Callable): """ - Internal use only, register a click in the viewer which propagates to all self.click_cbs + Remove a callback which will be called when a click is detected in the viewer. + + Args: + cb: The callback to remove """ - raise NotImplementedError() + if cb not in self._click_cbs: + raise ValueError(f"Callback {cb} not registered, cannot remove") + self.viser_server.remove_scene_click_callback(self._click_cbs[cb]) + self._click_cbs.pop(cb) @property def server(self): @@ -165,17 +195,20 @@ class ViewerElement(Generic[TValue]): Args: name: The name of the element disabled: If the element is disabled + visible: If the element is visible """ def __init__( self, name: str, disabled: bool = False, + visible: bool = True, cb_hook: Callable = lambda element: None, ) -> None: self.name = name self.gui_handle: Optional[Union[GuiInputHandle[TValue], GuiButtonHandle, GuiButtonGroupHandle]] = None self.disabled = disabled + self.visible = visible self.cb_hook = cb_hook @abstractmethod @@ -204,6 +237,11 @@ class ViewerElement(Generic[TValue]): assert self.gui_handle is not None self.gui_handle.disabled = disabled + def set_visible(self, visible: bool) -> None: + """Sets the visible state of the gui element""" + assert self.gui_handle is not None + self.gui_handle.visible = visible + @abstractmethod def install(self, viser_server: ViserServer) -> None: """Installs the gui element into the given viser_server""" @@ -217,15 +255,16 @@ class ViewerButton(ViewerElement[bool]): name: The name of the button cb_hook: The function to call when the button is pressed disabled: If the button is disabled + visible: If the button is visible """ gui_handle: GuiButtonHandle - def __init__(self, name: str, cb_hook: Callable[[ViewerButton], Any], disabled: bool = False): - super().__init__(name, disabled=disabled, cb_hook=cb_hook) + def __init__(self, name: str, cb_hook: Callable[[ViewerButton], Any], disabled: bool = False, visible: bool = True): + super().__init__(name, disabled=disabled, visible=visible, cb_hook=cb_hook) def _create_gui_handle(self, viser_server: ViserServer) -> None: - self.gui_handle = viser_server.add_gui_button(label=self.name, disabled=self.disabled) + self.gui_handle = viser_server.add_gui_button(label=self.name, disabled=self.disabled, visible=self.visible) def install(self, viser_server: ViserServer) -> None: self._create_gui_handle(viser_server) @@ -241,6 +280,7 @@ class ViewerParameter(ViewerElement[TValue], Generic[TValue]): name: The name of the element default_value: The default value of the element disabled: If the element is disabled + visible: If the element is visible cb_hook: Callback to call on update """ @@ -251,9 +291,10 @@ class ViewerParameter(ViewerElement[TValue], Generic[TValue]): name: str, default_value: TValue, disabled: bool = False, + visible: bool = True, cb_hook: Callable = lambda element: None, ) -> None: - super().__init__(name, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, disabled=disabled, visible=visible, cb_hook=cb_hook) self.default_value = default_value def install(self, viser_server: ViserServer) -> None: @@ -300,6 +341,7 @@ class ViewerSlider(ViewerParameter[IntOrFloat], Generic[IntOrFloat]): max_value: The maximum value of the slider step: The step size of the slider disabled: If the slider is disabled + visible: If the slider is visible cb_hook: Callback to call on update hint: The hint text """ @@ -312,11 +354,12 @@ class ViewerSlider(ViewerParameter[IntOrFloat], Generic[IntOrFloat]): max_value: IntOrFloat, step: IntOrFloat = 0.1, disabled: bool = False, + visible: bool = True, cb_hook: Callable[[ViewerSlider], Any] = lambda element: None, hint: Optional[str] = None, ): assert isinstance(default_value, (float, int)) - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.min = min_value self.max = max_value self.step = step @@ -325,7 +368,14 @@ class ViewerSlider(ViewerParameter[IntOrFloat], Generic[IntOrFloat]): def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" self.gui_handle = viser_server.add_gui_slider( - self.name, self.min, self.max, self.step, self.default_value, disabled=self.disabled, hint=self.hint + self.name, + self.min, + self.max, + self.step, + self.default_value, + disabled=self.disabled, + visible=self.visible, + hint=self.hint, ) @@ -336,6 +386,7 @@ class ViewerText(ViewerParameter[str]): name: The name of the text field default_value: The default value of the text field disabled: If the text field is disabled + visible: If the text field is visible cb_hook: Callback to call on update hint: The hint text """ @@ -345,17 +396,18 @@ class ViewerText(ViewerParameter[str]): name: str, default_value: str, disabled: bool = False, + visible: bool = True, cb_hook: Callable[[ViewerText], Any] = lambda element: None, hint: Optional[str] = None, ): assert isinstance(default_value, str) - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" self.gui_handle = viser_server.add_gui_text( - self.name, self.default_value, disabled=self.disabled, hint=self.hint + self.name, self.default_value, disabled=self.disabled, visible=self.visible, hint=self.hint ) @@ -366,6 +418,7 @@ class ViewerNumber(ViewerParameter[IntOrFloat], Generic[IntOrFloat]): name: The name of the number field default_value: The default value of the number field disabled: If the number field is disabled + visible: If the number field is visible cb_hook: Callback to call on update hint: The hint text """ @@ -377,17 +430,18 @@ class ViewerNumber(ViewerParameter[IntOrFloat], Generic[IntOrFloat]): name: str, default_value: IntOrFloat, disabled: bool = False, + visible: bool = True, cb_hook: Callable[[ViewerNumber], Any] = lambda element: None, hint: Optional[str] = None, ): assert isinstance(default_value, (float, int)) - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" self.gui_handle = viser_server.add_gui_number( - self.name, self.default_value, disabled=self.disabled, hint=self.hint + self.name, self.default_value, disabled=self.disabled, visible=self.visible, hint=self.hint ) @@ -398,6 +452,7 @@ class ViewerCheckbox(ViewerParameter[bool]): name: The name of the checkbox default_value: The default value of the checkbox disabled: If the checkbox is disabled + visible: If the checkbox is visible cb_hook: Callback to call on update hint: The hint text """ @@ -407,17 +462,18 @@ class ViewerCheckbox(ViewerParameter[bool]): name: str, default_value: bool, disabled: bool = False, + visible: bool = True, cb_hook: Callable[[ViewerCheckbox], Any] = lambda element: None, hint: Optional[str] = None, ): assert isinstance(default_value, bool) - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" self.gui_handle = viser_server.add_gui_checkbox( - self.name, self.default_value, disabled=self.disabled, hint=self.hint + self.name, self.default_value, disabled=self.disabled, visible=self.visible, hint=self.hint ) @@ -432,6 +488,7 @@ class ViewerDropdown(ViewerParameter[TString], Generic[TString]): default_value: The default value of the dropdown options: The options of the dropdown disabled: If the dropdown is disabled + visible: If the dropdown is visible cb_hook: Callback to call on update hint: The hint text """ @@ -444,18 +501,24 @@ class ViewerDropdown(ViewerParameter[TString], Generic[TString]): default_value: TString, options: List[TString], disabled: bool = False, + visible: bool = True, cb_hook: Callable[[ViewerDropdown], Any] = lambda element: None, hint: Optional[str] = None, ): assert default_value in options - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.options = options self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" self.gui_handle = viser_server.add_gui_dropdown( - self.name, self.options, self.default_value, disabled=self.disabled, hint=self.hint # type: ignore + self.name, + self.options, + self.default_value, + disabled=self.disabled, + visible=self.visible, + hint=self.hint, # type: ignore ) def set_options(self, new_options: List[TString]) -> None: @@ -475,6 +538,7 @@ class ViewerButtonGroup(ViewerParameter[TString], Generic[TString]): Args: name: The name of the button group + visible: If the button group is visible options: The options of the button group cb_hook: Callback to call on update """ @@ -486,14 +550,15 @@ class ViewerButtonGroup(ViewerParameter[TString], Generic[TString]): name: str, default_value: TString, options: List[TString], + visible: bool = True, cb_hook: Callable[[ViewerDropdown], Any] = lambda element: None, ): - super().__init__(name, disabled=False, default_value=default_value, cb_hook=cb_hook) + super().__init__(name, disabled=False, visible=visible, default_value=default_value, cb_hook=cb_hook) self.options = options def _create_gui_handle(self, viser_server: ViserServer) -> None: assert self.gui_handle is None, "gui_handle should be initialized once" - self.gui_handle = viser_server.add_gui_button_group(self.name, self.options) + self.gui_handle = viser_server.add_gui_button_group(self.name, self.options, visible=self.visible) def install(self, viser_server: ViserServer) -> None: self._create_gui_handle(viser_server) @@ -510,6 +575,7 @@ class ViewerRGB(ViewerParameter[Tuple[int, int, int]]): name: The name of the color picker default_value: The default value of the color picker disabled: If the color picker is disabled + visible: If the color picker is visible cb_hook: Callback to call on update hint: The hint text """ @@ -519,16 +585,17 @@ class ViewerRGB(ViewerParameter[Tuple[int, int, int]]): name, default_value: Tuple[int, int, int], disabled=False, + visible=True, cb_hook: Callable[[ViewerRGB], Any] = lambda element: None, hint: Optional[str] = None, ): assert len(default_value) == 3 - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: self.gui_handle = viser_server.add_gui_rgb( - self.name, self.default_value, disabled=self.disabled, hint=self.hint + self.name, self.default_value, disabled=self.disabled, visible=self.visible, hint=self.hint ) @@ -541,6 +608,7 @@ class ViewerVec3(ViewerParameter[Tuple[float, float, float]]): default_value: The default value of the vector step: The step of the vector disabled: If the vector is disabled + visible: If the vector is visible cb_hook: Callback to call on update hint: The hint text """ @@ -551,15 +619,16 @@ class ViewerVec3(ViewerParameter[Tuple[float, float, float]]): default_value: Tuple[float, float, float], step=0.1, disabled=False, + visible=True, cb_hook: Callable[[ViewerVec3], Any] = lambda element: None, hint: Optional[str] = None, ): assert len(default_value) == 3 - super().__init__(name, default_value, disabled=disabled, cb_hook=cb_hook) + super().__init__(name, default_value, disabled=disabled, visible=visible, cb_hook=cb_hook) self.step = step self.hint = hint def _create_gui_handle(self, viser_server: ViserServer) -> None: self.gui_handle = viser_server.add_gui_vector3( - self.name, self.default_value, step=self.step, disabled=self.disabled, hint=self.hint + self.name, self.default_value, step=self.step, disabled=self.disabled, visible=self.visible, hint=self.hint ) diff --git a/nerfstudio/viewer_beta/render_panel.py b/nerfstudio/viewer_beta/render_panel.py deleted file mode 100644 index 7342b0f38..000000000 --- a/nerfstudio/viewer_beta/render_panel.py +++ /dev/null @@ -1,661 +0,0 @@ -# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import annotations -from pathlib import Path -import colorsys -import dataclasses -import threading -import time -from typing import Dict, List, Optional, Tuple -import datetime -from nerfstudio.viewer_beta.control_panel import ControlPanel -import numpy as onp -import splines -import splines.quaternion -import viser -import json -import viser.transforms as tf - - -@dataclasses.dataclass -class Keyframe: - position: onp.ndarray - wxyz: onp.ndarray - override_fov_enabled: bool - override_fov_value: float - aspect: float - - @staticmethod - def from_camera(camera: viser.CameraHandle, aspect: float) -> Keyframe: - return Keyframe( - camera.position, - camera.wxyz, - override_fov_enabled=False, - override_fov_value=camera.fov, - aspect=aspect, - ) - - -class CameraPath: - def __init__(self, server: viser.ViserServer): - self._server = server - self._keyframes: Dict[int, Tuple[Keyframe, viser.CameraFrustumHandle]] = {} - self._keyframe_counter: int = 0 - self._spline: Optional[viser.SceneNodeHandle] = None - self._camera_edit_panel: Optional[viser.Gui3dContainerHandle] = None - - self._orientation_spline: Optional[splines.quaternion.KochanekBartels] = None - self._position_spline: Optional[splines.KochanekBartels] = None - self._fov_spline: Optional[splines.KochanekBartels] = None - self._keyframes_visible: bool = True - - # These parameters should be overridden externally. - self.loop: bool = False - self.smoothness: float = 0.5 # Tension / alpha term. - self.default_fov: float = 0.0 - - def set_keyframes_visible(self, visible: bool) -> None: - self._keyframes_visible = visible - for keyframe in self._keyframes.values(): - keyframe[1].visible = visible - - def add_camera(self, keyframe: Keyframe, keyframe_index: Optional[int] = None) -> None: - """Add a new camera, or replace an old one if `keyframe_index` is passed in.""" - server = self._server - - # Add a keyframe if we aren't replacing an existing one. - if keyframe_index is None: - keyframe_index = self._keyframe_counter - self._keyframe_counter += 1 - - frustum_handle = server.add_camera_frustum( - f"/render_cameras/{keyframe_index}", - fov=keyframe.override_fov_value if keyframe.override_fov_enabled else self.default_fov, - aspect=keyframe.aspect, - scale=0.1, - color=(127, 127, 127), - wxyz=keyframe.wxyz, - position=keyframe.position, - visible=self._keyframes_visible, - ) - - @frustum_handle.on_click - def _(_) -> None: - with server.add_3d_gui_container( - "/camera_edit_panel", - wxyz=keyframe.wxyz, - position=keyframe.position, - ) as camera_edit_panel: - self._camera_edit_panel = camera_edit_panel - override_fov = server.add_gui_checkbox("Override FOV", initial_value=keyframe.override_fov_enabled) - override_fov_degrees = server.add_gui_slider( - "Override FOV (degrees)", - 5.0, - 175.0, - step=0.1, - initial_value=keyframe.override_fov_value * 180.0 / onp.pi, - disabled=not keyframe.override_fov_enabled, - ) - delete_button = server.add_gui_button("Delete", color="red", icon=viser.Icon.TRASH) - go_to_button = server.add_gui_button("Go to") - close_button = server.add_gui_button("Close") - - @override_fov.on_update - def _(_) -> None: - keyframe.override_fov_enabled = override_fov.value - override_fov_degrees.disabled = not override_fov.value - self.add_camera(keyframe, keyframe_index) - - @override_fov_degrees.on_update - def _(_) -> None: - keyframe.override_fov_value = override_fov_degrees.value / 180.0 * onp.pi - self.add_camera(keyframe, keyframe_index) - - @delete_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client is not None - with event.client.add_gui_modal("Confirm") as modal: - event.client.add_gui_markdown("Delete keyframe?") - confirm_button = event.client.add_gui_button("Yes", color="red", icon=viser.Icon.TRASH) - exit_button = event.client.add_gui_button("Cancel") - - @confirm_button.on_click - def _(_) -> None: - assert camera_edit_panel is not None - - keyframe_id = None - for i, keyframe_tuple in self._keyframes.items(): - if keyframe_tuple[1] is frustum_handle: - keyframe_id = i - break - assert keyframe_id is not None - - self._keyframes.pop(keyframe_id) - frustum_handle.remove() - camera_edit_panel.remove() - modal.close() - self.update_spline() - - @exit_button.on_click - def _(_) -> None: - modal.close() - - @go_to_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client is not None - client = event.client - T_world_current = tf.SE3.from_rotation_and_translation( - tf.SO3(client.camera.wxyz), client.camera.position - ) - T_world_target = tf.SE3.from_rotation_and_translation( - tf.SO3(keyframe.wxyz), keyframe.position - ) @ tf.SE3.from_translation(onp.array([0.0, 0.0, -0.5])) - - T_current_target = T_world_current.inverse() @ T_world_target - - for j in range(10): - T_world_set = T_world_current @ tf.SE3.exp(T_current_target.log() * j / 9.0) - - # Important bit: we atomically set both the orientation and the position - # of the camera. - with client.atomic(): - client.camera.wxyz = T_world_set.rotation().wxyz - client.camera.position = T_world_set.translation() - time.sleep(1.0 / 30.0) - - @close_button.on_click - def _(_) -> None: - assert camera_edit_panel is not None - camera_edit_panel.remove() - - self._keyframes[keyframe_index] = (keyframe, frustum_handle) - - def update_aspect(self, aspect: float) -> None: - for keyframe_index, frame in self._keyframes.items(): - frame = dataclasses.replace(frame[0], aspect=aspect) - self.add_camera(frame, keyframe_index=keyframe_index) - - def reset(self) -> None: - for frame in self._keyframes.values(): - frame[1].remove() - self._keyframes.clear() - self.update_spline() - - def interpolate_pose_and_fov(self, normalized_t: float) -> Optional[Tuple[tf.SE3, float]]: - if len(self._keyframes) < 2: - return None - # TODO: this doesn't need to be constantly re-instantiated. - self._fov_spline = splines.KochanekBartels( - [ - keyframe[0].override_fov_value if keyframe[0].override_fov_enabled else self.default_fov - for keyframe in self._keyframes.values() - ], - tcb=(self.smoothness, 0.0, 0.0), - endconditions="closed" if self.loop else "natural", - ) - - assert self._orientation_spline is not None - assert self._position_spline is not None - assert self._fov_spline is not None - max_t = len(self._keyframes) if self.loop else len(self._keyframes) - 1 - t = max_t * normalized_t - quat = self._orientation_spline.evaluate(t) - assert isinstance(quat, splines.quaternion.UnitQuaternion) - return ( - tf.SE3.from_rotation_and_translation( - tf.SO3(onp.array([quat.scalar, *quat.vector])), - self._position_spline.evaluate(t), - ), - float(self._fov_spline.evaluate(t)), - ) - - def update_spline(self) -> None: - keyframes = list(self._keyframes.values()) - if len(keyframes) <= 1: - if self._spline is not None: - self._spline.remove() - self._spline = None - return - - # Update internal splines. - self._orientation_spline = splines.quaternion.KochanekBartels( - [ - splines.quaternion.UnitQuaternion.from_unit_xyzw(onp.roll(keyframe[0].wxyz, shift=-1)) - for keyframe in keyframes - ], - tcb=(self.smoothness, 0.0, 0.0), - endconditions="closed" if self.loop else "natural", - ) - self._position_spline = splines.KochanekBartels( - [keyframe[0].position for keyframe in keyframes], - tcb=(self.smoothness, 0.0, 0.0), - endconditions="closed" if self.loop else "natural", - ) - - # Update visualized spline. - num_keyframes = len(keyframes) + 1 if self.loop else len(keyframes) - points_array = onp.array( - [self._position_spline.evaluate(t) for t in onp.linspace(0, num_keyframes - 1, num_keyframes * 100)] - ) - colors_array = onp.array([colorsys.hls_to_rgb(h, 0.5, 1.0) for h in onp.linspace(0.0, 1.0, len(points_array))]) - self._spline = self._server.add_point_cloud( - "/render_camera_spline", - points=points_array, - colors=colors_array, - point_size=0.035, - ) - - -def populate_render_tab( - server: viser.ViserServer, config_path: Path, datapath: Path, control_panel: ControlPanel -) -> None: - from nerfstudio.viewer_beta.viewer import VISER_NERFSTUDIO_SCALE_RATIO - - fov_degrees = server.add_gui_slider( - "FOV", - initial_value=90.0, - min=0.1, - max=175.0, - step=0.01, - hint="Field-of-view for rendering, which can also be overridden on a per-keyframe basis.", - ) - - @fov_degrees.on_update - def _(_) -> None: - fov_radians = fov_degrees.value / 180.0 * onp.pi - for client in server.get_clients().values(): - client.camera.fov = fov_radians - camera_path.default_fov = fov_radians - - # Updating the aspect ratio will also re-render the camera frustums. - # Could rethink this. - camera_path.update_aspect(resolution.value[0] / resolution.value[1]) - - resolution = server.add_gui_vector2( - "Resolution", - initial_value=(1920, 1080), - min=(50, 50), - max=(10_000, 10_000), - step=1, - hint="Tension parameter for adjusting smoothness of spline interpolation.", - ) - - @resolution.on_update - def _(_) -> None: - """Update the aspect ratio for all cameras when the resolution changes.""" - camera_path.update_aspect(resolution.value[0] / resolution.value[1]) - - add_button = server.add_gui_button( - "Add keyframe", - icon=viser.Icon.PLUS, - hint="Add a new keyframe at the current pose.", - ) - - @add_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client_id is not None - camera = server.get_clients()[event.client_id].camera - - # Add this camera to the path. - camera_path.add_camera( - Keyframe.from_camera(camera, aspect=resolution.value[0] / resolution.value[1]), - ) - camera_path.update_spline() - - reset_up_button = server.add_gui_button( - "Reset up direction", - icon=viser.Icon.ARROW_AUTOFIT_UP, - hint="Reset the orbit up direction.", - ) - - @reset_up_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client is not None - event.client.camera.up_direction = tf.SO3(event.client.camera.wxyz) @ onp.array([0.0, -1.0, 0.0]) - - clear_keyframes_button = server.add_gui_button( - "Clear keyframes", - icon=viser.Icon.TRASH, - hint="Remove all keyframes from the render path.", - ) - - @clear_keyframes_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client_id is not None - client = server.get_clients()[event.client_id] - with client.add_gui_modal("Confirm") as modal: - client.add_gui_markdown("Clear all keyframes?") - confirm_button = client.add_gui_button("Yes", color="red", icon=viser.Icon.TRASH) - exit_button = client.add_gui_button("Cancel") - - @confirm_button.on_click - def _(_) -> None: - camera_path.reset() - modal.close() - - # Clear move handles. - if len(transform_controls) > 0: - for t in transform_controls: - t.remove() - transform_controls.clear() - return - - @exit_button.on_click - def _(_) -> None: - modal.close() - - loop = server.add_gui_checkbox("Loop", False) - - @loop.on_update - def _(_) -> None: - camera_path.loop = loop.value - camera_path.update_spline() - - smoothness = server.add_gui_slider( - "Spline Tension", - min=0.0, - max=1.0, - initial_value=0.0, - step=0.01, - hint="Tension parameter for adjusting smoothness of spline interpolation.", - ) - - @smoothness.on_update - def _(_) -> None: - camera_path.smoothness = smoothness.value - camera_path.update_spline() - - move_checkbox = server.add_gui_checkbox( - "Move keyframes", - initial_value=False, - hint="Toggle move handles for keyframes in the scene.", - ) - - @move_checkbox.on_update - def _(event: viser.GuiEvent) -> None: - # Clear move handles when toggled off. - if move_checkbox.value is False: - for t in transform_controls: - t.remove() - transform_controls.clear() - return - - def _make_transform_controls_callback( - keyframe: Tuple[Keyframe, viser.SceneNodeHandle], - controls: viser.TransformControlsHandle, - ) -> None: - @controls.on_update - def _(_) -> None: - keyframe[0].wxyz = controls.wxyz - keyframe[0].position = controls.position - - keyframe[1].wxyz = controls.wxyz - keyframe[1].position = controls.position - - camera_path.update_spline() - - # Show move handles. - assert event.client is not None - for keyframe_index, keyframe in camera_path._keyframes.items(): - controls = event.client.add_transform_controls( - f"/keyframe_move/{keyframe_index}", - scale=0.4, - wxyz=keyframe[0].wxyz, - position=keyframe[0].position, - ) - transform_controls.append(controls) - _make_transform_controls_callback(keyframe, controls) - - playback_folder = server.add_gui_folder("Playback") - with playback_folder: - duration_number = server.add_gui_number("Duration (sec)", min=0.0, max=1e8, step=0.0001, initial_value=4.0) - framerate_slider = server.add_gui_slider("FPS", min=1.0, max=240.0, step=1e-8, initial_value=30.0) - framerate_buttons = server.add_gui_button_group("", ("24", "30", "60")) - - @framerate_buttons.on_click - def _(_) -> None: - framerate_slider.value = float(framerate_buttons.value) - - play_button = server.add_gui_button("Play", icon=viser.Icon.PLAYER_PLAY) - pause_button = server.add_gui_button("Pause", icon=viser.Icon.PLAYER_PAUSE, visible=False) - attach_viewport_checkbox = server.add_gui_checkbox("Attach viewport", initial_value=False) - show_checkbox = server.add_gui_checkbox( - "Show keyframes", - initial_value=True, - hint="Show keyframes in the scene.", - ) - - @show_checkbox.on_update - def _(_: viser.GuiEvent) -> None: - camera_path.set_keyframes_visible(show_checkbox.value) - - def add_preview_frame_slider() -> Optional[viser.GuiInputHandle[int]]: - """Helper for creating the current frame # slider. This is removed and - re-added anytime the `max` value changes.""" - max_frame_index = int(framerate_slider.value * duration_number.value) - 1 - - if max_frame_index <= 0: - return None - with playback_folder: - preview_frame_slider = server.add_gui_slider( - "Preview frame", - min=0, - max=max_frame_index, - step=1, - initial_value=0, - # Place right after the pause button. - order=pause_button.order + 0.01, - ) - - @preview_frame_slider.on_update - def _(_) -> None: - max_frame_index = int(framerate_slider.value * duration_number.value) - 1 - maybe_pose_and_fov = camera_path.interpolate_pose_and_fov( - preview_frame_slider.value / max_frame_index if max_frame_index > 0 else 0 - ) - if maybe_pose_and_fov is None: - return - pose, fov = maybe_pose_and_fov - server.add_camera_frustum( - "/preview_camera", - fov=fov, - aspect=resolution.value[0] / resolution.value[1], - scale=0.35, - wxyz=pose.rotation().wxyz, - position=pose.translation(), - color=(10, 200, 30), - # Hack: hide green frustum if the viewport is attached. - # This is a waste of bandwidth, but will ensure that any old - # frustums are removed/aren't rendered. - # - # Easy to fix with a global variable. - visible=not attach_viewport_checkbox.value, - ) - if attach_viewport_checkbox.value: - for client in server.get_clients().values(): - client.camera.wxyz = pose.rotation().wxyz - client.camera.position = pose.translation() - client.camera.fov = fov - - return preview_frame_slider - - @attach_viewport_checkbox.on_update - def _(_) -> None: - if not attach_viewport_checkbox.value: - for client in server.get_clients().values(): - client.camera.fov = fov_degrees.value - - preview_frame_slider = add_preview_frame_slider() - - @duration_number.on_update - @framerate_slider.on_update - def _(_) -> None: - nonlocal preview_frame_slider - old = preview_frame_slider - assert old is not None - - preview_frame_slider = add_preview_frame_slider() - if preview_frame_slider is not None: - old.remove() - else: - preview_frame_slider = old - - # Play the camera trajectory when the play button is pressed. - @play_button.on_click - def _(_) -> None: - play_button.visible = False - pause_button.visible = True - - def play() -> None: - while not play_button.visible: - max_frame = int(framerate_slider.value * duration_number.value) - if max_frame > 0: - assert preview_frame_slider is not None - preview_frame_slider.value = (preview_frame_slider.value + 1) % max_frame - time.sleep(1.0 / framerate_slider.value) - - threading.Thread(target=play).start() - - # Play the camera trajectory when the play button is pressed. - @pause_button.on_click - def _(_) -> None: - play_button.visible = True - pause_button.visible = False - - # set the initial value to the current date-time string - now = datetime.datetime.now() - render_name_text = server.add_gui_text( - "Render Name", initial_value=now.strftime("%Y-%m-%d-%H-%M-%S"), hint="Name of the render" - ) - render_button = server.add_gui_button( - "Generate Command", - color="green", - icon=viser.Icon.FILE_EXPORT, - hint="Generate the ns-render command for rendering the camera path.", - ) - - @render_button.on_click - def _(event: viser.GuiEvent) -> None: - assert event.client is not None - num_frames = int(framerate_slider.value * duration_number.value) - json_data = {} - # json data has the properties: - # keyframes: list of keyframes with - # matrix : flattened 4x4 matrix - # fov: float in degrees - # aspect: float - # camera_type: string of camera type - # render_height: int - # render_width: int - # fps: int - # seconds: float - # is_cycle: bool - # smoothness_value: float - # camera_path: list of frames with properties - # camera_to_world: flattened 4x4 matrix - # fov: float in degrees - # aspect: float - # first populate the keyframes: - keyframes = [] - for keyframe, dummy in camera_path._keyframes.values(): - pose = tf.SE3.from_rotation_and_translation( - tf.SO3(keyframe.wxyz) @ tf.SO3.from_x_radians(onp.pi), - keyframe.position / VISER_NERFSTUDIO_SCALE_RATIO, - ) - keyframes.append( - { - "matrix": pose.as_matrix().flatten().tolist(), - "fov": onp.rad2deg(keyframe.override_fov_value) - if keyframe.override_fov_enabled - else fov_degrees.value, - "aspect": keyframe.aspect, - } - ) - json_data["keyframes"] = keyframes - json_data["camera_type"] = "perspective" - json_data["render_height"] = resolution.value[1] - json_data["render_width"] = resolution.value[0] - json_data["fps"] = framerate_slider.value - json_data["seconds"] = duration_number.value - json_data["is_cycle"] = loop.value - json_data["smoothness_value"] = smoothness.value - # now populate the camera path: - camera_path_list = [] - for i in range(num_frames): - maybe_pose_and_fov = camera_path.interpolate_pose_and_fov(i / num_frames) - if maybe_pose_and_fov is None: - return - pose, fov = maybe_pose_and_fov - # rotate the axis of the camera 180 about x axis - pose = tf.SE3.from_rotation_and_translation( - pose.rotation() @ tf.SO3.from_x_radians(onp.pi), - pose.translation() / VISER_NERFSTUDIO_SCALE_RATIO, - ) - camera_path_list.append( - { - "camera_to_world": pose.as_matrix().flatten().tolist(), - "fov": onp.rad2deg(fov), - "aspect": resolution.value[0] / resolution.value[1], - } - ) - json_data["camera_path"] = camera_path_list - # finally add crop data if crop is enabled - if control_panel.crop_viewport: - obb = control_panel.crop_obb - rpy = tf.SO3.from_matrix(obb.R.numpy()).as_rpy_radians() - color = control_panel.background_color - json_data["crop"] = { - "crop_center": obb.T.tolist(), - "crop_scale": obb.S.tolist(), - "crop_rot": [rpy.roll, rpy.pitch, rpy.yaw], - "crop_bg_color": {"r": color[0], "g": color[1], "b": color[2]}, - } - - # now write the json file - json_outfile = datapath / "camera_paths" / f"{render_name_text.value}.json" - with open(json_outfile.absolute(), "w") as outfile: - json.dump(json_data, outfile) - # now show the command - with event.client.add_gui_modal("Render Command") as modal: - dataname = datapath.name - command = " ".join( - [ - "ns-render camera-path", - f"--load-config {config_path}", - f"--camera-path-filename {json_outfile.absolute()}", - f"--output-path renders/{dataname}/{render_name_text.value}.mp4", - ] - ) - event.client.add_gui_markdown( - "\n".join( - [ - "To render the trajectory, run the following from the command line:", - "", - "```", - command, - "```", - ] - ) - ) - close_button = event.client.add_gui_button("Close") - - @close_button.on_click - def _(_) -> None: - modal.close() - - camera_path = CameraPath(server) - camera_path.default_fov = fov_degrees.value / 180.0 * onp.pi - - transform_controls: List[viser.SceneNodeHandle] = [] diff --git a/nerfstudio/viewer/server/__init__.py b/nerfstudio/viewer_legacy/__init__.py similarity index 100% rename from nerfstudio/viewer/server/__init__.py rename to nerfstudio/viewer_legacy/__init__.py diff --git a/nerfstudio/viewer/app/.env.development b/nerfstudio/viewer_legacy/app/.env.development similarity index 67% rename from nerfstudio/viewer/app/.env.development rename to nerfstudio/viewer_legacy/app/.env.development index 106b7ee75..d67e92938 100644 --- a/nerfstudio/viewer/app/.env.development +++ b/nerfstudio/viewer_legacy/app/.env.development @@ -2,4 +2,4 @@ BROWSER=none FAST_REFRESH=false HOST=0.0.0.0 PORT=4000 -ESLINT_NO_DEV_ERRORS=true \ No newline at end of file +ESLINT_NO_DEV_ERRORS=true diff --git a/nerfstudio/viewer/app/.eslintrc.json b/nerfstudio/viewer_legacy/app/.eslintrc.json similarity index 100% rename from nerfstudio/viewer/app/.eslintrc.json rename to nerfstudio/viewer_legacy/app/.eslintrc.json diff --git a/nerfstudio/viewer/app/.gitignore b/nerfstudio/viewer_legacy/app/.gitignore similarity index 96% rename from nerfstudio/viewer/app/.gitignore rename to nerfstudio/viewer_legacy/app/.gitignore index 2deb81763..3ae88f84d 100644 --- a/nerfstudio/viewer/app/.gitignore +++ b/nerfstudio/viewer_legacy/app/.gitignore @@ -11,4 +11,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* /.changelog -.npm/ \ No newline at end of file +.npm/ diff --git a/nerfstudio/viewer/app/package.json b/nerfstudio/viewer_legacy/app/package.json similarity index 99% rename from nerfstudio/viewer/app/package.json rename to nerfstudio/viewer_legacy/app/package.json index 136739355..7dfcb3b45 100644 --- a/nerfstudio/viewer/app/package.json +++ b/nerfstudio/viewer_legacy/app/package.json @@ -93,4 +93,4 @@ "resolve-url-loader": "^5.0.0", "yaml": "^2.2.2" } -} \ No newline at end of file +} diff --git a/nerfstudio/viewer/app/public/electron.js b/nerfstudio/viewer_legacy/app/public/electron.js similarity index 100% rename from nerfstudio/viewer/app/public/electron.js rename to nerfstudio/viewer_legacy/app/public/electron.js diff --git a/nerfstudio/viewer/app/public/favicon.png b/nerfstudio/viewer_legacy/app/public/favicon.png similarity index 100% rename from nerfstudio/viewer/app/public/favicon.png rename to nerfstudio/viewer_legacy/app/public/favicon.png diff --git a/nerfstudio/viewer/app/public/index.html b/nerfstudio/viewer_legacy/app/public/index.html similarity index 100% rename from nerfstudio/viewer/app/public/index.html rename to nerfstudio/viewer_legacy/app/public/index.html diff --git a/nerfstudio/viewer/app/public/manifest.json b/nerfstudio/viewer_legacy/app/public/manifest.json similarity index 100% rename from nerfstudio/viewer/app/public/manifest.json rename to nerfstudio/viewer_legacy/app/public/manifest.json diff --git a/nerfstudio/viewer/app/public/robots.txt b/nerfstudio/viewer_legacy/app/public/robots.txt similarity index 100% rename from nerfstudio/viewer/app/public/robots.txt rename to nerfstudio/viewer_legacy/app/public/robots.txt diff --git a/nerfstudio/viewer/app/requirements.txt b/nerfstudio/viewer_legacy/app/requirements.txt similarity index 100% rename from nerfstudio/viewer/app/requirements.txt rename to nerfstudio/viewer_legacy/app/requirements.txt diff --git a/nerfstudio/viewer/app/run_deploy.py b/nerfstudio/viewer_legacy/app/run_deploy.py similarity index 100% rename from nerfstudio/viewer/app/run_deploy.py rename to nerfstudio/viewer_legacy/app/run_deploy.py diff --git a/nerfstudio/viewer/app/src/App.jsx b/nerfstudio/viewer_legacy/app/src/App.jsx similarity index 100% rename from nerfstudio/viewer/app/src/App.jsx rename to nerfstudio/viewer_legacy/app/src/App.jsx diff --git a/nerfstudio/viewer/app/src/SceneNode.js b/nerfstudio/viewer_legacy/app/src/SceneNode.js similarity index 100% rename from nerfstudio/viewer/app/src/SceneNode.js rename to nerfstudio/viewer_legacy/app/src/SceneNode.js diff --git a/nerfstudio/viewer/app/src/index.jsx b/nerfstudio/viewer_legacy/app/src/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/index.jsx rename to nerfstudio/viewer_legacy/app/src/index.jsx diff --git a/nerfstudio/viewer/app/src/index.scss b/nerfstudio/viewer_legacy/app/src/index.scss similarity index 100% rename from nerfstudio/viewer/app/src/index.scss rename to nerfstudio/viewer_legacy/app/src/index.scss diff --git a/nerfstudio/viewer/app/src/modules/Banner/Banner.jsx b/nerfstudio/viewer_legacy/app/src/modules/Banner/Banner.jsx similarity index 96% rename from nerfstudio/viewer/app/src/modules/Banner/Banner.jsx rename to nerfstudio/viewer_legacy/app/src/modules/Banner/Banner.jsx index baedf714d..e2156606a 100644 --- a/nerfstudio/viewer/app/src/modules/Banner/Banner.jsx +++ b/nerfstudio/viewer_legacy/app/src/modules/Banner/Banner.jsx @@ -62,7 +62,7 @@ export default function Banner() {
The favicon.
diff --git a/nerfstudio/viewer/app/src/modules/Banner/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/Banner/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/Banner/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/Banner/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/ConfigPanel/ConfigPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/ConfigPanel/ConfigPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/ConfigPanel/ConfigPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/ConfigPanel/ConfigPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/LandingModal/LandingModal.jsx b/nerfstudio/viewer_legacy/app/src/modules/LandingModal/LandingModal.jsx similarity index 98% rename from nerfstudio/viewer/app/src/modules/LandingModal/LandingModal.jsx rename to nerfstudio/viewer_legacy/app/src/modules/LandingModal/LandingModal.jsx index faf2db4c2..42caec03a 100644 --- a/nerfstudio/viewer/app/src/modules/LandingModal/LandingModal.jsx +++ b/nerfstudio/viewer_legacy/app/src/modules/LandingModal/LandingModal.jsx @@ -100,7 +100,7 @@ export default function LandingModel(props: LandingModalProps) {
The favicon.
diff --git a/nerfstudio/viewer/app/src/modules/LandingModal/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/LandingModal/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/LandingModal/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/LandingModal/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/LoadPathModal/LoadPathModal.jsx b/nerfstudio/viewer_legacy/app/src/modules/LoadPathModal/LoadPathModal.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/LoadPathModal/LoadPathModal.jsx rename to nerfstudio/viewer_legacy/app/src/modules/LoadPathModal/LoadPathModal.jsx diff --git a/nerfstudio/viewer/app/src/modules/LoadPathModal/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/LoadPathModal/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/LoadPathModal/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/LoadPathModal/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/RenderModal/RenderModal.jsx b/nerfstudio/viewer_legacy/app/src/modules/RenderModal/RenderModal.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/RenderModal/RenderModal.jsx rename to nerfstudio/viewer_legacy/app/src/modules/RenderModal/RenderModal.jsx diff --git a/nerfstudio/viewer/app/src/modules/RenderModal/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/RenderModal/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/RenderModal/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/RenderModal/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/Scene/Scene.jsx b/nerfstudio/viewer_legacy/app/src/modules/Scene/Scene.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/Scene/Scene.jsx rename to nerfstudio/viewer_legacy/app/src/modules/Scene/Scene.jsx diff --git a/nerfstudio/viewer/app/src/modules/Scene/drawing.js b/nerfstudio/viewer_legacy/app/src/modules/Scene/drawing.js similarity index 99% rename from nerfstudio/viewer/app/src/modules/Scene/drawing.js rename to nerfstudio/viewer_legacy/app/src/modules/Scene/drawing.js index e64f3214b..9de449a5e 100644 --- a/nerfstudio/viewer/app/src/modules/Scene/drawing.js +++ b/nerfstudio/viewer_legacy/app/src/modules/Scene/drawing.js @@ -162,4 +162,4 @@ export function drawCameras(cameras): Record { cameraObjects[key] = drawCamera(camera); } return cameraObjects; -} \ No newline at end of file +} diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraHelper.js b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraHelper.js similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraHelper.js rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraHelper.js diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraPropPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraPropPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/CameraPropPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/CameraPropPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/curve.js b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/curve.js similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/curve.js rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/curve.js diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/CameraPanel/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/CameraPanel/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/ExportPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/ExportPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/ExportPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/ExportPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/MeshSubPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/MeshSubPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/MeshSubPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/MeshSubPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/PointcloudSubPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/PointcloudSubPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/PointcloudSubPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/PointcloudSubPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ExportPanel/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ExportPanel/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ScenePanel/ScenePanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ScenePanel/ScenePanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ScenePanel/ScenePanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ScenePanel/ScenePanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/ScenePanel/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/ScenePanel/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/ScenePanel/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/ScenePanel/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/SidePanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/SidePanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/SidePanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/SidePanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/StatusPanel/StatusPanel.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/StatusPanel/StatusPanel.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/StatusPanel/StatusPanel.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/StatusPanel/StatusPanel.jsx diff --git a/nerfstudio/viewer/app/src/modules/SidePanel/StatusPanel/index.jsx b/nerfstudio/viewer_legacy/app/src/modules/SidePanel/StatusPanel/index.jsx similarity index 100% rename from nerfstudio/viewer/app/src/modules/SidePanel/StatusPanel/index.jsx rename to nerfstudio/viewer_legacy/app/src/modules/SidePanel/StatusPanel/index.jsx diff --git a/nerfstudio/viewer/app/src/modules/ViewerWindow/ViewerWindow.jsx b/nerfstudio/viewer_legacy/app/src/modules/ViewerWindow/ViewerWindow.jsx similarity index 98% rename from nerfstudio/viewer/app/src/modules/ViewerWindow/ViewerWindow.jsx rename to nerfstudio/viewer_legacy/app/src/modules/ViewerWindow/ViewerWindow.jsx index a8a596c33..e6daf60ad 100644 --- a/nerfstudio/viewer/app/src/modules/ViewerWindow/ViewerWindow.jsx +++ b/nerfstudio/viewer_legacy/app/src/modules/ViewerWindow/ViewerWindow.jsx @@ -333,7 +333,7 @@ export default function ViewerWindow(props) { mouseVector.y > 1 || mouseVector.y < -1 ); - if (!mouse_in_scene) { return; } + if (!mouse_in_scene) { return; } const raycaster = new THREE.Raycaster(); raycaster.setFromCamera(mouseVector, sceneTree.metadata.camera); @@ -341,13 +341,13 @@ export default function ViewerWindow(props) { throttledClickSender({ type: 'ClickMessage', origin: [ - raycaster.ray.origin.x, - raycaster.ray.origin.y, + raycaster.ray.origin.x, + raycaster.ray.origin.y, raycaster.ray.origin.z ], direction: [ - raycaster.ray.direction.x, - raycaster.ray.direction.y, + raycaster.ray.direction.x, + raycaster.ray.direction.y, raycaster.ray.direction.z ], }); diff --git a/nerfstudio/viewer/app/src/modules/ViewerWindow/ViewerWindowSlice.js b/nerfstudio/viewer_legacy/app/src/modules/ViewerWindow/ViewerWindowSlice.js similarity index 100% rename from nerfstudio/viewer/app/src/modules/ViewerWindow/ViewerWindowSlice.js rename to nerfstudio/viewer_legacy/app/src/modules/ViewerWindow/ViewerWindowSlice.js diff --git a/nerfstudio/viewer/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx b/nerfstudio/viewer_legacy/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx similarity index 94% rename from nerfstudio/viewer/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx rename to nerfstudio/viewer_legacy/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx index 192bc262b..953c4d435 100644 --- a/nerfstudio/viewer/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx +++ b/nerfstudio/viewer_legacy/app/src/modules/ViewportControlsModal/ViewportControlsModal.jsx @@ -30,7 +30,7 @@ export default function ControlsModal() {
The favicon. None: + def add_element(self, e: ViewerElement, additional_tags: Tuple[str, ...] = tuple()) -> None: """Adds an element to the control panel Args: diff --git a/nerfstudio/viewer/server/gui_utils.py b/nerfstudio/viewer_legacy/server/gui_utils.py similarity index 100% rename from nerfstudio/viewer/server/gui_utils.py rename to nerfstudio/viewer_legacy/server/gui_utils.py diff --git a/nerfstudio/viewer/server/path.py b/nerfstudio/viewer_legacy/server/path.py similarity index 100% rename from nerfstudio/viewer/server/path.py rename to nerfstudio/viewer_legacy/server/path.py diff --git a/nerfstudio/viewer/server/render_state_machine.py b/nerfstudio/viewer_legacy/server/render_state_machine.py similarity index 87% rename from nerfstudio/viewer/server/render_state_machine.py rename to nerfstudio/viewer_legacy/server/render_state_machine.py index 390390d48..d842598d1 100644 --- a/nerfstudio/viewer/server/render_state_machine.py +++ b/nerfstudio/viewer_legacy/server/render_state_machine.py @@ -24,13 +24,14 @@ import torch from nerfstudio.cameras.cameras import Cameras from nerfstudio.model_components.renderers import background_color_override_context +from nerfstudio.models.gaussian_splatting import GaussianSplattingModel from nerfstudio.utils import colormaps, writer from nerfstudio.utils.writer import GLOBAL_BUFFER, EventName, TimeWriter -from nerfstudio.viewer.server import viewer_utils -from nerfstudio.viewer.viser.messages import CameraMessage +from nerfstudio.viewer_legacy.server import viewer_utils +from nerfstudio.viewer_legacy.viser.messages import CameraMessage if TYPE_CHECKING: - from nerfstudio.viewer.server.viewer_state import ViewerState + from nerfstudio.viewer_legacy.server.viewer_state import ViewerLegacyState RenderStates = Literal["low_move", "low_static", "high"] RenderActions = Literal["rerender", "move", "static", "step"] @@ -54,7 +55,7 @@ class RenderStateMachine(threading.Thread): viewer: the viewer state """ - def __init__(self, viewer: ViewerState): + def __init__(self, viewer: ViewerLegacyState): threading.Thread.__init__(self) self.transitions: Dict[RenderStates, Dict[RenderActions, RenderStates]] = { s: {} for s in get_args(RenderStates) @@ -127,7 +128,14 @@ class RenderStateMachine(threading.Thread): assert camera is not None, "render called before viewer connected" with self.viewer.train_lock if self.viewer.train_lock is not None else contextlib.nullcontext(): - camera_ray_bundle = camera.generate_rays(camera_indices=0, aabb_box=self.viewer.get_model().render_aabb) + # TODO jake-austin: Make this check whether the model inherits from a camera based model or a ray based model + # TODO Zhuoyang: First made some dummy judgements, need to be fixed later + isGaussianSplattingModel = isinstance(self.viewer.get_model(), GaussianSplattingModel) + if isGaussianSplattingModel: + # TODO fix me before ship + camera_ray_bundle = camera.generate_rays(camera_indices=0, aabb_box=self.viewer.get_model().render_aabb) + else: + camera_ray_bundle = camera.generate_rays(camera_indices=0, aabb_box=self.viewer.get_model().render_aabb) with TimeWriter(None, None, write=False) as vis_t: self.viewer.get_model().eval() @@ -142,12 +150,21 @@ class RenderStateMachine(threading.Thread): device=self.viewer.get_model().device, ) with background_color_override_context(background_color), torch.no_grad(): - outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) + if isGaussianSplattingModel: + outputs = self.viewer.get_model().get_outputs_for_camera(camera) + else: + outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) else: with torch.no_grad(): - outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) + if isGaussianSplattingModel: + outputs = self.viewer.get_model().get_outputs_for_camera(camera) + else: + outputs = self.viewer.get_model().get_outputs_for_camera_ray_bundle(camera_ray_bundle) self.viewer.get_model().train() - num_rays = len(camera_ray_bundle) + if True: + num_rays = (camera.height * camera.width).item() + else: + num_rays = len(camera_ray_bundle) render_time = vis_t.duration if writer.is_initialized(): writer.put_time( diff --git a/nerfstudio/viewer/server/state/node.py b/nerfstudio/viewer_legacy/server/state/node.py similarity index 100% rename from nerfstudio/viewer/server/state/node.py rename to nerfstudio/viewer_legacy/server/state/node.py diff --git a/nerfstudio/viewer/server/state/state_node.py b/nerfstudio/viewer_legacy/server/state/state_node.py similarity index 93% rename from nerfstudio/viewer/server/state/state_node.py rename to nerfstudio/viewer_legacy/server/state/state_node.py index 3aebfeff2..f7330a3ca 100644 --- a/nerfstudio/viewer/server/state/state_node.py +++ b/nerfstudio/viewer_legacy/server/state/state_node.py @@ -13,7 +13,7 @@ # limitations under the License. -from nerfstudio.viewer.server.state.node import Node +from nerfstudio.viewer_legacy.server.state.node import Node class StateNode(Node): diff --git a/nerfstudio/viewer/server/utils.py b/nerfstudio/viewer_legacy/server/utils.py similarity index 98% rename from nerfstudio/viewer/server/utils.py rename to nerfstudio/viewer_legacy/server/utils.py index 0c9d894ab..0b26741db 100644 --- a/nerfstudio/viewer/server/utils.py +++ b/nerfstudio/viewer_legacy/server/utils.py @@ -21,7 +21,7 @@ import numpy as np import torch from typing_extensions import assert_never -from nerfstudio.viewer.viser.messages import CameraMessage +from nerfstudio.viewer_legacy.viser.messages import CameraMessage def get_chunks( diff --git a/nerfstudio/viewer/server/viewer_elements.py b/nerfstudio/viewer_legacy/server/viewer_elements.py similarity index 95% rename from nerfstudio/viewer/server/viewer_elements.py rename to nerfstudio/viewer_legacy/server/viewer_elements.py index 802e0d2eb..b8065f886 100644 --- a/nerfstudio/viewer/server/viewer_elements.py +++ b/nerfstudio/viewer_legacy/server/viewer_elements.py @@ -25,12 +25,12 @@ from typing import TYPE_CHECKING, Any, Callable, Generic, List, Optional, Tuple from typing_extensions import TypeVar from nerfstudio.cameras.cameras import Cameras -from nerfstudio.viewer.viser import GuiHandle, GuiSelectHandle, ViserServer -from nerfstudio.viewer.viser.messages import ClickMessage +from nerfstudio.viewer_legacy.viser import GuiHandle, GuiSelectHandle, ViserServer +from nerfstudio.viewer_legacy.viser.messages import ClickMessage if TYPE_CHECKING: - from nerfstudio.viewer.server.control_panel import ControlPanel - from nerfstudio.viewer.server.viewer_state import ViewerState + from nerfstudio.viewer_legacy.server.control_panel import ControlPanel + from nerfstudio.viewer_legacy.server.viewer_state import ViewerLegacyState TValue = TypeVar("TValue") @@ -61,8 +61,14 @@ class ViewerControl: def __init__(self): # this should be a user-facing constructor, since it will be used inside the model/pipeline class self.click_cbs = [] + from nerfstudio.utils.rich_utils import CONSOLE - def _setup(self, viewer_state: ViewerState): + CONSOLE.print( + "WARNING: viewer_legacy.server.viewer_elements is deprecated and will be removed in the future. Use the new nerfstudio.viewer instead", + style="bold yellow", + ) + + def _setup(self, viewer_state: ViewerLegacyState): """ Internal use only, setup the viewer control with the viewer state object @@ -162,6 +168,12 @@ class ViewerElement(Generic[TValue]): self.gui_handle: Optional[GuiHandle[TValue]] = None self.disabled = disabled self.cb_hook = cb_hook + from nerfstudio.utils.rich_utils import CONSOLE + + CONSOLE.print( + "WARNING: viewer_legacy.server.viewer_elements is deprecated and will be removed in the future. Use the new nerfstudio.viewer instead", + style="bold yellow", + ) @abstractmethod def _create_gui_handle(self, viser_server: ViserServer) -> None: diff --git a/nerfstudio/viewer/server/viewer_state.py b/nerfstudio/viewer_legacy/server/viewer_state.py similarity index 95% rename from nerfstudio/viewer/server/viewer_state.py rename to nerfstudio/viewer_legacy/server/viewer_state.py index ad80822ce..ee02d2a4b 100644 --- a/nerfstudio/viewer/server/viewer_state.py +++ b/nerfstudio/viewer_legacy/server/viewer_state.py @@ -35,14 +35,14 @@ from nerfstudio.utils.decorators import check_main_thread, decorate_all from nerfstudio.utils.io import load_from_json, write_to_json from nerfstudio.utils.rich_utils import CONSOLE from nerfstudio.utils.writer import GLOBAL_BUFFER, EventName -from nerfstudio.viewer.server import viewer_utils -from nerfstudio.viewer.server.control_panel import ControlPanel -from nerfstudio.viewer.server.gui_utils import parse_object -from nerfstudio.viewer.server.render_state_machine import RenderAction, RenderStateMachine -from nerfstudio.viewer.server.utils import get_intrinsics_matrix_and_camera_to_world_h -from nerfstudio.viewer.server.viewer_elements import ViewerControl, ViewerElement -from nerfstudio.viewer.viser import ViserServer -from nerfstudio.viewer.viser.messages import ( +from nerfstudio.viewer_legacy.server import viewer_utils +from nerfstudio.viewer_legacy.server.control_panel import ControlPanel +from nerfstudio.viewer_legacy.server.gui_utils import parse_object +from nerfstudio.viewer_legacy.server.render_state_machine import RenderAction, RenderStateMachine +from nerfstudio.viewer_legacy.server.utils import get_intrinsics_matrix_and_camera_to_world_h +from nerfstudio.viewer_legacy.server.viewer_elements import ViewerControl, ViewerElement +from nerfstudio.viewer_legacy.viser import ViserServer +from nerfstudio.viewer_legacy.viser.messages import ( CameraMessage, CameraPathOptionsRequest, CameraPathPayloadMessage, @@ -59,7 +59,7 @@ if TYPE_CHECKING: @decorate_all([check_main_thread]) -class ViewerState: +class ViewerLegacyState: """Class to hold state for viewer variables Args: @@ -198,8 +198,8 @@ class ViewerState: self.viser_server.send_crop_params( crop_enabled=self.control_panel.crop_viewport, crop_bg_color=self.control_panel.background_color, - crop_scale=tuple(crop_scale.tolist()), - crop_center=tuple(crop_center.tolist()), + crop_scale=tuple(crop_scale.tolist()), # type: ignore + crop_center=tuple(crop_center.tolist()), # type: ignore ) if self.camera_message is not None: self.render_statemachine.action(RenderAction("rerender", self.camera_message)) @@ -258,8 +258,8 @@ class ViewerState: scale = np.array(message.crop_scale) crop_min = center - scale / 2.0 crop_max = center + scale / 2.0 - self.control_panel.crop_min = tuple(crop_min.tolist()) - self.control_panel.crop_max = tuple(crop_max.tolist()) + self.control_panel.crop_min = tuple(crop_min.tolist()) # type: ignore + self.control_panel.crop_max = tuple(crop_max.tolist()) # type: ignore def _handle_click_message(self, message: NerfstudioMessage) -> None: """Handle click message from viewer.""" diff --git a/nerfstudio/viewer/server/viewer_utils.py b/nerfstudio/viewer_legacy/server/viewer_utils.py similarity index 100% rename from nerfstudio/viewer/server/viewer_utils.py rename to nerfstudio/viewer_legacy/server/viewer_utils.py diff --git a/nerfstudio/viewer/viser/__init__.py b/nerfstudio/viewer_legacy/viser/__init__.py similarity index 100% rename from nerfstudio/viewer/viser/__init__.py rename to nerfstudio/viewer_legacy/viser/__init__.py diff --git a/nerfstudio/viewer/viser/gui.py b/nerfstudio/viewer_legacy/viser/gui.py similarity index 100% rename from nerfstudio/viewer/viser/gui.py rename to nerfstudio/viewer_legacy/viser/gui.py diff --git a/nerfstudio/viewer/viser/message_api.py b/nerfstudio/viewer_legacy/viser/message_api.py similarity index 99% rename from nerfstudio/viewer/viser/message_api.py rename to nerfstudio/viewer_legacy/viser/message_api.py index 3c8ce752c..bb6449cf8 100644 --- a/nerfstudio/viewer/viser/message_api.py +++ b/nerfstudio/viewer_legacy/viser/message_api.py @@ -501,8 +501,8 @@ class MessageApi(abc.ABC): """ self._queue( messages.SceneBoxMessage( - min=tuple(scene_box.aabb[0].tolist()), - max=tuple(scene_box.aabb[1].tolist()), + min=tuple(scene_box.aabb[0].tolist()), # type: ignore + max=tuple(scene_box.aabb[1].tolist()), # type: ignore ) ) diff --git a/nerfstudio/viewer/viser/messages.py b/nerfstudio/viewer_legacy/viser/messages.py similarity index 100% rename from nerfstudio/viewer/viser/messages.py rename to nerfstudio/viewer_legacy/viser/messages.py diff --git a/nerfstudio/viewer/viser/server.py b/nerfstudio/viewer_legacy/viser/server.py similarity index 100% rename from nerfstudio/viewer/viser/server.py rename to nerfstudio/viewer_legacy/viser/server.py diff --git a/pyproject.toml b/pyproject.toml index c041858ba..b00deccff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ "av>=9.2.0", "comet_ml>=3.33.8", "cryptography>=38", - "tyro>=0.5.3", + "tyro>=0.5.10", "gdown>=4.6.0", "ninja>=1.10", "h5py>=2.9.0", @@ -39,11 +39,14 @@ dependencies = [ "protobuf<=3.20.3,!=3.20.0", # TODO(1480) enable when pycolmap windows wheels are available # "pycolmap==0.3.0", - "pymeshlab>=2022.2.post2", + "pymeshlab>=2022.2.post2; platform_machine != 'arm64'", "pyngrok>=5.1.0", "python-socketio>=5.7.1", "pyquaternion>=0.9.9", - "rawpy>=0.18.1", + # TODO we can switch back to (non-new) rawpy if they start releasing arm64 + # wheels. https://github.com/letmaik/rawpy/issues/171#issuecomment-1572627747 + "rawpy>=0.18.1; platform_machine != 'arm64'", + "newrawpy>=0.18.1; platform_machine == 'arm64'", "requests", "rich>=12.5.1", "scikit-image>=0.19.3", @@ -53,12 +56,15 @@ dependencies = [ "torchvision>=0.14.1", "torchmetrics[image]>=1.0.1", "typing_extensions>=4.4.0", - "viser==0.1.3", + "viser==0.1.17", "nuscenes-devkit>=1.1.1", "wandb>=0.13.3", "xatlas", "trimesh>=3.20.2", - "timm==0.6.7" + "timm==0.6.7", + "gsplat==0.1.0", + "pytorch-msssim", + "pathos" ] [project.urls] @@ -90,7 +96,8 @@ dev = [ "diffusers==0.16.1", "opencv-stubs==0.0.7", "transformers==4.29.2", - "pyright==1.1.308", + "pyright==1.1.331", + "projectaria_tools[all]>=1.2.0", ] # Documentation related packages @@ -159,6 +166,7 @@ pythonVersion = "3.8" pythonPlatform = "Linux" [tool.ruff] +line-length = 120 select = [ "E", # pycodestyle errors. "F", # Pyflakes rules. diff --git a/tests/cameras/test_cameras.py b/tests/cameras/test_cameras.py index fd2bdcacb..2c7d0b840 100644 --- a/tests/cameras/test_cameras.py +++ b/tests/cameras/test_cameras.py @@ -120,6 +120,70 @@ def test_pinhole_camera(): pinhole_camera.generate_rays(camera_indices=0, coords=coords) +def test_orthophoto_camera(): + """Test that the orthographic camera model works.""" + c2w = torch.eye(4)[None, :3, :] + # apply R and T. + R = torch.Tensor( + [ + [0.5, -0.14644661, 0.85355339], + [0.5, 0.85355339, -0.14644661], + [-0.70710678, 0.5, 0.5], + ] + ).unsqueeze(0) + T = torch.Tensor([[0.5, 0, -0.5]]) + c2w[..., :3, :3] = R + c2w[..., :3, 3] = T + + ortho_cam = Cameras(cx=1.5, cy=1.5, fx=1.0, fy=1.0, camera_to_worlds=c2w, camera_type=CameraType.ORTHOPHOTO) + ortho_rays = ortho_cam.generate_rays(camera_indices=0) + # campare with `PERSPECTIVE` to validate `ORTHOPHOTO`. + pinhole_cam = Cameras(cx=1.5, cy=1.5, fx=1.0, fy=1.0, camera_to_worlds=c2w, camera_type=CameraType.PERSPECTIVE) + pinhole_rays = pinhole_cam.generate_rays(camera_indices=0) + + assert ortho_rays.shape == pinhole_rays.shape + # `ortho_rays.directions` should equal to the center ray of `pinhole_rays.directions`. + assert torch.allclose( + ortho_rays.directions, pinhole_rays.directions[1, 1].broadcast_to(ortho_rays.directions.shape) + ) + # `ortho_rays.origins` should be grid points with a mean value of `pinhole_rays.origins`. + assert torch.allclose(ortho_rays.origins.mean(dim=(0, 1)), pinhole_rays.origins[1, 1]) + + +def test_multi_camera_type(): + """Test that the orthographic camera model works.""" + # here we test two different camera types. + num_cams = [2] + c2w = torch.eye(4)[None, :3, :].broadcast_to(*num_cams, 3, 4) + cx = torch.Tensor([20]).broadcast_to(*num_cams, 1) + cy = torch.Tensor([10]).broadcast_to(*num_cams, 1) + fx = torch.Tensor([10]).broadcast_to(*num_cams, 1) + fy = torch.Tensor([10]).broadcast_to(*num_cams, 1) + h = torch.Tensor([40]).long().broadcast_to(*num_cams, 1) + w = torch.Tensor([20]).long().broadcast_to(*num_cams, 1) + camera_type = [CameraType.PERSPECTIVE, CameraType.ORTHOPHOTO] + multitype_cameras = Cameras(c2w, fx, fy, cx, cy, w, h, camera_type=camera_type) + + # test `generate_rays`, 1 cam. + ray0 = multitype_cameras.generate_rays(camera_indices=0) + assert ray0.shape == torch.Size([40, 20]) + + # test `generate_rays`, multiple cams. + num_rays = [30, 30] + camera_indices = torch.randint(0, 2, [*num_rays, len(num_cams)]) # (*num_rays, num_cameras_batch_dims) + ray1 = multitype_cameras.generate_rays(camera_indices=camera_indices) + assert ray1.shape == torch.Size([40, 20, *num_rays]) + + # test `_generate_rays_from_coords`, 1 cam. + coords = torch.randint(0, 10, [*num_rays, 2]).float() # (*num_rays 2) + ray2 = multitype_cameras.generate_rays(camera_indices=0, coords=coords) + assert ray2.shape == torch.Size([*num_rays]) + + # test `_generate_rays_from_coords`, multiple cam. + ray3 = multitype_cameras.generate_rays(camera_indices=camera_indices, coords=coords) + assert ray3.shape == torch.Size([*num_rays]) + + def test_equirectangular_camera(): """Test that the equirectangular camera model works.""" height = 100 # width is twice the height @@ -311,3 +375,5 @@ if __name__ == "__main__": test_pinhole_camera() test_equirectangular_camera() test_camera_as_tensordataclass() + test_orthophoto_camera() + test_multi_camera_type() diff --git a/tests/data/configs/test_config1.yml b/tests/data/configs/test_config1.yml index 3cf014760..67ce18cda 100644 --- a/tests/data/configs/test_config1.yml +++ b/tests/data/configs/test_config1.yml @@ -111,8 +111,6 @@ pipeline: !!python/object:nerfstudio.pipelines.base_pipeline.VanillaPipelineConf - data - varun - cheezit-scaled - eval_image_indices: !!python/tuple - - 0 eval_num_images_to_sample_from: -1 eval_num_rays_per_batch: 4096 eval_num_times_to_repeat_images: -1 diff --git a/tests/data/configs/test_config2.yml b/tests/data/configs/test_config2.yml index c1ccda379..90737789f 100644 --- a/tests/data/configs/test_config2.yml +++ b/tests/data/configs/test_config2.yml @@ -105,8 +105,6 @@ pipeline: !!python/object:nerfstudio.pipelines.base_pipeline.VanillaPipelineConf - data - varun - cheezit-scaled - eval_image_indices: !!python/tuple - - 0 eval_num_images_to_sample_from: -1 eval_num_rays_per_batch: 4096 eval_num_times_to_repeat_images: -1 diff --git a/tests/test_train.py b/tests/test_train.py index a88283a59..45d8e348f 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -26,6 +26,7 @@ BLACKLIST = [ "neus", "generfacto", "neus-facto", + "gaussian-splatting", ] diff --git a/tests/utils/test_math.py b/tests/utils/test_math.py index c0e1edc43..952c6a1be 100644 --- a/tests/utils/test_math.py +++ b/tests/utils/test_math.py @@ -13,4 +13,4 @@ def test_spherical_harmonics(components): dx = dx / torch.linalg.norm(dx, dim=-1, keepdim=True) sh = components_from_spherical_harmonics(components, dx) matrix = (sh.T @ sh) / N * 4 * torch.pi - torch.testing.assert_close(matrix, torch.eye(components**2), rtol=0, atol=1e-2) + torch.testing.assert_close(matrix, torch.eye(components**2), rtol=0, atol=1.5e-2)