-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpredict_depth.py
65 lines (50 loc) · 1.82 KB
/
predict_depth.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
import tensorflow as tf
import numpy as np
import cv2
from monodepth_model import monodepth_parameters, MonodepthModel
def post_process_disparity(disp):
_, h, w = disp.shape
l_disp = disp[0, :, :]
r_disp = np.fliplr(disp[1, :, :])
m_disp = 0.5 * (l_disp + r_disp)
l, _ = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
l_mask = 1.0 - np.clip(20 * (l - 0.05), 0, 1)
r_mask = np.fliplr(l_mask)
return r_mask * l_disp + l_mask * r_disp + (1.0 - l_mask - r_mask) * m_disp
def predict(restore_path, input_image):
"""Predict depth function."""
INPUT_HEIGHT, INPUT_WIDTH = 256, 512
input_image = cv2.resize(
input_image, (INPUT_WIDTH, INPUT_HEIGHT), cv2.INTER_AREA)
input_image = input_image.astype(np.float32) / 255
input_images = np.stack((input_image, np.fliplr(input_image)), 0)
params = monodepth_parameters(
encoder='vgg',
height=INPUT_HEIGHT,
width=INPUT_WIDTH,
batch_size=2,
num_threads=1,
num_epochs=1,
do_stereo=False,
wrap_mode="border",
use_deconv=False,
alpha_image_loss=0,
disp_gradient_loss_weight=0,
lr_loss_weight=0,
full_summary=False)
left = tf.placeholder(
tf.float32, [2, INPUT_HEIGHT, INPUT_WIDTH, 3])
model = MonodepthModel(params, "test", left, None)
# SESSION
config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)
# SAVER
train_saver = tf.train.Saver()
# INIT
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
# RESTORE
train_saver.restore(sess, restore_path)
disp = sess.run(model.disp_left_est[0], feed_dict={left: input_images})
disp_pp = post_process_disparity(disp.squeeze()).astype(np.float32)
return disp_pp