How to use VAD decoding in FunASR #236
-
|
There are two decoding mode for VAD model(offline、 online),for more details, please refer to VAD. If you want to decode in offline mode from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
if __name__ == '__main__':
audio_in = 'https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/vad_example.wav'
output_dir = None
inference_pipline = pipeline(
task=Tasks.voice_activity_detection,
model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch",
model_revision='v1.2.0',
output_dir=output_dir,
batch_size=1,
)
segments_result = inference_pipline(audio_in=audio_in)
print(segments_result)If you want to decode in online mode, you should set the mode to from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.logger import get_logger
import logging
logger = get_logger(log_level=logging.CRITICAL)
logger.setLevel(logging.CRITICAL)
import soundfile
import os
if __name__ == '__main__':
os.environ["MODELSCOPE_CACHE"] = "./"
output_dir = None
inference_pipline = pipeline(
task=Tasks.voice_activity_detection,
model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch",
model_revision='v1.2.0',
output_dir=output_dir,
batch_size=1,
mode='online',
)
model_dir = os.path.join(os.environ["MODELSCOPE_CACHE"], "damo/speech_fsmn_vad_zh-cn-16k-common-pytorch")
wav_file = os.path.join(model_dir, "example/vad_example.wav")
speech, sample_rate = soundfile.read(wav_file)
speech_length = speech.shape[0]
sample_offset = 0
step = 160 * 10
param_dict = {'in_cache': dict()}
for sample_offset in range(0, speech_length, min(step, speech_length - sample_offset)):
if sample_offset + step >= speech_length - 1:
step = speech_length - sample_offset
is_final = True
else:
is_final = False
param_dict['is_final'] = is_final
segments_result = inference_pipline(audio_in=speech[sample_offset: sample_offset + step],
param_dict=param_dict)
print(segments_result) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi, could you please give a short example to refresh timestamp cache that calling an online-mode VAD returns? "When a new request is started, you should set the param_dict = {'in_cache': dict()}" |
Beta Was this translation helpful? Give feedback.
-
|
Update for current FunASR ( The important distinction is:
The original post uses the historical ModelScope pipeline API. The current native FunASR API is Non-streaming VADfrom funasr import AutoModel
model = AutoModel(model="fsmn-vad", device="cpu", disable_update=True)
wav_file = f"{model.model_path}/example/vad_example.wav"
result = model.generate(input=wav_file)[0]
print(result["value"])Each pair is Streaming VAD with request-scoped cacheimport soundfile as sf
from funasr import AutoModel
CHUNK_MS = 200
model = AutoModel(model="fsmn-vad", device="cpu", disable_update=True)
wav_file = f"{model.model_path}/example/vad_example.wav"
speech, sample_rate = sf.read(wav_file)
def decode_one_request(speech, sample_rate):
# A new dictionary for every independent audio/request/connection.
cache = {}
stride = int(CHUNK_MS * sample_rate / 1000)
total = (len(speech) - 1) // stride + 1
events = []
for index in range(total):
chunk = speech[index * stride : (index + 1) * stride]
is_final = index == total - 1
result = model.generate(
input=chunk,
cache=cache,
is_final=is_final,
chunk_size=CHUNK_MS,
)[0]["value"]
if result:
events.extend(result)
return events
first = decode_one_request(speech, sample_rate)
second = decode_one_request(speech, sample_rate)
assert first == second
print(first)Streaming output uses endpoint events:
For the bundled 70,470.625 ms example, both independent runs returned the same events: Why timestamps can appear to "never refresh"
I reproduced the accumulated-timestamp symptom by intentionally omitting Therefore:
Do not clear the cache after every chunk: that would reset the acoustic/model history and break streaming endpoint detection. The current official streaming example is also available in the FunASR tutorial. The reproduction above used Python 3.12.3, FunASR 1.3.14, ModelScope 1.38.1, PyTorch 2.11.0 CPU, and the official |
Beta Was this translation helpful? Give feedback.
Update for current FunASR (
mainat1d8080a, verified on 2026-07-15).The important distinction is:
cache. Timestamps are absolute milliseconds from the start of that request, so they are expected to increase across chunks.The original post uses the historical ModelScope pipeline API. The current native FunASR API is
AutoModel.Non-streaming VAD