|
| 1 | +# Copyright 2020 Huy Le Nguyen (@usimarit) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import math |
| 17 | +import argparse |
| 18 | +from tensorflow_asr.utils import setup_environment, setup_strategy |
| 19 | + |
| 20 | +setup_environment() |
| 21 | +import tensorflow as tf |
| 22 | + |
| 23 | +DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "config.yml") |
| 24 | + |
| 25 | +tf.keras.backend.clear_session() |
| 26 | + |
| 27 | +parser = argparse.ArgumentParser(prog="Conformer Training") |
| 28 | + |
| 29 | +parser.add_argument("--config", type=str, default=DEFAULT_YAML, |
| 30 | + help="The file path of model configuration file") |
| 31 | + |
| 32 | +parser.add_argument("--max_ckpts", type=int, default=10, |
| 33 | + help="Max number of checkpoints to keep") |
| 34 | + |
| 35 | +parser.add_argument("--tfrecords", default=False, action="store_true", |
| 36 | + help="Whether to use tfrecords") |
| 37 | + |
| 38 | +parser.add_argument("--tbs", type=int, default=None, |
| 39 | + help="Train batch size per replica") |
| 40 | + |
| 41 | +parser.add_argument("--ebs", type=int, default=None, |
| 42 | + help="Evaluation batch size per replica") |
| 43 | + |
| 44 | +parser.add_argument("--acs", type=int, default=None, |
| 45 | + help="Train accumulation steps") |
| 46 | + |
| 47 | +parser.add_argument("--devices", type=int, nargs="*", default=[0], |
| 48 | + help="Devices' ids to apply distributed training") |
| 49 | + |
| 50 | +parser.add_argument("--mxp", default=False, action="store_true", |
| 51 | + help="Enable mixed precision") |
| 52 | + |
| 53 | +parser.add_argument("--cache", default=False, action="store_true", |
| 54 | + help="Enable caching for dataset") |
| 55 | + |
| 56 | +parser.add_argument("--subwords", type=str, default=None, |
| 57 | + help="Path to file that stores generated subwords") |
| 58 | + |
| 59 | +parser.add_argument("--subwords_corpus", nargs="*", type=str, default=[], |
| 60 | + help="Transcript files for generating subwords") |
| 61 | + |
| 62 | +args = parser.parse_args() |
| 63 | + |
| 64 | +tf.config.optimizer.set_experimental_options({"auto_mixed_precision": args.mxp}) |
| 65 | + |
| 66 | +strategy = setup_strategy(args.devices) |
| 67 | + |
| 68 | +from tensorflow_asr.configs.config import Config |
| 69 | +from tensorflow_asr.datasets.asr_dataset import ASRTFRecordDataset, ASRSliceDataset |
| 70 | +from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer |
| 71 | +from tensorflow_asr.featurizers.text_featurizers import SubwordFeaturizer |
| 72 | +from trainer import TrainerWithMaskingGA |
| 73 | +from tensorflow_asr.models.conformer import Conformer |
| 74 | +from tensorflow_asr.optimizers.schedules import TransformerSchedule |
| 75 | + |
| 76 | +config = Config(args.config, learning=True) |
| 77 | +speech_featurizer = TFSpeechFeaturizer(config.speech_config) |
| 78 | + |
| 79 | +if args.subwords and os.path.exists(args.subwords): |
| 80 | + print("Loading subwords ...") |
| 81 | + text_featurizer = SubwordFeaturizer.load_from_file(config.decoder_config, args.subwords) |
| 82 | +else: |
| 83 | + print("Generating subwords ...") |
| 84 | + text_featurizer = SubwordFeaturizer.build_from_corpus( |
| 85 | + config.decoder_config, |
| 86 | + corpus_files=args.subwords_corpus |
| 87 | + ) |
| 88 | + text_featurizer.save_to_file(args.subwords) |
| 89 | + |
| 90 | +if args.tfrecords: |
| 91 | + train_dataset = ASRTFRecordDataset( |
| 92 | + data_paths=config.learning_config.dataset_config.train_paths, |
| 93 | + tfrecords_dir=config.learning_config.dataset_config.tfrecords_dir, |
| 94 | + speech_featurizer=speech_featurizer, |
| 95 | + text_featurizer=text_featurizer, |
| 96 | + augmentations=config.learning_config.augmentations, |
| 97 | + stage="train", cache=args.cache, shuffle=True |
| 98 | + ) |
| 99 | + eval_dataset = ASRTFRecordDataset( |
| 100 | + data_paths=config.learning_config.dataset_config.eval_paths, |
| 101 | + tfrecords_dir=config.learning_config.dataset_config.tfrecords_dir, |
| 102 | + speech_featurizer=speech_featurizer, |
| 103 | + text_featurizer=text_featurizer, |
| 104 | + stage="eval", cache=args.cache, shuffle=True |
| 105 | + ) |
| 106 | +else: |
| 107 | + train_dataset = ASRSliceDataset( |
| 108 | + data_paths=config.learning_config.dataset_config.train_paths, |
| 109 | + speech_featurizer=speech_featurizer, |
| 110 | + text_featurizer=text_featurizer, |
| 111 | + augmentations=config.learning_config.augmentations, |
| 112 | + stage="train", cache=args.cache, shuffle=True |
| 113 | + ) |
| 114 | + eval_dataset = ASRSliceDataset( |
| 115 | + data_paths=config.learning_config.dataset_config.eval_paths, |
| 116 | + speech_featurizer=speech_featurizer, |
| 117 | + text_featurizer=text_featurizer, |
| 118 | + stage="eval", cache=args.cache, shuffle=True |
| 119 | + ) |
| 120 | + |
| 121 | +conformer_trainer = TrainerWithMaskingGA( |
| 122 | + config=config.learning_config.running_config, |
| 123 | + text_featurizer=text_featurizer, strategy=strategy |
| 124 | +) |
| 125 | + |
| 126 | +with conformer_trainer.strategy.scope(): |
| 127 | + # build model |
| 128 | + conformer = Conformer(**config.model_config, vocabulary_size=text_featurizer.num_classes) |
| 129 | + conformer._build(speech_featurizer.shape) |
| 130 | + conformer.summary(line_length=120) |
| 131 | + |
| 132 | + optimizer = tf.keras.optimizers.Adam( |
| 133 | + TransformerSchedule( |
| 134 | + d_model=config.model_config["encoder_dmodel"], |
| 135 | + warmup_steps=config.learning_config.optimizer_config["warmup_steps"], |
| 136 | + max_lr=(0.05 / math.sqrt(config.model_config["encoder_dmodel"])) |
| 137 | + ), |
| 138 | + beta_1=config.learning_config.optimizer_config["beta1"], |
| 139 | + beta_2=config.learning_config.optimizer_config["beta2"], |
| 140 | + epsilon=config.learning_config.optimizer_config["epsilon"] |
| 141 | + ) |
| 142 | + |
| 143 | +conformer_trainer.compile(model=conformer, optimizer=optimizer, |
| 144 | + max_to_keep=args.max_ckpts) |
| 145 | + |
| 146 | +conformer_trainer.fit(train_dataset, eval_dataset, |
| 147 | + train_bs=args.tbs, eval_bs=args.ebs, train_acs=args.acs) |
0 commit comments