|
| 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("--devices", type=int, nargs="*", default=[0], |
| 45 | + help="Devices' ids to apply distributed training") |
| 46 | + |
| 47 | +parser.add_argument("--mxp", default=False, action="store_true", |
| 48 | + help="Enable mixed precision") |
| 49 | + |
| 50 | +parser.add_argument("--cache", default=False, action="store_true", |
| 51 | + help="Enable caching for dataset") |
| 52 | + |
| 53 | +parser.add_argument("--subwords", type=str, default=None, |
| 54 | + help="Path to file that stores generated subwords") |
| 55 | + |
| 56 | +parser.add_argument("--subwords_corpus", nargs="*", type=str, default=[], |
| 57 | + help="Transcript files for generating subwords") |
| 58 | + |
| 59 | +args = parser.parse_args() |
| 60 | + |
| 61 | +tf.config.optimizer.set_experimental_options({"auto_mixed_precision": args.mxp}) |
| 62 | + |
| 63 | +strategy = setup_strategy(args.devices) |
| 64 | + |
| 65 | +from tensorflow_asr.configs.user_config import UserConfig |
| 66 | +from tensorflow_asr.datasets.asr_dataset import ASRTFRecordDataset, ASRSliceDataset |
| 67 | +from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer |
| 68 | +from tensorflow_asr.featurizers.text_featurizers import SubwordFeaturizer |
| 69 | +from tensorflow_asr.runners.transducer_runners import TransducerTrainerGA |
| 70 | +from tensorflow_asr.models.conformer import Conformer |
| 71 | +from tensorflow_asr.optimizers.schedules import TransformerSchedule |
| 72 | + |
| 73 | +config = UserConfig(DEFAULT_YAML, args.config, learning=True) |
| 74 | +speech_featurizer = TFSpeechFeaturizer(config["speech_config"]) |
| 75 | + |
| 76 | +if args.subwords and os.path.exists(args.subwords): |
| 77 | + print("Loading subwords ...") |
| 78 | + text_featurizer = SubwordFeaturizer.load_from_file(config["decoder_config"], args.subwords) |
| 79 | +else: |
| 80 | + print("Generating subwords ...") |
| 81 | + text_featurizer = SubwordFeaturizer.build_from_corpus( |
| 82 | + config["decoder_config"], |
| 83 | + corpus_files=args.subwords_corpus |
| 84 | + ) |
| 85 | + text_featurizer.subwords.save_to_file(args.subwords_prefix) |
| 86 | + |
| 87 | +if args.tfrecords: |
| 88 | + train_dataset = ASRTFRecordDataset( |
| 89 | + data_paths=config["learning_config"]["dataset_config"]["train_paths"], |
| 90 | + tfrecords_dir=config["learning_config"]["dataset_config"]["tfrecords_dir"], |
| 91 | + speech_featurizer=speech_featurizer, |
| 92 | + text_featurizer=text_featurizer, |
| 93 | + augmentations=config["learning_config"]["augmentations"], |
| 94 | + stage="train", cache=args.cache, shuffle=True |
| 95 | + ) |
| 96 | + eval_dataset = ASRTFRecordDataset( |
| 97 | + data_paths=config["learning_config"]["dataset_config"]["eval_paths"], |
| 98 | + tfrecords_dir=config["learning_config"]["dataset_config"]["tfrecords_dir"], |
| 99 | + speech_featurizer=speech_featurizer, |
| 100 | + text_featurizer=text_featurizer, |
| 101 | + stage="eval", cache=args.cache, shuffle=True |
| 102 | + ) |
| 103 | +else: |
| 104 | + train_dataset = ASRSliceDataset( |
| 105 | + data_paths=config["learning_config"]["dataset_config"]["train_paths"], |
| 106 | + speech_featurizer=speech_featurizer, |
| 107 | + text_featurizer=text_featurizer, |
| 108 | + augmentations=config["learning_config"]["augmentations"], |
| 109 | + stage="train", cache=args.cache, shuffle=True |
| 110 | + ) |
| 111 | + eval_dataset = ASRSliceDataset( |
| 112 | + data_paths=config["learning_config"]["dataset_config"]["eval_paths"], |
| 113 | + speech_featurizer=speech_featurizer, |
| 114 | + text_featurizer=text_featurizer, |
| 115 | + stage="eval", cache=args.cache, shuffle=True |
| 116 | + ) |
| 117 | + |
| 118 | +conformer_trainer = TransducerTrainerGA( |
| 119 | + config=config["learning_config"]["running_config"], |
| 120 | + text_featurizer=text_featurizer, strategy=strategy |
| 121 | +) |
| 122 | + |
| 123 | +with conformer_trainer.strategy.scope(): |
| 124 | + # build model |
| 125 | + conformer = Conformer( |
| 126 | + **config["model_config"], |
| 127 | + vocabulary_size=text_featurizer.num_classes |
| 128 | + ) |
| 129 | + conformer._build(speech_featurizer.shape) |
| 130 | + conformer.summary(line_length=120) |
| 131 | + |
| 132 | + optimizer_config = config["learning_config"]["optimizer_config"] |
| 133 | + optimizer = tf.keras.optimizers.Adam( |
| 134 | + TransformerSchedule( |
| 135 | + d_model=config["model_config"]["dmodel"], |
| 136 | + warmup_steps=optimizer_config["warmup_steps"], |
| 137 | + max_lr=(0.05 / math.sqrt(config["model_config"]["dmodel"])) |
| 138 | + ), |
| 139 | + beta_1=optimizer_config["beta1"], |
| 140 | + beta_2=optimizer_config["beta2"], |
| 141 | + epsilon=optimizer_config["epsilon"] |
| 142 | + ) |
| 143 | + |
| 144 | +conformer_trainer.compile(model=conformer, optimizer=optimizer, |
| 145 | + max_to_keep=args.max_ckpts) |
| 146 | + |
| 147 | +conformer_trainer.fit(train_dataset, eval_dataset, train_bs=args.tbs, eval_bs=args.ebs) |
0 commit comments