Skip to content

Commit 69ccaaa

Browse files
committed
misc: enhance simple_trainer with viewer integration and GOF configuration
1 parent 07438d6 commit 69ccaaa

File tree

9 files changed

+1311
-200
lines changed

9 files changed

+1311
-200
lines changed

examples/datasets/INVR_N3D.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from examples.helper.STG.time_utils import timer, timeblock
2525

2626
# reference to STG's scene __init__.py
27-
@timer
27+
# @timer
2828
class Parser:
2929
"""COLMAP parser."""
3030

@@ -56,14 +56,15 @@ def __init__(
5656
raydict = {}
5757

5858
# Get scene info
59-
if loader == "colmap": # colmapvalid only for testing
59+
## Get cam parameters & merged point cloud for splats initialization
60+
if loader == "colmap":
6061
scene_info = sceneLoadTypeCallbacks["Colmap"](self.source_path, self.images_phrase, self.eval, multiview, duration=self.duration, test_view_id=self.test_view_id, downscale_factor=downscale_factor) # SceneInfo() - NamedTuple
6162
# elif loader == "invr":
6263
# scene_info = sceneLoadTypeCallbacks["INVR"](self.source_path, self.images_phrase, self.eval, multiview, duration=self.duration) # SceneInfo() - NamedTuple
6364
else:
6465
assert False, "Could not recognize scene type!"
6566

66-
with open(scene_info.ply_path, 'rb') as src_file, open(os.path.join(self.model_path, "input.ply") , 'wb') as dest_file:
67+
with open(scene_info.ply_path, 'rb') as src_file, open(os.path.join(self.model_path, "init_pcd.ply") , 'wb') as dest_file:
6768
dest_file.write(src_file.read())
6869

6970
self.cameras_extent = scene_info.nerf_normalization["radius"]
@@ -187,7 +188,8 @@ def __getitem__(self, index: int) -> Dict[str, Any]:
187188
images.append(PILtoTorch_new(self.fetch_image(finfo.image_path), resolution).permute(1,2,0))
188189
image_paths.append(finfo.image_path)
189190
camtoworlds.append(torch.from_numpy(self.camtoworld[globalid]))
190-
timesteps.append(tid/len(self.scene_by_t))
191+
# timesteps.append(tid/len(self.scene_by_t)) # old version: norm to [0,1]
192+
timesteps.append(tid/30) # default framerate is 30
191193
Ks.append(torch.from_numpy(self.parser.K))
192194
rays.append(cami.rays[0])
193195

examples/helper/STG/dataset_readers.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ def readColmapCameras(cam_extrinsics, cam_intrinsics, images_folder, near, far,
124124
sys.stdout.flush()
125125

126126
extr = cam_extrinsics[key]
127-
128-
129-
130-
131127
intr = cam_intrinsics[extr.camera_id]
132128
height = intr.height
133129
width = intr.width
@@ -147,8 +143,6 @@ def readColmapCameras(cam_extrinsics, cam_intrinsics, images_folder, near, far,
147143
else:
148144
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
149145

150-
151-
152146
# loop for timestamps/frames
153147
for j in range(startime, startime+ int(duration)):
154148
image_path = os.path.join(images_folder, os.path.basename(extr.name))
@@ -757,6 +751,7 @@ def readColmapSceneInfoMv(path, images, eval, llffhold=8, multiview=False, durat
757751

758752

759753
def readColmapSceneInfo(path, images, eval, llffhold=8, multiview=False, duration=50, test_view_id=[0], downscale_factor=1):
754+
# get camera extrinsic & intrinsic
760755
try:
761756
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.bin")
762757
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.bin")
@@ -787,12 +782,14 @@ def readColmapSceneInfo(path, images, eval, llffhold=8, multiview=False, duratio
787782
train_cam_infos = cam_infos[duration:] # Camera 1~20 train set
788783
test_cam_infos = cam_infos[:duration] # Camera 0 test set
789784

785+
# make sure only have one test view
790786
uniquecheck = []
791787
for cam_info in test_cam_infos:
792788
if cam_info.image_name not in uniquecheck:
793789
uniquecheck.append(cam_info.image_name)
794790
assert len(uniquecheck) == 1
795791

792+
# make sure test view is not in the set of train view
796793
sanitycheck = []
797794
for cam_info in train_cam_infos:
798795
if cam_info.image_name not in sanitycheck:
@@ -829,12 +826,9 @@ def readColmapSceneInfo(path, images, eval, llffhold=8, multiview=False, duratio
829826
nerf_normalization = getNerfppNorm(train_cam_infos)
830827
nerf_normalization_test = getNerfppNorm(test_cam_infos)
831828

832-
ply_path = os.path.join(path, "sparse/0/points3D.ply")
833-
bin_path = os.path.join(path, "sparse/0/points3D.bin")
834-
txt_path = os.path.join(path, "sparse/0/points3D.txt")
835829
totalply_path = os.path.join(path, "sparse/0/points3D_total" + str(duration) + ".ply")
836830

837-
831+
### operation from STG
838832
# merge SfM point clouds from consecutive frames into a single .ply file
839833
# pc from different frames have corresponding time index
840834
# these points will be used for initialization
@@ -849,14 +843,17 @@ def readColmapSceneInfo(path, images, eval, llffhold=8, multiview=False, duratio
849843
xyz, rgb, _ = read_points3D_binary(thisbin_path)
850844
totalxyz.append(xyz)
851845
totalrgb.append(rgb)
852-
totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration)
846+
# totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration) # old version: norm timestamp into [0,1]
847+
totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / 30) # new version:change the unit from index into seconds, assume framerate as 30 frames per second
848+
853849
xyz = np.concatenate(totalxyz, axis=0)
854850
rgb = np.concatenate(totalrgb, axis=0)
855851
totaltime = np.concatenate(totaltime, axis=0)
856852
assert xyz.shape[0] == rgb.shape[0]
857853
xyzt =np.concatenate( (xyz, totaltime), axis=1)
858854
storePly(totalply_path, xyzt, rgb)
859855
try:
856+
print(f"Read merged point cloud file directly from: {totalply_path}")
860857
pcd = fetchPly(totalply_path)
861858
except:
862859
pcd = None
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
SCENE_DIR="examples/data/neural_3d"
2+
3+
SCENE_LIST="coffee_martini cook_spinach cut_roasted_beef flame_salmon_1 flame_steak sear_steak" # SCENE_LIST="coffee_martini cook_spinach cut_roasted_beef flame_salmon_1 flame_steak sear_steak"
4+
5+
GPU_LIST=(0 1 2 3 4 5)
6+
7+
RESULT_DIR="results/stg_neu3d"
8+
9+
GOF_SIZE=50
10+
11+
DEFAULT_GPU_ID=${GPU_LIST[0]}
12+
13+
run_single_scene() {
14+
local rd_lambda="$1"
15+
local scene="$2"
16+
local gpu_id="${3:-$DEFAULT_GPU_ID}"
17+
18+
# Training Phase
19+
CUDA_VISIBLE_DEVICES=$gpu_id python examples/simple_trainer_STG.py default \
20+
--model_path "$RESULT_DIR/default/$scene" \
21+
--data_dir "$SCENE_DIR/$scene" \
22+
--result_dir "$RESULT_DIR/default/$scene" \
23+
--gof_num $GOF_SIZE \
24+
--test_view_id 0 \
25+
--disable_viewer \
26+
27+
# Compression Phase
28+
CUDA_VISIBLE_DEVICES=$gpu_id python examples/simple_trainer_STG.py default \
29+
--model_path "$RESULT_DIR/default/$scene" \
30+
--data_dir "$SCENE_DIR/$scene" \
31+
--result_dir "$RESULT_DIR/default/$scene" \
32+
--gof_num $GOF_SIZE \
33+
--test_view_id 0 \
34+
--disable_viewer \
35+
--compression_only \
36+
--ckpt_name "ckpt_best_rank0.pt" \
37+
--compression "stg"
38+
}
39+
40+
41+
idx=0
42+
for scene in $SCENE_LIST; do
43+
gpu_id=${GPU_LIST[$((idx % ${#GPU_LIST[@]}))]}
44+
run_single_scene "$rd_lambda" "$scene" "$gpu_id" &
45+
idx=$((idx+1))
46+
done
47+
wait
48+
echo "Finished running all scenes."
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
SCENE_DIR="examples/data/neural_3d"
2+
3+
SCENE_LIST="coffee_martini cook_spinach cut_roasted_beef flame_salmon_1 flame_steak sear_steak" # SCENE_LIST="coffee_martini cook_spinach cut_roasted_beef flame_salmon_1 flame_steak sear_steak"
4+
5+
GPU_LIST=(0 1 2 3 4 5)
6+
7+
RESULT_DIR="results/stg_neu3d_comp_sim"
8+
9+
GOF_SIZE=50
10+
11+
RD_LAMBDA=(0.005 0.01 0.02) # refers to 3 ratepoints
12+
13+
DEFAULT_GPU_ID=${GPU_LIST[0]}
14+
15+
run_single_scene() {
16+
local rd_lambda="$1"
17+
local scene="$2"
18+
local gpu_id="${3:-$DEFAULT_GPU_ID}"
19+
20+
# Training Phase
21+
CUDA_VISIBLE_DEVICES=$gpu_id python examples/simple_trainer_STG.py compression_sim \
22+
--model_path "$RESULT_DIR/rd_${rd_lambda}/$scene" \
23+
--data_dir "$SCENE_DIR/$scene" \
24+
--result_dir "$RESULT_DIR/rd_${rd_lambda}/$scene" \
25+
--gof_num $GOF_SIZE \
26+
--test_view_id 0 \
27+
--disable_viewer \
28+
--entropy_model_opt --rd_lambda $rd_lambda # enable entropy model with rd_lambda 0.01
29+
30+
# Compression Phase
31+
CUDA_VISIBLE_DEVICES=$gpu_id python examples/simple_trainer_STG.py default \
32+
--model_path "$RESULT_DIR/rd_${rd_lambda}/$scene" \
33+
--data_dir "$SCENE_DIR/$scene" \
34+
--result_dir "$RESULT_DIR/rd_${rd_lambda}/$scene" \
35+
--gof_num $GOF_SIZE \
36+
--test_view_id 0 \
37+
--disable_viewer \
38+
--compression_only \
39+
--ckpt_name "ckpt_best_rank0.pt" \
40+
--compression "stg"
41+
}
42+
43+
for rd_lambda in "${RD_LAMBDA[@]}"; do
44+
idx=0
45+
for scene in $SCENE_LIST; do
46+
gpu_id=${GPU_LIST[$((idx % ${#GPU_LIST[@]}))]}
47+
run_single_scene "$rd_lambda" "$scene" "$gpu_id" &
48+
idx=$((idx+1))
49+
done
50+
wait
51+
echo "Finished running $rd_lambda on all scenes."
52+
done

0 commit comments

Comments
 (0)