forked from espnet/espnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uasr_extract_feature.py
179 lines (158 loc) · 4.79 KB
/
uasr_extract_feature.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
#!/usr/bin/env python3
import argparse
import logging
import sys
from pathlib import Path
from typing import Optional, Sequence, Tuple, Union
from torch.nn.parallel import data_parallel
from typeguard import check_argument_types
from espnet2.fileio.npy_scp import NpyScpWriter
from espnet2.tasks.uasr import UASRTask
from espnet2.torch_utils.device_funcs import to_device
from espnet2.torch_utils.forward_adaptor import ForwardAdaptor
from espnet2.utils.types import str2bool, str2triple_str, str_or_none
from espnet.utils.cli_utils import get_commandline_args
def get_parser():
parser = argparse.ArgumentParser(
description="UASR Decoding",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--data_path_and_name_and_type",
type=str2triple_str,
required=True,
action="append",
)
parser.add_argument(
"--uasr_train_config",
type=str,
help="uasr training configuration",
)
parser.add_argument(
"--uasr_model_file",
type=str,
help="uasr model parameter file",
)
parser.add_argument(
"--key_file",
type=str_or_none,
help="key file",
)
parser.add_argument(
"--allow_variable_data_keys",
type=str2bool,
default=False,
)
parser.add_argument(
"--ngpu",
type=int,
default=0,
help="The number of gpus. 0 indicates CPU mode",
)
parser.add_argument(
"--num_workers",
type=int,
default=1,
help="The number of workers used for DataLoader",
)
parser.add_argument(
"--batch_size",
type=int,
default=1,
help="The batch size for feature extraction",
)
parser.add_argument(
"--dtype",
default="float32",
choices=["float16", "float32", "float64"],
help="Data type",
)
parser.add_argument(
"--dset",
type=str,
help="dataset",
)
parser.add_argument(
"--output_dir",
type=str,
help="Output directory",
)
parser.add_argument(
"--log_level",
type=lambda x: x.upper(),
default="INFO",
choices=("ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"),
help="The verbose level of logging",
)
return parser
def extract_feature(
uasr_train_config: Optional[str],
uasr_model_file: Optional[str],
data_path_and_name_and_type: Sequence[Tuple[str, str, str]],
key_file: Optional[str],
batch_size: int,
dtype: str,
num_workers: int,
allow_variable_data_keys: bool,
ngpu: int,
output_dir: str,
dset: str,
log_level: Union[int, str],
):
assert check_argument_types()
logging.basicConfig(
level=log_level,
format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
)
output_dir_path = Path(output_dir)
if ngpu >= 1:
device = "cuda"
else:
device = "cpu"
uasr_model, uasr_train_args = UASRTask.build_model_from_file(
uasr_train_config, uasr_model_file, device
)
test_iter = UASRTask.build_streaming_iterator(
data_path_and_name_and_type=data_path_and_name_and_type,
key_file=key_file,
batch_size=batch_size,
dtype=dtype,
num_workers=num_workers,
preprocess_fn=UASRTask.build_preprocess_fn(uasr_train_args, False),
collate_fn=UASRTask.build_collate_fn(uasr_train_args, False),
allow_variable_data_keys=allow_variable_data_keys,
inference=True,
)
npy_scp_writers = {}
for keys, batch in test_iter:
batch = to_device(batch, "cuda" if ngpu > 0 else "cpu")
if ngpu <= 1:
data = uasr_model.collect_feats(**batch)
else:
data = data_parallel(
ForwardAdaptor(uasr_model, "collect_feats"),
(),
range(ngpu),
module_kwargs=batch,
)
for key, v in data.items():
for i, (uttid, seq) in enumerate(zip(keys, v.cpu().detach().numpy())):
if f"{key}_lengths" in data:
length = data[f"{key}_lengths"][i]
seq = seq[:length]
else:
seq = seq[None]
if (key, dset) not in npy_scp_writers:
p = output_dir_path / dset / "collect_feats"
npy_scp_writers[(key, dset)] = NpyScpWriter(
p / f"data_{key}", p / f"{key}.scp"
)
npy_scp_writers[(key, dset)][uttid] = seq
def main(cmd=None):
print(get_commandline_args(), file=sys.stderr)
parser = get_parser()
args = parser.parse_args(cmd)
kwargs = vars(args)
extract_feature(**kwargs)
if __name__ == "__main__":
main()