-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathDS_test.py
221 lines (185 loc) · 8.26 KB
/
DS_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
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
import json
import os
import math
import time
import argparse
from datetime import datetime
import deepSpeech
import numpy as np
import tensorflow as tf
from Levenshtein import distance
# Note this definition must match the ALPHABET chosen in
# preprocess_Librispeech.py
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ' "
IX_TO_CHAR = {i: ch for (i, ch) in enumerate(ALPHABET)}
def parse_args():
""" Parses command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('--eval_dir', type=str,
default='../models/librispeech/eval',
help='Directory to write event logs')
parser.add_argument('--checkpoint_dir', type=str,
default='../models/librispeech/train',
help='Directory where to read model checkpoints.')
parser.add_argument('--eval_data', type=str, default='val',
help="Either 'test' or 'val' or 'train' ")
parser.add_argument('--batch_size', type=int, default=32,
help='Number of feats to process in a batch')
parser.add_argument('--eval_interval_secs', type=int, default=60 * 5,
help='How often to run the eval')
parser.add_argument('--data_dir', type=str,
default='../data/librispeech/processed/',
help='Path to the deepSpeech data directory')
parser.add_argument('--run_once', type=bool, default=False,
help='Whether to run eval only once')
args = parser.parse_args()
# Read saved parameters from file
param_file = os.path.join(args.checkpoint_dir,
'deepSpeech_parameters.json')
with open(param_file, 'r') as file:
params = json.load(file)
# Read network architecture parameters from
# previously saved parameter file.
args.num_hidden = params['num_hidden']
args.num_rnn_layers = params['num_rnn_layers']
args.rnn_type = params['rnn_type']
args.num_filters = params['num_filters']
args.use_fp16 = params['use_fp16']
args.temporal_stride = params['temporal_stride']
args.moving_avg_decay = params['moving_avg_decay']
return args
def sparse_to_labels(sparse_matrix):
""" Convert index based transcripts to strings"""
results = ['']*sparse_matrix.dense_shape[0]
for i, val in enumerate(sparse_matrix.values.tolist()):
results[sparse_matrix.indices[i, 0]] += IX_TO_CHAR[val]
return results
def initialize_from_checkpoint(sess, saver):
""" Initialize variables on the graph"""
# Initialise variables from a checkpoint file, if provided.
ckpt = tf.train.get_checkpoint_state(ARGS.checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
# Restores from checkpoint
saver.restore(sess, ckpt.model_checkpoint_path)
# Assuming model_checkpoint_path looks something like:
# /my-favorite-path/train/model.ckpt-0,
# extract global_step from it.
checkpoint_path = ckpt.model_checkpoint_path
global_step = checkpoint_path.split('/')[-1].split('-')[-1]
return global_step
else:
print('No checkpoint file found')
return
def inference(predictions_op, true_labels_op, display, sess):
""" Perform inference per batch on pre-trained model.
This function performs inference and computes the CER per utterance.
Args:
predictions_op: Prediction op
true_labels_op: True Labels op
display: print sample predictions if True
sess: default session to evaluate the ops.
Returns:
char_err_rate: list of CER per utterance.
"""
char_err_rate = []
# Perform inference of batch worth of data at a time.
[predictions, true_labels] = sess.run([predictions_op,
true_labels_op])
pred_label = sparse_to_labels(predictions[0][0])
actual_label = sparse_to_labels(true_labels)
for (label, pred) in zip(actual_label, pred_label):
char_err_rate.append(distance(label, pred)/len(label))
if display:
# Print sample responses
for i in range(ARGS.batch_size):
print(actual_label[i] + ' vs ' + pred_label[i])
return char_err_rate
def eval_once(saver, summary_writer, predictions_op, summary_op,
true_labels_op):
"""Run Eval once.
Args:
saver: Saver.
summary_writer: Summary writer.
predictions_ops: Op to compute predictions.
summary_op: Summary op.
"""
with tf.Session() as sess:
# Initialize weights from checkpoint file.
global_step = initialize_from_checkpoint(sess, saver)
# Start the queue runners.
coord = tf.train.Coordinator()
try:
threads = []
for queue_runners in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
threads.extend(queue_runners.create_threads(sess, coord=coord,
daemon=True,
start=True))
# Only using a subset of the training data
if ARGS.eval_data == 'train':
num_examples = 2048
elif ARGS.eval_data == 'val':
num_examples = 2703
elif ARGS.eval_data == 'test':
num_examples = 2620
num_iter = int(math.ceil(num_examples / ARGS.batch_size))
step = 0
char_err_rate = []
while step < num_iter and not coord.should_stop():
char_err_rate.append(inference(predictions_op, true_labels_op,
step == 0, sess))
step += 1
# Compute and print mean CER
avg_cer = np.mean(char_err_rate)*100
print('%s: char_err_rate = %.3f %%' % (datetime.now(), avg_cer))
# Add summary ops
summary = tf.Summary()
summary.ParseFromString(sess.run(summary_op))
summary.value.add(tag='char_err_rate', simple_value=avg_cer)
summary_writer.add_summary(summary, global_step)
except Exception as exc: # pylint: disable=broad-except
coord.request_stop(exc)
# Close threads
coord.request_stop()
coord.join(threads, stop_grace_period_secs=10)
def evaluate():
""" Evaluate deepSpeech modelfor a number of steps."""
with tf.Graph().as_default() as graph:
# Get feats and labels for deepSpeech.
feats, labels, seq_lens = deepSpeech.inputs(ARGS.eval_data,
data_dir=ARGS.data_dir,
batch_size=ARGS.batch_size,
use_fp16=ARGS.use_fp16,
shuffle=True)
# Build ops that computes the logits predictions from the
# inference model.
ARGS.keep_prob = 1.0 # Disable dropout during testing.
logits = deepSpeech.inference(feats, seq_lens, ARGS)
# Calculate predictions.
output_log_prob = tf.nn.log_softmax(logits)
decoder = tf.nn.ctc_greedy_decoder
strided_seq_lens = tf.div(seq_lens, ARGS.temporal_stride)
predictions = decoder(output_log_prob, strided_seq_lens)
# Restore the moving average version of the learned variables for eval.
variable_averages = tf.train.ExponentialMovingAverage(
ARGS.moving_avg_decay)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
# Build the summary operation based on the TF collection of Summaries.
summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(ARGS.eval_dir, graph)
while True:
eval_once(saver, summary_writer, predictions, summary_op, labels)
if ARGS.run_once:
break
time.sleep(ARGS.eval_interval_secs)
def main():
"""
Create eval directory and perform inference on checkpointed model.
"""
if tf.gfile.Exists(ARGS.eval_dir):
tf.gfile.DeleteRecursively(ARGS.eval_dir)
tf.gfile.MakeDirs(ARGS.eval_dir)
evaluate()
if __name__ == '__main__':
ARGS = parse_args()
main()