forked from yaojieliu/ECCV20-STDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
93 lines (82 loc) · 3.65 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
# Copyright 2020
#
# Yaojie Liu, Joel Stehouwer, Xiaoming Liu, Michigan State University
#
# All Rights Reserved.
#
# This research is based upon work supported by the Office of the Director of
# National Intelligence (ODNI), Intelligence Advanced Research Projects Activity
# (IARPA), via IARPA R&D Contract No. 2017-17020200004. The views and
# conclusions contained herein are those of the authors and should not be
# interpreted as necessarily representing the official policies or endorsements,
# either expressed or implied, of the ODNI, IARPA, or the U.S. Government. The
# U.S. Government is authorized to reproduce and distribute reprints for
# Governmental purposes not withstanding any copyright annotation thereon.
# ==============================================================================
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import time
from model.dataset import Dataset
from model.config import Config
from model.model import Gen
def _step(config, data_batch, training_nn):
global_step = tf.train.get_or_create_global_step()
bsize = config.BATCH_SIZE
imsize = config.IMAGE_SIZE
# Get images and labels for CNN.
img, im_name = data_batch.nextit
img = tf.reshape(img, [bsize, imsize, imsize, 3])
# Forward the Generator
M, s, b, C, T = Gen(img, training_nn=training_nn, scope='STDN')
M = tf.reduce_mean(M, axis=[1,2,3])
s = tf.reduce_mean(s, axis=[1,2,3])
b = tf.reduce_mean(b, axis=[1,2,3])
C = tf.reduce_mean(C, axis=[1,2,3])
T = tf.reduce_mean(T, axis=[1,2,3])
return M, s, b, C, T, im_name
def main(argv=None):
# Configurations
config = Config(gpu='1',
root_dir='./data/test/',
root_dir_val=None,
mode='testing')
config.BATCH_SIZE = 1
# Get images and labels.
dataset_test = Dataset(config, 'test')
# Train
_M, _s, _b, _C, _T, _imname = _step(config, dataset_test, False)
# Add ops to save and restore all the variables.
saver = tf.train.Saver(max_to_keep=50,)
with tf.Session(config=config.GPU_CONFIG) as sess:
# Restore the model
ckpt = tf.train.get_checkpoint_state(config.LOG_DIR)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
last_epoch = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
print('**********************************************************')
print('Restore from Epoch '+str(last_epoch))
print('**********************************************************')
else:
init = tf.initializers.global_variables()
last_epoch = 0
sess.run(init)
print('**********************************************************')
print('Train from scratch.')
print('**********************************************************')
step_per_epoch = int(len(dataset_test.name_list) / config.BATCH_SIZE)
with open(config.LOG_DIR + '/test/score.txt', 'w') as f:
for step in range(step_per_epoch):
M, s, b, C, T, imname = sess.run([_M, _s, _b, _C, _T, _imname])
# save the score
for i in range(config.BATCH_SIZE):
_name = imname[i].decode('UTF-8')
_line = _name + ',' + str("{0:.3f}".format(M[i])) + ','\
+ str("{0:.3f}".format(s[i])) + ','\
+ str("{0:.3f}".format(b[i])) + ','\
+ str("{0:.3f}".format(C[i])) + ','\
+ str("{0:.3f}".format(T[i]))
f.write(_line + '\n')
print(str(step+1)+'/'+str(step_per_epoch)+':'+_line, end='\r')
print("\n")
if __name__ == '__main__':
tf.app.run()