forked from WrathOfGrapes/asr_project_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
129 lines (110 loc) · 3.9 KB
/
train.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
import argparse
import collections
import warnings
import numpy as np
import torch
import torch_optimizer as optim
import hw_asr.loss as module_loss
import hw_asr.metric as module_metric
import hw_asr.model as module_arch
from hw_asr.datasets.utils import get_dataloaders
from hw_asr.text_encoder.ctc_char_text_encoder import CTCCharTextEncoder
from hw_asr.trainer import Trainer
from hw_asr.utils import prepare_device
from hw_asr.utils.parse_config import ConfigParser
warnings.filterwarnings("ignore", category=UserWarning)
# fix random seeds for reproducibility
SEED = 123
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
# {
# "type": "BeamSearchWERMetric",
# "args": {
# "name": "WER (beam_search)"
# }
# },
# {
# "type": "BeamSearchCERMetric",
# "args": {
# "name": "CER (beam_search)"
# }
# }
# saved/models/default_config/1019_225927/checkpoint-epoch5.pth
# saved/models/default_config/1021_115508/
def main(config):
logger = config.get_logger("train")
# text_encoder
text_encoder = CTCCharTextEncoder.get_simple_alphabet()
# setup data_loader instances
dataloaders = get_dataloaders(config, text_encoder)
# build model architecture, then print to console
model = config.init_obj(config["arch"], module_arch, n_class=len(text_encoder))
logger.info(model)
# prepare for (multi-device) GPU training
# print(f"Train: {config['arch']['args']['model_name']}")
device, device_ids = prepare_device(config["n_gpu"])
model = model.to(device)
print(device, device_ids)
if len(device_ids) > 1:
model = torch.nn.DataParallel(model, device_ids=device_ids)
# get function handles of loss and metrics
loss_module = config.init_obj(config["loss"], module_loss).to(device)
metrics = [
config.init_obj(metric_dict, module_metric, text_encoder=text_encoder)
for metric_dict in config["metrics"]
]
# build optimizer, learning rate scheduler. delete every lines containing lr_scheduler for disabling scheduler
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optim_lib = optim if config["optimizer"]["type"] == "NovoGrad" else torch.optim
optimizer = config.init_obj(config["optimizer"], optim_lib, trainable_params)
lr_scheduler = config.init_obj(config["lr_scheduler"], torch.optim.lr_scheduler, optimizer)
valid_data_loader = dataloaders["train"] if config["trainer"]["overfit_on_one_batch"] else dataloaders["val"]
trainer = Trainer(
model,
loss_module,
metrics,
optimizer,
text_encoder=text_encoder,
config=config,
device=device,
data_loader=dataloaders["train"],
valid_data_loader=valid_data_loader,
lr_scheduler=lr_scheduler,
len_epoch=config["trainer"].get("len_epoch", None)
)
trainer.train()
if __name__ == "__main__":
args = argparse.ArgumentParser(description="PyTorch Template")
args.add_argument(
"-c",
"--config",
default=None,
type=str,
help="config file path (default: None)",
)
args.add_argument(
"-r",
"--resume",
default=None,
type=str,
help="path to latest checkpoint (default: None)",
)
args.add_argument(
"-d",
"--device",
default=None,
type=str,
help="indices of GPUs to enable (default: all)",
)
# custom cli options to modify configuration from default values given in json file.
CustomArgs = collections.namedtuple("CustomArgs", "flags type target")
options = [
CustomArgs(["--lr", "--learning_rate"], type=float, target="optimizer;args;lr"),
CustomArgs(
["--bs", "--batch_size"], type=int, target="data_loader;args;batch_size"
),
]
config = ConfigParser.from_args(args, options)
main(config)