-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dispnet.py
128 lines (101 loc) · 5.23 KB
/
test_dispnet.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
import torch
from path import Path
import numpy as np
import argparse
from skimage.transform import resize as imresize
from scipy.ndimage import zoom
from tqdm import tqdm
from kitti_eval.depth_evaluation_utils import test_framework_KITTI as test_framework
from models import DispNetS, PoseExpNet
parser = argparse.ArgumentParser(description='Script for DispNet testing with corresponding groundTruth',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--pretrained-dispnet", required=True, type=str, help="pretrained DispNet path")
parser.add_argument("--pretrained-posenet", default=None, type=str, help="pretrained PoseNet path (for scale factor)")
parser.add_argument("--dataset-dir", default='.', type=str, help="Dataset directory")
parser.add_argument("--dataset-list", default=None, type=str, help="Dataset list file")
parser.add_argument("--output-dir", default=None, type=str, help="Output directory for saving predictions in a big 3D numpy file")
MIN_DEPTH = 1e-3
MAX_DEPTH = 80
IMAGE_HEIGHT = 128
IMAGE_WIDTH = 416
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
@torch.no_grad()
def main():
args = parser.parse_args()
disp_net = DispNetS().to(device)
weights = torch.load(args.pretrained_dispnet)
disp_net.load_state_dict(weights['state_dict'])
disp_net.eval()
weights = torch.load(args.pretrained_posenet)
seq_length = int(weights['state_dict']['conv1.0.weight'].size(1)/3)
pose_net = PoseExpNet(nb_ref_imgs=seq_length - 1, output_exp=False).to(device)
pose_net.load_state_dict(weights['state_dict'], strict=False)
with open(args.dataset_list, 'r') as f:
test_files = list(f.read().splitlines())
framework = test_framework(args.dataset_dir, test_files, seq_length,
MIN_DEPTH, MAX_DEPTH,
use_gps=False)
errors = np.zeros((2, 3, len(test_files)), np.float32)
if args.output_dir is not None:
output_dir = Path(args.output_dir)
output_dir.makedirs_p()
for j, sample in enumerate(tqdm(framework)):
tgt_img = sample['tgt']
ref_imgs = sample['ref']
h,w,_ = tgt_img.shape
if (h != IMAGE_HEIGHT or w != IMAGE_WIDTH):
tgt_img = imresize(tgt_img, (IMAGE_HEIGHT, IMAGE_WIDTH)).astype(np.float32)
ref_imgs = [imresize(img, (IMAGE_HEIGHT, IMAGE_WIDTH)).astype(np.float32) for img in ref_imgs]
tgt_img = np.transpose(tgt_img, (2, 0, 1))
ref_imgs = [np.transpose(img, (2,0,1)) for img in ref_imgs]
tgt_img = torch.from_numpy(tgt_img).unsqueeze(0)
tgt_img = ((tgt_img/255 - 0.5)/0.5).to(device)
for i, img in enumerate(ref_imgs):
img = torch.from_numpy(img).unsqueeze(0)
img = ((img/255 - 0.5)/0.5).to(device)
ref_imgs[i] = img
pred_disp = disp_net(tgt_img)[0].cpu().numpy()[0,0]
if args.output_dir is not None:
if j == 0:
predictions = np.zeros((len(test_files), *pred_disp.shape))
predictions[j] = 1/pred_disp
gt_depth = sample['gt_depth']
pred_depth = 1/pred_disp
pred_depth_zoomed = zoom(pred_depth,
(gt_depth.shape[0]/pred_depth.shape[0],
gt_depth.shape[1]/pred_depth.shape[1])
).clip(MIN_DEPTH, MAX_DEPTH)
if sample['mask'] is not None:
pred_depth_zoomed = pred_depth_zoomed[sample['mask']]
gt_depth = gt_depth[sample['mask']]
if seq_length > 1:
# Reorganize ref_imgs : tgt is middle frame but not necessarily the one used in DispNetS
# (in case sample to test was in end or beginning of the image sequence)
middle_index = seq_length//2
tgt = ref_imgs[middle_index]
reorganized_refs = ref_imgs[:middle_index] + ref_imgs[middle_index + 1:]
_, poses = pose_net(tgt, reorganized_refs)
displacement_magnitudes = poses[0,:,:3].norm(2,1).cpu().numpy()
scale_factor = np.mean(sample['displacements'] / displacement_magnitudes)
errors[0,:,j] = compute_errors(gt_depth, pred_depth_zoomed*scale_factor)
scale_factor = np.median(gt_depth)/np.median(pred_depth_zoomed)
errors[1,:,j] = compute_errors(gt_depth, pred_depth_zoomed*scale_factor)
mean_errors = errors.mean(2)
error_names = ['abs_rel','sq_rel','rms']
if args.pretrained_posenet:
print("Results with scale factor determined by PoseNet : ")
print("{:>10}, {:>10}, {:>10}".format(*error_names))
print("{:10.4f}, {:10.4f}, {:10.4f}".format(*mean_errors[0]))
print("Results with scale factor determined by GT/prediction ratio (like the original paper) : ")
print("{:>10}, {:>10}, {:>10}".format(*error_names))
print("{:10.4f}, {:10.4f}, {:10.4f}".format(*mean_errors[1]))
if args.output_dir is not None:
np.save(output_dir/'predictions.npy', predictions)
def compute_errors(gt, pred):
rmse = (gt - pred) ** 2
rmse = np.sqrt(rmse.mean())
abs_rel = np.mean(np.abs(gt - pred) / gt)
sq_rel = np.mean(((gt - pred)**2) / gt)
return abs_rel, sq_rel, rmse
if __name__ == '__main__':
main()