Skip to content

Commit 2cea5de

Browse files
authored
Try add depth cov loss to improve quality (#142)
1 parent 9c2a7d5 commit 2cea5de

6 files changed

Lines changed: 124 additions & 45 deletions

File tree

‎Dockerfile.aws‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ RUN pip install --upgrade pip && \
55
pip install --no-cache-dir -U taichi==1.6.0 matplotlib numpy pytorch_msssim dataclass-wizard pillow pyyaml pandas[parquet]==2.0.0 scipy argparse tensorboard
66
COPY . /opt/ml/code
77
WORKDIR /opt/ml/code
8+
RUN pip install -i https://pypi.taichi.graphics/simple/ taichi-nightly
89
RUN pip install -r requirements.txt
910
RUN pip install -e .

‎requirements.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
taichi>=1.6.0
21
matplotlib
32
numpy
43
pytorch_msssim
@@ -8,4 +7,4 @@ pyyaml
87
pandas[parquet]>=2.0.0
98
scipy
109
argparse
11-
tensorboard
10+
tensorboard

‎scratch/playground.py‎

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# %%
2+
from scipy.stats import multivariate_normal
3+
import open3d as o3d
4+
from sympy import symbols, Matrix, diff, exp
5+
import matplotlib.pyplot as plt
6+
import pandas as pd
27
from Camera import CameraInfo
38
from utils import get_ray_origin_and_direction_from_camera, get_ray_origin_and_direction_from_camera_by_gpt
49
from utils import torch_single_point_alpha_forward
@@ -88,7 +93,7 @@
8893
homogeneous_translation_camera[1, 0], homogeneous_translation_camera[2, 0]])
8994

9095
D_translation_camrea_D_q = translation_camera.jacobian(q)
91-
D_translation_camrea_D_t = translation_camera.jacobian(translation)
96+
D_translation_camrea_D_t = translation_camera.jacobian(t)
9297
print(latex(D_translation_camrea_D_q))
9398
pprint(D_translation_camrea_D_q, use_unicode=True)
9499
print(latex(D_translation_camrea_D_t))
@@ -303,7 +308,6 @@ def rotation_matrix_from_quaternion(q: ti.math.vec4) -> ti.math.mat3:
303308
print(sympy.python(J))
304309

305310
# %%
306-
import sympy
307311
xy = sympy.MatrixSymbol('xy', 2, 1)
308312
mu = sympy.Matrix(["mu_x", "mu_y"])
309313
cov = sympy.MatrixSymbol('cov', 2, 2)
@@ -325,7 +329,6 @@ def rotation_matrix_from_quaternion(q: ti.math.vec4) -> ti.math.mat3:
325329
print(J.shape)
326330
print(sympy.python(J))
327331
# %%
328-
import numpy as np
329332
xy = np.array([1, 2])
330333
x = xy[0]
331334
y = xy[1]
@@ -377,7 +380,6 @@ def gradient_cov(x, mean, cov):
377380
print(gradient_mean(xy, mu, cov))
378381
print(gradient_cov(xy, mu, cov))
379382
# %%
380-
import torch
381383
xy = torch.tensor([1., 2.])
382384
mu = torch.tensor([3., 1.], requires_grad=True)
383385
cov = torch.tensor([[0.8, 0.1], [0.1, 0.8]], requires_grad=True)
@@ -585,14 +587,11 @@ def quaternion_to_rotation_matrix_torch(q):
585587
T_pointcloud_camera=T_pointcloud_camera)
586588

587589
# %%
588-
import pandas as pd
589-
import numpy as np
590590
path = "logs/sigmoid_on_image_fix_bug/scene_66000.parquet"
591591
df = pd.read_parquet(path)
592592
# %%
593593
df.head()
594594
# %%
595-
import matplotlib.pyplot as plt
596595
plt.hist(np.exp(df.cov_s0), bins=100)
597596
# %%
598597
np.exp(df.cov_s0).argmax()
@@ -608,11 +607,11 @@ def quaternion_to_rotation_matrix_torch(q):
608607
print(col, df[col].isnull().sum())
609608

610609
# %%
611-
df = df.dropna()
610+
df = df.dropna()
612611

613612
# %%
614-
import numpy as np
615-
from sympy import symbols, Matrix, diff, exp
613+
614+
616615
def compute_derivatives(mu, Sigma, x):
617616
# 定义符号变量
618617
mu1, mu2, x1, x2 = symbols('mu1 mu2 x1 x2')
@@ -637,13 +636,14 @@ def compute_derivatives(mu, Sigma, x):
637636
"""
638637

639638
# 用实际值替换符号变量
640-
subs = {mu1: mu[0], mu2: mu[1], x1: x[0], x2: x[1],
639+
subs = {mu1: mu[0], mu2: mu[1], x1: x[0], x2: x[1],
641640
s11: Sigma[0, 0], s12: Sigma[0, 1], s21: Sigma[1, 0], s22: Sigma[1, 1]}
642641
dp_dmu_val = dp_dmu.subs(subs)
643642
dp_dSigma_val = dp_dSigma.subs(subs)
644643

645644
return dp_dmu_val, dp_dSigma_val
646645

646+
647647
# 测试函数
648648
mu = np.array([1, 2])
649649
Sigma = np.array([[100., 0], [0, 100]])
@@ -653,7 +653,7 @@ def compute_derivatives(mu, Sigma, x):
653653
print("dp/dmu:", dp_dmu)
654654
print("dp/dSigma:", dp_dSigma)
655655
# %%
656-
import torch
656+
657657

658658
def compute_derivatives_torch(mu, Sigma, x):
659659
# 将输入转换为PyTorch张量,并设置requires_grad=True以启用自动微分
@@ -671,6 +671,7 @@ def compute_derivatives_torch(mu, Sigma, x):
671671

672672
return mu_torch.grad, Sigma_torch.grad
673673

674+
674675
def my_compute(mu, Sigma, x):
675676
gaussian_mean = mu
676677
xy = x
@@ -680,7 +681,7 @@ def my_compute(mu, Sigma, x):
680681
det_cov = Sigma[0, 0] * Sigma[1, 1] - Sigma[0, 1] * Sigma[1, 0]
681682
inv_cov = (1. / det_cov) * \
682683
np.array([[gaussian_covariance[1, 1], -gaussian_covariance[0, 1]],
683-
[-gaussian_covariance[1, 0], gaussian_covariance[0, 0]]])
684+
[-gaussian_covariance[1, 0], gaussian_covariance[0, 0]]])
684685
cov_inv_xy_mean = inv_cov @ xy_mean
685686
xy_mean_T_cov_inv_xy_mean = xy_mean @ cov_inv_xy_mean
686687
exponent = -0.5 * xy_mean_T_cov_inv_xy_mean
@@ -689,9 +690,10 @@ def my_compute(mu, Sigma, x):
689690
xy_mean_outer_xy_mean = np.array([[xy_mean[0] * xy_mean[0], xy_mean[0] * xy_mean[1]],
690691
[xy_mean[1] * xy_mean[0], xy_mean[1] * xy_mean[1]]])
691692
d_p_d_cov = 0.5 * p * (inv_cov @
692-
xy_mean_outer_xy_mean @ inv_cov)
693+
xy_mean_outer_xy_mean @ inv_cov)
693694
return d_p_d_mean, d_p_d_cov
694695

696+
695697
# 测试函数
696698
mu = np.array([1.0, 2.0])
697699
Sigma = np.array([[1.0, 0.0], [0.0, 1.0]])
@@ -711,7 +713,8 @@ def my_compute(mu, Sigma, x):
711713
tmp = np.random.rand(2, 2)
712714
Sigma = tmp @ tmp.T
713715
dp_dmu, dp_dSigma = compute_derivatives(mu, Sigma, x)
714-
dp_dmu, dp_dSigma = np.array(dp_dmu, dtype=np.float32), np.array(dp_dSigma, dtype=np.float32)
716+
dp_dmu, dp_dSigma = np.array(dp_dmu, dtype=np.float32), np.array(
717+
dp_dSigma, dtype=np.float32)
715718
dp_dmu = dp_dmu.reshape(-1)
716719
dp_dSigma = dp_dSigma.reshape(2, 2)
717720
dp_dmu_torch, dp_dSigma_torch = compute_derivatives_torch(mu, Sigma, x)
@@ -723,25 +726,24 @@ def my_compute(mu, Sigma, x):
723726
print("dp/dSigma:", dp_dSigma)
724727
print("dp/dSigma (my):", dp_dSigma_my)
725728
print("dp/dSigma (torch):", dp_dSigma_torch)
726-
729+
727730
# assert np.allclose(dp_dmu, dp_dmu_torch.detach().numpy(), rtol=1e-3), f"dp_dmu: {dp_dmu}, dp_dmu_torch: {dp_dmu_torch}"
728731
# assert np.allclose(dp_dSigma, dp_dSigma_torch.detach().numpy(), rtol=1e-3), f"dp_dSigma: {dp_dSigma}, dp_dSigma_torch: {dp_dSigma_torch}"
729-
assert np.allclose(dp_dmu, dp_dmu_my, rtol=1e-3), f"dp_dmu: {dp_dmu}, dp_dmu_my: {dp_dmu_my}"
730-
assert np.allclose(dp_dSigma, dp_dSigma_my, rtol=1e-3), f"dp_dSigma: {dp_dSigma}, dp_dSigma_my: {dp_dSigma_my}"
731-
732-
732+
assert np.allclose(dp_dmu, dp_dmu_my,
733+
rtol=1e-3), f"dp_dmu: {dp_dmu}, dp_dmu_my: {dp_dmu_my}"
734+
assert np.allclose(dp_dSigma, dp_dSigma_my,
735+
rtol=1e-3), f"dp_dSigma: {dp_dSigma}, dp_dSigma_my: {dp_dSigma_my}"
736+
737+
733738
# %%
734-
import pandas as pd
735-
import numpy as np
736-
import open3d as o3d
737739
parquet_path = "/home/kuangyuan/hdd/Development/taichi_3d_gaussian_splatting/logs/tat_truck_experiment_more_val/scene_13750.parquet"
738740
df = pd.read_parquet(parquet_path)
739741
# %%
740742
df.head()
741743
# %%
742744
point_cloud = df[["x", "y", "z"]].values
743745
point_cloud_rgb = df[["r_sh0", "g_sh0", "b_sh0"]].values
744-
# here rgb are actually sh coefficients (-inf, inf),
746+
# here rgb are actually sh coefficients (-inf, inf),
745747
# need to apply sigmoid to get (0, 1) rgb
746748
point_cloud_rgb = 1.0 / (1.0 + np.exp(-point_cloud_rgb))
747749
# %%
@@ -789,6 +791,7 @@ def rotation_matrix_from_quaternion(q: ti.math.vec4) -> ti.math.mat3:
789791
])
790792
"""
791793

794+
792795
def rotation_matrix_from_quaternion(q: np.ndarray) -> np.ndarray:
793796
xx = q[0] * q[0]
794797
yy = q[1] * q[1]
@@ -805,6 +808,7 @@ def rotation_matrix_from_quaternion(q: np.ndarray) -> np.ndarray:
805808
[2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)]
806809
])
807810

811+
808812
S = np.exp(s)
809813

810814
rotated_S = np.zeros((len(q), 3))
@@ -814,8 +818,7 @@ def rotation_matrix_from_quaternion(q: np.ndarray) -> np.ndarray:
814818
normal[i] = rotation_matrix_from_quaternion(q[i]) @ base_vector[i]
815819
normal[i] *= np.linalg.norm(rotated_S[i])
816820

817-
818-
821+
819822
# %%
820823
point_cloud_o3d = o3d.geometry.PointCloud()
821824
point_cloud_o3d.points = o3d.utility.Vector3dVector(point_cloud[mask])
@@ -824,8 +827,9 @@ def rotation_matrix_from_quaternion(q: np.ndarray) -> np.ndarray:
824827
o3d.visualization.draw_geometries([point_cloud_o3d])
825828
# %%
826829

827-
import taichi as ti
828830
ti.init(arch=ti.cpu)
831+
832+
829833
@ti.kernel
830834
def test():
831835
Cov = ti.Matrix([
@@ -835,11 +839,10 @@ def test():
835839
eig, V = ti.sym_eig(Cov)
836840
print(eig)
837841
print(V)
842+
843+
838844
test()
839845
# %%
840-
import numpy as np
841-
import matplotlib.pyplot as plt
842-
from scipy.stats import multivariate_normal
843846

844847
# Define the mean and covariance matrix
845848
mean = np.array([0, 0])
@@ -875,8 +878,10 @@ def test():
875878
plt.imshow(mask, extent=(-50, 50, -50, 50), origin='lower')
876879

877880
# plt eigenvectors
878-
plt.quiver(mean[0], mean[1], eigen_vectors[0, 0], eigen_vectors[1, 0], color='r', scale=10 / np.sqrt(eigen_values[0]))
879-
plt.quiver(mean[0], mean[1], eigen_vectors[0, 1], eigen_vectors[1, 1], color='r', scale=10 / np.sqrt(eigen_values[1]))
881+
plt.quiver(mean[0], mean[1], eigen_vectors[0, 0], eigen_vectors[1,
882+
0], color='r', scale=10 / np.sqrt(eigen_values[0]))
883+
plt.quiver(mean[0], mean[1], eigen_vectors[0, 1], eigen_vectors[1,
884+
1], color='r', scale=10 / np.sqrt(eigen_values[1]))
880885

881886
plt.colorbar()
882887
plt.show()
@@ -885,13 +890,14 @@ def test():
885890
print(np.sqrt(eigen_values[1]) * 4)
886891

887892
# %%
888-
import pandas as pd
889893
"/home/kuangyuan/hdd/Development/other/taichi_3d_gaussian_splatting/logs/tat_truck_every_8_experiment/camera_poses_6000.parquet"
890-
df = pd.read_parquet("/home/kuangyuan/hdd/Development/other/taichi_3d_gaussian_splatting/logs/tat_truck_every_8_with_pose_noise_optimization/camera_poses_10000.parquet")
894+
df = pd.read_parquet(
895+
"/home/kuangyuan/hdd/Development/other/taichi_3d_gaussian_splatting/logs/tat_truck_every_8_with_pose_noise_optimization/camera_poses_10000.parquet")
891896
# %%
892897
df.head()
893898
# %%
894-
df1 = pd.read_parquet("/home/kuangyuan/hdd/Development/other/taichi_3d_gaussian_splatting/logs/tat_truck_every_8_baseline/camera_poses_30000.parquet")
899+
df1 = pd.read_parquet(
900+
"/home/kuangyuan/hdd/Development/other/taichi_3d_gaussian_splatting/logs/tat_truck_every_8_baseline/camera_poses_30000.parquet")
895901
# %%
896902

897903
df1.head()

‎taichi_3d_gaussian_splatting/GaussianPoint3D.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,14 @@ def project_to_camera_position_by_q_t_jacobian(
211211
d_uv_d_q = d_uv_d_translation_camera @ d_translation_camera_d_q
212212
# d_uv_d_t = d_uv_d_translation_camera @ d_translation_camera_d_t
213213
d_uv_d_t = d_uv_d_translation_camera
214-
return d_uv_d_translation, d_uv_d_q, d_uv_d_t
214+
d_depth_dq = ti.math.vec4([
215+
d_translation_camera_d_q[2, 0],
216+
d_translation_camera_d_q[2, 1],
217+
d_translation_camera_d_q[2, 2],
218+
d_translation_camera_d_q[2, 3]
219+
])
220+
d_depth_dt = ti.math.vec3([0, 0, 1])
221+
return d_uv_d_translation, d_uv_d_q, d_uv_d_t, d_depth_dq, d_depth_dt
215222

216223
@ti.func
217224
def project_to_camera_covariance(

0 commit comments

Comments
 (0)