You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_automatic_speech_recognition.py 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import shutil
  4. import unittest
  5. from typing import Any, Dict, Union
  6. import numpy as np
  7. import soundfile
  8. from modelscope.outputs import OutputKeys
  9. from modelscope.pipelines import pipeline
  10. from modelscope.utils.constant import ColorCodes, Tasks
  11. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  12. from modelscope.utils.logger import get_logger
  13. from modelscope.utils.test_utils import download_and_untar, test_level
  14. logger = get_logger()
  15. WAV_FILE = 'data/test/audios/asr_example.wav'
  16. URL_FILE = 'https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example.wav'
  17. LITTLE_TESTSETS_FILE = 'data_aishell.tar.gz'
  18. LITTLE_TESTSETS_URL = 'https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/datasets/data_aishell.tar.gz'
  19. TFRECORD_TESTSETS_FILE = 'tfrecord.tar.gz'
  20. TFRECORD_TESTSETS_URL = 'https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/datasets/tfrecord.tar.gz'
  21. class AutomaticSpeechRecognitionTest(unittest.TestCase,
  22. DemoCompatibilityCheck):
  23. action_info = {
  24. 'test_run_with_wav_pytorch': {
  25. 'checking_item': OutputKeys.TEXT,
  26. 'example': 'wav_example'
  27. },
  28. 'test_run_with_pcm_pytorch': {
  29. 'checking_item': OutputKeys.TEXT,
  30. 'example': 'wav_example'
  31. },
  32. 'test_run_with_wav_tf': {
  33. 'checking_item': OutputKeys.TEXT,
  34. 'example': 'wav_example'
  35. },
  36. 'test_run_with_pcm_tf': {
  37. 'checking_item': OutputKeys.TEXT,
  38. 'example': 'wav_example'
  39. },
  40. 'test_run_with_url_pytorch': {
  41. 'checking_item': OutputKeys.TEXT,
  42. 'example': 'wav_example'
  43. },
  44. 'test_run_with_url_tf': {
  45. 'checking_item': OutputKeys.TEXT,
  46. 'example': 'wav_example'
  47. },
  48. 'test_run_with_wav_dataset_pytorch': {
  49. 'checking_item': OutputKeys.TEXT,
  50. 'example': 'dataset_example'
  51. },
  52. 'test_run_with_wav_dataset_tf': {
  53. 'checking_item': OutputKeys.TEXT,
  54. 'example': 'dataset_example'
  55. },
  56. 'dataset_example': {
  57. 'Wrd': 49532, # the number of words
  58. 'Snt': 5000, # the number of sentences
  59. 'Corr': 47276, # the number of correct words
  60. 'Ins': 49, # the number of insert words
  61. 'Del': 152, # the number of delete words
  62. 'Sub': 2207, # the number of substitution words
  63. 'wrong_words': 2408, # the number of wrong words
  64. 'wrong_sentences': 1598, # the number of wrong sentences
  65. 'Err': 4.86, # WER/CER
  66. 'S.Err': 31.96 # SER
  67. },
  68. 'wav_example': {
  69. 'text': '每一天都要快乐喔'
  70. }
  71. }
  72. all_models_info = [
  73. {
  74. 'model_id':
  75. 'damo/speech_paraformer_asr_nat-zh-cn-16k-common-vocab8358-tensorflow1',
  76. 'wav_path': 'data/test/audios/asr_example.wav'
  77. },
  78. {
  79. 'model_id': 'damo/speech_paraformer_asr_nat-aishell1-pytorch',
  80. 'wav_path': 'data/test/audios/asr_example.wav'
  81. },
  82. {
  83. 'model_id': 'damo/speech_paraformer_asr_nat-aishell2-pytorch',
  84. 'wav_path': 'data/test/audios/asr_example.wav'
  85. },
  86. {
  87. 'model_id':
  88. 'damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8358-tensorflow1',
  89. 'wav_path': 'data/test/audios/asr_example.wav'
  90. },
  91. {
  92. 'model_id':
  93. 'damo/speech_paraformer_asr_nat-zh-cn-8k-common-vocab8358-tensorflow1',
  94. 'wav_path': 'data/test/audios/asr_example_8K.wav'
  95. },
  96. {
  97. 'model_id':
  98. 'damo/speech_UniASR_asr_2pass-zh-cn-16k-common-vocab8358-tensorflow1-online',
  99. 'wav_path': 'data/test/audios/asr_example.wav'
  100. },
  101. {
  102. 'model_id':
  103. 'damo/speech_UniASR_asr_2pass-zh-cn-16k-common-vocab8358-tensorflow1-offline',
  104. 'wav_path': 'data/test/audios/asr_example.wav'
  105. },
  106. {
  107. 'model_id':
  108. 'damo/speech_UniASR_asr_2pass-zh-cn-8k-common-vocab8358-tensorflow1-online',
  109. 'wav_path': 'data/test/audios/asr_example_8K.wav'
  110. },
  111. {
  112. 'model_id':
  113. 'damo/speech_UniASR_asr_2pass-zh-cn-8k-common-vocab8358-tensorflow1-offline',
  114. 'wav_path': 'data/test/audios/asr_example_8K.wav'
  115. },
  116. {
  117. 'model_id':
  118. 'damo/speech_UniASR-large_asr_2pass-zh-cn-16k-common-vocab8358-tensorflow1-offline',
  119. 'wav_path': 'data/test/audios/asr_example.wav'
  120. },
  121. {
  122. 'model_id':
  123. 'damo/speech_UniASR_asr_2pass-cn-en-moe-16k-vocab8358-tensorflow1-online',
  124. 'wav_path': 'data/test/audios/asr_example_cn_en.wav'
  125. },
  126. {
  127. 'model_id':
  128. 'damo/speech_UniASR_asr_2pass-cn-en-moe-16k-vocab8358-tensorflow1-offline',
  129. 'wav_path': 'data/test/audios/asr_example_cn_en.wav'
  130. },
  131. {
  132. 'model_id':
  133. 'damo/speech_UniASR_asr_2pass-cn-dialect-16k-vocab8358-tensorflow1-online',
  134. 'wav_path': 'data/test/audios/asr_example_cn_dialect.wav'
  135. },
  136. {
  137. 'model_id':
  138. 'damo/speech_UniASR_asr_2pass-cn-dialect-16k-vocab8358-tensorflow1-offline',
  139. 'wav_path': 'data/test/audios/asr_example_cn_dialect.wav'
  140. },
  141. {
  142. 'model_id':
  143. 'damo/speech_paraformer_asr_nat-zh-cn-16k-common-vocab3444-tensorflow1-online',
  144. 'wav_path': 'data/test/audios/asr_example.wav'
  145. },
  146. {
  147. 'model_id':
  148. 'damo/speech_paraformer_asr_nat-zh-cn-8k-common-vocab3444-tensorflow1-online',
  149. 'wav_path': 'data/test/audios/asr_example_8K.wav'
  150. },
  151. {
  152. 'model_id':
  153. 'damo/speech_UniASR_asr_2pass-en-16k-common-vocab1080-tensorflow1-offline',
  154. 'wav_path': 'data/test/audios/asr_example_en.wav'
  155. },
  156. {
  157. 'model_id':
  158. 'damo/speech_UniASR_asr_2pass-en-16k-common-vocab1080-tensorflow1-online',
  159. 'wav_path': 'data/test/audios/asr_example_en.wav'
  160. },
  161. {
  162. 'model_id':
  163. 'damo/speech_UniASR_asr_2pass-ru-16k-common-vocab1664-tensorflow1-offline',
  164. 'wav_path': 'data/test/audios/asr_example_ru.wav'
  165. },
  166. {
  167. 'model_id':
  168. 'damo/speech_UniASR_asr_2pass-ru-16k-common-vocab1664-tensorflow1-online',
  169. 'wav_path': 'data/test/audios/asr_example_ru.wav'
  170. },
  171. {
  172. 'model_id':
  173. 'damo/speech_UniASR_asr_2pass-es-16k-common-vocab3445-tensorflow1-offline',
  174. 'wav_path': 'data/test/audios/asr_example_es.wav'
  175. },
  176. {
  177. 'model_id':
  178. 'damo/speech_UniASR_asr_2pass-es-16k-common-vocab3445-tensorflow1-online',
  179. 'wav_path': 'data/test/audios/asr_example_es.wav'
  180. },
  181. {
  182. 'model_id':
  183. 'damo/speech_UniASR_asr_2pass-ko-16k-common-vocab6400-tensorflow1-offline',
  184. 'wav_path': 'data/test/audios/asr_example_ko.wav'
  185. },
  186. {
  187. 'model_id':
  188. 'damo/speech_UniASR_asr_2pass-ko-16k-common-vocab6400-tensorflow1-online',
  189. 'wav_path': 'data/test/audios/asr_example_ko.wav'
  190. },
  191. {
  192. 'model_id':
  193. 'damo/speech_UniASR_asr_2pass-ja-16k-common-vocab93-tensorflow1-online',
  194. 'wav_path': 'data/test/audios/asr_example_ja.wav'
  195. },
  196. {
  197. 'model_id':
  198. 'damo/speech_UniASR_asr_2pass-ja-16k-common-vocab93-tensorflow1-offline',
  199. 'wav_path': 'data/test/audios/asr_example_ja.wav'
  200. },
  201. {
  202. 'model_id':
  203. 'damo/speech_UniASR_asr_2pass-id-16k-common-vocab1067-tensorflow1-online',
  204. 'wav_path': 'data/test/audios/asr_example_id.wav'
  205. },
  206. {
  207. 'model_id':
  208. 'damo/speech_UniASR_asr_2pass-id-16k-common-vocab1067-tensorflow1-offline',
  209. 'wav_path': 'data/test/audios/asr_example_id.wav'
  210. },
  211. ]
  212. def setUp(self) -> None:
  213. self.am_pytorch_model_id = 'damo/speech_paraformer_asr_nat-aishell1-pytorch'
  214. self.am_tf_model_id = 'damo/speech_paraformer_asr_nat-zh-cn-16k-common-vocab8358-tensorflow1'
  215. # this temporary workspace dir will store waveform files
  216. self.workspace = os.path.join(os.getcwd(), '.tmp')
  217. self.task = Tasks.auto_speech_recognition
  218. if not os.path.exists(self.workspace):
  219. os.mkdir(self.workspace)
  220. def tearDown(self) -> None:
  221. # remove workspace dir (.tmp)
  222. shutil.rmtree(self.workspace, ignore_errors=True)
  223. def run_pipeline(self,
  224. model_id: str,
  225. audio_in: Union[str, bytes],
  226. sr: int = None) -> Dict[str, Any]:
  227. inference_16k_pipline = pipeline(
  228. task=Tasks.auto_speech_recognition, model=model_id)
  229. rec_result = inference_16k_pipline(audio_in, audio_fs=sr)
  230. return rec_result
  231. def log_error(self, functions: str, result: Dict[str, Any]) -> None:
  232. logger.error(ColorCodes.MAGENTA + functions + ': FAILED.'
  233. + ColorCodes.END)
  234. logger.error(
  235. ColorCodes.MAGENTA + functions + ' correct result example:'
  236. + ColorCodes.YELLOW
  237. + str(self.action_info[self.action_info[functions]['example']])
  238. + ColorCodes.END)
  239. raise ValueError('asr result is mismatched')
  240. def check_result(self, functions: str, result: Dict[str, Any]) -> None:
  241. if result.__contains__(self.action_info[functions]['checking_item']):
  242. logger.info(ColorCodes.MAGENTA + functions + ': SUCCESS.'
  243. + ColorCodes.END)
  244. logger.info(
  245. ColorCodes.YELLOW
  246. + str(result[self.action_info[functions]['checking_item']])
  247. + ColorCodes.END)
  248. else:
  249. self.log_error(functions, result)
  250. def wav2bytes(self, wav_file):
  251. audio, fs = soundfile.read(wav_file)
  252. # float32 -> int16
  253. audio = np.asarray(audio)
  254. dtype = np.dtype('int16')
  255. i = np.iinfo(dtype)
  256. abs_max = 2**(i.bits - 1)
  257. offset = i.min + abs_max
  258. audio = (audio * abs_max + offset).clip(i.min, i.max).astype(dtype)
  259. # int16(PCM_16) -> byte
  260. audio = audio.tobytes()
  261. return audio, fs
  262. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  263. def test_run_with_pcm(self):
  264. """run with wav data
  265. """
  266. logger.info('Run ASR test with wav data (tensorflow)...')
  267. audio, sr = self.wav2bytes(os.path.join(os.getcwd(), WAV_FILE))
  268. rec_result = self.run_pipeline(
  269. model_id=self.am_tf_model_id, audio_in=audio, sr=sr)
  270. self.check_result('test_run_with_pcm_tf', rec_result)
  271. logger.info('Run ASR test with wav data (pytorch)...')
  272. rec_result = self.run_pipeline(
  273. model_id=self.am_pytorch_model_id, audio_in=audio, sr=sr)
  274. self.check_result('test_run_with_pcm_pytorch', rec_result)
  275. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  276. def test_run_with_wav(self):
  277. """run with single waveform file
  278. """
  279. logger.info('Run ASR test with waveform file (tensorflow)...')
  280. wav_file_path = os.path.join(os.getcwd(), WAV_FILE)
  281. rec_result = self.run_pipeline(
  282. model_id=self.am_tf_model_id, audio_in=wav_file_path)
  283. self.check_result('test_run_with_wav_tf', rec_result)
  284. logger.info('Run ASR test with waveform file (pytorch)...')
  285. rec_result = self.run_pipeline(
  286. model_id=self.am_pytorch_model_id, audio_in=wav_file_path)
  287. self.check_result('test_run_with_wav_pytorch', rec_result)
  288. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  289. def test_run_with_url(self):
  290. """run with single url file
  291. """
  292. logger.info('Run ASR test with url file (tensorflow)...')
  293. rec_result = self.run_pipeline(
  294. model_id=self.am_tf_model_id, audio_in=URL_FILE)
  295. self.check_result('test_run_with_url_tf', rec_result)
  296. logger.info('Run ASR test with url file (pytorch)...')
  297. rec_result = self.run_pipeline(
  298. model_id=self.am_pytorch_model_id, audio_in=URL_FILE)
  299. self.check_result('test_run_with_url_pytorch', rec_result)
  300. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  301. def test_run_with_wav_dataset_pytorch(self):
  302. """run with datasets, and audio format is waveform
  303. datasets directory:
  304. <dataset_path>
  305. wav
  306. test # testsets
  307. xx.wav
  308. ...
  309. dev # devsets
  310. yy.wav
  311. ...
  312. train # trainsets
  313. zz.wav
  314. ...
  315. transcript
  316. data.text # hypothesis text
  317. """
  318. logger.info('Downloading waveform testsets file ...')
  319. dataset_path = download_and_untar(
  320. os.path.join(self.workspace, LITTLE_TESTSETS_FILE),
  321. LITTLE_TESTSETS_URL, self.workspace)
  322. dataset_path = os.path.join(dataset_path, 'wav', 'test')
  323. logger.info('Run ASR test with waveform dataset (tensorflow)...')
  324. rec_result = self.run_pipeline(
  325. model_id=self.am_tf_model_id, audio_in=dataset_path)
  326. self.check_result('test_run_with_wav_dataset_tf', rec_result)
  327. logger.info('Run ASR test with waveform dataset (pytorch)...')
  328. rec_result = self.run_pipeline(
  329. model_id=self.am_pytorch_model_id, audio_in=dataset_path)
  330. self.check_result('test_run_with_wav_dataset_pytorch', rec_result)
  331. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  332. def test_run_with_all_models(self):
  333. """run with all models
  334. """
  335. logger.info('Run ASR test with all models')
  336. for item in self.all_models_info:
  337. model_id = item['model_id']
  338. wav_path = item['wav_path']
  339. rec_result = self.run_pipeline(
  340. model_id=model_id, audio_in=wav_path)
  341. if rec_result.__contains__(OutputKeys.TEXT):
  342. logger.info(ColorCodes.MAGENTA + str(item['model_id']) + ' '
  343. + ColorCodes.YELLOW
  344. + str(rec_result[OutputKeys.TEXT])
  345. + ColorCodes.END)
  346. else:
  347. logger.info(ColorCodes.MAGENTA + str(rec_result)
  348. + ColorCodes.END)
  349. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  350. def test_demo_compatibility(self):
  351. self.compatibility_check()
  352. if __name__ == '__main__':
  353. unittest.main()