-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_middlebury.py
360 lines (241 loc) · 9.77 KB
/
testing_middlebury.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('CKPT_FOLDER_SCGAN', '../ScGAN/ckpt/driving/cgan1_with_encoder_dropout',
"""The name of the tower """)
tf.app.flags.DEFINE_string('CKPT_FOLDER', 'ckpt/driving/epe_all_ds/train/',
"""The name of the tower """)
tf.app.flags.DEFINE_boolean('TEST_GAN', False,
"""Whether to log device placement.""")
tf.app.flags.DEFINE_boolean('DRIVING', True,
"""Whether to log device placement.""")
if FLAGS.TEST_GAN == True:
import sys
sys.path.insert(0, '../ScGAN')
import network
import synthetic_tf_converter as stc
import losses_helper as lhpl
import numpy as np
import helpers as hpl
import math
from PIL import Image
import ijremote as ij
ij.setHost('tcp://linus:13463')
u_factor = 0.414814815
v_factor = 0.4
input_size = math.ceil(960 * v_factor), math.floor(540 * u_factor)
def get_depth_from_disp(disparity):
focal_length = 1050.0
disp_to_depth = disparity / focal_length
return disp_to_depth
def combine_depth_values(img,depth):
depth = np.expand_dims(depth,2)
return np.concatenate((img,depth),axis=2)
def parse_input(img1,img2,disp1,disp2):
img1 = Image.open(img1)
img2 = Image.open(img2)
img1 = img1.resize(input_size, Image.BILINEAR)
img2 = img2.resize(input_size, Image.BILINEAR)
if FLAGS.DRIVING == False:
disp1 = Image.open(disp1)
disp2 = Image.open(disp2)
disp1 = disp1.resize(input_size, Image.NEAREST)
disp2 = disp2.resize(input_size, Image.NEAREST)
else:
disp1 = hpl.readPFM(disp1)[0]
disp2 = hpl.readPFM(disp2)[0]
disp1 = Image.fromarray(disp1,mode='F')
disp2 = Image.fromarray(disp2,mode='F')
disp1 = disp1.resize(input_size, Image.NEAREST)
disp2 = disp2.resize(input_size, Image.NEAREST)
disp1 = np.array(disp1,dtype=np.float32)
disp2 = np.array(disp2,dtype=np.float32)
depth1 = get_depth_from_disp(disp1)
depth2 = get_depth_from_disp(disp2)
# normalize
depth_norm = np.max(depth1)
depth1 = depth1 / depth_norm
depth2 = depth2 / depth_norm
img1_orig = np.array(img1)
img2_orig = np.array(img2)
img1 = img1_orig / 255
img2 = img2_orig / 255
rgbd1 = combine_depth_values(img1,depth1)
rgbd2 = combine_depth_values(img2,depth2)
img_pair = np.concatenate((rgbd1,rgbd2),axis=2)
# optical_flow
return img_pair, disp1, img2_orig
def load_model_ckpt(sess,filename):
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(filename))
def downsample_opt_flow(data,size):
u = data[:,:,0]
v = data[:,:,1]
dt = Image.fromarray(u)
dt = dt.resize(size, Image.NEAREST)
dt2 = Image.fromarray(v)
dt2 = dt2.resize(size, Image.NEAREST)
u = np.array(dt)
v = np.array(dt2)
return np.stack((u,v),axis=2)
def predict(img_pair,optical_flow):
if FLAGS.TEST_GAN == True:
optical_flow = downsample_opt_flow(optical_flow,(192,112))
img_pair = np.expand_dims(img_pair,axis=0)
optical_flow = np.expand_dims(optical_flow,axis=0)
feed_dict = {
X: img_pair,
Y: optical_flow
}
loss, v = sess.run([loss_result,predict_flow2],feed_dict=feed_dict)
return denormalize_flow(v), loss
def denormalize_flow(flow):
u = flow[:,:,:,0] * input_size[0]
v = flow[:,:,:,1] * input_size[1]
# w = flow[:,:,2] * self.max_depth_driving_chng
u = np.expand_dims(u,axis=-1)
v = np.expand_dims(v,axis=-1)
flow = np.concatenate((u,v),axis=-1)
return flow
def warp(img,flow):
img = img.astype(np.float32)
x = list(range(0,input_size[0]))
y = list(range(0,input_size[1]))
X, Y = tf.meshgrid(x, y)
X = tf.cast(X,np.float32) + flow[:,:,0]
Y = tf.cast(Y,np.float32) + flow[:,:,1]
con = tf.stack([X,Y])
result = tf.transpose(con,[1,2,0])
result = tf.expand_dims(result,0)
return tf.contrib.resampler.resampler(img[np.newaxis,:,:,:],result)
def denormalize_flow_tensor(flow):
u = flow[:,:,:,0] * input_size[0]
v = flow[:,:,:,1] * input_size[1]
# w = flow[:,:,2] * self.max_depth_driving_chng
flow = tf.concat([tf.expand_dims(u,axis=-1),tf.expand_dims(v,axis=-1)],axis=3)
return flow
def parse_results(img_pair, optical_flow, img2_orig):
# optical_flow_normed = normalizeOptFlow(optical_flow,(384,224))
predicted_flow, loss = predict(img_pair,optical_flow)
if FLAGS.TEST_GAN == True:
img2_to_tensor = further_resize_imgs(img2_to_tensor)
orig_flow_to_tensor = further_resize_lbls(orig_flow_to_tensor)
predicted_flow = np.squeeze(predicted_flow)
# warped_img = lhpl.flow_warp(img2_to_tensor,orig_flow_denormed_to_tensor)
warped_img = warp(img2_orig,predicted_flow)
result = warped_img.eval()[0].astype(np.uint8)
show_image(result,'warped_img_pr')
# denorm_u,denorm_v = sess.run([predicted_flow[0,:,:,0],predicted_flow[0,:,:,1]])
# ij.setImage('gt_flow_u',optical_flow[:,:,0])
# ij.setImage('gt_flow_v',optical_flow[:,:,1])
# ij.setImage('de_opt_flow_u',predicted_flow[0,:,:,0])
# ij.setImage('de_opt_flow_v',predicted_flow[0,:,:,1])
# ij.setImage('warped',np.transpose(warped_img2,0,1]))
# ij.setImage('orig',np.transpose(img2_orig,[2,0,1]))
# ij.setImage('orig',img2_orig)
# ij.setImage('warped',warped_img)
# Image.fromarray(np.uint8(img2_orig)).show()
# Image.fromarray(np.uint8(warped_img)).show()
# print(loss)
def show_image(array,img_title):
# shaper = array.shape
a = Image.fromarray(array)
# a = a.resize((math.ceil(shaper[1] * 2),math.ceil(shaper[0] * 2)), Image.BILINEAR)
a.show(title=img_title)
# a.save('prediction_without_pc_loss.jpg')
def perform_testing_with_driving():
dataset_root = '../dataset_synthetic/driving/'
IMG_ROOT = 'frames_finalpass_webp/35mm_focallength/scene_backwards/fast/left/'
DISP_ROOT = 'disparity/35mm_focallength/scene_backwards/fast/left/'
FLOW_ROOT = 'optical_flow/35mm_focallength/scene_backwards/fast/into_future/left/'
DISPARITY_CHNG_ROOT = 'disparity_change/35mm_focallength/scene_backwards/fast/into_future/left/'
IMG1_NUMBER = '0001'
IMG2_NUMBER = '0002'
IMG1 = dataset_root + IMG_ROOT + IMG1_NUMBER + '.webp'
IMG2 = dataset_root + IMG_ROOT + IMG2_NUMBER + '.webp'
DISPARITY1 = dataset_root + DISP_ROOT + IMG1_NUMBER + '.pfm'
DISPARITY2 = dataset_root + DISP_ROOT + IMG2_NUMBER + '.pfm'
DISPARITY_CHNG = dataset_root + DISPARITY_CHNG_ROOT + IMG1_NUMBER + '.pfm'
FLOW = dataset_root + FLOW_ROOT + 'OpticalFlowIntoFuture_' + IMG1_NUMBER + '_L.pfm'
img_pair, optical_flow, img2_orig = parse_input(IMG1,IMG2,DISPARITY1,DISPARITY2)
optical_flow = hpl.readPFM(FLOW)[0]
optical_flow = downsample_opt_flow(optical_flow,(384,224))
parse_results(img_pair, optical_flow, img2_orig)
def perform_testing_with_middlebury():
dataset_root = '../dataset_synthetic/middlebury/'
dataset_type = ['middlebury2003','middlebury2005']
for typee in dataset_type:
ds_current_root = dataset_root + typee
if typee == dataset_type[0]:
print('parsing middlebury 2003 ... ')
folders = ['conesF','teddyF']
img1_path = 'im2.ppm'
img2_path = 'im6.ppm'
disp1_path = 'disp2.pgm'
disp2_path = 'disp6.pgm'
else:
print('parsing middlebury 2005 ... ')
folders = ['Art','Books','Dolls','Laundry','Moebius','Reindeer']
img1_path = 'view1.png'
img2_path = 'view5.png'
disp1_path = 'disp1.png'
disp2_path = 'disp5.png'
for folder in folders:
final_path = ''
final_path = ds_current_root + '/' +folder + '/'
img1_path_final = final_path + img1_path
img2_path_final = final_path + img2_path
disp1_path_final = final_path + disp1_path
disp2_path_final = final_path + disp2_path
print('')
print('folder = '+ folder)
print('')
img_pair, optical_flow, img2_orig = parse_input(img1_path_final,img2_path_final,disp1_path_final,disp2_path_final)
flow_expanded_u = np.expand_dims(optical_flow,axis=2)
flow_expanded_v = np.expand_dims(np.zeros_like(optical_flow),axis=2)
optical_flow = np.concatenate([flow_expanded_u,flow_expanded_v],axis=-1)
parse_results(img_pair, optical_flow, img2_orig)
def normalizeOptFlow(flow,input_size):
# remove the values bigger than the image size
flow[:,:,0][flow[:,:,0] > input_size[0] ] = 0
flow[:,:,1][flow[:,:,1] > input_size[1] ] = 0
# separate the u and v values
flow_u = flow[:,:,0]
flow_v = flow[:,:,1]
# normalize the values by the image dimensions
flow_u = flow_u / input_size[0]
flow_v = flow_v / input_size[1]
# combine them back and return
return np.dstack((flow_u,flow_v))
def further_resize_imgs(network_input_images):
network_input_images = tf.image.resize_images(network_input_images,[112,192],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return network_input_images
def further_resize_lbls(network_input_labels):
network_input_labels = tf.image.resize_images(network_input_labels,[112,192],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
network_input_labels_u = network_input_labels[:,:,:,0] * 0.5
network_input_labels_v = network_input_labels[:,:,:,1] * 0.5
network_input_labels_u = tf.expand_dims(network_input_labels_u,axis=-1)
network_input_labels_v = tf.expand_dims(network_input_labels_v,axis=-1)
network_input_labels = tf.concat([network_input_labels_u,network_input_labels_v],axis=3)
return network_input_labels
sess = tf.InteractiveSession()
X = tf.placeholder(dtype=tf.float32, shape=(1, 224, 384, 8))
Y = tf.placeholder(dtype=tf.float32, shape=(1, 224, 384, 2))
if FLAGS.TEST_GAN == True:
predict_flow2 = network.generator(X)
predict_flow2 = predict_flow2[1]
Y = further_resize_lbls(Y)
else:
predict_flow2 = network.train_network(X)
predict_flow2 = predict_flow2[0]
predict_flow2 = predict_flow2[:,:,:,0:2]
predict_flow2 = denormalize_flow_tensor(predict_flow2)
loss_result = lhpl.endpoint_loss(Y,predict_flow2,1)
# loss_result = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(Y, predict_flow2))))
if FLAGS.TEST_GAN == True:
load_model_ckpt(sess,FLAGS.CKPT_FOLDER_SCGAN)
else:
load_model_ckpt(sess,FLAGS.CKPT_FOLDER)
if FLAGS.DRIVING == True:
perform_testing_with_driving()
else:
perform_testing_with_middlebury()