|
|
@ -11,13 +11,9 @@
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# limitations under the License.
|
|
|
|
"""Evaluation for U2 model."""
|
|
|
|
"""Quantzation U2 model."""
|
|
|
|
import os
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import paddle
|
|
|
|
import paddle
|
|
|
|
import soundfile
|
|
|
|
from kaldiio import ReadHelper
|
|
|
|
from paddleslim import PTQ
|
|
|
|
from paddleslim import PTQ
|
|
|
|
from yacs.config import CfgNode
|
|
|
|
from yacs.config import CfgNode
|
|
|
|
|
|
|
|
|
|
|
@ -34,7 +30,7 @@ class U2Infer():
|
|
|
|
def __init__(self, config, args):
|
|
|
|
def __init__(self, config, args):
|
|
|
|
self.args = args
|
|
|
|
self.args = args
|
|
|
|
self.config = config
|
|
|
|
self.config = config
|
|
|
|
self.audio_file = args.audio_file
|
|
|
|
self.audio_scp = args.audio_scp
|
|
|
|
|
|
|
|
|
|
|
|
self.preprocess_conf = config.preprocess_config
|
|
|
|
self.preprocess_conf = config.preprocess_config
|
|
|
|
self.preprocess_args = {"train": False}
|
|
|
|
self.preprocess_args = {"train": False}
|
|
|
@ -63,13 +59,15 @@ class U2Infer():
|
|
|
|
self.model.set_state_dict(model_dict)
|
|
|
|
self.model.set_state_dict(model_dict)
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
def run(self):
|
|
|
|
check(args.audio_file)
|
|
|
|
cnt = 0
|
|
|
|
|
|
|
|
with ReadHelper(f"scp:{self.audio_scp}") as reader:
|
|
|
|
|
|
|
|
for key, (rate, audio) in reader:
|
|
|
|
|
|
|
|
assert rate == 16000
|
|
|
|
|
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
if cnt > args.num_utts:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
with paddle.no_grad():
|
|
|
|
with paddle.no_grad():
|
|
|
|
# read
|
|
|
|
|
|
|
|
audio, sample_rate = soundfile.read(
|
|
|
|
|
|
|
|
self.audio_file, dtype="int16", always_2d=True)
|
|
|
|
|
|
|
|
audio = audio[:, 0]
|
|
|
|
|
|
|
|
logger.info(f"audio shape: {audio.shape}")
|
|
|
|
logger.info(f"audio shape: {audio.shape}")
|
|
|
|
|
|
|
|
|
|
|
|
# fbank
|
|
|
|
# fbank
|
|
|
@ -80,7 +78,6 @@ class U2Infer():
|
|
|
|
xs = paddle.to_tensor(feat, dtype='float32').unsqueeze(0)
|
|
|
|
xs = paddle.to_tensor(feat, dtype='float32').unsqueeze(0)
|
|
|
|
decode_config = self.config.decode
|
|
|
|
decode_config = self.config.decode
|
|
|
|
logger.info(f"decode cfg: {decode_config}")
|
|
|
|
logger.info(f"decode cfg: {decode_config}")
|
|
|
|
reverse_weight = getattr(decode_config, 'reverse_weight', 0.0)
|
|
|
|
|
|
|
|
result_transcripts = self.model.decode(
|
|
|
|
result_transcripts = self.model.decode(
|
|
|
|
xs,
|
|
|
|
xs,
|
|
|
|
ilen,
|
|
|
|
ilen,
|
|
|
@ -89,11 +86,12 @@ class U2Infer():
|
|
|
|
beam_size=decode_config.beam_size,
|
|
|
|
beam_size=decode_config.beam_size,
|
|
|
|
ctc_weight=decode_config.ctc_weight,
|
|
|
|
ctc_weight=decode_config.ctc_weight,
|
|
|
|
decoding_chunk_size=decode_config.decoding_chunk_size,
|
|
|
|
decoding_chunk_size=decode_config.decoding_chunk_size,
|
|
|
|
num_decoding_left_chunks=decode_config.num_decoding_left_chunks,
|
|
|
|
num_decoding_left_chunks=decode_config.
|
|
|
|
|
|
|
|
num_decoding_left_chunks,
|
|
|
|
simulate_streaming=decode_config.simulate_streaming,
|
|
|
|
simulate_streaming=decode_config.simulate_streaming,
|
|
|
|
reverse_weight=reverse_weight)
|
|
|
|
reverse_weight=decode_config.reverse_weight)
|
|
|
|
rsl = result_transcripts[0][0]
|
|
|
|
rsl = result_transcripts[0][0]
|
|
|
|
utt = Path(self.audio_file).name
|
|
|
|
utt = key
|
|
|
|
logger.info(f"hyp: {utt} {rsl}")
|
|
|
|
logger.info(f"hyp: {utt} {rsl}")
|
|
|
|
# print(self.model)
|
|
|
|
# print(self.model)
|
|
|
|
# print(self.model.forward_encoder_chunk)
|
|
|
|
# print(self.model.forward_encoder_chunk)
|
|
|
@ -161,35 +159,12 @@ class U2Infer():
|
|
|
|
|
|
|
|
|
|
|
|
# jit save
|
|
|
|
# jit save
|
|
|
|
logger.info(f"export save: {self.args.export_path}")
|
|
|
|
logger.info(f"export save: {self.args.export_path}")
|
|
|
|
config = {
|
|
|
|
self.ptq.ptq._convert(self.model)
|
|
|
|
'is_static': True,
|
|
|
|
paddle.jit.save(
|
|
|
|
'combine_params': True,
|
|
|
|
self.model,
|
|
|
|
'skip_forward': True
|
|
|
|
self.args.export_path,
|
|
|
|
}
|
|
|
|
combine_params=True,
|
|
|
|
self.ptq.save_quantized_model(self.model, self.args.export_path)
|
|
|
|
skip_forward=True)
|
|
|
|
# paddle.jit.save(
|
|
|
|
|
|
|
|
# self.model,
|
|
|
|
|
|
|
|
# self.args.export_path,
|
|
|
|
|
|
|
|
# combine_params=True,
|
|
|
|
|
|
|
|
# skip_forward=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check(audio_file):
|
|
|
|
|
|
|
|
if not os.path.isfile(audio_file):
|
|
|
|
|
|
|
|
print("Please input the right audio file path")
|
|
|
|
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("checking the audio file format......")
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
sig, sample_rate = soundfile.read(audio_file)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
logger.error(str(e))
|
|
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
|
|
"can not open the wav file, please check the audio file format")
|
|
|
|
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
logger.info("The sample rate is %d" % sample_rate)
|
|
|
|
|
|
|
|
assert (sample_rate == 16000)
|
|
|
|
|
|
|
|
logger.info("The audio file format is right")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(config, args):
|
|
|
|
def main(config, args):
|
|
|
@ -202,11 +177,16 @@ if __name__ == "__main__":
|
|
|
|
parser.add_argument(
|
|
|
|
parser.add_argument(
|
|
|
|
"--result_file", type=str, help="path of save the asr result")
|
|
|
|
"--result_file", type=str, help="path of save the asr result")
|
|
|
|
parser.add_argument(
|
|
|
|
parser.add_argument(
|
|
|
|
"--audio_file", type=str, help="path of the input audio file")
|
|
|
|
"--audio_scp", type=str, help="path of the input audio file")
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
|
|
|
"--num_utts",
|
|
|
|
|
|
|
|
type=int,
|
|
|
|
|
|
|
|
default=200,
|
|
|
|
|
|
|
|
help="num utts for quant calibrition.")
|
|
|
|
parser.add_argument(
|
|
|
|
parser.add_argument(
|
|
|
|
"--export_path",
|
|
|
|
"--export_path",
|
|
|
|
type=str,
|
|
|
|
type=str,
|
|
|
|
default='export',
|
|
|
|
default='export.jit.quant',
|
|
|
|
help="path of the input audio file")
|
|
|
|
help="path of the input audio file")
|
|
|
|
args = parser.parse_args()
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|