-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
executable file
·160 lines (138 loc) · 4.72 KB
/
test.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
import os
import time
import datetime
import tensorflow as tf
from PIL import Image
import numpy as np
import argparse
import json
from util import *
# parse args
argParser = argparse.ArgumentParser(description="runs validation and visualizations")
argParser.add_argument("-f",dest="showFlow",action="store_true")
argParser.add_argument("-e",dest="showError",action="store_true")
argParser.add_argument("-w",dest="showWarp",action="store_true")
argParser.add_argument("-a",dest="testAll",action="store_true")
argParser.add_argument("-t",dest="use2015",action="store_true")
argParser.add_argument("-i","--iteration",dest="iteration",action="store",default=0,type=int)
argParser.add_argument("-g","--gpu",dest="gpu",action="store",default=0,type=int)
cmdArgs = argParser.parse_args()
# multi gpu management
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(cmdArgs.gpu)
# load instance params
with open("hyperParams.json") as f:
instanceParams = json.load(f)
# find latest snapshot
snapshotFiles = os.listdir("snapshots")
snapshotFiles = [filename for filename in snapshotFiles if filename[-11:] == ".ckpt.index"]
snapshotFiles.sort()
if cmdArgs.iteration > 0:
iter = cmdArgs.iteration
print "testing "+str(iter)
elif len(snapshotFiles) > 0:
iter = int(snapshotFiles[-1][5:-11])
print "testing "+ str(iter)
else:
print "No snapshots found"
exit()
# kitti2015 override
if cmdArgs.use2015:
instanceParams["dataset"] = "kitti2015"
# import data
if instanceParams["dataset"] == "kitti2012":
datasetRoot = "../example_data/"
frame0Path = datasetRoot+"datalists/valPath1.txt";
frame1Path = datasetRoot+"datalists/valPath2.txt";
if cmdArgs.testAll:
flowPath = datasetRoot+"datalists/valPathFloAll.txt";
else:
flowPath = datasetRoot+"datalists/valPathFlo.txt";
desiredHeight = 384
desiredWidth = 1280
elif instanceParams["dataset"] == "kitti2015":
datasetRoot = "/home/jjyu/datasets/KITTI2015/"
frame0Path = datasetRoot+"datalists/val_im1.txt";
frame1Path = datasetRoot+"datalists/val_im2.txt";
if cmdArgs.testAll:
flowPath = datasetRoot+"datalists/val_flo_all.txt";
else:
flowPath = datasetRoot+"datalists/val_flo.txt";
desiredHeight = 384
desiredWidth = 1280
elif instanceParams["dataset"] == "sintel":
datasetRoot = "/home/jjyu/datasets/Sintel/"
frame0Path = datasetRoot+"datalists/val_im1.txt";
frame1Path = datasetRoot+"datalists/val_im2.txt";
flowPath = datasetRoot+"datalists/val_flow.txt";
desiredHeight = 448
desiredWidth = 1024
else:
print "unknown dataset"
exit()
with open(frame0Path) as f:
imagePairs0 = [datasetRoot+x[:-1] for x in f.readlines()]
with open(frame1Path) as f:
imagePairs1 = [datasetRoot+x[:-1] for x in f.readlines()]
with open(flowPath) as f:
imageFlows = [datasetRoot+x[:-1] for x in f.readlines()]
iterations = len(imageFlows)
testData = TestData(imagePairs0,imagePairs1,imageFlows,1,desiredHeight,desiredWidth)
# build graph
with tf.device("/gpu:"+str(cmdArgs.gpu)):
with tf.variable_scope("netShare"):
networkBody = NetworkBody(testData,instanceParams)
flowFinal = networkBody.flows[0]
epe, errorMap = epeEval(flowFinal,testData.flow,testData.flowMask)
# visualize
flowViz = flowToRgb(flowFinal)
errorMap = errorMap/20
transformGrid = flowTransformGrid(flowFinal)
mean = [0.448553, 0.431021, 0.410602]
mean = tf.expand_dims(tf.expand_dims(tf.expand_dims(mean,0),0),0)
warped = flowWarp(testData.frame1["rgb"]+mean,flowFinal)
# saver
saver = tf.train.Saver()
# config tensorflow
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
# start testing
epes = []
with tf.Session(config=config) as sess:
saver.restore(sess,"snapshots/iter_"+str(iter).zfill(16)+".ckpt")
# run
lastPrint = time.time()
for i in range(iterations):
feed_dict = {
testData.im0File: imagePairs0[i],
testData.im1File: imagePairs1[i],
testData.flowFile: imageFlows[i],
}
result = sess.run([epe,flowViz,errorMap,warped,testData.height,testData.width],feed_dict=feed_dict)
h = result[4]
w = result[5]
flattened = [result[0][0]]
epes += flattened
print sum(epes)/float(len(epes))
if cmdArgs.showFlow:
arr = np.minimum(np.asarray(result[1]),1)
arr = np.maximum(arr,0)
arr = np.squeeze(np.asarray(arr*255,np.uint8))
im = Image.fromarray(arr[:h,:w,:])
im.show()
raw_input("press to continue")
if cmdArgs.showError:
arr = np.minimum(np.asarray(result[2]),1)
arr = np.maximum(arr,0)
arr = np.squeeze(np.asarray(arr*255,np.uint8))
im = Image.fromarray(arr[:h,:w])
im.show()
raw_input("press to continue")
if cmdArgs.showWarp:
arr = np.minimum(np.asarray(result[3]),1)
arr = np.maximum(arr,0)
arr = np.squeeze(np.asarray(arr*255,np.uint8))
im = Image.fromarray(arr[:h,:w,:])
im.show()
raw_input("press to continue")