From b7cd512978b02e476ea45be66af30f4599455536 Mon Sep 17 00:00:00 2001 From: tsukumi Date: Mon, 23 Sep 2024 07:40:17 +0900 Subject: [PATCH] Fix: During ONNX inference, the tensor of the return value of the tokenizer is now obtained in Numpy's NDArray format to eliminate the dependency on PyTorch --- style_bert_vits2/nlp/chinese/bert_feature.py | 16 ++++++++-------- style_bert_vits2/nlp/english/bert_feature.py | 12 ++++++------ style_bert_vits2/nlp/japanese/bert_feature.py | 12 ++++++------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/style_bert_vits2/nlp/chinese/bert_feature.py b/style_bert_vits2/nlp/chinese/bert_feature.py index 0107965..40a705a 100644 --- a/style_bert_vits2/nlp/chinese/bert_feature.py +++ b/style_bert_vits2/nlp/chinese/bert_feature.py @@ -98,7 +98,7 @@ def extract_bert_feature_onnx( """ tokenizer = onnx_bert_models.load_tokenizer(Languages.ZH) - inputs = tokenizer(text, return_tensors="pt") + inputs = tokenizer(text, return_tensors="np") session = onnx_bert_models.load_model( language=Languages.ZH, @@ -108,21 +108,21 @@ def extract_bert_feature_onnx( res = session.run( [output_name], { - "input_ids": inputs["input_ids"].detach().numpy(), # type: ignore - "token_type_ids": inputs["token_type_ids"].detach().numpy(), # type: ignore - "attention_mask": inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": inputs["input_ids"], + "token_type_ids": inputs["token_type_ids"], + "attention_mask": inputs["attention_mask"], }, )[0] style_res_mean = None if assist_text: - style_inputs = tokenizer(assist_text, return_tensors="pt") + style_inputs = tokenizer(assist_text, return_tensors="np") style_res = session.run( [output_name], { - "input_ids": style_inputs["input_ids"].detach().numpy(), # type: ignore - "token_type_ids": style_inputs["token_type_ids"].detach().numpy(), # type: ignore - "attention_mask": style_inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": style_inputs["input_ids"], + "token_type_ids": style_inputs["token_type_ids"], + "attention_mask": style_inputs["attention_mask"], }, )[0] style_res_mean = np.mean(style_res, axis=0) diff --git a/style_bert_vits2/nlp/english/bert_feature.py b/style_bert_vits2/nlp/english/bert_feature.py index b4b5fb4..6ff143d 100644 --- a/style_bert_vits2/nlp/english/bert_feature.py +++ b/style_bert_vits2/nlp/english/bert_feature.py @@ -98,7 +98,7 @@ def extract_bert_feature_onnx( """ tokenizer = onnx_bert_models.load_tokenizer(Languages.EN) - inputs = tokenizer(text, return_tensors="pt") + inputs = tokenizer(text, return_tensors="np") session = onnx_bert_models.load_model( language=Languages.EN, @@ -108,19 +108,19 @@ def extract_bert_feature_onnx( res = session.run( [output_name], { - "input_ids": inputs["input_ids"].detach().numpy(), # type: ignore - "attention_mask": inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": inputs["input_ids"], + "attention_mask": inputs["attention_mask"], }, )[0] style_res_mean = None if assist_text: - style_inputs = tokenizer(assist_text, return_tensors="pt") + style_inputs = tokenizer(assist_text, return_tensors="np") style_res = session.run( [output_name], { - "input_ids": style_inputs["input_ids"].detach().numpy(), # type: ignore - "attention_mask": style_inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": style_inputs["input_ids"], + "attention_mask": style_inputs["attention_mask"], }, )[0] style_res_mean = np.mean(style_res, axis=0) diff --git a/style_bert_vits2/nlp/japanese/bert_feature.py b/style_bert_vits2/nlp/japanese/bert_feature.py index 7183190..9a91249 100644 --- a/style_bert_vits2/nlp/japanese/bert_feature.py +++ b/style_bert_vits2/nlp/japanese/bert_feature.py @@ -111,7 +111,7 @@ def extract_bert_feature_onnx( assist_text = "".join(text_to_sep_kata(assist_text, raise_yomi_error=False)[0]) tokenizer = onnx_bert_models.load_tokenizer(Languages.JP) - inputs = tokenizer(text, return_tensors="pt") + inputs = tokenizer(text, return_tensors="np") session = onnx_bert_models.load_model( language=Languages.JP, @@ -121,19 +121,19 @@ def extract_bert_feature_onnx( res = session.run( [output_name], { - "input_ids": inputs["input_ids"].detach().numpy(), # type: ignore - "attention_mask": inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": inputs["input_ids"], + "attention_mask": inputs["attention_mask"], }, )[0] style_res_mean = None if assist_text: - style_inputs = tokenizer(assist_text, return_tensors="pt") + style_inputs = tokenizer(assist_text, return_tensors="np") style_res = session.run( [output_name], { - "input_ids": style_inputs["input_ids"].detach().numpy(), # type: ignore - "attention_mask": style_inputs["attention_mask"].detach().numpy(), # type: ignore + "input_ids": style_inputs["input_ids"], + "attention_mask": style_inputs["attention_mask"], }, )[0] style_res_mean = np.mean(style_res, axis=0)