|
| 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 argparse |
| 17 | +from tensorflow_asr.utils import setup_environment, setup_strategy |
| 18 | + |
| 19 | +setup_environment() |
| 20 | +import tensorflow as tf |
| 21 | + |
| 22 | +DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml") |
| 23 | + |
| 24 | +tf.keras.backend.clear_session() |
| 25 | + |
| 26 | +parser = argparse.ArgumentParser(prog="Deep Speech 2 Training") |
| 27 | + |
| 28 | +parser.add_argument("--config", "-c", type=str, default=DEFAULT_YAML, |
| 29 | + help="The file path of model configuration file") |
| 30 | + |
| 31 | +parser.add_argument("--max_ckpts", type=int, default=10, |
| 32 | + help="Max number of checkpoints to keep") |
| 33 | + |
| 34 | +parser.add_argument("--tbs", type=int, default=None, |
| 35 | + help="Train batch size per replicas") |
| 36 | + |
| 37 | +parser.add_argument("--ebs", type=int, default=None, |
| 38 | + help="Evaluation batch size per replicas") |
| 39 | + |
| 40 | +parser.add_argument("--acs", type=int, default=None, |
| 41 | + help="Train accumulation steps") |
| 42 | + |
| 43 | +parser.add_argument("--tfrecords", default=False, action="store_true", |
| 44 | + help="Whether to use tfrecords dataset") |
| 45 | + |
| 46 | +parser.add_argument("--devices", type=int, nargs="*", default=[0], |
| 47 | + help="Devices' ids to apply distributed training") |
| 48 | + |
| 49 | +parser.add_argument("--mxp", default=False, action="store_true", |
| 50 | + help="Enable mixed precision") |
| 51 | + |
| 52 | +parser.add_argument("--cache", default=False, action="store_true", |
| 53 | + help="Enable caching for dataset") |
| 54 | + |
| 55 | +args = parser.parse_args() |
| 56 | + |
| 57 | +tf.config.optimizer.set_experimental_options({"auto_mixed_precision": args.mxp}) |
| 58 | + |
| 59 | +strategy = setup_strategy(args.devices) |
| 60 | + |
| 61 | +from tensorflow_asr.configs.config import Config |
| 62 | +from tensorflow_asr.datasets.asr_dataset import ASRTFRecordDataset, ASRSliceDataset |
| 63 | +from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer |
| 64 | +from tensorflow_asr.featurizers.text_featurizers import CharFeaturizer |
| 65 | +from tensorflow_asr.runners.ctc_runners import CTCTrainerGA |
| 66 | +from tensorflow_asr.models.deepspeech2 import DeepSpeech2 |
| 67 | + |
| 68 | +config = Config(args.config, learning=True) |
| 69 | +speech_featurizer = TFSpeechFeaturizer(config.speech_config) |
| 70 | +text_featurizer = CharFeaturizer(config.decoder_config) |
| 71 | + |
| 72 | +if args.tfrecords: |
| 73 | + train_dataset = ASRTFRecordDataset( |
| 74 | + data_paths=config.learning_config.dataset_config.train_paths, |
| 75 | + tfrecords_dir=config.learning_config.dataset_config.tfrecords_dir, |
| 76 | + speech_featurizer=speech_featurizer, |
| 77 | + text_featurizer=text_featurizer, |
| 78 | + augmentations=config.learning_config.augmentations, |
| 79 | + stage="train", cache=args.cache, shuffle=True |
| 80 | + ) |
| 81 | + eval_dataset = ASRTFRecordDataset( |
| 82 | + data_paths=config.learning_config.dataset_config.eval_paths, |
| 83 | + tfrecords_dir=config.learning_config.dataset_config.tfrecords_dir, |
| 84 | + speech_featurizer=speech_featurizer, |
| 85 | + text_featurizer=text_featurizer, |
| 86 | + stage="eval", cache=args.cache, shuffle=True |
| 87 | + ) |
| 88 | +else: |
| 89 | + train_dataset = ASRSliceDataset( |
| 90 | + speech_featurizer=speech_featurizer, |
| 91 | + text_featurizer=text_featurizer, |
| 92 | + data_paths=config.learning_config.dataset_config.train_paths, |
| 93 | + augmentations=config.learning_config.augmentations, |
| 94 | + stage="train", cache=args.cache, shuffle=True |
| 95 | + ) |
| 96 | + eval_dataset = ASRSliceDataset( |
| 97 | + speech_featurizer=speech_featurizer, |
| 98 | + text_featurizer=text_featurizer, |
| 99 | + data_paths=config.learning_config.dataset_config.eval_paths, |
| 100 | + stage="eval", cache=args.cache, shuffle=True |
| 101 | + ) |
| 102 | + |
| 103 | +ctc_trainer = CTCTrainerGA(text_featurizer, config.learning_config.running_config) |
| 104 | +# Build DS2 model |
| 105 | +with ctc_trainer.strategy.scope(): |
| 106 | + ds2_model = DeepSpeech2(**config.model_config, vocabulary_size=text_featurizer.num_classes) |
| 107 | + ds2_model._build(speech_featurizer.shape) |
| 108 | + ds2_model.summary(line_length=120) |
| 109 | +# Compile |
| 110 | +ctc_trainer.compile(ds2_model, config.learning_config.optimizer_config, |
| 111 | + max_to_keep=args.max_ckpts) |
| 112 | + |
| 113 | +ctc_trainer.fit(train_dataset, eval_dataset, |
| 114 | + train_bs=args.tbs, eval_bs=args.ebs, train_acs=args.acs) |
0 commit comments