-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdepth_uncrop.py
82 lines (62 loc) · 2.52 KB
/
depth_uncrop.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
#!/usr/bin/env python3
import os
import argparse
import numpy as np
import tensorflow as tf
from prdepth import sampler
from prdepth import metric
import prdepth.utils as ut
from prdepth.optimization.uncrop_optimizer import UncropOptimizer as Optimizer
parser = argparse.ArgumentParser()
parser.add_argument(
'--height', default=120, type=int, help='height of the cropped depth')
parser.add_argument(
'--width', default=160, type=int, help='width of the cropped depth')
parser.add_argument(
'--save_dir', default=None, help='Save predictions to where')
opts = parser.parse_args()
save_dir = opts.save_dir
TLIST = 'data/test.txt'
MAXITER = 200
TOLERANCE = 1e-8
LMD = 150.
#########################################################################
depth_sampler = sampler.Sampler(nsamples=100, read_gt=True)
optimizer = Optimizer(depth_sampler, LMD)
sess = tf.Session()
depth_sampler.load_model(sess)
#########################################################################
# Main Loop
flist = [i.strip('\n') for i in open(TLIST).readlines()]
depths, preds, masks = [], [], []
for filename in flist:
# Run VAE to sample patch-wise predictions.
depth_sampler.sample_predictions(filename, sess)
# Load cropped depth.
depth = sess.run(depth_sampler.image_depth).squeeze()
cropped_depth = ut.read_depth(
filename + '_crop%dx%d.png' % (opts.height, opts.width))
optimizer.initialize(sess)
optimizer.compute_additional_cost(cropped_depth, sess)
for i in range(MAXITER):
global_current = optimizer.update_global_estimation(sess)
diff = optimizer.update_sample_selection(sess)
if diff < TOLERANCE:
break
pred = optimizer.update_global_estimation(sess)
pred = np.clip(pred.squeeze(), 0.01, 1.).astype(np.float64)
pred[cropped_depth > 0] = cropped_depth[cropped_depth > 0]
preds.append(pred)
depth = sess.run(depth_sampler.image_depth).squeeze().astype(np.float64)
depths.append(depth)
masks.append(cropped_depth == 0)
if save_dir is not None:
nm = os.path.join(save_dir, os.path.basename(filename))
min_depth = np.maximum(0.01, np.min(depth))
max_depth = np.minimum(1., np.max(depth))
ut.save_color_depth(nm + '_gt.png', depth, min_depth, max_depth)
ut.save_color_depth(nm + '_uncropped.png', pred, min_depth, max_depth)
# Metrics computed only on filled-in regions.
metrics = metric.get_metrics(depths, preds, projection_mask=True, masks=masks)
for k in metric.METRIC_NAMES:
print("%s: %.3f" % (k, metrics[k]))