|
| 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 argparse |
| 16 | +import os |
| 17 | + |
| 18 | +from tensorflow_asr.utils import env_util |
| 19 | + |
| 20 | +logger = env_util.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 Testing") |
| 28 | + |
| 29 | +parser.add_argument( |
| 30 | + "--config", |
| 31 | + type=str, |
| 32 | + default=DEFAULT_YAML, |
| 33 | + help="The file path of model configuration file", |
| 34 | +) |
| 35 | + |
| 36 | +parser.add_argument( |
| 37 | + "--h5", |
| 38 | + type=str, |
| 39 | + default=None, |
| 40 | + help="Path to saved h5 weights", |
| 41 | +) |
| 42 | + |
| 43 | +parser.add_argument( |
| 44 | + "--sentence_piece", |
| 45 | + default=False, |
| 46 | + action="store_true", |
| 47 | + help="Whether to use `SentencePiece` model", |
| 48 | +) |
| 49 | + |
| 50 | +parser.add_argument( |
| 51 | + "--subwords", |
| 52 | + default=False, |
| 53 | + action="store_true", |
| 54 | + help="Use subwords", |
| 55 | +) |
| 56 | + |
| 57 | +parser.add_argument( |
| 58 | + "--output_dir", |
| 59 | + type=str, |
| 60 | + default=None, |
| 61 | + help="Output directory for saved model", |
| 62 | +) |
| 63 | + |
| 64 | +args = parser.parse_args() |
| 65 | + |
| 66 | +assert args.h5 |
| 67 | +assert args.output_dir |
| 68 | + |
| 69 | +from tensorflow_asr.configs.config import Config |
| 70 | +from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer |
| 71 | +from tensorflow_asr.featurizers.text_featurizers import CharFeaturizer, SentencePieceFeaturizer, SubwordFeaturizer |
| 72 | +from tensorflow_asr.models.transducer.conformer import Conformer |
| 73 | + |
| 74 | +config = Config(args.config) |
| 75 | +speech_featurizer = TFSpeechFeaturizer(config.speech_config) |
| 76 | + |
| 77 | +if args.sentence_piece: |
| 78 | + logger.info("Use SentencePiece ...") |
| 79 | + text_featurizer = SentencePieceFeaturizer(config.decoder_config) |
| 80 | +elif args.subwords: |
| 81 | + logger.info("Use subwords ...") |
| 82 | + text_featurizer = SubwordFeaturizer(config.decoder_config) |
| 83 | +else: |
| 84 | + logger.info("Use characters ...") |
| 85 | + text_featurizer = CharFeaturizer(config.decoder_config) |
| 86 | + |
| 87 | +tf.random.set_seed(0) |
| 88 | + |
| 89 | +# build model |
| 90 | +conformer = Conformer(**config.model_config, vocabulary_size=text_featurizer.num_classes) |
| 91 | +conformer.make(speech_featurizer.shape) |
| 92 | +conformer.load_weights(args.h5, by_name=True) |
| 93 | +conformer.summary(line_length=100) |
| 94 | +conformer.add_featurizers(speech_featurizer, text_featurizer) |
| 95 | + |
| 96 | + |
| 97 | +class aModule(tf.Module): |
| 98 | + def __init__(self, model): |
| 99 | + super().__init__() |
| 100 | + self.model = model |
| 101 | + |
| 102 | + @tf.function( |
| 103 | + input_signature=[ |
| 104 | + { |
| 105 | + "inputs": tf.TensorSpec(shape=[None, None, 80, 1], dtype=tf.float32, name="inputs"), |
| 106 | + "inputs_length": tf.TensorSpec(shape=[None], dtype=tf.int32, name="inputs_length"), |
| 107 | + } |
| 108 | + ] |
| 109 | + ) |
| 110 | + def pred(self, input_batch): |
| 111 | + result = self.model.recognize(input_batch) |
| 112 | + return {"ASR": result} |
| 113 | + |
| 114 | + |
| 115 | +module = aModule(conformer) |
| 116 | +tf.saved_model.save(module, args.output_dir, signatures={"serving_default": module.pred}) |
0 commit comments