-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtensorflow_lstm_ctc_decode.py
executable file
·62 lines (49 loc) · 1.63 KB
/
tensorflow_lstm_ctc_decode.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
#!/usr/bin/env python3
from __future__ import generator_stop
import time
# On my setup an annoying, but benign warning keeps appearing, messing
# with my visual cortex impeding testing.
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import tensorflow as tf
import numpy as np
import sys
import scipy.io.wavfile
import os
from config import *
from tensorflow_lstm_ctc_train import *
def main(args):
if len(args) < 2:
print("Usage: ./tensorflow_lstm_ctc_decode.py <filename.wav>")
exit()
rate, data = scipy.io.wavfile.read(args[1])
timesteps = len(data) // CHUNK
data = np.asarray(data[:timesteps * CHUNK], dtype=np.float32)
data = (data - np.mean(data)) / np.std(data)
# For prediction (or decoding), a separate,
# smaller estimator is created
estimator = tf.estimator.Estimator(
model_fn=cw_model,
model_dir='./model_use',
params={
'max_timesteps': timesteps,
'batch_size': 1,
'num_features': CHUNK,
'input_layer_depth': 0,
'input_layer_width': CHUNK,
'recurrent_layer_depth': 2,
'recurrent_layer_width': 128,
'output_layer_depth': 1,
'output_layer_width': 128
}
)
def wav_input_fn():
return tf.data.Dataset.from_tensors((tf.reshape(data, (timesteps,CHUNK)), []))
res = estimator.predict(
input_fn=wav_input_fn
)
for r in res:
decoded_str = list(map(lambda x: MORSE_CHR[x], r['decoded']))
print(''.join(decoded_str))
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)