-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate.py
80 lines (58 loc) · 2.37 KB
/
evaluate.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import datetime
from pipeline import inputs
from libs.architecture import inference
from libs.utilities import *
def evaluator(params, checkpoint):
with tf.Graph().as_default() as graph:
# Inputs
images, labels = inputs(params)
# Model
logits = inference(params, images)
# Predictions
predictions = predict(logits)
scores = score_digits_in_image(predictions)
correct = correct_digits_in_image(scores, labels)
seq_acc = sequence_accuracy(predictions, labels)
dig_acc = digit_accuracy(correct, labels)
# Initializer
init = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer(),
name="initializer")
with tf.Session(graph=graph) as sess:
# Start session
sess.run(init)
# Saver
saver = tf.train.Saver(max_to_keep=500, name="Saver")
# Coordinator
coord = tf.train.Coordinator()
# Treads
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# Restore model
print("\nRestoring...\n", checkpoint)
saver.restore(sess, checkpoint)
start_time = datetime.now()
results = {
'checkpoint' : [],
'sequence_accuracy': [],
'digit_accuracy' : []
}
try:
step = 0
while not coord.should_stop():
seq_score, dig_score, y_pred, X_test, y_true = sess.run(
[seq_acc, dig_acc, predictions, images, labels])
results['sequence_accuracy'].append(seq_score)
results['digit_accuracy'].append(dig_score)
results['checkpoint'].append(checkpoint)
step += 1
except tf.errors.OutOfRangeError:
print('Stopping evaluation at {:4d} epochs, {:3d} steps.'.format(params.num_epochs, step))
finally:
coord.request_stop()
coord.join(threads)
sess.close()
print("Total time to run: {}".format(datetime.now() - start_time))
return results