-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathvggt_long.py
More file actions
699 lines (562 loc) · 29 KB
/
vggt_long.py
File metadata and controls
699 lines (562 loc) · 29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
import numpy as np
import argparse
import os
import glob
import threading
import torch
from tqdm.auto import tqdm
import cv2
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
import gc
import sys
current_dir = os.path.dirname(os.path.abspath(__file__))
base_models_path = os.path.join(current_dir, 'base_models')
if base_models_path not in sys.path:
sys.path.append(base_models_path)
try:
import onnxruntime
except ImportError:
print("onnxruntime not found. Sky segmentation may not work.")
from LoopModels.LoopModel import LoopDetector
from LoopModelDBoW.retrieval.retrieval_dbow import RetrievalDBOW
from base_models.base_model import VGGTAdapter,Pi3Adapter,MapAnythingAdapter
import numpy as np
from loop_utils.sim3loop import Sim3LoopOptimizer
from loop_utils.sim3utils import *
from datetime import datetime
from PIL import Image
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import sys
from loop_utils.config_utils import load_config
from pathlib import Path
def remove_duplicates(data_list):
"""
data_list: [(67, (3386, 3406), 48, (2435, 2455)), ...]
"""
seen = {}
result = []
for item in data_list:
if item[0] == item[2]:
continue
key = (item[0], item[2])
if key not in seen.keys():
seen[key] = True
result.append(item)
return result
def extract_p2_k_matrix(calib_path):
"""from calib.txt get K (kitti)"""
calib_path = Path(calib_path)
if not calib_path.exists():
raise FileNotFoundError(f"Calibration file not found: {calib_path}")
with open(calib_path, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('P2:'):
values = line.split(':')[1].split()
values = [float(v) for v in values]
p2_matrix = np.array(values).reshape(3, 4)
k_matrix = p2_matrix[:3, :3]
return k_matrix, p2_matrix
raise ValueError("P2 not found in calibration file")
class LongSeqResult:
def __init__(self):
self.combined_extrinsics = []
self.combined_intrinsics = []
self.combined_depth_maps = []
self.combined_depth_confs = []
self.combined_world_points = []
self.combined_world_points_confs = []
self.all_camera_poses = []
self.all_camera_intrinsics = []
class VGGT_Long:
def __init__(self, image_dir, save_dir, config):
self.config = config
self.chunk_size = self.config['Model']['chunk_size']
self.overlap = self.config['Model']['overlap']
self.seed = 42
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
self.sky_mask = False
self.useDBoW = self.config['Model']['useDBoW']
self.img_dir = image_dir
self.img_list = None
self.output_dir = save_dir
self.result_unaligned_dir = os.path.join(save_dir, '_tmp_results_unaligned')
self.result_aligned_dir = os.path.join(save_dir, '_tmp_results_aligned')
self.result_loop_dir = os.path.join(save_dir, '_tmp_results_loop')
self.pcd_dir = os.path.join(save_dir, 'pcd')
os.makedirs(self.result_unaligned_dir, exist_ok=True)
os.makedirs(self.result_aligned_dir, exist_ok=True)
os.makedirs(self.result_loop_dir, exist_ok=True)
os.makedirs(self.pcd_dir, exist_ok=True)
self.all_camera_poses = []
self.all_camera_intrinsics = []
self.delete_temp_files = self.config['Model']['delete_temp_files']
if self.config['Weights']['model'] == 'VGGT':
self.model = VGGTAdapter(self.config)
elif self.config['Weights']['model'] == 'Pi3':
self.model = Pi3Adapter(self.config)
elif self.config['Weights']['model'] == 'Mapanything':
self.model = MapAnythingAdapter(self.config)
else:
raise ValueError(f"Unsupported model: {self.config['Weights']['model']}. ")
self.skyseg_session = None
self.chunk_indices = None # [(begin_idx, end_idx), ...]
self.loop_list = [] # e.g. [(1584, 139), ...]
self.loop_optimizer = Sim3LoopOptimizer(self.config)
self.sim3_list = [] # [(s [1,], R [3,3], T [3,]), ...]
self.loop_sim3_list = [] # [(chunk_idx_a, chunk_idx_b, s [1,], R [3,3], T [3,]), ...]
self.loop_predict_list = []
self.loop_enable = self.config['Model']['loop_enable']
if self.loop_enable:
if self.useDBoW:
self.retrieval = RetrievalDBOW(config=self.config)
else:
loop_info_save_path = os.path.join(save_dir, "loop_closures.txt")
self.loop_detector = LoopDetector(
image_dir=image_dir,
output=loop_info_save_path,
config=self.config
)
print('init done.')
def get_loop_pairs(self):
if self.useDBoW: # DBoW2
for frame_id, img_path in tqdm(enumerate(self.img_list)):
image_ori = np.array(Image.open(img_path))
if len(image_ori.shape) == 2:
# gray to rgb
image_ori = cv2.cvtColor(image_ori, cv2.COLOR_GRAY2RGB)
frame = image_ori # (height, width, 3)
frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
self.retrieval(frame, frame_id)
cands = self.retrieval.detect_loop(thresh=self.config['Loop']['DBoW']['thresh'],
num_repeat=self.config['Loop']['DBoW']['num_repeat'])
if cands is not None:
(i, j) = cands # e.g. cands = (812, 67)
self.retrieval.confirm_loop(i, j)
self.retrieval.found.clear()
self.loop_list.append(cands)
self.retrieval.save_up_to(frame_id)
else: # DNIO v2
self.loop_detector.run()
self.loop_list = self.loop_detector.get_loop_list()
def process_single_chunk(self, range_1, chunk_idx=None, range_2=None, is_loop=False):
start_idx, end_idx = range_1
chunk_image_paths = self.img_list[start_idx:end_idx]
if range_2 is not None:
start_idx, end_idx = range_2
chunk_image_paths += self.img_list[start_idx:end_idx]
predictions = self.model.infer_chunk(chunk_image_paths)
for key in predictions.keys():
if isinstance(predictions[key], torch.Tensor):
predictions[key] = predictions[key].cpu().numpy().squeeze(0)
# Save predictions to disk instead of keeping in memory
if is_loop:
save_dir = self.result_loop_dir
filename = f"loop_{range_1[0]}_{range_1[1]}_{range_2[0]}_{range_2[1]}.npy"
else:
if chunk_idx is None:
raise ValueError("chunk_idx must be provided when is_loop is False")
save_dir = self.result_unaligned_dir
filename = f"chunk_{chunk_idx}.npy"
save_path = os.path.join(save_dir, filename)
if not is_loop and range_2 is None:
extrinsics = predictions['extrinsic']
intrinsics = predictions['intrinsic']
chunk_range = self.chunk_indices[chunk_idx]
self.all_camera_poses.append((chunk_range, extrinsics))
self.all_camera_intrinsics.append((chunk_range, intrinsics))
predictions['depth'] = np.squeeze(predictions['depth'])
np.save(save_path, predictions)
return predictions if is_loop or range_2 is not None else None
def process_long_sequence(self):
if self.overlap >= self.chunk_size:
raise ValueError(f"[SETTING ERROR] Overlap ({self.overlap}) must be less than chunk size ({self.chunk_size})")
if len(self.img_list) <= self.chunk_size:
num_chunks = 1
self.chunk_indices = [(0, len(self.img_list))]
else:
step = self.chunk_size - self.overlap
num_chunks = (len(self.img_list) - self.overlap + step - 1) // step
self.chunk_indices = []
for i in range(num_chunks):
start_idx = i * step
end_idx = min(start_idx + self.chunk_size, len(self.img_list))
self.chunk_indices.append((start_idx, end_idx))
for chunk_idx in range(len(self.chunk_indices)):
print(f'[Progress]: {chunk_idx}/{len(self.chunk_indices)-1}')
self.process_single_chunk(self.chunk_indices[chunk_idx], chunk_idx=chunk_idx)
torch.cuda.empty_cache()
if self.loop_enable:
print('Loop SIM(3) estimating...')
loop_results = process_loop_list(self.chunk_indices,
self.loop_list,
half_window = int(self.config['Model']['loop_chunk_size'] / 2))
loop_results = remove_duplicates(loop_results)
print(loop_results)
# return e.g. (31, (1574, 1594), 2, (129, 149))
for item in loop_results:
single_chunk_predictions = self.process_single_chunk(item[1], range_2=item[3], is_loop=True)
self.loop_predict_list.append((item, single_chunk_predictions))
print(item)
print(
f"Processing {len(self.img_list)} images in {num_chunks} chunks of size {self.chunk_size} with {self.overlap} overlap")
del self.model # Save GPU Memory
torch.cuda.empty_cache()
print("Aligning all the chunks...")
for chunk_idx in range(len(self.chunk_indices)-1):
print(f"Aligning {chunk_idx} and {chunk_idx+1} (Total {len(self.chunk_indices)-1})")
chunk_data1 = np.load(os.path.join(self.result_unaligned_dir, f"chunk_{chunk_idx}.npy"), allow_pickle=True).item()
chunk_data2 = np.load(os.path.join(self.result_unaligned_dir, f"chunk_{chunk_idx+1}.npy"), allow_pickle=True).item()
point_map1 = chunk_data1['world_points'][-self.overlap:]
point_map2 = chunk_data2['world_points'][:self.overlap]
conf1 = chunk_data1['world_points_conf'][-self.overlap:]
conf2 = chunk_data2['world_points_conf'][:self.overlap]
mask = None
if chunk_data1["mask"] is not None:
mask1 = chunk_data1["mask"][-self.overlap:]
mask2 = chunk_data2["mask"][:self.overlap]
mask = mask1.squeeze() & mask2.squeeze()
if self.config['Model']['Pointcloud_Save'].get('use_conf_filter', True):
conf_threshold = min(np.median(conf1), np.median(conf2)) * 0.1
else:
conf_threshold = -1.0
s, R, t = weighted_align_point_maps(point_map1,
conf1,
point_map2,
conf2,
mask,
conf_threshold=conf_threshold,
config=self.config)
print("Estimated Scale:", s)
print("Estimated Rotation:\n", R)
print("Estimated Translation:", t)
self.sim3_list.append((s, R, t))
if self.loop_enable:
for item in self.loop_predict_list:
chunk_idx_a = item[0][0]
chunk_idx_b = item[0][2]
chunk_a_range = item[0][1]
chunk_b_range = item[0][3]
print('chunk_a align')
point_map_loop = item[1]['world_points'][:chunk_a_range[1] - chunk_a_range[0]]
conf_loop = item[1]['world_points_conf'][:chunk_a_range[1] - chunk_a_range[0]]
chunk_a_rela_begin = chunk_a_range[0] - self.chunk_indices[chunk_idx_a][0]
chunk_a_rela_end = chunk_a_rela_begin + chunk_a_range[1] - chunk_a_range[0]
print(self.chunk_indices[chunk_idx_a])
print(chunk_a_range)
print(chunk_a_rela_begin, chunk_a_rela_end)
chunk_data_a = np.load(os.path.join(self.result_unaligned_dir, f"chunk_{chunk_idx_a}.npy"), allow_pickle=True).item()
point_map_a = chunk_data_a['world_points'][chunk_a_rela_begin:chunk_a_rela_end]
conf_a = chunk_data_a['world_points_conf'][chunk_a_rela_begin:chunk_a_rela_end]
if self.config['Model']['Pointcloud_Save'].get('use_conf_filter', True):
conf_threshold = min(np.median(conf_a), np.median(conf_loop)) * 0.1
else:
conf_threshold = -1.0
mask = None
if item[1]['mask'] is not None:
mask_loop = item[1]['mask'][:chunk_a_range[1] - chunk_a_range[0]]
mask_a = chunk_data_a['mask'][chunk_a_rela_begin:chunk_a_rela_end]
mask = mask_loop.squeeze() & mask_a.squeeze()
s_a, R_a, t_a = weighted_align_point_maps(point_map_a,
conf_a,
point_map_loop,
conf_loop,
mask,
conf_threshold=conf_threshold,
config=self.config)
print("Estimated Scale:", s_a)
print("Estimated Rotation:\n", R_a)
print("Estimated Translation:", t_a)
print('chunk_a align')
point_map_loop = item[1]['world_points'][-chunk_b_range[1] + chunk_b_range[0]:]
conf_loop = item[1]['world_points_conf'][-chunk_b_range[1] + chunk_b_range[0]:]
chunk_b_rela_begin = chunk_b_range[0] - self.chunk_indices[chunk_idx_b][0]
chunk_b_rela_end = chunk_b_rela_begin + chunk_b_range[1] - chunk_b_range[0]
print(self.chunk_indices[chunk_idx_b])
print(chunk_b_range)
print(chunk_b_rela_begin, chunk_b_rela_end)
chunk_data_b = np.load(os.path.join(self.result_unaligned_dir, f"chunk_{chunk_idx_b}.npy"), allow_pickle=True).item()
point_map_b = chunk_data_b['world_points'][chunk_b_rela_begin:chunk_b_rela_end]
conf_b = chunk_data_b['world_points_conf'][chunk_b_rela_begin:chunk_b_rela_end]
if self.config['Model']['Pointcloud_Save'].get('use_conf_filter', True):
conf_threshold = min(np.median(conf_b), np.median(conf_loop)) * 0.1
else:
conf_threshold = -1.0
mask = None
if item[1]['mask'] is not None:
mask_loop = item[1]['mask'][-chunk_b_range[1] + chunk_b_range[0]:]
mask_b = chunk_data_b['mask'][chunk_b_rela_begin:chunk_b_rela_end]
mask = mask_loop.squeeze() & mask_b.squeeze()
s_b, R_b, t_b = weighted_align_point_maps(point_map_b,
conf_b,
point_map_loop,
conf_loop,
mask,
conf_threshold=conf_threshold,
config=self.config)
print("Estimated Scale:", s_b)
print("Estimated Rotation:\n", R_b)
print("Estimated Translation:", t_b)
print('a -> b SIM 3')
s_ab, R_ab, t_ab = compute_sim3_ab((s_a, R_a, t_a), (s_b, R_b, t_b))
print("Estimated Scale:", s_ab)
print("Estimated Rotation:\n", R_ab)
print("Estimated Translation:", t_ab)
self.loop_sim3_list.append((chunk_idx_a, chunk_idx_b, (s_ab, R_ab, t_ab)))
if self.loop_enable:
input_abs_poses = self.loop_optimizer.sequential_to_absolute_poses(self.sim3_list)
self.sim3_list = self.loop_optimizer.optimize(self.sim3_list, self.loop_sim3_list)
optimized_abs_poses = self.loop_optimizer.sequential_to_absolute_poses(self.sim3_list)
def extract_xyz(pose_tensor):
poses = pose_tensor.cpu().numpy()
return poses[:, 0], poses[:, 1], poses[:, 2]
x0, _, y0 = extract_xyz(input_abs_poses)
x1, _, y1 = extract_xyz(optimized_abs_poses)
# Visual in png format
plt.figure(figsize=(8, 6))
plt.plot(x0, y0, 'o--', alpha=0.45, label='Before Optimization')
plt.plot(x1, y1, 'o-', label='After Optimization')
for i, j, _ in self.loop_sim3_list:
plt.plot([x0[i], x0[j]], [y0[i], y0[j]], 'r--', alpha=0.25, label='Loop (Before)' if i == 5 else "")
plt.plot([x1[i], x1[j]], [y1[i], y1[j]], 'g-', alpha=0.35, label='Loop (After)' if i == 5 else "")
plt.gca().set_aspect('equal')
plt.title("Sim3 Loop Closure Optimization")
plt.xlabel("x")
plt.ylabel("z")
plt.legend()
plt.grid(True)
plt.axis("equal")
save_path = os.path.join(self.output_dir, 'sim3_opt_result.png')
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
print('Apply alignment')
self.sim3_list = accumulate_sim3_transforms(self.sim3_list)
for chunk_idx in range(len(self.chunk_indices) - 1):
print(f'Applying {chunk_idx + 1} -> {chunk_idx} (Total {len(self.chunk_indices) - 1})')
s, R, t = self.sim3_list[chunk_idx]
chunk_data = np.load(os.path.join(self.result_unaligned_dir, f"chunk_{chunk_idx + 1}.npy"),
allow_pickle=True).item()
chunk_data['world_points'] = apply_sim3_direct(chunk_data['world_points'], s, R, t)
aligned_path = os.path.join(self.result_aligned_dir, f"chunk_{chunk_idx + 1}.npy")
np.save(aligned_path, chunk_data)
if chunk_idx == 0:
chunk_data_first = np.load(os.path.join(self.result_unaligned_dir, f"chunk_0.npy"),
allow_pickle=True).item()
np.save(os.path.join(self.result_aligned_dir, "chunk_0.npy"), chunk_data_first)
points_first = chunk_data_first['world_points'].reshape(-1, 3)
colors_first = (chunk_data_first['images'].transpose(0, 2, 3, 1).reshape(-1, 3) * 255).astype(np.uint8)
confs_first = chunk_data_first['world_points_conf'].reshape(-1)
ply_path_first = os.path.join(self.pcd_dir, f'0_pcd.ply')
save_confident_pointcloud_batch(
points=points_first, # shape: (H, W, 3)
colors=colors_first, # shape: (H, W, 3)
confs=confs_first, # shape: (H, W)
output_path=ply_path_first,
conf_threshold=(np.mean(confs_first) * self.config['Model']['Pointcloud_Save']['conf_threshold_coef']
if self.config['Model']['Pointcloud_Save'].get('use_conf_filter', True) else -1.0),
sample_ratio=self.config['Model']['Pointcloud_Save']['sample_ratio']
)
aligned_chunk_data = np.load(os.path.join(self.result_aligned_dir, f"chunk_{chunk_idx+1}.npy"),
allow_pickle=True).item() if chunk_idx > 0 else chunk_data_first
points = aligned_chunk_data['world_points'].reshape(-1, 3)
colors = (aligned_chunk_data['images'].transpose(0, 2, 3, 1).reshape(-1, 3) * 255).astype(np.uint8)
confs = aligned_chunk_data['world_points_conf'].reshape(-1)
ply_path = os.path.join(self.pcd_dir, f'{chunk_idx + 1}_pcd.ply')
save_confident_pointcloud_batch(
points=points, # shape: (H, W, 3)
colors=colors, # shape: (H, W, 3)
confs=confs, # shape: (H, W)
output_path=ply_path,
conf_threshold=(np.mean(confs) * self.config['Model']['Pointcloud_Save']['conf_threshold_coef']
if self.config['Model']['Pointcloud_Save'].get('use_conf_filter', True) else -1.0),
sample_ratio=self.config['Model']['Pointcloud_Save']['sample_ratio']
)
self.save_camera_poses()
print('Done.')
def run(self):
print(f"Loading images from {self.img_dir}...")
self.img_list = sorted(glob.glob(os.path.join(self.img_dir, "*.jpg")) +
glob.glob(os.path.join(self.img_dir, "*.png")))
# print(self.img_list)
if len(self.img_list) == 0:
raise ValueError(f"[DIR EMPTY] No images found in {self.img_dir}!")
print(f"Found {len(self.img_list)} images")
if self.loop_enable:
self.get_loop_pairs()
if self.useDBoW:
self.retrieval.close() # Save CPU Memory
gc.collect()
else:
del self.loop_detector # Save GPU Memory
torch.cuda.empty_cache()
print('Loading model...')
self.model.load()
if self.config['Model']['calib']:
calib_path = Path(self.img_dir).parent / 'calib.txt'
k, p2_matrix = extract_p2_k_matrix(calib_path)
self.model.k = k
self.process_long_sequence()
def save_camera_poses(self):
'''
Save camera poses from all chunks to txt and ply files
- txt file: Each line contains a 4x4 C2W matrix flattened into 16 numbers
- ply file: Camera poses visualized as points with different colors for each chunk
'''
chunk_colors = [
[255, 0, 0], # Red
[0, 255, 0], # Green
[0, 0, 255], # Blue
[255, 255, 0], # Yellow
[255, 0, 255], # Magenta
[0, 255, 255], # Cyan
[128, 0, 0], # Dark Red
[0, 128, 0], # Dark Green
[0, 0, 128], # Dark Blue
[128, 128, 0], # Olive
]
print("Saving all camera poses to txt file...")
all_poses = [None] * len(self.img_list)
all_intrinsics = [None] * len(self.img_list)
first_chunk_range, first_chunk_extrinsics = self.all_camera_poses[0]
_, first_chunk_intrinsics = self.all_camera_intrinsics[0]
for i, idx in enumerate(range(first_chunk_range[0], first_chunk_range[1])):
c2w = first_chunk_extrinsics[i]
all_poses[idx] = c2w
if first_chunk_intrinsics is not None:
all_intrinsics[idx] = first_chunk_intrinsics[i]
for chunk_idx in range(1, len(self.all_camera_poses)):
chunk_range, chunk_extrinsics = self.all_camera_poses[chunk_idx]
_, chunk_intrinsics = self.all_camera_intrinsics[chunk_idx]
s, R, t = self.sim3_list[
chunk_idx - 1] # When call self.save_camera_poses(), all the sim3 are aligned to the first chunk.
S = np.eye(4)
S[:3, :3] = s * R
S[:3, 3] = t
for i, idx in enumerate(range(chunk_range[0], chunk_range[1])):
c2w = chunk_extrinsics[i] #
transformed_c2w = S @ c2w # Be aware of the left multiplication!
transformed_c2w[:3, :3] /= s # Normalize rotation
all_poses[idx] = transformed_c2w
if chunk_intrinsics is not None:
all_intrinsics[idx] = chunk_intrinsics[i]
poses_path = os.path.join(self.output_dir, 'camera_poses.txt')
with open(poses_path, 'w') as f:
for pose in all_poses:
flat_pose = pose.flatten()
f.write(' '.join([str(x) for x in flat_pose]) + '\n')
print(f"Camera poses saved to {poses_path}")
if all_intrinsics[0] is not None:
intrinsics_path = os.path.join(self.output_dir, 'intrinsic.txt')
with open(intrinsics_path, 'w') as f:
for intrinsic in all_intrinsics:
fx = intrinsic[0, 0]
fy = intrinsic[1, 1]
cx = intrinsic[0, 2]
cy = intrinsic[1, 2]
f.write(f'{fx} {fy} {cx} {cy}\n')
print(f"Camera intrinsics saved to {intrinsics_path}")
ply_path = os.path.join(self.output_dir, 'camera_poses.ply')
with open(ply_path, 'w') as f:
# Write PLY header
f.write('ply\n')
f.write('format ascii 1.0\n')
f.write(f'element vertex {len(all_poses)}\n')
f.write('property float x\n')
f.write('property float y\n')
f.write('property float z\n')
f.write('property uchar red\n')
f.write('property uchar green\n')
f.write('property uchar blue\n')
f.write('end_header\n')
color = chunk_colors[0]
for pose in all_poses:
position = pose[:3, 3]
f.write(f'{position[0]} {position[1]} {position[2]} {color[0]} {color[1]} {color[2]}\n')
print(f"Camera poses visualization saved to {ply_path}")
def close(self):
'''
Clean up temporary files and calculate reclaimed disk space.
This method deletes all temporary files generated during processing from three directories:
- Unaligned results
- Aligned results
- Loop results
~50 GiB for 4500-frame KITTI 00,
~35 GiB for 2700-frame KITTI 05,
or ~5 GiB for 300-frame short seq.
'''
if not self.delete_temp_files:
return
total_space = 0
print(f'Deleting the temp files under {self.result_unaligned_dir}')
for filename in os.listdir(self.result_unaligned_dir):
file_path = os.path.join(self.result_unaligned_dir, filename)
if os.path.isfile(file_path):
total_space += os.path.getsize(file_path)
os.remove(file_path)
print(f'Deleting the temp files under {self.result_aligned_dir}')
for filename in os.listdir(self.result_aligned_dir):
file_path = os.path.join(self.result_aligned_dir, filename)
if os.path.isfile(file_path):
total_space += os.path.getsize(file_path)
os.remove(file_path)
print(f'Deleting the temp files under {self.result_loop_dir}')
for filename in os.listdir(self.result_loop_dir):
file_path = os.path.join(self.result_loop_dir, filename)
if os.path.isfile(file_path):
total_space += os.path.getsize(file_path)
os.remove(file_path)
print('Deleting temp files done.')
print(f"Saved disk space: {total_space/1024/1024/1024:.4f} GiB")
import shutil
def copy_file(src_path, dst_dir):
try:
os.makedirs(dst_dir, exist_ok=True)
dst_path = os.path.join(dst_dir, os.path.basename(src_path))
shutil.copy2(src_path, dst_path)
print(f"config yaml file has been copied to: {dst_path}")
return dst_path
except FileNotFoundError:
print("File Not Found")
except PermissionError:
print("Permission Error")
except Exception as e:
print(f"Copy Error: {e}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='VGGT-Long')
parser.add_argument('--image_dir', type=str, required=True,
help='Image path')
parser.add_argument('--config', type=str, required=False, default='./configs/base_config.yaml',
help='config path')
args = parser.parse_args()
config = load_config(args.config)
image_dir = args.image_dir
path = image_dir.split("/")
current_datetime = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
exp_dir = './exps'
save_dir = os.path.join(
exp_dir, image_dir.replace("/", "_"), current_datetime
)
# save_dir = os.path.join(
# exp_dir, path[-3] + "_" + path[-2] + "_" + path[-1], current_datetime
# )
if not os.path.exists(save_dir):
os.makedirs(save_dir)
print(f'The exp will be saved under dir: {save_dir}')
copy_file(args.config, save_dir)
if config['Model']['align_method'] == 'numba':
warmup_numba()
vggt_long = VGGT_Long(image_dir, save_dir, config)
vggt_long.run()
vggt_long.close()
del vggt_long
torch.cuda.empty_cache()
gc.collect()
all_ply_path = os.path.join(save_dir, f'pcd/combined_pcd.ply')
input_dir = os.path.join(save_dir, f'pcd')
print("Saving all the point clouds")
merge_ply_files(input_dir, all_ply_path)
print('All done.')
sys.exit()