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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#!/usr/bin/env python3
"""Fine-tune the HuBERTPretrainModel on 10 hours of LibriLightLimited dataset.
Example:
python finetune.py --dataset-path ./root/datasets/ --exp-dir ./exp_finetune \
--checkpoint /exp_iter2/checkpoints_librispeech_hubert_pretrain_base/epoch=361-step=399999.ckpt \
--gpus 1 --debug --warmup-updates 2000 --hold-updates 8000 --decay-updates 10000 \
--max-updates 20000 --learning-rate 5e-5
"""
import logging
import pathlib
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, RawDescriptionHelpFormatter
from typing import Tuple
from lightning.pytorch import seed_everything, Trainer
from lightning.pytorch.callbacks import ModelCheckpoint
from lightning_modules import HuBERTFineTuneModule
logger = logging.getLogger(__name__)
class _Formatter(ArgumentDefaultsHelpFormatter, RawDescriptionHelpFormatter):
# To use ArgumentDefaultsHelpFormatter as the formatter_class and
# RawDescriptionHelpFormatter to add custom formatting to description or epilog.
# Check: https://stackoverflow.com/a/18462760
pass
def run_train(args):
seed_everything(1337)
checkpoint_dir = args.exp_dir / f"checkpoints_{args.model_name}"
checkpoint = ModelCheckpoint(
checkpoint_dir,
monitor="val_loss",
mode="min",
save_top_k=5,
save_weights_only=False,
verbose=True,
)
train_checkpoint = ModelCheckpoint(
checkpoint_dir,
monitor="train_loss",
mode="min",
save_top_k=5,
save_weights_only=False,
verbose=True,
)
callbacks = [
checkpoint,
train_checkpoint,
]
trainer = Trainer(
default_root_dir=args.exp_dir,
max_steps=args.max_updates,
num_nodes=args.num_nodes,
devices=args.gpus,
accelerator="gpu",
strategy="ddp_find_unused_parameters_true",
use_distributed_sampler=False,
callbacks=callbacks,
reload_dataloaders_every_n_epochs=1,
val_check_interval=500,
check_val_every_n_epoch=None,
)
model = HuBERTFineTuneModule(
model_name=args.model_name,
encoder_projection_dropout=args.encoder_projection_dropout,
encoder_attention_dropout=args.encoder_attention_dropout,
encoder_ff_interm_dropout=args.encoder_ff_interm_dropout,
encoder_dropout=args.encoder_dropout,
encoder_layer_drop=args.encoder_layer_drop,
mask_prob=args.mask_prob,
mask_channel_prob=args.mask_channel_prob,
mask_channel_length=args.mask_channel_length,
num_classes=args.num_classes,
aux_num_out=args.aux_num_out,
checkpoint=args.checkpoint,
dataset_path=args.dataset_path,
seconds_per_batch=args.seconds_per_batch,
subset=args.subset,
learning_rate=args.learning_rate,
betas=args.betas,
adam_eps=args.adam_eps,
weight_decay=args.weight_decay,
freeze_encoder_updates=args.freeze_encoder_updates,
warmup_updates=args.warmup_updates,
hold_updates=args.hold_updates,
decay_updates=args.decay_updates,
)
trainer.fit(model, ckpt_path=args.resume_checkpoint)
def _parse_args():
parser = ArgumentParser(
description=__doc__,
formatter_class=_Formatter,
)
parser.add_argument(
"--dataset-path",
type=pathlib.Path,
required=True,
help="Path to the LibriSpeech and LibriLightLimited datasets.",
)
parser.add_argument(
"--checkpoint",
type=str,
required=True,
help="Path to the pre-trained HuBERTPretrainModel checkpoint as the initialization.",
)
parser.add_argument(
"--resume-checkpoint",
default=None,
type=str,
help="The path to the checkpoint to resume the fine-tuning if training fails in the middle.",
)
parser.add_argument(
"--exp-dir",
default=pathlib.Path("./exp_finetune"),
type=pathlib.Path,
help="Directory to save checkpoints and logs to. (Default: './exp_finetune')",
)
parser.add_argument(
"--model-name",
default="hubert_pretrain_base",
choices=["hubert_pretrain_base", "hubert_pretrain_large", "hubert_pretrain_xlarge"],
type=str,
help="The HuBERTPretrainModel to fine-tune. (Default: 'hubert_pretrain_base')",
)
parser.add_argument(
"--encoder-projection-dropout",
default=0.0,
type=float,
help="The dropout probability applied after the input feature "
"is projected to ``encoder_embed_dim``. (Default: 0.0)",
)
parser.add_argument(
"--encoder-attention-dropout",
default=0.0,
type=float,
help="The dropout probability applied after softmax in self-attention layer." "(Default: 0.0)",
)
parser.add_argument(
"--encoder-ff-interm-dropout",
default=0.1,
type=float,
help="The dropout probability applied in feedforward layer." "(Default: 0.1)",
)
parser.add_argument(
"--encoder-dropout",
default=0.0,
type=float,
help="The dropout probability applied at the end of feed forward layer." "(Default: 0.0)",
)
parser.add_argument(
"--encoder-layer-drop",
default=0.05,
type=float,
help="Probability to drop each encoder layer during training. (Default: 0.1)",
)
parser.add_argument(
"--mask-prob",
default=0.65,
type=float,
help="Probability to mask the frames of the convolutional layer feature." "(Default: 0.75)",
)
parser.add_argument(
"--mask-channel-prob",
default=0.5,
type=float,
help="Probability to mask the feature dimension of the convolutional layer feature." "(Default: 0.5)",
)
parser.add_argument(
"--mask-channel-length",
default=64,
type=int,
help="Minimum space between spans (if no overlap is enabled) for channel masking." "(Default: 64)",
)
parser.add_argument(
"--num-classes",
choices=[100, 500],
type=int,
default=500,
help="The ``num_class`` in the pre-trained checkpoint. (Default: 500)",
)
parser.add_argument(
"--aux-num-out",
default=29,
type=int,
help="The dimension of linear layer for CTC training. (Default: 29)",
)
parser.add_argument(
"--learning-rate", default=5e-5, type=float, help="The learning rate of Adam optimizer. (Default: 5e-5)"
)
parser.add_argument(
"--betas",
default=(0.9, 0.98),
type=Tuple,
help="The coefficients for computing running averages of gradient and its square (Default: (0.9, 0.98))",
)
parser.add_argument(
"--adam-eps",
default=1e-8,
type=float,
help="Epsilon value in Adam optimizer. (Default: 1e-8)",
)
parser.add_argument(
"--weight-decay",
default=0.0,
type=float,
help="Weight decay (L2 penalty) (Default: 0.0)",
)
parser.add_argument(
"--num_nodes",
default=1,
type=int,
help="Number of nodes to use for fine-tuning. (Default: 1)",
)
parser.add_argument(
"--gpus",
default=1,
type=int,
help="Number of GPUs per node to use for fine-tuning. (Default: 1)",
)
parser.add_argument(
"--freeze-encoder-updates",
default=10000,
type=int,
help="Number of steps to freeze the transformer encoder in HuBERT. (Default: 10000)",
)
parser.add_argument(
"--warmup-updates",
default=2000,
type=int,
help="Number of steps for warm up the learning rate. (Default: 8000)",
)
parser.add_argument(
"--hold-updates",
default=8000,
type=int,
help="Number of steps for keeping the peak learning rate. (Default: 0)",
)
parser.add_argument(
"--decay-updates",
default=10000,
type=int,
help="Number of steps for decreasing the learning rate. (Default: 72000)",
)
parser.add_argument(
"--max-updates",
default=20000,
type=int,
help="Total number of training steps. (Default: 250000)",
)
parser.add_argument(
"--seconds-per-batch",
default=200,
type=float,
help="Number of seconds of audio in a mini-batch. (Default: 200)",
)
parser.add_argument(
"--subset",
default="10h",
type=str,
choices=["10min", "1h", "10h"],
help="The subset of LibriLightLimited dataset for fine-tuning. (Default: '10h')",
)
parser.add_argument("--debug", action="store_true", help="whether to use debug level for logging")
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)
run_train(args)
if __name__ == "__main__":
cli_main()
|