Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2DGS #208

Merged
merged 118 commits into from
Sep 12, 2024
Merged

2DGS #208

Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
118 commits
Select commit Hold shift + click to select a range
086dfe0
cuda legacy with 2DGS
FantasticOven2 Jun 8, 2024
906a955
clean up changes
FantasticOven2 Jun 9, 2024
b288462
add torch implementation
FantasticOven2 Jun 9, 2024
436b424
adding _wrapper for cuda legacy
FantasticOven2 Jun 9, 2024
9a549d4
working on forward (projection)
FantasticOven2 Jun 9, 2024
5764be5
working on forward (rasterization)
FantasticOven2 Jun 10, 2024
cd7786f
rasterize forward continue
FantasticOven2 Jun 10, 2024
f408b13
starting backward (rasterization)
FantasticOven2 Jun 11, 2024
8580e5e
stubs for 2dgs testing
Jun 11, 2024
e596d75
Merge branch 'main' of https://github.com/FantasticOven2/gsplat
FantasticOven2 Jun 12, 2024
fecabc5
ctn.rasterize (backward)
FantasticOven2 Jun 12, 2024
151ee3d
start projection backward
FantasticOven2 Jun 13, 2024
8cb5154
setup simple 2D experiments and debug forward
FantasticOven2 Jun 14, 2024
9a92cca
ctn. forward debugging
FantasticOven2 Jun 15, 2024
d6ead12
ctn. forward debugging
FantasticOven2 Jun 15, 2024
449e1a4
2d example works
FantasticOven2 Jun 15, 2024
af66b5e
add slant view
FantasticOven2 Jun 15, 2024
4c3717c
variable num points for 2D example
FantasticOven2 Jun 15, 2024
f15625b
working on bwd
FantasticOven2 Jun 17, 2024
39ca680
working on backward
FantasticOven2 Jun 17, 2024
966ec1f
working on backward
FantasticOven2 Jun 17, 2024
9530c89
working on bwd
FantasticOven2 Jun 17, 2024
79298a4
continue adding tests and torch impl for 2dgs
Jun 18, 2024
6a0c8c9
pipeline running nning
FantasticOven2 Jun 18, 2024
d7024ec
debugging gradient
FantasticOven2 Jun 22, 2024
3b19b70
image fitting working
FantasticOven2 Jun 23, 2024
f484aa0
3D works
FantasticOven2 Jun 24, 2024
3a00b5f
setting up numerical tests
FantasticOven2 Jun 27, 2024
37a9f3e
add projection numerical test
FantasticOven2 Jun 27, 2024
f2248b6
adding torch impl and tests for rasterization
Jun 28, 2024
0473dc9
updating 2dgs numerical tests -- rasterize_indices_in_range_2dgs is b…
Jun 28, 2024
84ac77a
correct rasterize_to_indices_in_range_2dgs
FantasticOven2 Jun 28, 2024
3909121
working on 2dgs rasterization numerical tests
FantasticOven2 Jun 28, 2024
7fabcf4
resolving rasterization numerical test differences
FantasticOven2 Jun 28, 2024
5ab8391
linter
Jun 29, 2024
406cd6f
migrating main PR 240 for background
Jun 29, 2024
b0b5da1
fixed bug in rasterize_to_indices_2dgs
Jun 29, 2024
0ff18e2
still failing forward but diffs are much smaller ~2e-5
Jun 29, 2024
1d245be
adding gitignore and removing committed images
Jun 29, 2024
39b6e1a
adding backward tests, currently all failing, will look later
Jun 29, 2024
1b25093
fixed viewmat, pass the correct viewmat, not its transpose
FantasticOven2 Jun 29, 2024
fd36aa9
added densification
FantasticOven2 Jun 30, 2024
4c11611
start normal rendering and potential deadlock
FantasticOven2 Jul 1, 2024
b37f171
normal dual visible
FantasticOven2 Jul 2, 2024
780936e
depth to normal
FantasticOven2 Jul 2, 2024
3cd6690
start normal loss gradient
FantasticOven2 Jul 3, 2024
4d68c74
normal loss gradient
FantasticOven2 Jul 4, 2024
37106b8
ctn. normal consistency gradient
FantasticOven2 Jul 4, 2024
9d47e82
normal gradient works, need to debug the output normal
FantasticOven2 Jul 5, 2024
30108ec
normal consistency done
FantasticOven2 Jul 5, 2024
b55ea01
adding depth distortion loss
FantasticOven2 Jul 7, 2024
76fdf71
added L1 distortion
FantasticOven2 Jul 7, 2024
ca9f0e1
fixed normal bug
FantasticOven2 Jul 9, 2024
7101d5f
working on numerical tests, now only v_ray_Ms didn't pass
FantasticOven2 Jul 11, 2024
164c1bc
find the potential deadlock bug
FantasticOven2 Jul 15, 2024
e4646dd
passing basic numerical tests
FantasticOven2 Jul 16, 2024
0bb01b7
working on halting problem
FantasticOven2 Jul 21, 2024
6574f15
fixed hanging problem due to thread sync
FantasticOven2 Jul 21, 2024
6e7b302
code optimization
FantasticOven2 Jul 23, 2024
bb508c0
add normal tests
FantasticOven2 Jul 24, 2024
7afd909
save changes
FantasticOven2 Jul 29, 2024
56d0a81
improve densification gradient, now working on migration to main brac…
FantasticOven2 Jul 30, 2024
6c1dee0
save changes
FantasticOven2 Aug 4, 2024
8bcf6ab
update to main version
FantasticOven2 Aug 4, 2024
eebe72b
clean tests
FantasticOven2 Aug 4, 2024
bc4fdfe
adding packed projection
FantasticOven2 Aug 5, 2024
091fc71
adding distortion
FantasticOven2 Aug 7, 2024
0a62fe8
adding tests for packed version, forward passed, backward bug
FantasticOven2 Aug 7, 2024
7e86657
cleanup
FantasticOven2 Aug 7, 2024
65ce16d
remove submodule
FantasticOven2 Aug 7, 2024
c79a32e
all tests passed
FantasticOven2 Aug 7, 2024
c31a4dc
added packed tests
FantasticOven2 Aug 8, 2024
2a8f0d6
sync with main
FantasticOven2 Aug 9, 2024
7784955
update readme
FantasticOven2 Aug 9, 2024
2b350c9
update default reg params
FantasticOven2 Aug 9, 2024
8345c5d
Merge branch 'main' into 2dgs
Aug 12, 2024
a006f94
remove mcmc script
Aug 12, 2024
c82ce96
minor cleanup
Aug 12, 2024
353fc27
black format
Aug 12, 2024
6e2cc36
fixed bug in packed version, working on cleanup
FantasticOven2 Aug 15, 2024
fd7c866
adding median depth
FantasticOven2 Aug 16, 2024
e0f2879
support median depth
FantasticOven2 Aug 17, 2024
c2420a8
backup
FantasticOven2 Aug 20, 2024
666705b
cleanup
FantasticOven2 Aug 21, 2024
d57b75f
cleaned up most of the code
FantasticOven2 Aug 21, 2024
a57c525
more cleanup
FantasticOven2 Aug 22, 2024
6dfd704
add docstring
FantasticOven2 Aug 22, 2024
48abf70
adding argument for densification gradient variable
FantasticOven2 Aug 23, 2024
2e8da2e
done cleanup
FantasticOven2 Aug 23, 2024
c538b51
update return type
FantasticOven2 Aug 28, 2024
7daa1ef
additional cleanup
FantasticOven2 Aug 28, 2024
d34baa2
fixed floaters in normal rendering
FantasticOven2 Aug 29, 2024
2f9eb75
adding docs
FantasticOven2 Sep 5, 2024
5566aa5
adding docs
FantasticOven2 Sep 5, 2024
bade5bf
generate teaser vid
FantasticOven2 Sep 5, 2024
afe0308
all done
FantasticOven2 Sep 6, 2024
1a91b03
update to align with main
FantasticOven2 Sep 6, 2024
7bb5af9
test passes
Sep 8, 2024
b1697c2
format
Sep 8, 2024
408f16e
cleanup torch_impl
Sep 8, 2024
6987c00
Merge branch 'main' into 2dgs
Sep 8, 2024
217fdc8
cleanup utils and rasterization fn
Sep 8, 2024
896cc45
black format
Sep 8, 2024
4630230
script cleanup
Sep 8, 2024
61ce019
image fitting cleanup
Sep 8, 2024
c5edf16
image fitting cleanup
Sep 8, 2024
4176666
add matplotlib to requirements
Sep 8, 2024
935cb1d
cleanup
FantasticOven2 Sep 8, 2024
cf6b32f
resolve ruilong's note
FantasticOven2 Sep 9, 2024
c5d1449
rename ray_M to ray_transform
FantasticOven2 Sep 9, 2024
75de227
update doc
FantasticOven2 Sep 9, 2024
8f39645
update doc
FantasticOven2 Sep 9, 2024
14a097a
adding docstring
FantasticOven2 Sep 9, 2024
9d80b19
remove renders
FantasticOven2 Sep 11, 2024
24add4d
Merge branch 'main' into main
liruilong940607 Sep 11, 2024
3487c74
black formatting
FantasticOven2 Sep 12, 2024
fd219f8
black format 22.3
FantasticOven2 Sep 12, 2024
8c4efba
remove renders folder in examples
FantasticOven2 Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,8 @@ venv.bak/
compile_commands.json
*.dump

data
results
/data
/result
/results
/renders
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should dump all outputs to a single results folder so that the local file structure is cleaner and we dont need to put all these variation folders into gitignore.

*.png
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid puting this into gitignore as png file might be needed for the doc page at some point in the future.

124 changes: 124 additions & 0 deletions examples/2d_example.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could merge this file with image_fitting.py?

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import math
import os
from pathlib import Path
from typing import Optional, Literal

import torch
import numpy as np
import tyro
import matplotlib
from PIL import Image

from gsplat import rasterization, rasterization_2dgs

class SimpleTrainer:

def __init__(
self,
num_points_per_axis: int = 8,
):
self.device = torch.device("cuda:0")
self.num_points_per_axis = num_points_per_axis

fov_x = math.pi / 2.0
self.H, self.W = 256, 256
self.focal = 0.5 * float(self.W) / math.tan(0.5 * fov_x)
self.img_size = torch.tensor([self.W, self.H, 1], device=self.device)

self._init_gaussians()

def _init_gaussians(self):
length = 0.4
x = np.linspace(-1, 1, self.num_points_per_axis)
y = np.linspace(-1, 1, self.num_points_per_axis)
x, y = np.meshgrid(x, y)
means3D = torch.from_numpy(np.stack([x, y, np.ones_like(x)], axis=-1).reshape(-1, 3)).cuda().float()
quats = torch.zeros(1, 4).repeat(len(means3D), 1).cuda()
quats[..., 0] = 1.
scale = 0.6 / (self.num_points_per_axis - 1)
scales = torch.zeros(1, 3).repeat(len(means3D), 1).fill_(scale).cuda()
num_points = self.num_points_per_axis ** 2
colors = matplotlib.colormaps['Accent'](np.random.randint(1, num_points, num_points) / num_points)[..., :3]
colors = torch.from_numpy(colors).cuda()
opacity = torch.ones_like(means3D[:, 0])

self.viewmat = torch.tensor(
[[-8.6086e-01, 3.7950e-01, -3.3896e-01, 6.7791e-01],
[ 5.0884e-01, 6.4205e-01, -5.7346e-01, 1.1469e+00],
[ 1.0934e-08, -6.6614e-01, -7.4583e-01, 1.4917e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 1.0000e+00]],
device=self.device,
)

self.means = means3D.float()
self.scales = scales.float()
self.rgbs = colors.float()
self.opacities = opacity.float()
self.quats = quats.float()

def render(
self,
model_type: Literal["3dgs", "2dgs"] = "3dgs",
):
frames = []
K = torch.tensor(
[
[self.focal, 0, self.W / 2],
[0, self.focal, self.H / 2],
[0, 0, 1],
],
device=self.device,
)
if model_type == "3dgs":
renders, _ = rasterization(
self.means,
self.quats / self.quats.norm(dim=-1, keepdim=True),
self.scales,
self.opacities,
self.rgbs,
self.viewmat[None],
K[None],
self.W,
self.H,
packed=False,
)

elif model_type == "2dgs":
renders, _ = rasterization_2dgs(
self.means,
self.quats / self.quats.norm(dim=-1, keepdim=True),
self.scales,
self.opacities,
self.rgbs,
self.viewmat[None],
K[None],
self.W,
self.H,
packed=False,
)
else:
raise NotImplementedError("Model not implemented")
out_img = renders[0].squeeze(0)
torch.cuda.synchronize()

frame = (out_img.detach().cpu().numpy() * 255).astype(np.uint8)
frame_img = Image.fromarray(frame)
out_dir = os.path.join(os.getcwd(), "renders")
os.makedirs(out_dir, exist_ok=True)
frame_img.save(f"{out_dir}/{model_type}.png")

def main(
height: int = 256,
width: int = 256,
num_points_per_axis: int = 8,
save_imgs: bool = True,
img_path: Optional[Path] = None,
model_type: Literal["3dgs", "2dgs"] = "3dgs",
) -> None:
trainer = SimpleTrainer(num_points_per_axis=num_points_per_axis)
trainer.render(
model_type=model_type,
)

if __name__ == "__main__":
tyro.cli(main)
33 changes: 29 additions & 4 deletions examples/image_fitting.py
liruilong940607 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
import os
import time
from pathlib import Path
from typing import Optional
from typing import Optional, Literal

import numpy as np
import torch
import tyro
from PIL import Image
from torch import Tensor, optim

from gsplat import rasterization
from gsplat import rasterization, rasterization_2dgs

os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
os.environ['TORCH_USE_CUDA_DSA'] = '1'

class SimpleTrainer:
"""Trains random gaussians to fit an image."""
Expand Down Expand Up @@ -79,6 +81,7 @@ def train(
iterations: int = 1000,
lr: float = 0.01,
save_imgs: bool = False,
model_type: Literal["3dgs", "2dgs"] = "3dgs",
liruilong940607 marked this conversation as resolved.
Show resolved Hide resolved
):
optimizer = optim.Adam(
[self.rgbs, self.means, self.scales, self.opacities, self.quats], lr
Expand All @@ -94,9 +97,27 @@ def train(
],
device=self.device,
)

if model_type == "3dgs":
rasterize_fnc = rasterization
elif model_type == "2dgs":
rasterize_fnc = rasterization_2dgs

for iter in range(iterations):
start = time.time()
renders, _, _ = rasterization(
# renders, _, _ = rasterization(
# self.means,
# self.quats / self.quats.norm(dim=-1, keepdim=True),
# self.scales,
# torch.sigmoid(self.opacities),
# torch.sigmoid(self.rgbs),
# self.viewmat[None],
# K[None],
# self.W,
# self.H,
# packed=False,
# )
renders, _ = rasterize_fnc(
self.means,
self.quats / self.quats.norm(dim=-1, keepdim=True),
self.scales,
Expand All @@ -108,7 +129,7 @@ def train(
self.H,
packed=False,
)
out_img = renders[0]
out_img = renders[0].squeeze(0)
torch.cuda.synchronize()
times[0] += time.time() - start
loss = mse_loss(out_img, self.gt_image)
Expand All @@ -122,6 +143,8 @@ def train(

if save_imgs and iter % 5 == 0:
frames.append((out_img.detach().cpu().numpy() * 255).astype(np.uint8))
# break

if save_imgs:
# save them as a gif with PIL
frames = [Image.fromarray(frame) for frame in frames]
Expand Down Expand Up @@ -158,6 +181,7 @@ def main(
img_path: Optional[Path] = None,
iterations: int = 1000,
lr: float = 0.01,
model_type: Literal["3dgs", "2dgs"] = "3dgs",
) -> None:
if img_path:
gt_image = image_path_to_tensor(img_path)
Expand All @@ -172,6 +196,7 @@ def main(
iterations=iterations,
lr=lr,
save_imgs=save_imgs,
model_type=model_type,
)


Expand Down
1 change: 1 addition & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ opencv-python
tyro
Pillow
tensorboard
matplotlib
Loading