train.py 3.8 KB
Newer Older
Pingchuan Ma's avatar
Pingchuan Ma committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import logging
import os
from argparse import ArgumentParser

import sentencepiece as spm
from average_checkpoints import ensemble
from pytorch_lightning import seed_everything, Trainer
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
from pytorch_lightning.strategies import DDPStrategy
from transforms import get_data_module


def get_trainer(args):
    seed_everything(1)

    checkpoint = ModelCheckpoint(
Pingchuan Ma's avatar
Pingchuan Ma committed
17
        dirpath=os.path.join(args.exp_dir, args.exp_name) if args.exp_dir else None,
Pingchuan Ma's avatar
Pingchuan Ma committed
18
19
20
        monitor="monitoring_step",
        mode="max",
        save_last=True,
21
        filename="{epoch}",
Pingchuan Ma's avatar
Pingchuan Ma committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        save_top_k=10,
    )
    lr_monitor = LearningRateMonitor(logging_interval="step")
    callbacks = [
        checkpoint,
        lr_monitor,
    ]
    return Trainer(
        sync_batchnorm=True,
        default_root_dir=args.exp_dir,
        max_epochs=args.epochs,
        num_nodes=args.num_nodes,
        devices=args.gpus,
        accelerator="gpu",
        strategy=DDPStrategy(find_unused_parameters=False),
        callbacks=callbacks,
        reload_dataloaders_every_n_epochs=1,
39
        gradient_clip_val=10.0,
Pingchuan Ma's avatar
Pingchuan Ma committed
40
41
42
43
44
    )


def get_lightning_module(args):
    sp_model = spm.SentencePieceProcessor(model_file=str(args.sp_model_path))
Pingchuan Ma's avatar
Pingchuan Ma committed
45
    if args.modality == "audiovisual":
Pingchuan Ma's avatar
Pingchuan Ma committed
46
47
48
49
50
51
52
53
54
55
56
57
58
        from lightning_av import AVConformerRNNTModule

        model = AVConformerRNNTModule(args, sp_model)
    else:
        from lightning import ConformerRNNTModule

        model = ConformerRNNTModule(args, sp_model)
    return model


def parse_args():
    parser = ArgumentParser()
    parser.add_argument(
Pingchuan Ma's avatar
Pingchuan Ma committed
59
        "--modality",
Pingchuan Ma's avatar
Pingchuan Ma committed
60
61
62
63
64
65
66
67
68
69
70
        type=str,
        help="Modality",
        required=True,
    )
    parser.add_argument(
        "--mode",
        type=str,
        help="Perform online or offline recognition.",
        required=True,
    )
    parser.add_argument(
71
        "--root-dir",
Pingchuan Ma's avatar
Pingchuan Ma committed
72
        type=str,
73
        help="Root directory to LRS3 audio-visual datasets.",
Pingchuan Ma's avatar
Pingchuan Ma committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        required=True,
    )
    parser.add_argument(
        "--sp-model-path",
        type=str,
        help="Path to SentencePiece model.",
        required=True,
    )
    parser.add_argument(
        "--pretrained-model-path",
        type=str,
        help="Path to Pretraned model.",
    )
    parser.add_argument(
        "--exp-dir",
Pingchuan Ma's avatar
Pingchuan Ma committed
89
        default="./exp",
Pingchuan Ma's avatar
Pingchuan Ma committed
90
91
92
93
        type=str,
        help="Directory to save checkpoints and logs to. (Default: './exp')",
    )
    parser.add_argument(
Pingchuan Ma's avatar
Pingchuan Ma committed
94
        "--exp-name",
Pingchuan Ma's avatar
Pingchuan Ma committed
95
96
97
98
99
        type=str,
        help="Experiment name",
    )
    parser.add_argument(
        "--num-nodes",
Pingchuan Ma's avatar
Pingchuan Ma committed
100
        default=4,
Pingchuan Ma's avatar
Pingchuan Ma committed
101
        type=int,
Pingchuan Ma's avatar
Pingchuan Ma committed
102
        help="Number of nodes to use for training. (Default: 4)",
Pingchuan Ma's avatar
Pingchuan Ma committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    )
    parser.add_argument(
        "--gpus",
        default=8,
        type=int,
        help="Number of GPUs per node to use for training. (Default: 8)",
    )
    parser.add_argument(
        "--epochs",
        default=55,
        type=int,
        help="Number of epochs to train for. (Default: 55)",
    )
    parser.add_argument(
Pingchuan Ma's avatar
Pingchuan Ma committed
117
118
119
120
121
122
123
124
125
        "--resume-from-checkpoint",
        default=None,
        type=str,
        help="Path to the checkpoint to resume from",
    )
    parser.add_argument(
        "--debug",
        action="store_true",
        help="Whether to use debug level for logging",
Pingchuan Ma's avatar
Pingchuan Ma committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    )
    return parser.parse_args()


def init_logger(debug):
    fmt = "%(asctime)s %(message)s" if debug else "%(message)s"
    level = logging.DEBUG if debug else logging.INFO
    logging.basicConfig(format=fmt, level=level, datefmt="%Y-%m-%d %H:%M:%S")


def cli_main():
    args = parse_args()
    init_logger(args.debug)
    model = get_lightning_module(args)
    data_module = get_data_module(args, str(args.sp_model_path))
    trainer = get_trainer(args)
    trainer.fit(model, data_module)

    ensemble(args)


if __name__ == "__main__":
    cli_main()