Skip to content

Commit

Permalink
multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ducha-aiki committed Sep 9, 2021
1 parent b21b050 commit 7fd64f1
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 91 deletions.
2 changes: 1 addition & 1 deletion compute_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def main(cfg):
# Feature Matching
print('Computing matches')
num_cores = cfg.num_opencv_threads if cfg.num_opencv_threads > 0 else int(
len(os.sched_getaffinity(0)) * 0.9)
len(os.sched_getaffinity(0)) * 0.75)
if WITH_FAISS:
num_cores = min(4, num_cores)
result = Parallel(n_jobs=num_cores)(
Expand Down
9 changes: 6 additions & 3 deletions compute_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def main(cfg):
if os.path.exists(get_geom_file(cfg)):
print(' -- already exists, skipping model computation')
return

try:
pairwise_keypoints = cfg.method_dict['config_common']['pairwise_keypoints']
except:
pairwise_keypoints = False
# Get data directory
keypoints_dict = load_h5(get_kp_file(cfg))

Expand Down Expand Up @@ -160,11 +163,11 @@ def main(cfg):
scale_dict = defaultdict(list)

random.shuffle(pairs_per_th['0.0'])
if cfg.pairwise_keypoints: # picks keypoints per pair
if pairwise_keypoints: # picks keypoints per pair
result = Parallel(n_jobs=num_cores)(delayed(compute_model)(
cfg, np.asarray(matches_dict[pair]),
np.asarray(keypoints_dict[pair.split('-')[0]+'-'+pair.split('-')[1]]),
np.asarray(keypoints_dict[pair.split('-')[1]+'-'+pair.split('-')[0]]]),
np.asarray(keypoints_dict[pair.split('-')[1]+'-'+pair.split('-')[0]]),
calib_dict[pair.split(
'-')[0]], calib_dict[pair.split('-')[1]], images_list[
image_names.index(pair.split('-')[0])], images_list[
Expand Down
10 changes: 7 additions & 3 deletions compute_stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def main(cfg):

# Get data directory
data_dir = get_data_path(cfg)
try:
pairwise_keypoints = cfg.method_dict['config_common']['pairwise_keypoints']
except:
pairwise_keypoints = False

# Load pre-computed pairs with the new visibility criteria
pairs_per_th = get_pairs_per_threshold(data_dir)
Expand Down Expand Up @@ -90,15 +94,15 @@ def main(cfg):
print('Compute stereo metrics for all pairs')
#num_cores = int(multiprocessing.cpu_count() * 0.9)
num_cores = int(len(os.sched_getaffinity(0)) * 0.9)
if cfg.pairwise_keypoints: # picks keypoints per pair
if pairwise_keypoints: # picks keypoints per pair
result = Parallel(n_jobs=num_cores)(delayed(compute_stereo_metrics_from_E)(
images_list[image_names.index(pair.split('-')[0])], images_list[
image_names.index(pair.split('-')[1])],
depth_maps_list[image_names.index(pair.split('-')[0])] if cfg.
dataset != 'googleurban' else None, depth_maps_list[image_names.index(
pair.split('-')[1])] if cfg.dataset != 'googleurban' else None,
np.asarray(keypoints_dict[pair.split('-')[0]+'-'+pair.split('-')[1]]),
np.asarray(keypoints_dict[pair.split('-')[1]+'-'+pair.split('-')[0]]]),
np.asarray(keypoints_dict[pair.split('-')[1]+'-'+pair.split('-')[0]]),
calib_dict[pair.split(
'-')[0]], calib_dict[pair.split('-')
[1]], geom_dict[pair], matches_dict[pair],
Expand Down Expand Up @@ -183,7 +187,7 @@ def main(cfg):
err_q = result[i][2]
err_t = result[i][3]
_rep_s_dict[
all_keys[i]] = result[i][4] if result[i][4] else None
all_keys[i]] = result[i][4] if result[i][4] else []#None
_err_dict[all_keys[i]] = [err_q, err_t]
geo_s_dict_pre_match_th[th] = _geo_s_dict_pre_match
geo_s_dict_refined_match_th[th] = _geo_s_dict_refined_match
Expand Down
167 changes: 84 additions & 83 deletions import_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,86 +179,63 @@ def import_features(cfg):
print('Descriptor file is not given')
# Import
print('Importing features...')
for _data in data_list:
print('--- On "{}"...'.format(_data))

fn_kp = os.path.join(cfg.path_features, _data, 'keypoints.h5')
fn_desc = os.path.join(cfg.path_features, _data, 'descriptors.h5')
fn_score = os.path.join(cfg.path_features, _data, 'scores.h5')
fn_scale = os.path.join(cfg.path_features, _data, 'scales.h5')
fn_ori = os.path.join(cfg.path_features, _data, 'orientations.h5')
fn_match = os.path.join(cfg.path_features, _data, 'matches.h5')
fn_multiview_match = os.path.join(cfg.path_features, _data, 'matches_multiview.h5')
fn_stereo_match_list = [os.path.join(cfg.path_features, _data,'matches_stereo_{}.h5').
format(idx) for idx in range(3)]

# create keypoints folder
if cfg.pairwise_matching:
tgt_cur = os.path.join(
cfg.path_results, _data,
'_'.join([cfg.kp_name, str(-1), cfg.desc_name]))
else:
tgt_cur = os.path.join(
cfg.path_results, _data,
'_'.join([cfg.kp_name, str(numkp), cfg.desc_name]))
if not os.path.isdir(tgt_cur):
os.makedirs(tgt_cur)

# Both keypoints and descriptors files are provided
if os.path.isfile(fn_kp) and os.path.isfile(fn_desc) and not \
(os.path.isfile(fn_match) or (
(os.path.isfile(fn_multiview_match) and os.path.isfile(fn_stereo_match_list[0])))):
# We cannot downsample the keypoints without scores
if numkp < max(size_kp_file) and not os.path.isfile(fn_score):
raise RuntimeError('------ No scores, and subsampling is required!'
'(wanted: {}, found: {})'.format(
numkp, max(size_kp_file)))

# Import keypoints
print('------ Importing keypoints and descriptors')

# If there is no need to subsample, we can just copy the files
if numkp >= max(size_kp_file):
copy(fn_kp, tgt_cur)
copy(fn_desc, tgt_cur)
if os.path.isfile(fn_score):
copy(fn_score, tgt_cur)
if os.path.isfile(fn_scale):
copy(fn_scale, tgt_cur)
if os.path.isfile(fn_ori):
copy(fn_ori, tgt_cur)
# Otherwise, crop each file separately
for _dataset in cfg.datasets:
for _seq in seqs_dict[_dataset]:
print('--- On "{}: {}"...'.format(_dataset,_seq))

fn_kp = os.path.join(cfg.path_features, _dataset, _seq, 'keypoints.h5')
fn_desc = os.path.join(cfg.path_features, _dataset, _seq, 'descriptors.h5')
fn_score = os.path.join(cfg.path_features, _dataset, _seq, 'scores.h5')
fn_scale = os.path.join(cfg.path_features, _dataset, _seq, 'scales.h5')
fn_ori = os.path.join(cfg.path_features, _dataset, _seq, 'orientations.h5')
fn_match = os.path.join(cfg.path_features, _dataset, _seq, 'matches.h5')
fn_multiview_match = os.path.join(cfg.path_features, _dataset, _seq, 'matches_multiview.h5')
fn_stereo_match = os.path.join(cfg.path_features, _dataset, _seq,'matches_stereo.h5')
fn_stereo_match_list = [os.path.join(cfg.path_features, _dataset, _seq,'matches_stereo_{}.h5').
format(idx) for idx in range(3)]

# create keypoints folder
if cfg.pairwise_matching:
tgt_cur = os.path.join(
cfg.path_results, _dataset, _seq,
'_'.join([cfg.kp_name, str(-1), cfg.desc_name]))
else:
subsampled_indices = {}
with h5py.File(fn_score, 'r') as h5_r, \
h5py.File(os.path.join(tgt_cur, 'scores.h5'), 'w') as h5_w:
for k in h5_r:
sorted_indices = np.argsort(h5_r[k])[::-1]
subsampled_indices[k] = sorted_indices[:min(
h5_r[k].size, numkp)]
crop = h5_r[k].value[subsampled_indices[k]]
h5_w[k] = crop
with h5py.File(fn_kp, 'r') as h5_r, \
h5py.File(
os.path.join(tgt_cur, 'keypoints.h5'),
'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k], :]
h5_w[k] = crop
with h5py.File(fn_desc, 'r') as h5_r, \
h5py.File(
os.path.join(
tgt_cur, 'descriptors.h5'), 'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k], :]
h5_w[k] = crop
if os.path.isfile(fn_scale):
with h5py.File(fn_scale, 'r') as h5_r, \
h5py.File(
os.path.join(tgt_cur, 'scales.h5'),
'w') as h5_w:
tgt_cur = os.path.join(
cfg.path_results, _dataset, _seq,
'_'.join([cfg.kp_name, str(numkp), cfg.desc_name]))
if not os.path.isdir(tgt_cur):
os.makedirs(tgt_cur)

# Both keypoints and descriptors files are provided
if os.path.isfile(fn_kp) and os.path.isfile(fn_desc) and not \
(os.path.isfile(fn_match) or (
(os.path.isfile(fn_multiview_match) and os.path.isfile(fn_stereo_match_list[0])))):
# We cannot downsample the keypoints without scores
if numkp < max(size_kp_file) and not os.path.isfile(fn_score):
raise RuntimeError('------ No scores, and subsampling is required!'
'(wanted: {}, found: {})'.format(
numkp, max(size_kp_file)))

# Import keypoints
print('------ Importing keypoints and descriptors')

# If there is no need to subsample, we can just copy the files
if numkp >= max(size_kp_file):
copy(fn_kp, tgt_cur)
copy(fn_desc, tgt_cur)
if os.path.isfile(fn_score):
copy(fn_score, tgt_cur)
if os.path.isfile(fn_scale):
copy(fn_scale, tgt_cur)
if os.path.isfile(fn_ori):
copy(fn_ori, tgt_cur)
# Otherwise, crop each file separately
else:
subsampled_indices = {}
with h5py.File(fn_score, 'r') as h5_r, \
h5py.File(os.path.join(tgt_cur, 'scores.h5'), 'w') as h5_w:
for k in h5_r:
sorted_indices = np.argsort(h5_r[k].value.reshape(-1))[::-1]
sorted_indices = np.argsort(h5_r[k])[::-1]
subsampled_indices[k] = sorted_indices[:min(
h5_r[k].size, numkp)]
crop = h5_r[k].value[subsampled_indices[k]]
Expand All @@ -283,21 +260,45 @@ def import_features(cfg):
os.path.join(tgt_cur, 'scales.h5'),
'w') as h5_w:
for k in h5_r:
sorted_indices = np.argsort(h5_r[k].value.reshape(-1))[::-1]
subsampled_indices[k] = sorted_indices[:min(
h5_r[k].size, numkp)]
crop = h5_r[k].value[subsampled_indices[k]]
h5_w[k] = crop
if os.path.isfile(fn_ori):
with h5py.File(fn_ori, 'r') as h5_r, \
with h5py.File(fn_kp, 'r') as h5_r, \
h5py.File(
os.path.join(tgt_cur, 'orientations.h5'),
os.path.join(tgt_cur, 'keypoints.h5'),
'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k]]
crop = h5_r[k].value[subsampled_indices[k], :]
h5_w[k] = crop
with h5py.File(fn_desc, 'r') as h5_r, \
h5py.File(
os.path.join(
tgt_cur, 'descriptors.h5'), 'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k], :]
h5_w[k] = crop
if os.path.isfile(fn_scale):
with h5py.File(fn_scale, 'r') as h5_r, \
h5py.File(
os.path.join(tgt_cur, 'scales.h5'),
'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k]]
h5_w[k] = crop
if os.path.isfile(fn_ori):
with h5py.File(fn_ori, 'r') as h5_r, \
h5py.File(
os.path.join(tgt_cur, 'orientations.h5'),
'w') as h5_w:
for k in h5_r:
crop = h5_r[k].value[subsampled_indices[k]]
h5_w[k] = crop
elif os.path.isfile(fn_kp) and \
(os.path.isfile(fn_match) or \
(os.path.isfile(fn_multiview_match) and
(os.path.isfile(fn_stereo_match) or os.path.isfile(fn_stereo_match_list[0])))):

if os.path.isfile(fn_desc):
print('------ Matches file is provided')
print('------ Importing matches')
Expand Down
7 changes: 6 additions & 1 deletion viz_stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def main(cfg):
'''

try:
pairwise_keypoints = cfg.method_dict['config_common']['pairwise_keypoints']
except:
pairwise_keypoints = False

# Files should not be named to prevent (easy) abuse
# Instead we use 0, ..., cfg.num_viz_stereo_pairs
viz_folder_hq, viz_folder_lq = get_stereo_viz_folder(cfg)
Expand Down Expand Up @@ -114,7 +119,7 @@ def main(cfg):
inl = ransac_inl_dict[pair]

# Get depth for keypoints
if cfg.pairwise_keypoints:
if pairwise_keypoints:
kp1 = keypoints_dict[f'{fn1}-{fn2}']
kp2 = keypoints_dict[f'{fn2}-{fn1}']
else:
Expand Down

0 comments on commit 7fd64f1

Please sign in to comment.