-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlarge_dataset_traversal.py
207 lines (164 loc) · 7.79 KB
/
large_dataset_traversal.py
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
from object_memory import *
import ast, pickle, shutil, time
import psutil, os
from tqdm import tqdm
import pdb
@dataclass
class LocalArgs:
"""
Class to hold local configuration arguments.
"""
lora_path: str='models/vit_finegrained_5x40_procthor.pt'
test_folder_path: str='/scratch/aneesh.chavan/8room/8-room-v1/1/'
device: str='cuda'
sam_checkpoint_path: str = '/scratch/aneesh.chavan/sam_vit_h_4b8939.pth'
ram_pretrained_path: str = '/scratch/aneesh.chavan/ram_swin_large_14m.pth'
sampling_period: int = 20
downsampling_rate: int = 5 # downsample points every these many frames
save_dir: str = "/scratch/aneesh.chavan/results/trav/"
start_file_index: int = 1
last_file_index: int = 400 # test with no noise also
rot_correction: float = 0.0 # keep as 30 for 8-room-new
look_around_range: int = 1 # number of sucessive frames to consider at every frame
save_individual_objects: bool = True
down_sample_voxel_size: float = 0.01
add_pose_noise: bool = True
if __name__=="__main__":
start_time = time.time()
largs = tyro.cli(LocalArgs, description=__doc__)
print(largs)
# creating save dir
os.makedirs(largs.save_dir, exist_ok=True)
print(f"Created save directory {largs.save_dir}")
files = os.listdir(os.path.join(largs.test_folder_path, "rgb"))
num_files = len(files)
print(f"We have {num_files} files")
print("\nBegin Memory Initialization")
mem = ObjectMemory(device = largs.device,
ram_pretrained_path=largs.ram_pretrained_path,
sam_checkpoint_path = largs.sam_checkpoint_path,
lora_path=largs.lora_path)
print("Memory Init'ed\n")
if largs.last_file_index == -1:
largs.last_file_index = num_files
frame_counter = 0
for cur_frame in tqdm(range(largs.start_file_index, largs.last_file_index + 1, largs.sampling_period), total=(largs.last_file_index-largs.start_file_index)//largs.sampling_period):
for i in range(cur_frame, min(largs.last_file_index + 1, cur_frame + largs.look_around_range + 1)):
print(f"\n\tSeeing image {i} currently")
image_file_path = os.path.join(largs.test_folder_path,
f"rgb/{i}.png")
depth_file_path = os.path.join(largs.test_folder_path,
f"depth/{i}.npy")
pose_file_path = os.path.join(largs.test_folder_path,
f"pose/{i}.txt")
with open(pose_file_path, 'r') as file:
pose_dict = file.read()
pose_dict = ast.literal_eval(pose_dict)
pose_dict = {
"position": {
"x": pose_dict[0]['x'],
"y": pose_dict[0]['y'],
"z": pose_dict[0]['z']
},
"rotation": {
"x": pose_dict[1]['x'] + largs.rot_correction,
"y": pose_dict[1]['y'],
"z": pose_dict[1]['z']
}
}
q = Rotation.from_euler('xyz', [r for _, r in pose_dict["rotation"].items()], degrees=True).as_quat()
t = np.array([x for _, x in pose_dict["position"].items()])
pose = np.concatenate([t, q])
mem.process_image(testname=f"view%d" % i,
image_path = image_file_path,
depth_image_path = depth_file_path,
pose=pose,
verbose=False, add_noise=largs.add_pose_noise)
pid = psutil.Process()
memory_info = pid.memory_info()
memory_info_GBs = memory_info.rss / (1e3 ** 3)
print(f"Memory usage: {memory_info_GBs:.3f} GB")
cuda_memory_stats = torch.cuda.memory_stats()
max_cuda_memory_GBs = int(cuda_memory_stats["allocated_bytes.all.peak"]) / (1e3 ** 3)
print(f"Max GPU memory usage: {max_cuda_memory_GBs:.3f} GB")
print("\t ----------------")
if frame_counter % largs.downsampling_rate == 0:
if largs.down_sample_voxel_size > 0:
print(f"Downsampling at {frame_counter} frame voxel size as {largs.down_sample_voxel_size}")
mem.downsample_all_objects(voxel_size=largs.down_sample_voxel_size)
mem.remove_object_floors()
frame_counter += 1
# begin debug
pcd_list = []
for info in mem.memory:
object_pcd = info.pcd
pcd_list.append(object_pcd)
combined_pcd = o3d.geometry.PointCloud()
for bhencho in range(len(pcd_list)):
pcd_np = pcd_list[bhencho]
pcd_vec = o3d.utility.Vector3dVector(pcd_np.T)
pcd = o3d.geometry.PointCloud()
pcd.points = pcd_vec
pcd.paint_uniform_color(np.random.rand(3))
combined_pcd += pcd
save_path = os.path.join(largs.save_dir,
f"/home2/aneesh.chavan/Change_detection/temp/{i}.pcd")
o3d.io.write_point_cloud(save_path, combined_pcd)
print("Memory's pointcloud saved to", save_path)
pdb.set_trace()
# end debug
if largs.down_sample_voxel_size > 0:
print(f"Downsampling using voxel size as {largs.down_sample_voxel_size}")
mem.downsample_all_objects(voxel_size=largs.down_sample_voxel_size)
end_time = time.time()
print(f"Traversal completed in {end_time - start_time} seconds")
pcd_list = []
for info in mem.memory:
object_pcd = info.pcd
pcd_list.append(object_pcd)
combined_pcd = o3d.geometry.PointCloud()
if largs.save_individual_objects:
individual_mem_save_dir = os.path.join(largs.save_dir,
f"ind_mems_{largs.start_file_index}_{largs.last_file_index}_{largs.sampling_period}_{largs.look_around_range}")
os.makedirs(individual_mem_save_dir, exist_ok=True)
for i in range(len(pcd_list)):
pcd_np = pcd_list[i]
pcd_vec = o3d.utility.Vector3dVector(pcd_np.T)
pcd = o3d.geometry.PointCloud()
pcd.points = pcd_vec
if largs.save_individual_objects:
cur_save_path = os.path.join(individual_mem_save_dir,
f"memory_{i}.pcd")
o3d.io.write_point_cloud(cur_save_path, pcd)
print(f"{i} pointcloud saved to", cur_save_path)
combined_pcd += pcd
save_path = os.path.join(largs.save_dir,
f"normal_mem_{largs.start_file_index}_{largs.last_file_index}_{largs.sampling_period}_{largs.look_around_range}.pcd")
o3d.io.write_point_cloud(save_path, combined_pcd)
print("Memory's pointcloud saved to", save_path)
# consolidate and check
mem.consolidate_memory(verbose=True)
pcd_list = []
for info in mem.memory:
object_pcd = info.pcd
pcd_list.append(object_pcd)
for i in range(len(pcd_list)):
pcd_np = pcd_list[i]
pcd_vec = o3d.utility.Vector3dVector(pcd_np.T)
pcd = o3d.geometry.PointCloud()
pcd.points = pcd_vec
if largs.save_individual_objects:
cur_save_path = os.path.join(individual_mem_save_dir,
f"cons_{i}.pcd")
o3d.io.write_point_cloud(cur_save_path, pcd)
print(f"{i} pointcloud saved to", cur_save_path)
save_path = os.path.join(largs.save_dir,
f"mem_{largs.start_file_index}_{largs.last_file_index}_{largs.sampling_period}_{largs.look_around_range}.pcd")
o3d.io.write_point_cloud(save_path, combined_pcd)
print("Memory's pointcloud saved to", save_path)
save_path = os.path.join(largs.save_dir,
f"/home2/aneesh.chavan/Change_detection/temp/cons_{i}.pcd")
o3d.io.write_point_cloud(save_path, combined_pcd)
print("Memory's pointcloud saved to", save_path)
print("\n\n\t---------------------")
mem.view_memory()