Skip to content

Commit 95a78ed

Browse files
committed
Update benchmark.py
1 parent 865612b commit 95a78ed

File tree

1 file changed

+131
-68
lines changed

1 file changed

+131
-68
lines changed

benchmark/benchmark.py

+131-68
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import yaml
1212
import glob
1313
import argparse
14+
import subprocess
1415
from pathlib import Path
1516

1617
import pandas
@@ -21,19 +22,23 @@
2122
logger = logging.getLogger("benchmark")
2223

2324
# Global settings
25+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
2426
DATA_DIR = "/data"
25-
CONFIGS_DIR = "/home/chutsu/projects/proto/benchmark/configs"
27+
RESOURCE_DIR = f"{SCRIPT_DIR}/resource"
28+
29+
################################################################################
30+
# UTILS
31+
################################################################################
2632

2733

2834
def docker_command(docker_image, command):
2935
""" Form docker command """
3036
return f"""\
31-
xhost +local:docker; \
3237
docker run -e DISPLAY \
38+
--privileged \
3339
-v /tmp:/tmp \
3440
-v {DATA_DIR}:{DATA_DIR} \
35-
-v {CONFIGS_DIR}:/home/docker/configs \
36-
--network="host" \
41+
-v {RESOURCE_DIR}:/home/docker/resource \
3742
-it \
3843
--rm \
3944
{docker_image} \
@@ -93,7 +98,12 @@ def dataset_adjust_timestamps(src_dir, dst_dir, dataset_name, calib_file):
9398
imu_df.to_csv(imu_path, index=False)
9499

95100

96-
def run_orbslam3(mode, ds_path, run_name, calib_file, res_dir, **kwargs):
101+
################################################################################
102+
# ORBSLAM3
103+
################################################################################
104+
105+
106+
def run_orbslam3(mode, ds_path, run_name, config_path, res_dir, **kwargs):
97107
""" Run ORBSLAM """
98108
# Setup
99109
retries = kwargs.get("retries", 3)
@@ -107,6 +117,14 @@ def run_orbslam3(mode, ds_path, run_name, calib_file, res_dir, **kwargs):
107117
orbslam3_path = "/home/docker/ORB_SLAM3"
108118
docker_image = "benchmark/orbslam3"
109119

120+
# Check dataset path
121+
if os.path.exists(ds_path) is False:
122+
raise RuntimeError(f"Dataset [{ds_path}] does not exist")
123+
124+
# Check calib file
125+
if os.path.exists(config_path) is False:
126+
raise RuntimeError(f"Calibration file [{config_path}] does not exist")
127+
110128
# Create a timestamp file for ORBSLAM3
111129
# -- Get timestamps from camera images
112130
timestamps = {}
@@ -120,6 +138,14 @@ def run_orbslam3(mode, ds_path, run_name, calib_file, res_dir, **kwargs):
120138
else:
121139
timestamps[timestamp] += 1
122140

141+
# -- Check if number of cameras found
142+
if num_cams == 0:
143+
raise RuntimeError(f"Found 0 cameras in [{ds_path}]?")
144+
145+
# -- Check number of timestamps
146+
if len(timestamps) == 0:
147+
raise RuntimeError(f"Found 0 timestamps in [{ds_path}]?")
148+
123149
# -- Write out timestamps to file
124150
timestamps_file = open(timestamps_path, "w")
125151
for timestamp, count in timestamps.items():
@@ -132,81 +158,109 @@ def run_orbslam3(mode, ds_path, run_name, calib_file, res_dir, **kwargs):
132158
run_cmd = f"""\
133159
{orbslam3_path}/Examples/Monocular/mono_euroc \
134160
{orbslam3_path}/Vocabulary/ORBvoc.txt \
135-
{calib_file} \
161+
{config_path} \
136162
{ds_path} \
137163
{timestamps_path} \
138164
dataset-{uuid_str}-{run_name}_mono"""
139165
cmd = docker_command(docker_image, run_cmd)
140166
f_file = f"f_dataset-{uuid_str}-{run_name}_mono.txt"
141167
kf_file = f"kf_dataset-{uuid_str}-{run_name}_mono.txt"
142168

143-
# elif mode == "stereo":
144-
# run_cmd = f"""\
145-
# {orbslam3_path}/Examples/Stereo/stereo_euroc \
146-
# {orbslam3_path}/Vocabulary/ORBvoc.txt \
147-
# {calib_file} \
148-
# {ds_path} \
149-
# {timestamps_path} \
150-
# dataset-{uuid_str}-{run_name}_stereo"""
151-
# cmd = docker_command(docker_image, run_cmd)
152-
# f_file = f"f_dataset-{uuid_str}-{run_name}_stereo.txt"
153-
# kf_file = f"kf_dataset-{uuid_str}-{run_name}_stereo.txt"
154-
#
155-
# elif mode == "stereo_imu":
156-
# run_cmd = f"""\
157-
# {orbslam3_path}/Examples/Stereo-Inertial/stereo_inertial_euroc \
158-
# {orbslam3_path}/Vocabulary/ORBvoc.txt \
159-
# {calib_file} \
160-
# {ds_path} \
161-
# {timestamps_path} \
162-
# dataset-{uuid_str}-{run_name}_stereo_imu"""
163-
# cmd = docker_command(docker_image, run_cmd)
164-
# f_file = f"f_dataset-{uuid_str}-{run_name}_stereo_imu.txt"
165-
# kf_file = f"kf_dataset-{uuid_str}-{run_name}_stereo_imu.txt"
166-
# else:
167-
# logger.error("ERROR! ORBSLAM3 [%s] mode not supported!", mode)
168-
# sys.exit(-1)
169-
#
170-
# # Check if results already exists
171-
# f_dst = Path(res_dir, f_file.replace(f"-{uuid_str}", ""))
172-
# kf_dst = Path(res_dir, kf_file.replace(f"-{uuid_str}", ""))
173-
# if os.path.exists(f_dst) or os.path.exists(kf_dst):
174-
# return True
169+
elif mode == "stereo":
170+
run_cmd = f"""\
171+
{orbslam3_path}/Examples/Stereo/stereo_euroc \
172+
{orbslam3_path}/Vocabulary/ORBvoc.txt \
173+
{config_path} \
174+
{ds_path} \
175+
{timestamps_path} \
176+
dataset-{uuid_str}-{run_name}_stereo"""
177+
cmd = docker_command(docker_image, run_cmd)
178+
f_file = f"f_dataset-{uuid_str}-{run_name}_stereo.txt"
179+
kf_file = f"kf_dataset-{uuid_str}-{run_name}_stereo.txt"
180+
181+
elif mode == "stereo_imu":
182+
run_cmd = f"""\
183+
{orbslam3_path}/Examples/Stereo-Inertial/stereo_inertial_euroc \
184+
{orbslam3_path}/Vocabulary/ORBvoc.txt \
185+
{config_path} \
186+
{ds_path} \
187+
{timestamps_path} \
188+
dataset-{uuid_str}-{run_name}_stereo_imu"""
189+
cmd = docker_command(docker_image, run_cmd)
190+
f_file = f"f_dataset-{uuid_str}-{run_name}_stereo_imu.txt"
191+
kf_file = f"kf_dataset-{uuid_str}-{run_name}_stereo_imu.txt"
192+
else:
193+
logger.error("ERROR! ORBSLAM3 [%s] mode not supported!", mode)
194+
sys.exit(-1)
195+
196+
# Check if results already exists
197+
f_dst = Path(res_dir, f_file.replace(f"-{uuid_str}", ""))
198+
kf_dst = Path(res_dir, kf_file.replace(f"-{uuid_str}", ""))
199+
if os.path.exists(f_dst) or os.path.exists(kf_dst):
200+
return True
175201

176202
# Run
177-
# for _ in range(retries):
178-
# # Run ORBSLAM3
179-
# time.sleep(2)
180-
# # print(f"{cmd}")
181-
# os.system(cmd)
182-
#
183-
# # Check if result files exists
184-
# if os.path.exists(f_file) is False:
185-
# logger.error("ERROR! [%s] DOES NOT EXIST! RETRYING!", f_file)
186-
# continue
187-
# if os.path.exists(kf_file) is False:
188-
# logger.error("ERROR! [%s] DOES NOT EXIST! RETRYING!", kf_file)
189-
# continue
190-
#
191-
# # Move results
192-
# os.system(f"mv {f_file} {f_dst}")
193-
# os.system(f"rm {kf_file}") # Remove (not useful for evaluation)
194-
# return True
195-
#
196-
# # Failed to run orbslam
197-
# logger.error("FAILED TO RUN ORBSLAM")
198-
# logger.error("COMMAND:")
199-
# logger.error("%s\n", cmd)
200-
# return False
203+
for _ in range(retries):
204+
# Run ORBSLAM3
205+
time.sleep(2)
206+
result = subprocess.run(cmd.split())
207+
208+
# Check if result files exists
209+
if os.path.exists(f_file) is False:
210+
logger.error("ERROR! [%s] DOES NOT EXIST! RETRYING!", f_file)
211+
continue
212+
if os.path.exists(kf_file) is False:
213+
logger.error("ERROR! [%s] DOES NOT EXIST! RETRYING!", kf_file)
214+
continue
215+
216+
# Move results
217+
os.system(f"mv {f_file} {f_dst}")
218+
os.system(f"rm {kf_file}") # Remove (not useful for evaluation)
219+
return True
201220

221+
# Failed to run orbslam
222+
logger.error("FAILED TO RUN ORBSLAM")
223+
logger.error("COMMAND:")
224+
logger.error("%s\n", cmd)
225+
return False
202226

203-
def run_vins_fusion(mode, ds_path, run_name, calib_file, output, **kwargs):
227+
228+
def run_orbslam3_euroc(run_name, mode, config_path, res_dir, **kwargs):
229+
"""Run OBSLAM3 on EuRoC dataset"""
230+
data_dir = kwargs.get("data_dir", "/data/euroc")
231+
sequences = kwargs.get("sequences", [
232+
"MH_01",
233+
"MH_02",
234+
"MH_03",
235+
"MH_04",
236+
"MH_05",
237+
"V1_01",
238+
"V1_02",
239+
"V1_03",
240+
"V2_01",
241+
"V2_02",
242+
"V2_03",
243+
])
244+
for seq in sequences:
245+
run_orbslam3(mode, f"{data_dir}/{seq}", run_name, config_path, res_dir)
246+
247+
248+
################################################################################
249+
# VINS-FUSION
250+
################################################################################
251+
252+
253+
def run_vins_fusion(mode, ds_path, run_name, config_path, output, **kwargs):
204254
""" Run VINS-Fusion """
205255
# Setup
206256
retries = kwargs.get("retries", 3)
207257
docker_image = kwargs.get("docker_image", "benchmark/vins-fusion")
208258
uuid_str = str(uuid.uuid4())
209259

260+
# Check dataset path
261+
if os.path.exists(ds_path) is False:
262+
raise RuntimeError(f"Dataset [{ds_path}] does not exist")
263+
210264
# Run VINS-Fusion
211265
ds_dir = os.path.dirname(ds_path)
212266
est_path = f"{ds_dir}/vins-fusion-estimation-{uuid_str}.bag"
@@ -217,7 +271,7 @@ def run_vins_fusion(mode, ds_path, run_name, calib_file, output, **kwargs):
217271
roslaunch configs/vins-fusion/vins-fusion.launch \
218272
rosbag_input_path:={ds_path} \
219273
rosbag_output_path:={est_path} \
220-
config_file:={calib_file}"""
274+
config_file:={config_path}"""
221275
cmd = docker_command(docker_image, run_cmd)
222276

223277
else:
@@ -247,7 +301,12 @@ def run_vins_fusion(mode, ds_path, run_name, calib_file, output, **kwargs):
247301
return False
248302

249303

250-
def run_okvis(mode, ds_path, run_name, calib_file, output, **kwargs):
304+
################################################################################
305+
# OKVIS
306+
################################################################################
307+
308+
309+
def run_okvis(mode, ds_path, run_name, config_path, output, **kwargs):
251310
""" Run OKVIS """
252311
# Setup
253312
retries = kwargs.get("retries", 3)
@@ -258,7 +317,7 @@ def run_okvis(mode, ds_path, run_name, calib_file, output, **kwargs):
258317
ds_dir = os.path.dirname(ds_path)
259318
cmd = ""
260319
if mode in ["stereo_imu"]:
261-
run_cmd = f"okvis_app_synchronous {calib_file} {ds_path}"
320+
run_cmd = f"okvis_app_synchronous {config_path} {ds_path}"
262321
cmd = docker_command(docker_image, run_cmd)
263322

264323
else:
@@ -275,12 +334,16 @@ def run_okvis(mode, ds_path, run_name, calib_file, output, **kwargs):
275334
return True
276335

277336
# Failed to run orbslam
278-
logger.error("FAILED TO RUN VINS-Fusion")
337+
logger.error("FAILED TO RUN OKVIS")
279338
logger.error("COMMAND:")
280339
logger.error("%s\n", cmd)
281340
return False
282341

283342

343+
################################################################################
344+
# MAIN
345+
################################################################################
346+
284347
if __name__ == "__main__":
285348
# # Parse command line arguments
286349
# prog_name = "benchmark"
@@ -313,6 +376,6 @@ def run_okvis(mode, ds_path, run_name, calib_file, output, **kwargs):
313376
mode = "mono"
314377
dataset = "/data/euroc/MH_01"
315378
run_name = "euroc-mh01-mono"
316-
config = "./configs/orbslam3/euroc/euroc-mono.yaml"
379+
config = "./resource/configs/orbslam3/euroc/euroc-mono.yaml"
317380
output = "/data/orbslam3-exp"
318381
run_orbslam3(mode, dataset, run_name, config, output)

0 commit comments

Comments
 (0)