-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patheval.py
More file actions
223 lines (183 loc) · 9.28 KB
/
eval.py
File metadata and controls
223 lines (183 loc) · 9.28 KB
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
# Given C classes in the classification task,
# the output of the last layer in our network architecture is a
# vector with C elements, i.e., V = {v 1 , v 2 , · · · , v C }. Each
# element represents the probability that the subject belongs
# to that category. And the category with the largest value is
# the category it belongs to
import datetime
import os
import tensorflow as tf
import eval_data
from nets import model
slim = tf.contrib.slim
flags = tf.app.flags
FLAGS = flags.FLAGS
NUM_GROUP = 10
# temporary constant
MODELNET_EVAL_DATA_SIZE = 150
# Dataset settings.
flags.DEFINE_string('dataset_path', '/home/ace19/dl_data/modelnet/test.record',
'Where the dataset reside.')
flags.DEFINE_string('checkpoint_path',
os.getcwd() + '/models',
'Directory where to read training checkpoints.')
flags.DEFINE_integer('batch_size', 4, 'batch size')
flags.DEFINE_integer('num_views', 6, 'number of views')
flags.DEFINE_integer('height', 299, 'height')
flags.DEFINE_integer('width', 299, 'width')
flags.DEFINE_string('labels',
'airplane,bed,bookshelf,toilet,vase',
'number of classes')
def main(unused_argv):
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
labels = FLAGS.labels.split(',')
num_classes = len(labels)
# Define the model
X = tf.compat.v1.placeholder(tf.float32,
[None, FLAGS.num_views, FLAGS.height, FLAGS.width, 3],
name='X')
# final_X = tf.compat.v1.placeholder(tf.float32,
# [FLAGS.num_views, None, 8, 8, 1536],
# name='final_X')
ground_truth = tf.compat.v1.placeholder(tf.int64, [None], name='ground_truth')
is_training = tf.compat.v1.placeholder(tf.bool, name='is_training')
dropout_keep_prob = tf.compat.v1.placeholder(tf.float32, name='dropout_keep_prob')
# grouping_scheme = tf.placeholder(tf.bool, [NUM_GROUP, FLAGS.num_views])
# grouping_weight = tf.placeholder(tf.float32, [NUM_GROUP, 1])
g_scheme = tf.compat.v1.placeholder(tf.int32, [FLAGS.num_group, FLAGS.num_views])
g_weight = tf.compat.v1.placeholder(tf.float32, [FLAGS.num_group])
# # Grouping Module
# d_scores, _, final_desc = model.discrimination_score(X,
# num_classes,
# is_training)
# # GVCNN
# logits, _ = model.gvcnn(final_X,
# grouping_scheme,
# grouping_weight,
# num_classes,
# is_training2,
# dropout_keep_prob)
# GVCNN
view_scores, _, logits = model.gvcnn(X,
num_classes,
g_scheme,
g_weight,
is_training,
dropout_keep_prob)
# prediction = tf.nn.softmax(logits)
# predicted_labels = tf.argmax(prediction, 1)
# prediction = tf.argmax(logits, 1, name='prediction')
# correct_prediction = tf.equal(prediction, ground_truth)
# confusion_matrix = tf.confusion_matrix(
# ground_truth, prediction, num_classes=num_classes)
# accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
prediction = tf.argmax(logits, 1, name='prediction')
correct_prediction = tf.equal(prediction, ground_truth)
confusion_matrix = tf.math.confusion_matrix(ground_truth,
prediction,
num_classes=num_classes)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
################
# Prepare data
################
filenames = tf.compat.v1.placeholder(tf.string, shape=[])
eval_dataset = eval_data.Dataset(filenames,
FLAGS.num_views,
FLAGS.height,
FLAGS.width,
FLAGS.batch_size)
iterator = eval_dataset.dataset.make_initializable_iterator()
next_batch = iterator.get_next()
sess_config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
with tf.compat.v1.Session(config=sess_config) as sess:
sess.run(tf.compat.v1.global_variables_initializer())
# Create a saver object which will save all the variables
saver = tf.compat.v1.train.Saver()
if FLAGS.checkpoint_path:
if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
else:
checkpoint_path = FLAGS.checkpoint_path
saver.restore(sess, checkpoint_path)
# global_step = checkpoint_path.split('/')[-1].split('-')[-1]
# Get the number of training/validation steps per epoch
batches = int(MODELNET_EVAL_DATA_SIZE / FLAGS.batch_size)
if MODELNET_EVAL_DATA_SIZE % FLAGS.batch_size > 0:
batches += 1
##############
# prediction
##############
start_time = datetime.datetime.now()
tf.logging.info("Start prediction: %s" % start_time)
eval_filenames = os.path.join(FLAGS.dataset_path)
sess.run(iterator.initializer, feed_dict={filenames: eval_filenames})
count = 0;
total_acc = 0
total_conf_matrix = None
for i in range(batches):
batch_xs, batch_ys, _ = sess.run(next_batch)
# # Sets up a graph with feeds and fetches for partial runs.
# handle = sess.partial_run_setup([d_scores, final_desc,
# accuracy, confusion_matrix],
# [X, final_X, ground_truth,
# grouping_scheme, grouping_weight, is_training,
# is_training2, dropout_keep_prob])
#
# scores, final = sess.partial_run(handle,
# [d_scores, final_desc],
# feed_dict={
# X: batch_xs,
# is_training: False}
# )
# schemes = model.grouping_scheme(scores, NUM_GROUP, FLAGS.num_views)
# weights = model.grouping_weight(scores, schemes)
#
# # Run the graph with this batch of training data.
# acc, conf_matrix = \
# sess.partial_run(handle,
# [accuracy, confusion_matrix],
# feed_dict={
# final_X: final,
# ground_truth: batch_ys,
# grouping_scheme: schemes,
# grouping_weight: weights,
# is_training2: False,
# dropout_keep_prob: 1.0}
# )
# Sets up a graph with feeds and fetches for partial run.
handle = sess.partial_run_setup([view_scores, accuracy, confusion_matrix],
[X, g_scheme, g_weight,
ground_truth, is_training, dropout_keep_prob])
_view_scores = sess.partial_run(handle,
[view_scores],
feed_dict={
X: batch_xs,
is_training: False,
dropout_keep_prob: 1.0}
)
_g_schemes = model.group_scheme(_view_scores, FLAGS.num_group, FLAGS.num_views)
_g_weights = model.group_weight(_g_schemes)
# Run the graph with this batch of training data.
acc, conf_matrix = \
sess.partial_run(handle,
[accuracy, confusion_matrix],
feed_dict={
ground_truth: batch_ys,
g_scheme: _g_schemes,
g_weight: _g_weights}
)
total_acc += acc
count += 1
if total_conf_matrix is None:
total_conf_matrix = conf_matrix
else:
total_conf_matrix += conf_matrix
total_acc /= count
tf.compat.v1.logging.info('Confusion Matrix:\n %s' % (total_conf_matrix))
tf.compat.v1.logging.info('Final test accuracy = %.3f%% (N=%d)' %
(total_acc * 100, MODELNET_EVAL_DATA_SIZE))
end_time = datetime.datetime.now()
tf.compat.v1.logging.info('End prediction: %s' % end_time)
tf.compat.v1.logging.info('prediction waste time: %s' % (end_time - start_time))
if __name__ == '__main__':
tf.app.run()