This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
preprocess_librispeech.py
126 lines (94 loc) · 2.96 KB
/
preprocess_librispeech.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
from absl import app, logging, flags
import os
import json
import tensorflow as tf
from utils import preprocessing, encoding
from utils.data import librispeech
from hparams import *
FLAGS = flags.FLAGS
flags.DEFINE_string(
'data_dir', None,
'Directory to read Librispeech data from.')
flags.DEFINE_string(
'output_dir', './data',
'Directory to save preprocessed data.')
flags.DEFINE_integer(
'max_length', 0,
'Max audio length in seconds.')
def write_dataset(dataset, name):
filepath = os.path.join(FLAGS.output_dir,
'{}.tfrecord'.format(name))
writer = tf.data.experimental.TFRecordWriter(filepath)
writer.write(dataset)
logging.info('Wrote {} dataset to {}'.format(
name, filepath))
def main(_):
hparams = {
HP_TOKEN_TYPE: HP_TOKEN_TYPE.domain.values[1],
HP_VOCAB_SIZE: HP_VOCAB_SIZE.domain.values[0],
# Preprocessing
HP_MEL_BINS: HP_MEL_BINS.domain.values[0],
HP_FRAME_LENGTH: HP_FRAME_LENGTH.domain.values[0],
HP_FRAME_STEP: HP_FRAME_STEP.domain.values[0],
HP_HERTZ_LOW: HP_HERTZ_LOW.domain.values[0],
HP_HERTZ_HIGH: HP_HERTZ_HIGH.domain.values[0],
HP_DOWNSAMPLE_FACTOR: HP_DOWNSAMPLE_FACTOR.domain.values[0]
}
train_splits = [
'dev-clean'
]
dev_splits = [
'dev-clean'
]
test_splits = [
'dev-clean'
]
# train_splits = [
# 'train-clean-100',
# 'train-clean-360',
# 'train-other-500'
# ]
# dev_splits = [
# 'dev-clean',
# 'dev-other'
# ]
# test_splits = [
# 'test-clean',
# 'test-other'
# ]
_hparams = {k.name: v for k, v in hparams.items()}
texts_gen = librispeech.texts_generator(FLAGS.data_dir,
split_names=train_splits)
encoder_fn, decoder_fn, vocab_size = encoding.get_encoder(
encoder_dir=FLAGS.output_dir,
hparams=_hparams,
texts_generator=texts_gen)
_hparams[HP_VOCAB_SIZE.name] = vocab_size
train_dataset = librispeech.load_dataset(
FLAGS.data_dir, train_splits)
dev_dataset = librispeech.load_dataset(
FLAGS.data_dir, dev_splits)
test_dataset = librispeech.load_dataset(
FLAGS.data_dir, test_splits)
train_dataset = preprocessing.preprocess_dataset(
train_dataset,
encoder_fn=encoder_fn,
hparams=_hparams,
max_length=FLAGS.max_length,
save_plots=True)
write_dataset(train_dataset, 'train')
dev_dataset = preprocessing.preprocess_dataset(
dev_dataset,
encoder_fn=encoder_fn,
hparams=_hparams,
max_length=FLAGS.max_length)
write_dataset(dev_dataset, 'dev')
test_dataset = preprocessing.preprocess_dataset(
test_dataset,
encoder_fn=encoder_fn,
hparams=_hparams,
max_length=FLAGS.max_length)
write_dataset(test_dataset, 'test')
if __name__ == '__main__':
flags.mark_flag_as_required('data_dir')
app.run(main)