-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_asr.py
executable file
·122 lines (97 loc) · 3.2 KB
/
run_asr.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
#!/usr/bin/env python3
"""
File [ run_asr.py ]
Author [ Heng-Jui Chang (MIT CSAIL) ]
Synopsis [ End-to-end ASR training. ]
"""
import argparse
import json
import logging
import os
from os.path import join
import torch
import torchaudio
import yaml
from easydict import EasyDict
from miniasr.bin.asr_trainer import create_asr_trainer, create_asr_trainer_test
from miniasr.utils import base_args, logging_args, override, set_random_seed
def parse_arguments() -> EasyDict:
"""Parses arguments from command line."""
parser = argparse.ArgumentParser("End-to-end ASR training.")
parser.add_argument(
"--config",
"-c",
type=str,
default="none",
help="Training configuration file (.yaml).",
)
# Testing
parser.add_argument(
"--test", "-t", action="store_true", help="Specify testing mode."
)
parser.add_argument(
"--ckpt", type=str, default="none", help="Checkpoint for testing."
)
parser.add_argument(
"--test-name",
type=str,
default="test_result",
help="Specify testing results' name.",
)
parser = base_args(parser) # Add basic arguments
args = parser.parse_args()
logging_args(args) # Set logging
if args.detect_anomaly:
torch.autograd.set_detect_anomaly(True)
# Load config file.
if args.config != "none":
config = yaml.load(open(args.config, "r"), Loader=yaml.FullLoader)
else:
config = {}
if args.ckpt != "none":
logging.info(f"Did not provide config file, using {args.ckpt} instead")
else:
raise RuntimeError("No config file and ckpt found!")
args = EasyDict({**config, **vars(args)})
if args.cpu:
args.trainer.gpus = 0
if args.override != "":
override(args.override, args)
return args
def main():
"""Main function of ASR training."""
# Basic setup
torch.multiprocessing.set_sharing_strategy("file_system")
torchaudio.set_audio_backend("sox_io")
# Parse arguments
args = parse_arguments()
# Set random seed for reproducibility
set_random_seed(args.seed)
# Create base directory
if args.test:
assert args.ckpt != "none"
# Path to save testing results.
args.test_res = "/".join(args.ckpt.split("/")[:-1])
# Save a copy of args
if args.config != "none":
os.makedirs(args.trainer.default_root_dir, exist_ok=True)
args_path = join(
args.trainer.default_root_dir, f"model_{args.mode}_config.yaml"
)
with open(args_path, "w") as fp:
yaml.dump(json.loads(json.dumps(args)), fp, indent=2, encoding="utf-8")
# Get device
device = torch.device("cpu" if args.cpu else "cuda:0")
if not args.test:
# Training
logging.info("Training mode.")
args, tr_loader, dv_loader, _, model, trainer = create_asr_trainer(args, device)
trainer.fit(model, tr_loader, dv_loader)
else:
# Testing
logging.info("Testing mode.")
args, _, dv_loader, _, model, trainer = create_asr_trainer_test(args, device)
model.eval()
trainer.test(model, dv_loader)
if __name__ == "__main__":
main()