-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
65 lines (49 loc) · 1.89 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
from model import ModelTF, ModelRNN, ModelCTC
import pytorch_lightning as pl
import argparse
from config import Config, initialize
from typing import Any
import os
from pytorch_lightning.callbacks import ModelCheckpoint
if __name__ == '__main__':
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--num_workers', type=int, default=8)
# Add trainer options to parser
parser = pl.Trainer.add_argparse_args(parser)
# figure out which model to use
parser.add_argument('model_name', type=str, choices=['tf', 'rnn', 'ctc'], help='Transformer or RNN or CTC')
parser.add_argument('config_path', type=str)
# THIS LINE IS KEY TO PULL THE MODEL NAME
temp_args, _ = parser.parse_known_args()
# Add model options to parser
Model: Any
if temp_args.model_name == 'tf':
parser = ModelTF.add_model_specific_args(parser)
Model = ModelTF
elif temp_args.model_name == 'rnn':
parser = ModelRNN.add_model_specific_args(parser)
Model = ModelRNN
elif temp_args.model_name == 'ctc':
parser = ModelCTC.add_model_specific_args(parser)
Model = ModelCTC
args = parser.parse_args()
checkpoint_callback = ModelCheckpoint(
filepath=None,
save_top_k=1,
verbose=True,
monitor='WER',
mode='min',
prefix=''
)
trainer = pl.Trainer.from_argparse_args(args, checkpoint_callback=checkpoint_callback)
dict_args = vars(args)
config = Config(dict_args.pop('config_path'), **dict_args).config
# pl.seed_everything(dict_args['seed'])
# cnn = initialize(config['cnn'])
# pl.seed_everything(dict_args['seed'])
# vocab = initialize(config['vocab'], add_blank=False)
pl.seed_everything(dict_args['seed'])
model = Model(config)
pl.seed_everything(dict_args['seed'])
trainer.fit(model)