-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_c3d_fcn.py
140 lines (133 loc) · 6.46 KB
/
predict_c3d_fcn.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
import os
import time
import numpy
from six.moves import xrange # pylint: disable=redefined-builtin
import tensorflow as tf
import input_test_data
import c3d_model
import fcn_model
import math
import numpy as np
import sys
from utils import placeholder_inputs, _variable_with_weight_decay, fcn_model_loss, tower_acc
# Basic model parameters as external flags.
flags = tf.app.flags
flags.DEFINE_integer('batch_size',1 , 'Batch size.')
flags.DEFINE_boolean('output_to_file', True , 'print outputs to files or to the screen')
FLAGS = flags.FLAGS
pre_model_save_dir = './models/SGD'
def run_testing():
with tf.Graph().as_default():
global_step = tf.get_variable(
'global_step',
[],
initializer=tf.constant_initializer(0),
trainable=False
)
with tf.variable_scope('var_name') as var_scope:
weights = {
'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.005),
'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.005),
'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.005),
'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.005),
'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.005),
'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.005),
'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.005),
'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.005),
#'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.005),
#'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.005),
#'out': _variable_with_weight_decay('wout', [4096, c3d_model.NUM_CLASSES], 0.005)
}
biases = {
'bc1': _variable_with_weight_decay('bc1', [64], 0.000),
'bc2': _variable_with_weight_decay('bc2', [128], 0.000),
'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000),
'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000),
'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000),
'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000),
'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000),
'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000),
#'bd1': _variable_with_weight_decay('bd1', [4096], 0.000),
#'bd2': _variable_with_weight_decay('bd2', [4096], 0.000),
#'out': _variable_with_weight_decay('bout', [c3d_model.NUM_CLASSES], 0.000),
}
fcn_weights = {
'wconv6': _variable_with_weight_decay('conv6', [1, 4, 4, 512, 512], 0.005),
'wup6': _variable_with_weight_decay('up6', [2, 1, 1, 4096, 512], 0.005),
'wup7': _variable_with_weight_decay('up7', [2, 1, 1, 4096, 4096], 0.005),
'wup8': _variable_with_weight_decay('up8', [2, 1, 1, fcn_model.NUM_CLASSES, 4096], 0.005),
}
fcn_biases = {
'bconv6': _variable_with_weight_decay('bconv6', [512], 0.000),
'bup6': _variable_with_weight_decay('bup6', [4096], 0.000),
'bup7': _variable_with_weight_decay('bup7', [4096], 0.000),
'bup8': _variable_with_weight_decay('bup8', [fcn_model.NUM_CLASSES], 0.000),
}
with tf.name_scope('inputs'):
images_placeholder, labels_placeholder, keep_pro = placeholder_inputs( FLAGS.batch_size )
feature_map = c3d_model.inference_c3d(
images_placeholder,
keep_pro,
FLAGS.batch_size,
weights,
biases
)
logit=fcn_model.inference_fcn5(
feature_map,
keep_pro,
FLAGS.batch_size,
fcn_weights,
fcn_biases
)
loss = fcn_model_loss(
logit,
labels_placeholder,
FLAGS.batch_size
)
accuracy = tower_acc(logit, labels_placeholder, FLAGS.batch_size)
predictions = tf.nn.top_k(logit, 1)
# Create a saver for writing training checkpoints.
new_saver = tf.train.Saver(weights.values() + biases.values()+ fcn_weights.values() + fcn_biases.values())
init = tf.global_variables_initializer()
# Create a session for running Ops on the Graph.
sess = tf.Session(
config=tf.ConfigProto(allow_soft_placement=True)
)
sess.run(init)
ckpt = tf.train.get_checkpoint_state(pre_model_save_dir)
if ckpt and ckpt.model_checkpoint_path:
print "loading checkpoint,waiting......"
new_saver.restore(sess, ckpt.model_checkpoint_path)
print "load complete!"
if FLAGS.output_to_file:
# all output will be stored in 'output.txt'
print('outputs will be stored in test.txt')
sys.stdout = open( 'test.txt', 'a', 1)
predict_list = []
label_list = []
for i in xrange(3358):
start_time = time.time()
test_images, test_labels, _, _, _, _ = input_test_data.read_clip_and_label(
filename='annotation/test.list',
batch_size=1,
start_pos=-1,
num_frames_per_clip=c3d_model.NUM_FRAMES_PER_CLIP,
crop_size=c3d_model.CROP_SIZE,
video_list=[]
)
acc, predict = sess.run([accuracy, predictions], feed_dict={
images_placeholder: test_images,
labels_placeholder: test_labels,
keep_pro: 1
})
print ('acc: {}'.format(acc))
print ('predict: {}'.format(np.reshape(predict[1], [32])))
predict_list.append(np.reshape(predict[1], [32]))
print ('labels: {}'.format(np.reshape(test_labels, [32])))
label_list.append(np.reshape(test_labels, [32]))
np.save('./test/predict', predict_list)
np.save('./test/label', label_list)
def main(_):
run_testing()
if __name__ == '__main__':
tf.app.run()