Yunzhou Song (Tim), Zixuan Lin (Jack), Yexin Zhang
GRASP lab, University of Pennsylvania
This repository contains the unofficial implementation of the paper "2D Gaussian Splatting for Geometrically Accurate Radiance Fields". The detailed equations of our method are here.
Our implementation is as perfect in photometric reconstruction as that of 3D gs. We are eager to receive suggestions and open to feedback and criticism!
We are still debugging!
240502: Fix negative ray_M gradient.
240505: Add edge-aware distortion.
240511: Add precedure regularization weight and different Ln
240804: Use Blending mean depth, detach depth gradient in Ln
This project is built upon 3DGS.
Rendered RGB Image (left), Rendered Normal (right)
The repository contains submodules, thus please check it out with
# SSH
git clone git@github.com:TimSong412/2D-gaussian.git --recursive
or
# HTTPS
git clone https://github.com/TimSong412/2D-gaussian.git --recursive
The optimizer uses PyTorch and CUDA extensions in a Python environment to produce trained models.
Download datasets
sh download.sh
Set up environment
You can run the following in parallel with the previous step
conda create -n gs python=3.8
conda activate gs
pip install -r requirements.txt
pip install submodules/simple-knn
bash update_pkg.sh
Configuration
Checkout Config.h for options of regularization.
#define Ld
and
#define Ln
turn on and off depth distortion (Ld) regularizer and normal consistency regularizer (Ln).
The weight of Ld
and Ln
can be adjusted at (or around) line 163 of train.py
.
Compile Rasterizer
Build binary of cuda rasterier
bash update_pkg.sh
To run the optimizer, simply use
python train.py -s <path to COLMAP or NeRF Synthetic dataset>
example
python train.py -s dataset/garden
Note that similar to MipNeRF360, we target images at resolutions in the 1-1.6K pixel range. For convenience, arbitrary-size inputs can be passed and will be automatically resized if their width exceeds 1600 pixels. We recommend to keep this behavior, but you may force training to use your higher-resolution images by setting -r 1
.
The MipNeRF360 scenes are hosted by the paper authors here. You can find our SfM data sets for Tanks&Temples and Deep Blending here. If you do not provide an output model directory (-m
), trained models are written to folders with randomized unique names inside the output
directory. At this point, the trained models may be viewed with the real-time viewer (see further below).
In additional to RGB images, the script will render depth map, normal map, and 3D gaussian visualization
python vis_depth.py -m output/xxxxx --eval
By default, the trained models use all available images in the dataset. To train them while withholding a test set for evaluation, use the --eval
flag. This way, you can render training/test sets and produce error metrics as follows:
python train.py -s <path to COLMAP or NeRF Synthetic dataset> --eval # Train with train/test split
python render.py -m <path to trained model> # Generate renderings
python metrics.py -m <path to trained model> # Compute error metrics on renderings
If you want to evaluate our pre-trained models, you will have to download the corresponding source data sets and indicate their location to render.py
with an additional --source_path/-s
flag. Note: The pre-trained models were created with the release codebase. This code base has been cleaned up and includes bugfixes, hence the metrics you get from evaluating them will differ from those in the paper.
python render.py -m <path to pre-trained model> -s <path to COLMAP dataset>
python metrics.py -m <path to pre-trained model>
We further provide the full_eval.py
script. This script specifies the routine used in our evaluation and demonstrates the use of some additional parameters, e.g., --images (-i)
to define alternative image directories within COLMAP data sets. If you have downloaded and extracted all the training data, you can run it like this:
python full_eval.py -m360 <mipnerf360 folder> -tat <tanks and temples folder> -db <deep blending folder>
In the current version, this process takes about 7h on our reference machine containing an A6000. If you want to do the full evaluation on our pre-trained models, you can specify their download location and skip training.
python full_eval.py -o <directory with pretrained models> --skip_training -m360 <mipnerf360 folder> -tat <tanks and temples folder> -db <deep blending folder>
If you want to compute the metrics on our paper's evaluation images, you can also skip rendering. In this case it is not necessary to provide the source datasets. You can compute metrics for multiple image sets at a time.
python full_eval.py -m <directory with evaluation images>/garden ... --skip_training --skip_rendering
We provide two interactive viewers for our method: remote and real-time. Our viewing solutions are based on the SIBR framework, developed by the GRAPHDECO group for several novel-view synthesis projects.
We provide pre-built binaries for Windows here. We recommend using them on Windows for an efficient setup, since the building of SIBR involves several external dependencies that must be downloaded and compiled on-the-fly.
If you cloned with submodules (e.g., using --recursive
), the source code for the viewers is found in SIBR_viewers
. The network viewer runs within the SIBR framework for Image-based Rendering applications.
CMake should take care of your dependencies.
cd SIBR_viewers
cmake -Bbuild .
cmake --build build --target install --config RelWithDebInfo
You may specify a different configuration, e.g. Debug
if you need more control during development.
You will need to install a few dependencies before running the project setup.
# Dependencies
sudo apt install -y libglew-dev libassimp-dev libboost-all-dev libgtk-3-dev libopencv-dev libglfw3-dev libavdevice-dev libavcodec-dev libeigen3-dev libxxf86vm-dev libembree-dev
# Project setup
cd SIBR_viewers
cmake -Bbuild . -DCMAKE_BUILD_TYPE=Release # add -G Ninja to build faster
cmake --build build -j24 --target install
Backwards compatibility with Focal Fossa is not fully tested, but building SIBR with CMake should still work after invoking
git checkout fossa_compatibility
The SIBR interface provides several methods of navigating the scene. By default, you will be started with an FPS navigator, which you can control with W, A, S, D, Q, E
for camera translation and I, K, J, L, U, O
for rotation. Alternatively, you may want to use a Trackball-style navigator (select from the floating menu). You can also snap to a camera from the data set with the Snap to
button or find the closest camera with Snap to closest
. The floating menues also allow you to change the navigation speed. You can use the Scaling Modifier
to control the size of the displayed Gaussians, or show the initial point cloud.
After extracting or installing the viewers, you may run the compiled SIBR_remoteGaussian_app[_config]
app in <SIBR install dir>/bin
, e.g.:
./<SIBR install dir>/bin/SIBR_remoteGaussian_app
The network viewer allows you to connect to a running training process on the same or a different machine. If you are training on the same machine and OS, no command line parameters should be required: the optimizer communicates the location of the training data to the network viewer. By default, optimizer and network viewer will try to establish a connection on localhost on port 6009. You can change this behavior by providing matching --ip
and --port
parameters to both the optimizer and the network viewer. If for some reason the path used by the optimizer to find the training data is not reachable by the network viewer (e.g., due to them running on different (virtual) machines), you may specify an override location to the viewer by using -s <source path>
.
After extracting or installing the viewers, you may run the compiled SIBR_gaussianViewer_app[_config]
app in <SIBR install dir>/bin
, e.g.:
./<SIBR install dir>/bin/SIBR_gaussianViewer_app -m <path to trained model>
It should suffice to provide the -m
parameter pointing to a trained model directory. Alternatively, you can specify an override location for training input data using -s
. To use a specific resolution other than the auto-chosen one, specify --rendering-size <width> <height>
. Combine it with --force-aspect-ratio
if you want the exact resolution and don't mind image distortion.
To unlock the full frame rate, please disable V-Sync on your machine and also in the application (Menu → Display). In a multi-GPU system (e.g., laptop) your OpenGL/Display GPU should be the same as your CUDA GPU (e.g., by setting the application's GPU preference on Windows, see below) for maximum performance.
In addition to the initial point cloud and the splats, you also have the option to visualize the Gaussians by rendering them as ellipsoids from the floating menu. SIBR has many other functionalities, please see the documentation for more details on the viewer, navigation options etc. There is also a Top View (available from the menu) that shows the placement of the input cameras and the original SfM point cloud; please note that Top View slows rendering when enabled. The real-time viewer also uses slightly more aggressive, fast culling, which can be toggled in the floating menu. If you ever encounter an issue that can be solved by turning fast culling off, please let us know.
Our COLMAP loaders expect the following dataset structure in the source path location:
<location>
|---images
| |---<image 0>
| |---<image 1>
| |---...
|---sparse
|---0
|---cameras.bin
|---images.bin
|---points3D.bin
For rasterization, the camera models must be either a SIMPLE_PINHOLE or PINHOLE camera. We provide a converter script convert.py
, to extract undistorted images and SfM information from input images. Optionally, you can use ImageMagick to resize the undistorted images. This rescaling is similar to MipNeRF360, i.e., it creates images with 1/2, 1/4 and 1/8 the original resolution in corresponding folders. To use them, please first install a recent version of COLMAP (ideally CUDA-powered) and ImageMagick. Put the images you want to use in a directory <location>/input
.
<location>
|---input
|---<image 0>
|---<image 1>
|---...
If you have COLMAP and ImageMagick on your system path, you can simply run
python convert.py -s <location> [--resize] #If not resizing, ImageMagick is not needed
Alternatively, you can use the optional parameters --colmap_executable
and --magick_executable
to point to the respective paths. Please note that on Windows, the executable should point to the COLMAP .bat
file that takes care of setting the execution environment. Once done, <location>
will contain the expected COLMAP data set structure with undistorted, resized input images, in addition to your original images and some temporary (distorted) data in the directory distorted
.
If you have your own COLMAP dataset without undistortion (e.g., using OPENCV
camera), you can try to just run the last part of the script: Put the images in input
and the COLMAP info in a subdirectory distorted
:
<location>
|---input
| |---<image 0>
| |---<image 1>
| |---...
|---distorted
|---database.db
|---sparse
|---0
|---...
Then run
python convert.py -s <location> --skip_matching [--resize] #If not resizing, ImageMagick is not needed
Where do I get data sets, e.g., those referenced in full_eval.py
? The MipNeRF360 data set is provided by the authors of the original paper on the project site. Note that two of the data sets cannot be openly shared and require you to consult the authors directly. For Tanks&Temples and Deep Blending, please use the download links provided at the top of the page. Alternatively, you may access the cloned data (status: August 2023!) from HuggingFace
How can I use this for a much larger dataset, like a city district? The current method was not designed for these, but given enough memory, it should work out. However, the approach can struggle in multi-scale detail scenes (extreme close-ups, mixed with far-away shots). This is usually the case in, e.g., driving data sets (cars close up, buildings far away). For such scenes, you can lower the --position_lr_init
, --position_lr_final
and --scaling_lr
(x0.3, x0.1, ...). The more extensive the scene, the lower these values should be. Below, we use default learning rates (left) and --position_lr_init 0.000016 --scaling_lr 0.001"
(right).
I'm on Windows and I can't manage to build the submodules, what do I do? Consider following the steps in the excellent video tutorial here, hopefully they should help. The order in which the steps are done is important! Alternatively, consider using the linked Colab template.
It still doesn't work. It says something about cl.exe
. What do I do? User Henry Pearce found a workaround. You can you try adding the visual studio path to your environment variables (your version number might differ);
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64
Then make sure you start a new conda prompt and cd to your repo location and try this;
conda activate gaussian_splatting
cd <dir_to_repo>/gaussian-splatting
pip install submodules\diff-gaussian-rasterization
pip install submodules\simple-knn
I'm on macOS/Puppy Linux/Greenhat and I can't manage to build, what do I do? Sorry, we can't provide support for platforms outside of the ones we list in this README. Consider using the linked Colab template.
I don't have 24 GB of VRAM for training, what do I do? The VRAM consumption is determined by the number of points that are being optimized, which increases over time. If you only want to train to 7k iterations, you will need significantly less. To do the full training routine and avoid running out of memory, you can increase the --densify_grad_threshold
, --densification_interval
or reduce the value of --densify_until_iter
. Note however that this will affect the quality of the result. Also try setting --test_iterations
to -1
to avoid memory spikes during testing. If --densify_grad_threshold
is very high, no densification should occur and training should complete if the scene itself loads successfully.
24 GB of VRAM for reference quality training is still a lot! Can't we do it with less? Yes, most likely. By our calculations it should be possible with way less memory (~8GB). If we can find the time we will try to achieve this. If some PyTorch veteran out there wants to tackle this, we look forward to your pull request!
How can I use the differentiable Gaussian rasterizer for my own project? Easy, it is included in this repo as a submodule diff-gaussian-rasterization
. Feel free to check out and install the package. It's not really documented, but using it from the Python side is very straightforward (cf. gaussian_renderer/__init__.py
).
Wait, but <insert feature>
isn't optimized and could be much better? There are several parts we didn't even have time to think about improving (yet). The performance you get with this prototype is probably a rather slow baseline for what is physically possible.
Something is broken, how did this happen? We tried hard to provide a solid and comprehensible basis to make use of the paper's method. We have refactored the code quite a bit, but we have limited capacity to test all possible usage scenarios. Thus, if part of the website, the code or the performance is lacking, please create an issue. If we find the time, we will do our best to address it.