前文回溯,之前一篇:含辭未吐,聲若幽蘭,史上最強(qiáng)免費(fèi)人工智能AI語(yǔ)音合成TTS服務(wù)微軟Azure(.10接入),利用AI技術(shù)將文本合成語(yǔ)音,現(xiàn)在反過(guò)來(lái),利用開(kāi)源庫(kù)再將語(yǔ)音轉(zhuǎn)回文字,所謂聞其聲而知雅意 。
是一個(gè)開(kāi)源的語(yǔ)音識(shí)別庫(kù),它是由 AI(FAIR)開(kāi)發(fā)的 , 支持多種語(yǔ)言的語(yǔ)音識(shí)別 。它使用了雙向循環(huán)神經(jīng)網(wǎng)絡(luò)(bi- RNNs)來(lái)識(shí)別語(yǔ)音并將其轉(zhuǎn)換為文本 。支持自定義模型,可以用于實(shí)現(xiàn)在線語(yǔ)音識(shí)別,并且具有高級(jí)的語(yǔ)音識(shí)別功能,支持語(yǔ)音識(shí)別中的語(yǔ)音活動(dòng)檢測(cè)和語(yǔ)音識(shí)別中的語(yǔ)音轉(zhuǎn)文本 。它是使用進(jìn)行開(kāi)發(fā) , 可以使用 API來(lái)調(diào)用語(yǔ)音識(shí)別,并且提供了一系列的預(yù)訓(xùn)練模型和數(shù)據(jù)集來(lái)幫助用戶開(kāi)始使用 。
基于MPS的安裝
我們知道一直以來(lái)在M芯片的MacOs系統(tǒng)中都不支持cuda模式原人工智能語(yǔ)音計(jì)算器,而現(xiàn)在,新的MPS后端擴(kuò)展了生態(tài)系統(tǒng)并提供了現(xiàn)有的腳本功能來(lái)在 GPU上設(shè)置和運(yùn)行操作 。
截止本文發(fā)布,與 3.11不兼容 , 所以我們將使用最新的 3.10.x 版本 。
確保安裝.10最新版:
?transformers git:(stable) python3Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>
隨后運(yùn)行安裝命令:
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
安裝成功后,在終端里驗(yàn)證-MPS的狀態(tài):
?transformers git:(stable) python3Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import torch>>> torch.backends.mps.is_available()True>>>
返回True即可 。
MPS (Multi- )性能測(cè)試
MPS (Multi- )是中的一種分布式訓(xùn)練方式 。它是基于Apple的MPS(Metal) 框架開(kāi)發(fā)的 。MPS可以在多核的蘋果設(shè)備上加速的運(yùn)算 。MPS使用了多個(gè)設(shè)備上的多個(gè)核心來(lái)加速模型的訓(xùn)練 。它可以將模型的計(jì)算過(guò)程分配到多個(gè)核心上,并且可以在多個(gè)設(shè)備上進(jìn)行訓(xùn)練,從而提高訓(xùn)練速度 。
MPS 可以在 Apple 的設(shè)備(如和 iPad)上加速模型訓(xùn)練,也可以在 Mac 上使用 ??梢允褂肕PS來(lái)加速卷積神經(jīng)網(wǎng)絡(luò)(CNNs)、循環(huán)神經(jīng)網(wǎng)絡(luò)(RNNs)和其他類型的神經(jīng)網(wǎng)絡(luò) 。使用MPS可以在不改變模型結(jié)構(gòu)的情況下,通過(guò)分布式訓(xùn)練來(lái)加速模型的訓(xùn)練速度 。
現(xiàn)在我們來(lái)做一個(gè)簡(jiǎn)單測(cè)試:
import torchimport timeitimport randomx = torch.ones(50000000,device='cpu')print(timeit.timeit(lambda:x*random.randint(0,100),number=1))
首先創(chuàng)建一個(gè)大小為的全為1的張量 x,并將其設(shè)置為在cpu上運(yùn)算 。最后使用 . 函數(shù)來(lái)測(cè)量在 CPU 上執(zhí)行 x 乘以一個(gè)隨機(jī)整數(shù)的時(shí)間 。=1表示只運(yùn)行一次 。這段代碼的作用是在cpu上測(cè)量運(yùn)算一個(gè)張量的時(shí)間 。
運(yùn)行結(jié)果:
?nlp_chinese /opt/homebrew/bin/python3.10 "/Users/liuyue/wodfan/work/nlp_chinese/mps_test.py"0.020812375005334616
在10核M1pro的cpu芯片加持下原人工智能語(yǔ)音計(jì)算器,運(yùn)行時(shí)間為:0.
隨后換成MPS模式:
import torchimport timeitimport randomx = torch.ones(50000000,device='mps')print(timeit.timeit(lambda:x*random.randint(0,100),number=1))
程序返回:
?nlp_chinese /opt/homebrew/bin/python3.10 "/Users/liuyue/wodfan/work/nlp_chinese/mps_test.py"0.003058041911572218
16核的GPU僅用時(shí):0.
也就是說(shuō)MPS的運(yùn)行速度比CPU提升了7倍左右 。
語(yǔ)音識(shí)別
安裝好了,我們安裝:
pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git
安裝好之后進(jìn)行驗(yàn)證:
?transformers git:(stable) whisperusage: whisper [-h] [--model {tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large}] [--model_dir MODEL_DIR][--device DEVICE] [--output_dir OUTPUT_DIR] [--verbose VERBOSE] [--task {transcribe,translate}][--language {af,am,ar,as,az,ba,be,bg,bn,bo,br,bs,ca,cs,cy,da,de,el,en,es,et,eu,fa,fi,fo,fr,gl,gu,ha,haw,hi,hr,ht,hu,hy,id,is,it,iw,ja,jw,ka,kk,km,kn,ko,la,lb,ln,lo,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,my,ne,nl,nn,no,oc,pa,pl,ps,pt,ro,ru,sa,sd,si,sk,sl,sn,so,sq,sr,su,sv,sw,ta,te,tg,th,tk,tl,tr,tt,uk,ur,uz,vi,yi,yo,zh,Afrikaans,Albanian,Amharic,Arabic,Armenian,Assamese,Azerbaijani,Bashkir,Basque,Belarusian,Bengali,Bosnian,Breton,Bulgarian,Burmese,Castilian,Catalan,Chinese,Croatian,Czech,Danish,Dutch,English,Estonian,Faroese,Finnish,Flemish,French,Galician,Georgian,German,Greek,Gujarati,Haitian,Haitian Creole,Hausa,Hawaiian,Hebrew,Hindi,Hungarian,Icelandic,Indonesian,Italian,Japanese,Javanese,Kannada,Kazakh,Khmer,Korean,Lao,Latin,Latvian,Letzeburgesch,Lingala,Lithuanian,Luxembourgish,Macedonian,Malagasy,Malay,Malayalam,Maltese,Maori,Marathi,Moldavian,Moldovan,Mongolian,Myanmar,Nepali,Norwegian,Nynorsk,Occitan,Panjabi,Pashto,Persian,Polish,Portuguese,Punjabi,Pushto,Romanian,Russian,Sanskrit,Serbian,Shona,Sindhi,Sinhala,Sinhalese,Slovak,Slovenian,Somali,Spanish,Sundanese,Swahili,Swedish,Tagalog,Tajik,Tamil,Tatar,Telugu,Thai,Tibetan,Turkish,Turkmen,Ukrainian,Urdu,Uzbek,Valencian,Vietnamese,Welsh,Yiddish,Yoruba}]
隨后安裝:
brew install ffmpeg
然后編寫語(yǔ)音識(shí)別代碼:
import whispermodel = whisper.load_model("small")# load audio and pad/trim it to fit 30 secondsaudio = whisper.load_audio("/Users/liuyue/wodfan/work/mydemo/b1.wav")audio = whisper.pad_or_trim(audio)# make log-Mel spectrogram and move to the same device as the modelmel = whisper.log_mel_spectrogram(audio).to("cpu")# detect the spoken language_, probs = model.detect_language(mel)print(f"Detected language: {max(probs, key=probs.get)}")# decode the audiooptions = whisper.DecodingOptions(fp16 = False)result = whisper.decode(model, mel, options)# print the recognized textprint(result.text)
這里導(dǎo)入音頻后 , 通過(guò).方法自動(dòng)檢測(cè)語(yǔ)言,然后輸出文本:
?minGPT git:(master) ? /opt/homebrew/bin/python3.10 "/Users/liuyue/wodfan/work/minGPT/wisper_test.py"Detected language: zhHello大家好,這里是劉悅的技術(shù)博客,眾神殿內(nèi),高朋滿座,圣有如云,VMware,Virtual Box,UPM等虛擬機(jī)大神群英匯翠,指見(jiàn)位于C位王座上的Parallels唱網(wǎng)抬頭,緩緩群尋,屁膩群小,目光到處,無(wú)人敢抬頭對(duì)視 。是的,如果說(shuō)虛擬機(jī)領(lǐng)域有一位王者,非Parallels不能領(lǐng)袖群倫,畢竟大廠背書,功能滿格,美中不足之處就是價(jià)格略高,
這里使用的small模型,也可以用更大的模型比如:、large 。模型越大,效果越好 。
如果想使用MPS的方式,需要改寫一下源碼,將方法的參數(shù)改為mps即可:
def load_model(name: str, device: Optional[Union[str, torch.device]] = None, download_root: str = None, in_memory: bool = False) -> Whisper:"""Load a Whisper ASR modelParameters----------name : strone of the official model names listed by `whisper.available_models()`, orpath to a model checkpoint containing the model dimensions and the model state_dict.device : Union[str, torch.device]the PyTorch device to put the model intodownload_root: strpath to download the model files; by default, it uses "~/.cache/whisper"in_memory: boolwhether to preload the model weights into host memoryReturns-------model : WhisperThe Whisper ASR model instance"""if device is None:device = "cuda" if torch.cuda.is_available() else "mps"
代碼在第18行 。
隨后運(yùn)行腳本也改成mps:
import whispermodel = whisper.load_model("medium")# load audio and pad/trim it to fit 30 secondsaudio = whisper.load_audio("/Users/liuyue/wodfan/work/mydemo/b1.wav")audio = whisper.pad_or_trim(audio)# make log-Mel spectrogram and move to the same device as the modelmel = whisper.log_mel_spectrogram(audio).to("mps")# detect the spoken language_, probs = model.detect_language(mel)print(f"Detected language: {max(probs, key=probs.get)}")# decode the audiooptions = whisper.DecodingOptions(fp16 = False)result = whisper.decode(model, mel, options)# print the recognized textprint(result.text)
這回切換為模型,程序返回:
【mps 基于PyTorch的人工智能本地語(yǔ)音識(shí)別庫(kù)Whisper(Python】?minGPT git:(master) ? /opt/homebrew/bin/python3.10 "/Users/liuyue/wodfan/work/minGPT/wisper_test.py"100%|█████████████████████████████████████| 1.42G/1.42G [02:34<00:00, 9.90MiB/s]Detected language: zhHello 大家好,這里是劉悅的技術(shù)博客,眾神殿內(nèi),高朋滿座,圣有如云,VMware,Virtualbox,UTM等虛擬機(jī)大神群音惠翠,只見(jiàn)位于C位王座上的Parallels唱往抬頭,緩緩輕尋,屁逆群小,目光到處,無(wú)人敢抬頭對(duì)視 。
效率和精準(zhǔn)度提升了不少,但模型的體積也更大,達(dá)到了1.42g 。
結(jié)語(yǔ)
作為一個(gè)開(kāi)源的語(yǔ)音識(shí)別庫(kù),支持多種語(yǔ)言,并且使用雙向循環(huán)神經(jīng)網(wǎng)絡(luò)(bi- RNNs)來(lái)識(shí)別語(yǔ)音并將其轉(zhuǎn)換為文本,支持自定義模型 , 可以用于實(shí)現(xiàn)在線語(yǔ)音識(shí)別,并且具有高級(jí)的語(yǔ)音識(shí)別功能,支持語(yǔ)音識(shí)別中的語(yǔ)音活動(dòng)檢測(cè)和語(yǔ)音識(shí)別中的語(yǔ)音轉(zhuǎn)文本 , 在的MPS加成下,更是猛虎添翼,絕世好庫(kù),值得擁有 。
本文到此結(jié)束,希望對(duì)大家有所幫助 。
- Jumpsec安全研究人員上個(gè)月發(fā)現(xiàn)一個(gè)安全問(wèn)題
- 問(wèn)題1:你們是基于怎樣的靈感來(lái)開(kāi)辟這些創(chuàng)意文化活動(dòng)的?
- 基于Docker的漏洞驗(yàn)證框架的設(shè)計(jì)與實(shí)現(xiàn)-AET
- 原理+代碼 基于Python實(shí)現(xiàn)五大常用分類算法
- 基于Apache Flink實(shí)時(shí)計(jì)算數(shù)據(jù)流業(yè)務(wù)引擎在京東零售的實(shí)踐和落地
- 【知行曉莊】基于“人際交往”主題的小學(xué)心理課堂教學(xué)策略探析——記第16周棲霞區(qū)小
- 基于Mindspore2.0的GPT2預(yù)訓(xùn)練模型遷移教程
- 代碼開(kāi)源 基于STM32與ESP8266的太空人WiFi天氣時(shí)鐘
- 《都挺好》中的那些話:你給你的孩子投毒了嗎?
