| | |
| | | self.cache_dir = Path(cache_dir) |
| | | self.export_config = dict( |
| | | feats_dim=560, |
| | | onnx=onnx, |
| | | onnx=False, |
| | | ) |
| | | logging.info("output dir: {}".format(self.cache_dir)) |
| | | self.onnx = onnx |
| | |
| | | model, |
| | | self.export_config, |
| | | ) |
| | | self._export_onnx(model, verbose, export_dir) |
| | | if self.onnx: |
| | | self._export_onnx(model, verbose, export_dir) |
| | | else: |
| | | self._export_torchscripts(model, verbose, export_dir) |
| | | |
| | | logging.info("output dir: {}".format(export_dir)) |
| | | |
| | | |
| | | def _export_torchscripts(self, model, verbose, path, enc_size=None): |
| | | if enc_size: |
| | | dummy_input = model.get_dummy_inputs(enc_size) |
| | | else: |
| | | dummy_input = model.get_dummy_inputs_txt() |
| | | |
| | | # model_script = torch.jit.script(model) |
| | | model_script = torch.jit.trace(model, dummy_input) |
| | | model_script.save(os.path.join(path, f'{model.model_name}.torchscripts')) |
| | | |
| | | def export_from_modelscope( |
| | | self, |
| | |
| | | ) |
| | | self.export(model, tag_name) |
| | | |
| | | |
| | | def export_from_local( |
| | | self, |
| | | tag_name: str = '/root/cache/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch', |
| | | ): |
| | | |
| | | from funasr.tasks.asr import ASRTaskParaformer as ASRTask |
| | | |
| | | model_dir = tag_name |
| | | asr_train_config = os.path.join(model_dir, 'config.yaml') |
| | | asr_model_file = os.path.join(model_dir, 'model.pb') |
| | | cmvn_file = os.path.join(model_dir, 'am.mvn') |
| | | model, asr_train_args = ASRTask.build_model_from_file( |
| | | asr_train_config, asr_model_file, cmvn_file, 'cpu' |
| | | ) |
| | | self.export(model, tag_name) |
| | | |
| | | def _export_onnx(self, model, verbose, path, enc_size=None): |
| | | if enc_size: |
| | |
| | | ) |
| | | |
| | | if __name__ == '__main__': |
| | | export_model = ASRModelExportParaformer() |
| | | export_model.export_from_modelscope('damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch') |
| | | output_dir = "../export" |
| | | export_model = ASRModelExportParaformer(cache_dir=output_dir, onnx=False) |
| | | export_model.export_from_modelscope('damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch') |
| | | # export_model.export_from_local('/root/cache/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch') |