forked from michaelbrownid/tfccs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesthp.py
executable file
·136 lines (106 loc) · 4.9 KB
/
testhp.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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import time
import os
import sys
import numpy as np
import tensorflow as tf
from .model import Model
from . import data
################################
def test(args):
data_loader = data.data( args.batch_size, sys.argv[2],
inputdatName=args.inputdatName,
outputdatName=args.outputdatName)
#with tf.device("/gpu:2"):
if True:
# check compatibility if training is continued from previously saved model
if args.init_from is not None:
# check if all necessary files exist
assert os.path.isdir(args.init_from)," %s must be a a path" % args.init_from
ckpt = tf.train.latest_checkpoint(args.init_from)
assert ckpt, "No checkpoint found"
print("ckpt", ckpt, file=sys.stderr)
if not os.path.isdir(args.save_dir):
os.makedirs(args.save_dir)
model = Model(args)
num=0
# make it appear as though there is only one gpu and use it
os.environ["CUDA_VISIBLE_DEVICES"]="2"
tfconfig=tf.ConfigProto()
# tfconfig.allow_soft_placement=True
# tfconfig.log_device_placement=True
# tfconfig.gpu_options.allow_growth=True
with tf.Session( config=tfconfig ) as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.global_variables())
# restore model
if args.init_from is not None:
saver.restore(sess, ckpt)
################################
for b in range(data_loader.num_batches):
start = time.time()
x, y = data_loader.next_batch()
yid = y[:,:,0:4]
ylen = y[:,:,4:]
start = time.time()
predictions = model.model.predict(x)
end = time.time()
print("time batch",b,"x.shape[0]",x.shape[0],"duration",end-start)
print("predictions[0].shape",predictions[0].shape)
print("predictions[1].shape",predictions[1].shape)
print("yid.shape",yid.shape)
print("ylen.shape",ylen.shape)
# predictions[0].shape (256, 640, 4)
# predictions[1].shape (256, 640, 33)
# yid.shape (256, 640, 4)
# ylen.shape (256, 640, 29)
np.save("test.0.estimate",predictions[0])
np.save("test.1.estimate",predictions[1])
np.save("test.0.truth",yid)
np.save("test.1.truth",ylen)
#### dump the predictions with true position index
for myclass in [1,0]:
print("------",myclass)
numerr = 0
total = 0
fp = open("test.%d.preds.txt" % myclass,"w")
print("#myclass\tobject\tposition\ttruecallIdx\tdata",file=fp)
for ii in range(predictions[myclass].shape[0]):
for jj in range(predictions[myclass].shape[1]):
if myclass == 1:
truth = ylen[ii,jj]
else:
truth = yid[ii,jj]
estimate = predictions[myclass][ii,jj]
# skip null
# if truth[0]==0.25 and truth[1]==0.25 and truth[2]==0.25 and truth[3]==0.25:
# continue
truemax = np.argmax(truth)
estmax = np.argmax(estimate)
if truemax!=estmax:
numerr+=1
print("err %d %d %d true est prob" % (myclass, ii, jj), truemax,estmax,estimate[estmax])
total+=1
print("%d\t%d\t%d\t%d\t%d\t%s" % (myclass,
ii,
jj,
truemax,
estmax,
"\t".join([str(xx) for xx in estimate])
), file=fp)
fp.close()
print("error rate 1 %f = %d / %d" % (float(numerr)/total,numerr,total))
break # only look at 0th batch for time
if __name__ == '__main__':
exec(open(sys.argv[1]).read())
for aa in sys.argv:
if "EXEC:" in aa:
toexec = aa.replace("EXEC:","")
print("toexec",toexec)
exec(toexec)
print("-------")
print(help(args))
print("-------")
test(args)