-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathevaler.py
156 lines (118 loc) · 5.16 KB
/
evaler.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from six.moves import xrange
from util import log
from model_classifier import Model
from input_ops import create_input_ops, check_data_id
import os
import time
import numpy as np
import tensorflow as tf
class Evaler(object):
def __init__(self,
config,
dataset):
self.config = config
self.train_dir = config.train_dir
log.info("self.train_dir = %s", self.train_dir)
# --- input ops ---
self.batch_size = config.batch_size
self.dataset = dataset
check_data_id(dataset, config.data_id)
_, self.batch = create_input_ops(dataset, self.batch_size,
data_id=config.data_id,
is_training=False,
shuffle=False)
# --- create model ---
self.model = Model(config)
self.global_step = tf.contrib.framework.get_or_create_global_step(graph=None)
self.step_op = tf.no_op(name='step_no_op')
tf.set_random_seed(1234)
session_config = tf.ConfigProto(
allow_soft_placement=True,
gpu_options=tf.GPUOptions(allow_growth=True),
device_count={'GPU': 1},
)
self.session = tf.Session(config=session_config)
# --- checkpoint and monitoring ---
self.saver = tf.train.Saver(max_to_keep=100)
self.checkpoint_path = config.checkpoint_path
if self.checkpoint_path is None and self.train_dir:
self.checkpoint_path = tf.train.latest_checkpoint(self.train_dir)
if self.checkpoint_path is None:
log.warn("No checkpoint is given. Just random initialization :-)")
self.session.run(tf.global_variables_initializer())
else:
log.info("Checkpoint path : %s", self.checkpoint_path)
def eval_run(self):
# load checkpoint
if self.checkpoint_path:
self.saver.restore(self.session, self.checkpoint_path)
log.info("Loaded from checkpoint!")
log.infov("Start 1-epoch Inference and Evaluation")
log.info("# of examples = %d", len(self.dataset))
length_dataset = len(self.dataset)
max_steps = int(length_dataset / self.batch_size) + 1
log.info("max_steps = %d", max_steps)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(self.session,
coord=coord, start=True)
accuracy_total = []
for s in xrange(max_steps):
step, accuracy, step_time = \
self.run_single_step(self.batch)
self.log_step_message(s, accuracy, step_time)
accuracy_total.append(accuracy)
avg = np.average(accuracy_total)
log.infov("Average accuracy: {}%".format(avg*100))
log.infov("Evaluation complete.")
def run_single_step(self, batch, step=None, is_train=True):
_start_time = time.time()
batch_chunk = self.session.run(batch)
[step, accuracy, _] = self.session.run(
[self.global_step, self.model.accuracy, self.step_op],
feed_dict=self.model.get_feed_dict(batch_chunk)
)
_end_time = time.time()
return step, accuracy, (_end_time - _start_time)
def log_step_message(self, step, accuracy, step_time, is_train=False):
if step_time == 0: step_time = 0.001
log_fn = (is_train and log.info or log.infov)
log_fn((" [{split_mode:5s} step {step:4d}] " +
"batch total-accuracy (test): {test_accuracy:.2f}% " +
"({sec_per_batch:.3f} sec/batch, {instance_per_sec:.3f} instances/sec) "
).format(split_mode=(is_train and 'train' or 'val'),
step=step,
test_accuracy=accuracy*100,
sec_per_batch=step_time,
instance_per_sec=self.batch_size / step_time,
)
)
def check_data_path(path):
if os.path.isfile(os.path.join(path, 'data.hy')) \
and os.path.isfile(os.path.join(path, 'id.txt')):
return True
else:
return False
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--checkpoint_path', type=str)
parser.add_argument('--train_dir', type=str)
parser.add_argument('--dataset', type=str, default='ImageNet', choices=['ImageNet'])
parser.add_argument('--data_id', nargs='*', default=None)
config = parser.parse_args()
if config.dataset == 'ImageNet':
import datasets.ImageNet as dataset
else:
raise ValueError(config.dataset)
_, dataset = dataset.create_default_splits(ratio=0.999)
image, _, label, _ = dataset.get_data(dataset.ids[0], dataset.ids[0])
config.data_info = np.concatenate([np.asarray(image.shape), np.asarray(label.shape)])
evaler = Evaler(config, dataset)
log.warning("dataset: %s", dataset)
evaler.eval_run()
if __name__ == '__main__':
main()