-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheval.py
292 lines (212 loc) · 11.1 KB
/
eval.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import os
import shutil
import time
from pathlib import Path
import tensorflow as tf
import numpy as np
import models
import data_metrics
from data_utils import EvalDataReader, filter_labels
from data_provider import get_split
# Run evaluation with CPU
os.environ["CUDA_VISIBLE_DEVICES"] = '-1'
slim = tf.contrib.slim
DATASET_DIR = 'tfrecords/'
SENT_DATASET_DIR = 'tfrecords_1/'
WORD_DATASET_DIR = 'tfrecords_2/'
CKPT_DIR = 'checkpoints/'
TMP_DIR = 'tmp/'
LOG_PATH = 'ManuallyStore/log_eval.txt'
flags = tf.app.flags
flags.DEFINE_string('dataset_dir', DATASET_DIR, 'The tfrecords directory.') # specify the dataset path
flags.DEFINE_string('sent_dataset_dir', SENT_DATASET_DIR, 'The tfrecords directory for sentences')
flags.DEFINE_string('word_dataset_dir', WORD_DATASET_DIR, 'The tfrecords directory for words')
flags.DEFINE_string('checkpoint_dir', CKPT_DIR, 'The checkpoint directory.') # specify the saved model path
flags.DEFINE_integer('batch_size', 1, 'The batch size to use.')
flags.DEFINE_integer('hidden_units', 256, 'Recurrent network hidden units.')
flags.DEFINE_string('model', 'audio_model2', 'Which model is going to be used: audio, video, or both')
flags.DEFINE_integer('sequence_length', 100, 'Number of audio frames in one input')
flags.DEFINE_integer('eval_interval_secs', 75, 'The seconds to wait until next evaluation.')
flags.DEFINE_string('portion', 'Devel', '{Devel|Test} to evaluation on validation or test set.')
flags.DEFINE_string('data_unit', None, '{word|sentence} as data input')
flags.DEFINE_boolean('liking', True, 'Liking dimension is calculated in the losses function or not')
# tf.app.flags.DEFINE_string('log_dir', 'ckpt/eval', 'The checkpoint/evaluation directory.')
tf.app.flags.DEFINE_string('store_best_path', './ManuallyStore/current_best.ckpt', 'where to manually store model')
FLAGS = flags.FLAGS
def evaluate(file2eval, model_path):
with tf.Graph().as_default():
filename_queue = tf.FIFOQueue(capacity=1, dtypes=[tf.string])
# Load dataset
eval_reader = EvalDataReader(dataset_dir=FLAGS.dataset_dir,
batch_size=FLAGS.batch_size,
seq_length=FLAGS.sequence_length)
eval_reader.make_batches([file2eval])
audio_frames, word_embeddings, data_length, ground_truth = eval_reader.get_split()
num_batches = eval_reader.get_num_batches()
# Define model graph.
with slim.arg_scope([slim.layers.batch_norm, slim.layers.dropout], is_training=False):
prediction = models.get_model(FLAGS.model)(audio_frames,
emb=word_embeddings,
hidden_units=FLAGS.hidden_units)
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
saver.restore(sess, model_path)
evaluated_predictions = None
evaluated_labels = None
print('Evaluating file : {}'.format(file2eval))
sess.run(filename_queue.enqueue(file2eval))
for _ in range(num_batches):
pred, gt, s_len = sess.run([prediction, ground_truth, data_length])
pred_batch, gt_batch = [], []
for i, _ in enumerate(['arousal', 'valence', 'liking']):
pred_single, gt_single = filter_labels(pred[:, :, i],
gt[:, :, i],
s_len) # batch * seq * 1
pred_batch.append(pred_single)
gt_batch.append(gt_single)
pred_batch = tf.convert_to_tensor(pred_batch)
gt_batch = tf.convert_to_tensor(gt_batch)
if evaluated_predictions is not None:
evaluated_predictions = tf.concat([evaluated_predictions, pred_batch], axis=1)
evaluated_labels = tf.concat([evaluated_labels, gt_batch], axis=1)
else:
evaluated_predictions = pred_batch
evaluated_labels = gt_batch
evaluated_predictions = evaluated_predictions.eval()
evaluated_labels = evaluated_labels.eval()
for i in range(sess.run(filename_queue.size())):
sess.run(filename_queue.dequeue())
if sess.run(filename_queue.size()) != 0:
raise ValueError('Queue not empty!')
evaluated_predictions = evaluated_predictions.transpose()
evaluated_labels = evaluated_labels.transpose()
print(evaluated_predictions.shape, evaluated_labels.shape)
return evaluated_predictions, evaluated_labels
def evaluate_2(file2eval, model_path):
with tf.Graph().as_default():
filename_queue = tf.FIFOQueue(capacity=1, dtypes=[tf.string])
# Load dataset.
audio_frames, word_embeddings, labels = get_split(filename_queue, False,
FLAGS.batch_size, seq_length=FLAGS.sequence_length)
# Define model graph.
with slim.arg_scope([slim.layers.batch_norm, slim.layers.dropout], is_training=False):
predictions = models.get_model(FLAGS.model)(audio_frames,
emb=tf.cast(word_embeddings, tf.float32),
hidden_units=FLAGS.hidden_units)
coord = tf.train.Coordinator()
saver = tf.train.Saver(slim.get_variables_to_restore())
with tf.Session() as sess:
saver.restore(sess, model_path)
tf.train.start_queue_runners(sess=sess, coord=coord)
evaluated_predictions = []
evaluated_labels = []
nexamples = _get_num_examples(file2eval)
num_batches = int(math.ceil(nexamples / (float(FLAGS.sequence_length))))
print('Evaluating file : {}'.format(file2eval))
sess.run(filename_queue.enqueue(file2eval))
sess.run(filename_queue.enqueue(file2eval))
for i in range(num_batches):
prediction_, label_ = sess.run([predictions, labels])
evaluated_predictions.append(prediction_[0])
evaluated_labels.append(label_[0])
print(np.vstack(evaluated_predictions).shape)
evaluated_predictions = np.vstack(evaluated_predictions)[:nexamples]
print(np.vstack(evaluated_predictions).shape)
evaluated_labels = np.vstack(evaluated_labels)[:nexamples]
for i in range(sess.run(filename_queue.size())):
sess.run(filename_queue.dequeue())
if sess.run(filename_queue.size()) != 0:
raise ValueError('Queue not empty!')
coord.request_stop()
return evaluated_predictions, evaluated_labels
def _get_num_examples(tf_file):
c = 0
for _ in tf.python_io.tf_record_iterator(tf_file):
c += 1
return c
def copy2temporary(model_path):
shutil.copy(model_path + '.data-00000-of-00001', '{}temporary.ckpt.data-00000-of-00001'.format(TMP_DIR))
shutil.copy(model_path + '.index', '{}temporary.ckpt.index'.format(TMP_DIR))
shutil.copy(model_path + '.meta', '{}temporary.ckpt.meta'.format(TMP_DIR))
return '{}temporary.ckpt'.format(TMP_DIR)
# if you want to save the best model
def copy2best(model_path, inx):
shutil.copy(model_path + '.data-00000-of-00001', './Best_Audio_{}.ckpt.data-00000-of-00001'.format(inx))
shutil.copy(model_path + '.index', './Best_Audio_{}.ckpt.index'.format(inx))
shutil.copy(model_path + '.meta', './Best_Audio_{}.ckpt.meta'.format(inx))
def deltemporary(model_path):
os.remove(model_path + '.data-00000-of-00001')
os.remove(model_path + '.index')
os.remove(model_path + '.meta')
def main(_):
if FLAGS.data_unit == 'word':
dataset_dir = Path(FLAGS.word_dataset_dir)
elif FLAGS.data_unit == 'sentence':
dataset_dir = Path(FLAGS.sent_dataset_dir)
else:
dataset_dir = Path(FLAGS.dataset_dir)
best, inx = 0.62, 1
cnt = 0
while True:
if FLAGS.portion == 'Test':
model_path = FLAGS.store_best_path
else:
model_path = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
print('Current latest model: ' + model_path)
model_path = copy2temporary(model_path)
predictions, labels = None, None
eval_model = data_metrics.metric_graph()
eval_arousal = eval_model.eval_metric_arousal
eval_valence = eval_model.eval_metric_valence
eval_liking = eval_model.eval_metric_liking
files = os.listdir(str(dataset_dir))
portion_files = [str(dataset_dir / x) for x in files if FLAGS.portion in x]
for tf_file in portion_files:
predictions_file, labels_file = evaluate_2(str(tf_file), model_path)
print(tf_file)
if predictions is not None and labels is not None:
predictions = np.vstack((predictions, predictions_file))
labels = np.vstack((labels, labels_file))
else:
predictions = predictions_file
labels = labels_file
print(predictions.shape)
print(labels.shape)
with tf.Session() as sess:
e_arousal, e_valence, e_liking = sess.run([eval_arousal, eval_valence, eval_liking],
feed_dict={
eval_model.eval_predictions: predictions,
eval_model.eval_labels: labels
})
eval_res = np.array([e_arousal, e_valence, e_liking])
if FLAGS.liking:
eval_loss = 1 - (np.sum(eval_res) / eval_res.shape[0])
else:
eval_loss = 2 - eval_res[0] - eval_res[1]
print('Evaluation: %d, loss: _%.4f -- arousal: %.4f -- valence: %.4f -- liking: %.4f'
% (cnt, eval_loss, eval_res[0], eval_res[1], eval_res[2]))
cnt += 1
if eval_loss < best:
print('================================================================================')
if FLAGS.portion == 'Devel':
copy2best(model_path, inx)
log = open(LOG_PATH, 'a')
log.write('Evaluated Model %d: %s \n' % (inx, model_path))
log.write('Evaluated loss: %.4f, arousal: %.4f, valence: %.4f, liking: %.4f\n'
% (eval_loss, eval_res[0], eval_res[1], eval_res[2]))
log.write('========================================\n')
inx += 1
log.close()
else:
if FLAGS.portion == 'Devel':
print(model_path)
deltemporary(model_path)
print('Finished evaluation! Now waiting for {} secs'.format(FLAGS.eval_interval_secs))
time.sleep(FLAGS.eval_interval_secs)
if __name__ == '__main__':
tf.app.run()