Skip to content

Commit a2cf798

Browse files
committed
support saving disparity prediction as .pfm file
1 parent aa879db commit a2cf798

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

evaluate_depth.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dataloader.depth.datasets import ScannetDataset, DemonDataset
1111
from loss.depth_loss import compute_errors
1212
from utils.utils import InputPadder
13-
from utils.visualization import viz_depth_tensor_from_monodepth2
13+
from utils.visualization import viz_depth_tensor
1414

1515
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
1616

@@ -132,9 +132,9 @@ def validate_scannet(model,
132132

133133
if save_vis_depth:
134134
filename = os.path.join(save_dir, '%04d_depth_pred.png' % valid_samples)
135-
viz_inv_depth = viz_depth_tensor_from_monodepth2(1. / pred_depth.cpu(),
136-
return_numpy=True,
137-
colormap='plasma') # [H, W, 3] uint8
135+
viz_inv_depth = viz_depth_tensor(1. / pred_depth.cpu(),
136+
return_numpy=True,
137+
colormap='plasma') # [H, W, 3] uint8
138138
Image.fromarray(viz_inv_depth).save(filename)
139139

140140
gt_depth = gt_depth.cpu().numpy()
@@ -271,9 +271,9 @@ def validate_demon(model,
271271

272272
if save_vis_depth:
273273
filename = os.path.join(save_dir, '%04d.png' % valid_samples)
274-
viz_inv_depth = viz_depth_tensor_from_monodepth2(1. / pred_depth.cpu(),
275-
return_numpy=True,
276-
colormap='plasma') # [H, W, 3] uint8
274+
viz_inv_depth = viz_depth_tensor(1. / pred_depth.cpu(),
275+
return_numpy=True,
276+
colormap='plasma') # [H, W, 3] uint8
277277
Image.fromarray(viz_inv_depth).save(filename)
278278

279279
gt_depth = gt_depth.cpu().numpy()
@@ -402,8 +402,8 @@ def inference_depth(model,
402402
pr_depth = pred_depth[0]
403403

404404
filename = os.path.join(output_path, os.path.basename(imgs[i])[:-4] + '.png')
405-
viz_inv_depth = viz_depth_tensor_from_monodepth2(1. / pr_depth.cpu(),
406-
return_numpy=True) # [H, W, 3] uint8
405+
viz_inv_depth = viz_depth_tensor(1. / pr_depth.cpu(),
406+
return_numpy=True) # [H, W, 3] uint8
407407
Image.fromarray(viz_inv_depth).save(filename)
408408

409409
if pred_bidir_depth:
@@ -412,8 +412,8 @@ def inference_depth(model,
412412
pr_depth_bwd = pred_depth[1]
413413

414414
filename = os.path.join(output_path, os.path.basename(imgs[i])[:-4] + '_bwd.png')
415-
viz_inv_depth = viz_depth_tensor_from_monodepth2(1. / pr_depth_bwd.cpu(),
416-
return_numpy=True) # [H, W, 3] uint8
415+
viz_inv_depth = viz_depth_tensor(1. / pr_depth_bwd.cpu(),
416+
return_numpy=True) # [H, W, 3] uint8
417417
Image.fromarray(viz_inv_depth).save(filename)
418418

419419
print('Done!')

evaluate_flow.py

-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ def inference_flow(model,
798798
output_file_bwd = os.path.join(output_path, os.path.basename(filenames[test_id])[:-4] + '_pred_bwd.flo')
799799
frame_utils.writeFlow(output_file_bwd, flow_bwd)
800800

801-
802801
if save_video:
803802
suffix = '_flow_img.mp4' if concat_flow_img else '_flow.mp4'
804803
output_file = os.path.join(output_path, os.path.basename(inference_video)[:-4] + suffix)

evaluate_stereo.py

+9
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ def inference_stereo(model,
723723
num_reg_refine=1,
724724
pred_bidir_disp=False,
725725
pred_right_disp=False,
726+
save_pfm_disp=False,
726727
):
727728
model.eval()
728729

@@ -818,6 +819,10 @@ def inference_stereo(model,
818819

819820
disp = pred_disp[0].cpu().numpy()
820821

822+
if save_pfm_disp:
823+
save_name_pfm = save_name[:-4] + '.pfm'
824+
write_pfm(save_name_pfm, disp)
825+
821826
disp = vis_disparity(disp)
822827
cv2.imwrite(save_name, disp)
823828

@@ -828,6 +833,10 @@ def inference_stereo(model,
828833
# flip back
829834
disp = hflip(pred_disp[1]).cpu().numpy()
830835

836+
if save_pfm_disp:
837+
save_name_pfm = save_name[:-4] + '.pfm'
838+
write_pfm(save_name_pfm, disp)
839+
831840
disp = vis_disparity(disp)
832841
cv2.imwrite(save_name, disp)
833842

main_stereo.py

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def get_args_parser():
114114
help='predict both left and right disparities')
115115
parser.add_argument('--pred_right_disp', action='store_true',
116116
help='predict right disparity')
117+
parser.add_argument('--save_pfm_disp', action='store_true',
118+
help='save predicted disparity as .pfm format')
117119

118120
parser.add_argument('--debug', action='store_true')
119121

@@ -340,6 +342,7 @@ def main(args):
340342
num_reg_refine=args.num_reg_refine,
341343
pred_bidir_disp=args.pred_bidir_disp,
342344
pred_right_disp=args.pred_right_disp,
345+
save_pfm_disp=args.save_pfm_disp,
343346
)
344347

345348
return

utils/visualization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def tensor2numpy(var_dict):
8989
return var_dict
9090

9191

92-
def viz_depth_tensor_from_monodepth2(disp, return_numpy=False, colormap='plasma'):
92+
def viz_depth_tensor(disp, return_numpy=False, colormap='plasma'):
9393
# visualize inverse depth
9494
assert isinstance(disp, torch.Tensor)
9595

0 commit comments

Comments
 (0)