-
Notifications
You must be signed in to change notification settings - Fork 1
/
testPhase.py
124 lines (95 loc) · 4.63 KB
/
testPhase.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
import os
import tensorflow as tf
import time
from datetime import datetime
import numpy as np
import config
import batch_inputs
import evaluation
import inference_gray
inference = inference_gray
IMAGE_SIZE = 176
FLAGS = tf.app.flags.FLAGS
def test():
tf.reset_default_graph()
with tf.Graph().as_default():
# ++++++++++++++++++++++++ TESTING INPUT LAODING ++++++++++++++++++++++++
x_test, y_test, id_test = batch_inputs.inputs(['./record/test.tfrecords'],
FLAGS.batch_size, True)
y_test = tf.one_hot(y_test, FLAGS.num_class)
x_test = tf.expand_dims(x_test, -1)
# y_train = tf.expand_dims(y_train, -1)
x_test = tf.image.resize_image_with_crop_or_pad(x_test, IMAGE_SIZE,
IMAGE_SIZE)
y_test = tf.image.resize_image_with_crop_or_pad(y_test, IMAGE_SIZE,
IMAGE_SIZE)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
is_training = tf.placeholder(tf.bool, name='is_training')
keep_prob = tf.placeholder(tf.float32, name="keep_probabilty")
images = tf.placeholder(tf.float32,
shape=[None,
FLAGS.image_h, FLAGS.image_w, FLAGS.image_c])
labels = tf.placeholder(tf.int64,
[None,
FLAGS.image_h, FLAGS.image_w, FLAGS.num_class])
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
print('++++++++ Mode building starts here +++++++++')
if FLAGS.model == "basic":
logits = inference.inference_basic(images, is_training)
elif FLAGS.model == "extended":
logits = inference.inference_extended(images, is_training)
elif FLAGS.model == "basic_dropout":
logits = inference.inference_basic_dropout(images, is_training, keep_prob)
elif FLAGS.model == "extended_dropout":
logits = inference.inference_extended_dropout(images, is_training, keep_prob)
else:
raise ValueError("The selected model does not exist")
sfm_logits = tf.nn.softmax(logits)
class_pred = tf.argmax(logits, axis=3)
y_test_argmax = tf.argmax(y_test, axis=3)
saver = tf.train.Saver()
with tf.Session() as sess:
# print saver.restore(sess, FLAGS.model_ckpt_dir)
print tf.train.latest_checkpoint('./ckpt_dir/')
saver.restore(sess, tf.train.latest_checkpoint('./ckpt_dir/'))
# sess.run(tf.variables_initializer(tf.global_variables()))
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
test_img_batch, test_lbl_batch, \
test_id_batch, y_test_argmax_val = sess.run(fetches=[x_test,
y_test,
id_test,
y_test_argmax])
print(test_lbl_batch.shape)
val_feed_dict = {images: test_img_batch,
labels: test_lbl_batch,
is_training: True,
keep_prob: 1.0}
class_pred_val, sfm_logits_val, pred = sess.run([class_pred, sfm_logits, logits], feed_dict=val_feed_dict)
print('SFM Logits Shape : ', sfm_logits_val.shape)
print('Unique values in SFM Logits : ', np.unique(sfm_logits_val))
print('Predicted Class Label Shape : ', class_pred_val.shape)
#
import h5py
h5f = h5py.File('results.h5', 'w')
for i in range(10):
img = test_img_batch[i, :, :, 0]
lbl = y_test_argmax_val[i, :, :]
pred_img = class_pred_val[i, :, :]
ID = test_id_batch[i]
h5f.create_dataset('image_{}'.format(i), data=img)
h5f.create_dataset('label_{}'.format(i), data=lbl)
h5f.create_dataset('pred_{}'.format(i), data=pred_img)
h5f.create_dataset('id_{}'.format(i), data=ID)
#
h5f.close()
print('H5 file written !!')
coord.request_stop()
coord.join(threads)
def main(args):
if True:
print("Testing the model!")
test()
if __name__ == "__main__":
tf.app.run() # wrapper that handles flags parsing.