quant with wav scp

pull/2568/head
Hui Zhang 3 years ago
parent 09a735af24
commit c83c9800cc

@ -1,7 +1,7 @@
#!/bin/bash
if [ $# != 4 ];then
echo "usage: ${0} config_path decode_config_path ckpt_path_prefix audio_file"
echo "usage: ${0} config_path decode_config_path ckpt_path_prefix audio_scp"
exit -1
fi
@ -11,16 +11,15 @@ echo "using $ngpu gpus..."
config_path=$1
decode_config_path=$2
ckpt_prefix=$3
audio_file=$4
audio_scp=$4
mkdir -p data
wget -nc https://paddlespeech.bj.bcebos.com/datasets/single_wav/zh/demo_01_03.wav -P data/
if [ $? -ne 0 ]; then
exit 1
fi
if [ ! -f ${audio_file} ]; then
echo "Plase input the right audio_file path"
if [ ! -f ${audio_scp} ]; then
echo "Plase input the right audio_scp path"
exit 1
fi
@ -49,7 +48,7 @@ for type in attention_rescoring; do
--checkpoint_path ${ckpt_prefix} \
--opts decode.decoding_method ${type} \
--opts decode.decode_batch_size ${batch_size} \
--audio_file ${audio_file}
--audio_scp ${audio_scp}
if [ $? -ne 0 ]; then
echo "Failed in evaluation!"

@ -20,6 +20,7 @@ import paddle
import soundfile
from paddleslim import PTQ
from yacs.config import CfgNode
from kaldiio import ReadHelper
from paddlespeech.audio.transform.transformation import Transformation
from paddlespeech.s2t.frontend.featurizer.text_featurizer import TextFeaturizer
@ -34,7 +35,7 @@ class U2Infer():
def __init__(self, config, args):
self.args = args
self.config = config
self.audio_file = args.audio_file
self.audio_scp = args.audio_scp
self.preprocess_conf = config.preprocess_config
self.preprocess_args = {"train": False}
@ -63,13 +64,15 @@ class U2Infer():
self.model.set_state_dict(model_dict)
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():
# read
audio, sample_rate = soundfile.read(
self.audio_file, dtype="int16", always_2d=True)
audio = audio[:, 0]
logger.info(f"audio shape: {audio.shape}")
# fbank
@ -80,7 +83,6 @@ class U2Infer():
xs = paddle.to_tensor(feat, dtype='float32').unsqueeze(0)
decode_config = self.config.decode
logger.info(f"decode cfg: {decode_config}")
reverse_weight = getattr(decode_config, 'reverse_weight', 0.0)
result_transcripts = self.model.decode(
xs,
ilen,
@ -91,13 +93,14 @@ class U2Infer():
decoding_chunk_size=decode_config.decoding_chunk_size,
num_decoding_left_chunks=decode_config.num_decoding_left_chunks,
simulate_streaming=decode_config.simulate_streaming,
reverse_weight=reverse_weight)
reverse_weight=decode_config.reverse_weight)
rsl = result_transcripts[0][0]
utt = Path(self.audio_file).name
utt = key
logger.info(f"hyp: {utt} {rsl}")
# print(self.model)
# print(self.model.forward_encoder_chunk)
logger.info("-------------start quant ----------------------")
batch_size = 1
feat_dim = 80
@ -174,24 +177,6 @@ class U2Infer():
# 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):
U2Infer(config, args).run()
@ -202,7 +187,9 @@ if __name__ == "__main__":
parser.add_argument(
"--result_file", type=str, help="path of save the asr result")
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(
"--export_path",
type=str,

Loading…
Cancel
Save