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
27from Camera import CameraInfo
38from utils import get_ray_origin_and_direction_from_camera , get_ray_origin_and_direction_from_camera_by_gpt
49from utils import torch_single_point_alpha_forward
8893 homogeneous_translation_camera [1 , 0 ], homogeneous_translation_camera [2 , 0 ]])
8994
9095D_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 )
9297print (latex (D_translation_camrea_D_q ))
9398pprint (D_translation_camrea_D_q , use_unicode = True )
9499print (latex (D_translation_camrea_D_t ))
@@ -303,7 +308,6 @@ def rotation_matrix_from_quaternion(q: ti.math.vec4) -> ti.math.mat3:
303308print (sympy .python (J ))
304309
305310# %%
306- import sympy
307311xy = sympy .MatrixSymbol ('xy' , 2 , 1 )
308312mu = sympy .Matrix (["mu_x" , "mu_y" ])
309313cov = sympy .MatrixSymbol ('cov' , 2 , 2 )
@@ -325,7 +329,6 @@ def rotation_matrix_from_quaternion(q: ti.math.vec4) -> ti.math.mat3:
325329print (J .shape )
326330print (sympy .python (J ))
327331# %%
328- import numpy as np
329332xy = np .array ([1 , 2 ])
330333x = xy [0 ]
331334y = xy [1 ]
@@ -377,7 +380,6 @@ def gradient_cov(x, mean, cov):
377380print (gradient_mean (xy , mu , cov ))
378381print (gradient_cov (xy , mu , cov ))
379382# %%
380- import torch
381383xy = torch .tensor ([1. , 2. ])
382384mu = torch .tensor ([3. , 1. ], requires_grad = True )
383385cov = 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
590590path = "logs/sigmoid_on_image_fix_bug/scene_66000.parquet"
591591df = pd .read_parquet (path )
592592# %%
593593df .head ()
594594# %%
595- import matplotlib .pyplot as plt
596595plt .hist (np .exp (df .cov_s0 ), bins = 100 )
597596# %%
598597np .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+
616615def 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# 测试函数
648648mu = np .array ([1 , 2 ])
649649Sigma = np .array ([[100. , 0 ], [0 , 100 ]])
@@ -653,7 +653,7 @@ def compute_derivatives(mu, Sigma, x):
653653print ("dp/dmu:" , dp_dmu )
654654print ("dp/dSigma:" , dp_dSigma )
655655# %%
656- import torch
656+
657657
658658def 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+
674675def 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# 测试函数
696698mu = np .array ([1.0 , 2.0 ])
697699Sigma = 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
737739parquet_path = "/home/kuangyuan/hdd/Development/taichi_3d_gaussian_splatting/logs/tat_truck_experiment_more_val/scene_13750.parquet"
738740df = pd .read_parquet (parquet_path )
739741# %%
740742df .head ()
741743# %%
742744point_cloud = df [["x" , "y" , "z" ]].values
743745point_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
746748point_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+
792795def 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+
808812S = np .exp (s )
809813
810814rotated_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# %%
820823point_cloud_o3d = o3d .geometry .PointCloud ()
821824point_cloud_o3d .points = o3d .utility .Vector3dVector (point_cloud [mask ])
@@ -824,8 +827,9 @@ def rotation_matrix_from_quaternion(q: np.ndarray) -> np.ndarray:
824827o3d .visualization .draw_geometries ([point_cloud_o3d ])
825828# %%
826829
827- import taichi as ti
828830ti .init (arch = ti .cpu )
831+
832+
829833@ti .kernel
830834def 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+
838844test ()
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
845848mean = np .array ([0 , 0 ])
@@ -875,8 +878,10 @@ def test():
875878plt .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
881886plt .colorbar ()
882887plt .show ()
@@ -885,13 +890,14 @@ def test():
885890print (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# %%
892897df .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
897903df1 .head ()
0 commit comments