-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpredict.py
197 lines (173 loc) · 7.42 KB
/
predict.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import argparse
import os
import sys
from PIL import Image, ImageFile
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms as tf
from models.get_models import get_model_with_opts
from saver import load_model_for_evaluate
from utils.platform_loader import read_yaml_options
from visualizer import Visualizer
sys.path.append(os.getcwd())
ImageFile.LOAD_TRUNCATED_IMAGES = True
parser = argparse.ArgumentParser(description='SMDE Prediction Parser')
parser.add_argument('--image_path',
dest='image_path',
required=True,
help='the path to the images')
parser.add_argument('--exp_opts',
dest='exp_opts',
required=True,
help="the yaml file for model's options")
parser.add_argument('--model_path',
dest='trained_model',
required=True,
help='the path of trained model')
parser.add_argument('--input_size',
dest='input_size',
type=int,
nargs='+',
default=None,
help='the size of input images')
parser.add_argument('--out_dir',
dest='out_dir',
type=str,
default=None,
help='the folder name for the outputs')
parser.add_argument('--cpu',
dest='cpu',
action='store_true',
default=False,
help='predicting with cpu')
parser.add_argument('-gpp',
'--godard_post_process',
action='store_true',
default=False,
help='Post-processing as done in Godards paper')
parser.add_argument('-mspp',
'--multi_scale_post_process',
action='store_true',
default=False,
help='Post-processing as done in FAL-Net')
opts = parser.parse_args()
def batch_post_process_disparity(l_disp, r_disp):
"""Apply the disparity post-processing method as introduced in
Monodepthv1."""
_, _, h, w = l_disp.shape
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))[None, ...]
r_mask = l_mask[:, :, ::-1]
l_mask = torch.from_numpy(l_mask.copy()).unsqueeze(0).to(l_disp)
r_mask = torch.from_numpy(r_mask.copy()).unsqueeze(0).to(l_disp)
return r_mask * l_disp + l_mask * r_disp + (1.0 - l_mask - r_mask) * m_disp
def multi_scale_post_process(l_disp, r_down_disp):
norm = l_disp / (np.percentile(l_disp.detach().cpu().numpy(), 95) + 1e-6)
norm[norm > 1] = 1
return (1 - norm) * l_disp + norm * r_down_disp
def predict_one_image(network, inputs, visualizer, save_path, file):
outputs = network(inputs, is_train=False)
if opts.godard_post_process:
inputs['color_s'] = torch.flip(inputs['color_s'], dims=[3])
flip_outputs = network(inputs, is_train=False)
fflip_depth = torch.flip(flip_outputs[('depth', 's')],
dims=[3])
pp_depth = batch_post_process_disparity(
1 / outputs[('depth', 's')], 1 / fflip_depth)
pp_depth = 1 / pp_depth
inputs['color_s'] = torch.flip(inputs['color_s'], dims=[3])
outputs[('depth', 's')] = pp_depth
elif opts.multi_scale_post_process:
inputs['color_s'] = torch.flip(inputs['color_s'], dims=[3])
up_fac = 2/3
H, W = inputs['color_s'].shape[2:]
inputs['color_s'] = F.interpolate(inputs['color_s'],
scale_factor=up_fac,
mode='bilinear',
align_corners=True)
flip_outputs = network(inputs, is_train=False)
flip_depth = flip_outputs[('depth', 's')]
flip_depth = up_fac * F.interpolate(flip_depth,
size=(H, W),
mode='nearest')
fflip_depth = torch.flip(flip_depth,
dims=[3])
pp_depth = batch_post_process_disparity(
1 / outputs[('depth', 's')], 1 / fflip_depth)
pp_depth = 1 / pp_depth
inputs['color_s'] = torch.flip(inputs['color_s'], dims=[3])
outputs[('depth', 's')] = pp_depth
else:
pp_depth = outputs[('depth', 's')]
visual_map = {}
visual_map['pp_disp'] = 1 / pp_depth
visual_map['pp_depth'] = pp_depth
visualizer.update_visual_dict(inputs, outputs, visual_map)
visualizer.do_visualizion(os.path.splitext(file)[0] + '_visual')
pp_depth = pp_depth.squeeze(0).squeeze(0).cpu().numpy()
save_path = os.path.join(save_path, os.path.splitext(file)[0] + '_pred.npy')
np.save(save_path, pp_depth)
def predict():
# Initialize the random seed and device
if opts.cpu:
device = torch.device('cpu')
else:
device = torch.device('cuda')
# Initialize the options
opts_dic = read_yaml_options(opts.exp_opts)
# Initialize the network
print('->Load the pretrained model')
# print('->model name: {}'.format(opts_dic['model']['type']))
print('->pretrained path: {}'.format(opts.trained_model))
network = get_model_with_opts(opts_dic, device)
network = load_model_for_evaluate(opts.trained_model, network)
network.eval()
if opts.out_dir is not None:
os.makedirs(opts.out_dir, exist_ok=True)
save_path = opts.out_dir
else:
if os.path.isfile(opts.image_path):
save_path = os.path.dirname(opts.image_path)
else:
save_path = opts.image_path
visualizer = Visualizer(save_path, {'type':{'pp_depth': 'depth'},
'shape': [['pp_depth']]})
to_tensor = tf.ToTensor()
normalize = tf.Normalize(mean=opts_dic['pred_norm'],
std=[1, 1, 1])
if opts.input_size is not None:
image_size = opts.input_size
else:
image_size = opts_dic['pred_size']
print('->resize image(s) into: {}'.format(image_size))
resize = tf.Resize(image_size,interpolation=Image.ANTIALIAS)
# Predict
if opts.godard_post_process or opts.multi_scale_post_process:
print('->Use the post processing')
print('->Start prediction')
with torch.no_grad():
if os.path.isfile(opts.image_path):
img = Image.open(opts.image_path)
img = img.convert('RGB')
img = normalize(to_tensor(resize(img))).unsqueeze(0)
inputs = {}
inputs['color_s'] = img.to(device)
file = os.path.basename(opts.image_path)
predict_one_image(network, inputs, visualizer, save_path, file)
else:
for r, ds, fs in os.walk(opts.image_path):
for f in fs:
if f.endswith('.png') or f.endswith('.jpg'):
img = Image.open(os.path.join(r, f))
img = img.convert('RGB')
img = normalize(to_tensor(resize(img))).unsqueeze(0)
inputs = {}
inputs['color_s'] = img.to(device)
predict_one_image(network, inputs, visualizer,
save_path, f)
print('Finish Prediction!')
print('Prediction files are saved in {}'.format(save_path))
if __name__ == '__main__':
predict()