-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_pipeline.py
257 lines (212 loc) · 12.8 KB
/
run_pipeline.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
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
import os
from generate_image_poses import ImagePoseGenerator
from reconstructor import Reconstruction
from localizer import CameraLocalization
from evaluator import Evaluation
import time
import shutil
import warnings
class ReconstructionPipeline:
def __init__(self,
data_path,
output_path,
source_images_path,
initial_models_path,
image_poses_file_name,
experiment_name,
extractor_matchers,
gps_error=5.0,
use_previous_as_ref=False
):
self.data_path = data_path
self.output_path = os.path.join(output_path, experiment_name)
self.source_images_path = source_images_path
self.initial_models_path = initial_models_path
self.image_poses_file_name = image_poses_file_name
self.extractor_matchers = extractor_matchers
self.plot = True
self.gps_error = gps_error
self.use_previous_as_ref = use_previous_as_ref
subfolders = next(os.walk(self.data_path))[1]
self.subfolders = sorted(subfolders)
def build_inital_models(self):
for idx, subfolder in enumerate(self.subfolders):
start_time = time.time()
print('--------------------Intial Reconstruction--------------------')
print(f"Running intial reconstruction for subfolder {subfolder}...\n")
output_path = os.path.join(self.initial_models_path, subfolder)
data_path = os.path.join(self.data_path, subfolder)
source_images_path = os.path.join(self.source_images_path, subfolder, 'RAW/JPEG')
if not os.path.isfile(os.path.join(output_path, 'rec_done.txt')):
if idx == 0:
print("Running reconstructor ground truth model ...\n")
reconstructor = Reconstruction(data_path=data_path,
output_path=output_path,
image_poses_file_name=self.image_poses_file_name,
output_model_name='sparse/gt',
source_images_path=source_images_path,
error=0.0)
reconstructor.run()
else:
print(f"Running reconstructor temporal model {idx} ...\n")
reconstructor = Reconstruction(data_path=data_path,
output_path=output_path,
image_poses_file_name=self.image_poses_file_name,
output_model_name='sparse/noisy',
source_images_path=source_images_path,
error=self.gps_error)
reconstructor.run()
# done flag
with open(os.path.join(output_path, 'rec_done.txt'), 'w') as f:
f.write('done')
else:
print(f'Initial model of {subfolder} already exists\n')
end_time = time.time()
run_time = end_time - start_time
print(f"Initial Reconstruction Runtime for {subfolder}: {run_time}\n")
print('====================Intial Reconstruction Done====================\n')
def _localize_cameras(self, extractor, matcher):
print(f'==========Start localization with {extractor} / {matcher}==========\n')
previous_data_ref_path = None
previous_reconstruction_ref_path = None
identifier = extractor if extractor else matcher
for idx, subfolder in enumerate(self.subfolders):
start_time = time.time()
print(f'-----------------{identifier} Localization-----------------')
print(f"Running localization for subfolder {subfolder}...")
output_path = os.path.join(self.output_path, identifier, subfolder)
data_temp_path = os.path.join(self.data_path, subfolder)
reconstruction_temp_path = os.path.join(self.initial_models_path, subfolder, 'sparse/noisy')
if self.use_previous_as_ref and previous_reconstruction_ref_path is not None:
data_ref_path = previous_data_ref_path
reconstruction_ref_path = previous_reconstruction_ref_path
else:
data_ref_path = os.path.join(self.data_path, self.subfolders[0])
reconstruction_ref_path = os.path.join(self.output_path, identifier, self.subfolders[0], 'sparse/corrected')
if idx == 0:
if not os.path.isfile(os.path.join(output_path, 'loc_done.txt')):
reconstruction_temp_path = os.path.join(self.initial_models_path, subfolder, 'sparse/gt')
shutil.rmtree(reconstruction_ref_path, ignore_errors=True) # Remove the existing destination folder if it exists
shutil.copytree(reconstruction_temp_path, reconstruction_ref_path) # Copy the entire folder from source to destination
# done flag
with open(os.path.join(output_path, 'loc_done.txt'), 'w') as f:
f.write('done')
previous_data_ref_path = data_ref_path
previous_reconstruction_ref_path = reconstruction_ref_path
else:
if os.path.isfile(os.path.join(output_path, 'loc_failed.txt')):
if self.use_previous_as_ref:
print(f'Localization failed, abort localization for current & subsequent subfolders\n')
break
else:
print(f'Localization failed, abort localization for current subfolder\n')
continue
if not os.path.isfile(os.path.join(output_path, 'loc_done.txt')):
localizer = CameraLocalization(output_path=output_path,
images_ref_path=os.path.join(data_ref_path, 'images4reconstruction'),
images_temp_path=os.path.join(data_temp_path, 'images4reconstruction'),
reconstruction_ref_path=reconstruction_ref_path,
reconstruction_temp_path=reconstruction_temp_path,
image_poses_file_name=self.image_poses_file_name,
extractor=extractor,
matcher=matcher,
gps_noise=self.gps_error,
plotting=True)
localizer.run()
if localizer.is_successful is False:
# abort localization for subsequent subfolders if alignment failed. not enough inliers have been found
with open(os.path.join(output_path, 'loc_failed.txt'), 'w') as f:
f.write('failed')
if self.use_previous_as_ref:
print(f'Localization failed, abort localization for current & subsequent subfolders\n')
break
else:
print(f'Localization failed, abort localization for current subfolder\n')
continue
else:
# done flag
with open(os.path.join(output_path, 'loc_done.txt'), 'w') as f:
f.write('done')
else:
print(f'Localization for {subfolder} has already been done\n')
previous_data_ref_path = data_temp_path
previous_reconstruction_ref_path = os.path.join(output_path, 'sparse/corrected')
end_time = time.time()
run_time = end_time - start_time
print(f"{identifier} Localization Runtime for {subfolder}: {run_time}\n")
print(f'===================={identifier} Localization Done====================\n')
def localize_cameras(self):
for extractor_matcher in self.extractor_matchers:
extractor, matcher = extractor_matcher
self._localize_cameras(extractor, matcher)
def _evalate_localization(self, extractor, matcher, translation_error_thres, rotation_error_thres, ground_dist_thres):
print(f'==========Start evaluation for {extractor} / {matcher}==========\n')
identifier = extractor if extractor else matcher
for idx, subfolder in enumerate(self.subfolders):
start_time = time.time()
print(f'-----------------{identifier} Evaluation-----------------')
print(f"Running evaulation for subfolder {subfolder}...")
output_path = os.path.join(self.output_path, identifier, subfolder)
data_gt_path = os.path.join(self.data_path, subfolder)
reconstruction_path = os.path.join(output_path, 'sparse/corrected')
if os.path.isfile(os.path.join(output_path, 'loc_failed.txt')):
if self.use_previous_as_ref:
print(f'No localization found, abort evaluation for current & subsequent subfolders\n')
break
else:
print(f'No localization found, abort evaluation for current subfolders\n')
continue
if not os.path.isfile(os.path.join(output_path, 'eval_done.txt')):
print('-----------------corrected_aligned-----------------')
evaluator = Evaluation(data_gt_path=data_gt_path,
output_path=output_path,
reconstruction_path=reconstruction_path,
image_poses_file_name=self.image_poses_file_name,
translation_error_thres=translation_error_thres,
rotation_error_thres=rotation_error_thres,
ground_dist_thres=ground_dist_thres)
evaluator.run()
# done flag
with open(os.path.join(output_path, 'eval_done.txt'), 'w') as f:
f.write('done')
else:
print(f'Evaluation for {subfolder} has already been done\n')
end_time = time.time()
run_time = end_time - start_time
print(f"{identifier} Evaulation Runtime for {subfolder}: {run_time}\n")
print(f'===================={identifier} Evaluation Done====================\n')
def evalate_localization(self, translation_error_thres, rotation_error_thres, ground_dist_thres):
for extractor_matcher in self.extractor_matchers:
extractor, matcher = extractor_matcher
self._evalate_localization(extractor, matcher, translation_error_thres, rotation_error_thres, ground_dist_thres)
if __name__ == "__main__":
warnings.filterwarnings("ignore")
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
data_path = os.path.join(base_dir, 'dataset/crop/exp_pipeline')
output_path = os.path.join(base_dir, 'crop_alignment/output')
# TODO: Change the path to match the paths of the extracted downloaded dataset
source_images_path = '/path/to/Wheat_2019_images'
experiment_name = 'exp_pipeline'
initial_models_path = os.path.join(output_path, experiment_name, 'initial_models')
image_poses_file_name = 'image_poses.txt'
extractor_matchers = [
['sift', 'NN-ratio'],
['superpoint_aachen', 'superglue'],
[None, 'loftr'],
[None, 'loftr_23_0.5'], # retrained LoFTR without height change
[None, 'loftr_23_0.5_hc'], # retrained LoFTR with height change
]
pipeline = ReconstructionPipeline(data_path=data_path,
output_path=output_path,
source_images_path=source_images_path,
initial_models_path=initial_models_path,
image_poses_file_name=image_poses_file_name,
experiment_name=experiment_name,
extractor_matchers=extractor_matchers,
use_previous_as_ref=True
)
pipeline.build_inital_models()
pipeline.localize_cameras()
pipeline.evalate_localization(translation_error_thres=1.0,
rotation_error_thres=3.0,
ground_dist_thres=1.0)