|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import argparse |
16 | 15 | import os |
| 16 | +import fire |
17 | 17 |
|
18 | 18 | from tensorflow_asr.utils import env_util |
19 | 19 |
|
|
22 | 22 |
|
23 | 23 | DEFAULT_YAML = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config.yml") |
24 | 24 |
|
25 | | -tf.keras.backend.clear_session() |
26 | | - |
27 | | -parser = argparse.ArgumentParser(prog="Conformer Testing") |
28 | | - |
29 | | -parser.add_argument("--config", type=str, default=DEFAULT_YAML, help="The file path of model configuration file") |
30 | | - |
31 | | -parser.add_argument("--h5", type=str, default=None, help="Path to saved h5 weights") |
32 | | - |
33 | | -parser.add_argument("--sentence_piece", default=False, action="store_true", help="Whether to use `SentencePiece` model") |
34 | | - |
35 | | -parser.add_argument("--subwords", default=False, action="store_true", help="Use subwords") |
36 | | - |
37 | | -parser.add_argument("--output_dir", type=str, default=None, help="Output directory for saved model") |
38 | | - |
39 | | -args = parser.parse_args() |
40 | | - |
41 | | -assert args.h5 |
42 | | -assert args.output_dir |
43 | 25 |
|
44 | 26 | from tensorflow_asr.configs.config import Config |
45 | | -from tensorflow_asr.featurizers.speech_featurizers import TFSpeechFeaturizer |
46 | | -from tensorflow_asr.featurizers.text_featurizers import CharFeaturizer, SentencePieceFeaturizer, SubwordFeaturizer |
| 27 | +from tensorflow_asr.helpers import featurizer_helpers |
47 | 28 | from tensorflow_asr.models.transducer.conformer import Conformer |
48 | 29 |
|
49 | | -config = Config(args.config) |
50 | | -speech_featurizer = TFSpeechFeaturizer(config.speech_config) |
51 | | - |
52 | | -if args.sentence_piece: |
53 | | - logger.info("Use SentencePiece ...") |
54 | | - text_featurizer = SentencePieceFeaturizer(config.decoder_config) |
55 | | -elif args.subwords: |
56 | | - logger.info("Use subwords ...") |
57 | | - text_featurizer = SubwordFeaturizer(config.decoder_config) |
58 | | -else: |
59 | | - logger.info("Use characters ...") |
60 | | - text_featurizer = CharFeaturizer(config.decoder_config) |
61 | | - |
62 | | -tf.random.set_seed(0) |
63 | | - |
64 | | -# build model |
65 | | -conformer = Conformer(**config.model_config, vocabulary_size=text_featurizer.num_classes) |
66 | | -conformer.make(speech_featurizer.shape) |
67 | | -conformer.load_weights(args.h5, by_name=True) |
68 | | -conformer.summary(line_length=100) |
69 | | -conformer.add_featurizers(speech_featurizer, text_featurizer) |
70 | | - |
71 | | - |
72 | | -class ConformerModule(tf.Module): |
73 | | - def __init__(self, model: Conformer, name=None): |
74 | | - super().__init__(name=name) |
75 | | - self.model = model |
76 | | - self.num_rnns = config.model_config["prediction_num_rnns"] |
77 | | - self.rnn_units = config.model_config["prediction_rnn_units"] |
78 | | - self.rnn_nstates = 2 if config.model_config["prediction_rnn_type"] == "lstm" else 1 |
79 | | - |
80 | | - @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)]) |
81 | | - def pred(self, signal): |
82 | | - predicted = tf.constant(0, dtype=tf.int32) |
83 | | - states = tf.zeros([self.num_rnns, self.rnn_nstates, 1, self.rnn_units], dtype=tf.float32) |
84 | | - features = self.model.speech_featurizer.tf_extract(signal) |
85 | | - encoded = self.model.encoder_inference(features) |
86 | | - hypothesis = self.model._perform_greedy(encoded, tf.shape(encoded)[0], predicted, states, tflite=False) |
87 | | - transcript = self.model.text_featurizer.indices2upoints(hypothesis.prediction) |
88 | | - return transcript |
89 | | - |
90 | 30 |
|
91 | | -module = ConformerModule(model=conformer) |
92 | | -tf.saved_model.save(module, export_dir=args.output_dir, signatures=module.pred.get_concrete_function()) |
| 31 | +def main( |
| 32 | + config: str = DEFAULT_YAML, |
| 33 | + h5: str = None, |
| 34 | + sentence_piece: bool = False, |
| 35 | + subwords: bool = False, |
| 36 | + output_dir: str = None, |
| 37 | +): |
| 38 | + assert h5 and output_dir |
| 39 | + config = Config(config) |
| 40 | + tf.random.set_seed(0) |
| 41 | + tf.keras.backend.clear_session() |
| 42 | + |
| 43 | + speech_featurizer, text_featurizer = featurizer_helpers.prepare_featurizers( |
| 44 | + config=config, |
| 45 | + subwords=subwords, |
| 46 | + sentence_piece=sentence_piece, |
| 47 | + ) |
| 48 | + |
| 49 | + # build model |
| 50 | + conformer = Conformer(**config.model_config, vocabulary_size=text_featurizer.num_classes) |
| 51 | + conformer.make(speech_featurizer.shape) |
| 52 | + conformer.load_weights(h5, by_name=True) |
| 53 | + conformer.summary(line_length=100) |
| 54 | + conformer.add_featurizers(speech_featurizer, text_featurizer) |
| 55 | + |
| 56 | + class ConformerModule(tf.Module): |
| 57 | + def __init__(self, model: Conformer, name=None): |
| 58 | + super().__init__(name=name) |
| 59 | + self.model = model |
| 60 | + self.num_rnns = config.model_config["prediction_num_rnns"] |
| 61 | + self.rnn_units = config.model_config["prediction_rnn_units"] |
| 62 | + self.rnn_nstates = 2 if config.model_config["prediction_rnn_type"] == "lstm" else 1 |
| 63 | + |
| 64 | + @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)]) |
| 65 | + def pred(self, signal): |
| 66 | + predicted = tf.constant(0, dtype=tf.int32) |
| 67 | + states = tf.zeros([self.num_rnns, self.rnn_nstates, 1, self.rnn_units], dtype=tf.float32) |
| 68 | + features = self.model.speech_featurizer.tf_extract(signal) |
| 69 | + encoded = self.model.encoder_inference(features) |
| 70 | + hypothesis = self.model._perform_greedy(encoded, tf.shape(encoded)[0], predicted, states, tflite=False) |
| 71 | + transcript = self.model.text_featurizer.indices2upoints(hypothesis.prediction) |
| 72 | + return transcript |
| 73 | + |
| 74 | + module = ConformerModule(model=conformer) |
| 75 | + tf.saved_model.save(module, export_dir=output_dir, signatures=module.pred.get_concrete_function()) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + fire.Fire(main) |
0 commit comments