-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.py
78 lines (70 loc) · 3.75 KB
/
utils.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
import numpy as np
import tensorflow as tf
def target_list_to_sparse_tensor(targetList):
'''make tensorflow SparseTensor from list of targets, with each element
in the list being a list or array with the values of the target sequence
(e.g., the integer values of a character map for an ASR target string)
See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ctc/ctc_loss_op_test.py
for example of SparseTensor format'''
indices = []
vals = []
for tI, target in enumerate(targetList):
for seqI, val in enumerate(target):
indices.append([tI, seqI])
vals.append(val)
shape = [len(targetList), np.asarray(indices).max(0)[1]+1]
return (np.array(indices), np.array(vals), np.array(shape))
def test_edit_distance():
graph = tf.Graph()
with graph.as_default():
truth = tf.sparse_placeholder(tf.int32)
hyp = tf.sparse_placeholder(tf.int32)
editDist = tf.edit_distance(hyp, truth, normalize=False)
with tf.Session(graph=graph) as session:
truthTest = sparse_tensor_feed([[0,1,2], [0,1,2,3,4]])
hypTest = sparse_tensor_feed([[3,4,5], [0,1,2,2]])
feedDict = {truth: truthTest, hyp: hypTest}
dist = session.run([editDist], feed_dict=feedDict)
print(dist)
def data_lists_to_batches(inputList, targetList, batchSize):
'''Takes a list of input matrices and a list of target arrays and returns
a list of batches, with each batch being a 3-element tuple of inputs,
targets, and sequence lengths.
inputList: list of 2-d numpy arrays with dimensions nFeatures x timesteps
targetList: list of 1-d arrays or lists of ints
batchSize: int indicating number of inputs/targets per batch
returns: dataBatches: list of batch data tuples, where each batch tuple (inputs, targets, seqLengths) consists of
inputs = 3-d array w/ shape nTimeSteps x batchSize x nFeatures
targets = tuple required as input for SparseTensor
seqLengths = 1-d array with int number of timesteps for each sample in batch
maxSteps: maximum number of time steps across all samples'''
assert len(inputList) == len(targetList)
nFeatures = inputList[0].shape[0]
maxSteps = max(inp.shape[1] for inp in inputList)
randIxs = np.random.permutation(len(inputList))
start, end = (0, batchSize)
dataBatches = []
while end <= len(inputList):
batchSeqLengths = np.zeros(batchSize)
for batchI, origI in enumerate(randIxs[start:end]):
batchSeqLengths[batchI] = inputList[origI].shape[-1]
batchInputs = np.zeros((maxSteps, batchSize, nFeatures))
batchTargetList = []
for batchI, origI in enumerate(randIxs[start:end]):
padSecs = maxSteps - inputList[origI].shape[1]
batchInputs[:,batchI,:] = np.pad(inputList[origI].T, ((0,padSecs),(0,0)),
'constant', constant_values=0)
batchTargetList.append(targetList[origI])
dataBatches.append((batchInputs, target_list_to_sparse_tensor(batchTargetList),
batchSeqLengths))
start += batchSize
end += batchSize
return (dataBatches, maxSteps)
def load_batched_data(specPath, targetPath, batchSize):
import os
'''returns 3-element tuple: batched data (list), max # of time steps (int), and
total number of samples (int)'''
return data_lists_to_batches([np.load(os.path.join(specPath, fn)) for fn in os.listdir(specPath)],
[np.load(os.path.join(targetPath, fn)) for fn in os.listdir(targetPath)],
batchSize) + \
(len(os.listdir(specPath)),)