-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
144 lines (104 loc) · 4.45 KB
/
test.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
import numpy as np
import os,sys,inspect
import tensorflow as tf
import time
from datetime import datetime
import os
import hickle as hkl
import os.path as osp
from glob import glob
import sklearn.metrics as metrics
from input import Dataset
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import model
import globals as g_
import cv2
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('train_dir', 'tmp/',
"""Directory where to write event logs """
"""and checkpoint.""")
tf.app.flags.DEFINE_integer('max_steps', 1000000,
"""Number of batches to run.""")
tf.app.flags.DEFINE_boolean('log_device_placement', False,
"""Whether to log device placement.""")
tf.app.flags.DEFINE_string('weights', '',
"""finetune with a pretrained model""")
tf.app.flags.DEFINE_string('caffemodel', '',
"""finetune with a model converted by caffe-tensorflow""")
# Constants describing the training process.
MOVING_AVERAGE_DECAY = 0.9999 # The decay to use for the moving average.
NUM_EPOCHS_PER_DECAY = 20.0 # Epochs after which learning rate decays.
LEARNING_RATE_DECAY_FACTOR = 0.05 # Learning rate decay factor.
np.set_printoptions(precision=3)
def test(dataset, ckptfile):
print 'train() called'
batch_size = FLAGS.batch_size
data_size = dataset.size()
print 'training size:', data_size
with tf.Graph().as_default():
startstep = 0
global_step = tf.Variable(startstep, trainable=False)
image_, y_ = model.input()
keep_prob_ = tf.placeholder('float32', name='keep_prob')
phase_train_ = tf.placeholder(tf.bool, name='phase_train')
logits = model.inference(image_, keep_prob_, phase_train_)
prediction = model.classify(logits)
loss, print_op = model.loss(logits, y_)
train_op = model.train(loss, global_step, data_size)
# build the summary operation based on the F colection of Summaries
summary_op = tf.merge_all_summaries()
saver = tf.train.Saver(tf.all_variables(), max_to_keep=1000)
init_op = tf.initialize_all_variables()
sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement))
if FLAGS.caffemodel:
caffemodel = FLAGS.caffemodel
# sess.run(init_op)
model.load_model(sess, caffemodel, fc8=True)
print 'loaded pretrained caffemodel:', caffemodel
else:
saver.restore(sess, ckptfile)
print 'restore variables done'
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir,
graph_def=sess.graph_def)
step = startstep
predictions = []
labels = []
for batch_x, batch_y in dataset.batches(batch_size):
if step >= FLAGS.max_steps:
break
step += 1
if step == 1:
img = batch_x[0,...]
cv2.imwrite('img0.jpg', img)
start_time = time.time()
feed_dict = {image_: batch_x,
y_ : batch_y,
keep_prob_: 1.0}
pred, loss_value = sess.run(
[prediction, loss,],
feed_dict=feed_dict)
duration = time.time() - start_time
assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
if step % 10 == 0:
sec_per_batch = float(duration)
print '%s: step %d, loss=%.2f (%.1f examples/sec; %.3f sec/batch)' \
% (datetime.now(), step, loss_value,
FLAGS.batch_size/duration, sec_per_batch)
predictions.extend(pred.tolist())
labels.extend(batch_y.tolist())
# print pred
# print batch_y
print labels
print predictions
acc = metrics.accuracy_score(labels, predictions)
print 'acc:', acc*100
def main(argv):
st = time.time()
print 'start loading data'
dataset = Dataset(g_.IMAGE_LIST_TEST, subtract_mean=True, name='test')
print 'done loading data, time=', time.time() - st
test(dataset, FLAGS.weights)
if __name__ == '__main__':
main(sys.argv)