自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Colab上使用Meta的MusicGen生成音樂

譯文
人工智能
本文介紹如何在Colab上設(shè)置MusicGen。作為一個先進(jìn)的文本到音樂的模型,MusicGen可以使用人工智能算法生成迷人的音樂作品。

譯者 | 李睿

審校 | 重樓

在人工智能的廣闊領(lǐng)域,深度學(xué)習(xí)已經(jīng)徹底改變了許多領(lǐng)域,其中包括自然語言處理、計算機(jī)視覺和語音識別。然而,一個吸引研究人員和音樂愛好者的迷人領(lǐng)域是使用人工智能算法生成音樂。MusicGen是一種先進(jìn)的可控文本到音樂模型之一,可以無縫地將文本提示轉(zhuǎn)換為迷人的音樂作品。

什么是MusicGen?

MusicGen是為音樂生成設(shè)計的卓越模型,它提供了簡單和可控性。與MusicLM等現(xiàn)有方法不同,MusicGen的突出之處在于消除了對自我監(jiān)督語義表示的需要。該模型采用單級自回歸Transformer架構(gòu),并使用32kHz編碼器標(biāo)記器進(jìn)行訓(xùn)練。值得注意的是,MusicGen可以一次生成所有四個碼本,這與傳統(tǒng)方法有所不同。通過在碼本之間引入輕微的延遲,該模型展示了并行預(yù)測它們的能力,從而產(chǎn)生每秒僅50步的音頻自動回歸。這種創(chuàng)新的方法優(yōu)化了音樂生成過程的效率和速度。

MusicGen接受了2萬小時的授權(quán)音樂訓(xùn)練。開發(fā)人員還在1萬個高質(zhì)量音樂曲目的內(nèi)部數(shù)據(jù)集以及ShutterStock和Pond5音樂數(shù)據(jù)上對它進(jìn)行了訓(xùn)練。

先決條件

根據(jù)MusicGen GitHub官方的回購:

  • Python 3.9
  • Pytorch 2.0.0
  • 具有至少16 GB內(nèi)存的GPU

可用的MusicGen型號

預(yù)訓(xùn)練模型有4種,分別是:

  • 小型:300M型號,僅限文字轉(zhuǎn)換音樂
  • 中型:1.5B型號,僅限文字轉(zhuǎn)換音樂
  • 旋律:1.5B型號,文字轉(zhuǎn)換音樂和文字+旋律轉(zhuǎn)換音樂
  • 大型:3.3B型號,僅限文字轉(zhuǎn)換音樂

實(shí)驗

下面是使用MusicGen大型模型生成條件音樂的輸出。

Text Input: Jingle bell tune with violin and piano
Output: (Using MusicGen "large" model)

下面是MusicGen旋律模型的輸出。使用上面的音頻和文本輸入來生成以下音頻。

Text Input: Add heavy drums drums and only drums
Output: (Using MusicGen "melody" model)

如何在Colab上設(shè)置MusicGen

確保正在使用GPU進(jìn)行更快的推理。使用CPU需要9分鐘才能生成10秒的音頻,而使用GPU(T4)只需要35秒。

在開始之前,需要確保在Colab中安裝了Torch和TorchAudio。

從Facebook安裝AudioCraft庫。

python3 -m pip install –U git+https://github.com/facebookresearch/audiocraft#egg=audiocraft

導(dǎo)入必要的庫。

from audiocraft.models import musicgen
from audiocraft.utils.notebook import display_audio
import torchfrom audiocraft.data.audio import audio_write

加載模型。其型號列表如下:

# | model types are => small, medium, melody, large |
# | size of models are => 300M, 1.5B, 1.5B, 3.3B |
model = musicgen.MusicGen.get_pretrained('large', device='cuda')

設(shè)置參數(shù)(可選)

model.set_generation_params(duratinotallow=60) # this will generate 60 seconds of audio.

條件音樂生成(通過提供文本生成音樂)。

model.set_generation_params(duratinotallow=60)
res = model.generate( [ 'Jingle bell tune with violin and piano' ], progress=True)
# This will show the music controls on the colab

無條件音樂生成:

res = model.generate_unconditional( num_samples=1, progress=True)
# this will show the music controls on the screendisplay_audio(res, 16000)

1.生成音樂延續(xù)

要創(chuàng)建音樂延續(xù),需要一個音頻文件。將該文件提供給模型,模型將生成并添加更多的音樂。

from audiocraft.utils.notebook import display_audio
import torchaudio

path_to_audio = "path-to-audio-file.wav"
description = "Jazz jazz and only jazz"

# Load audio from a file. Make sure to trim the file if it is too long!
prompt_waveform, prompt_sr = torchaudio.load( path_to_audio )
prompt_duration = 15
prompt_waveform = prompt_waveform[..., :int(prompt_duration * prompt_sr)]
output = model.generate_continuation(prompt_waveform, prompt_sample_rate=prompt_sr,
descriptinotallow=[ description ], progress=True)
display_audio(output, sample_rate=32000)

生成旋律條件生成:

model = musicgen.MusicGen.get_pretrained('melody', device='cuda')

model.set_generation_params(duratinotallow=20)

melody_waveform, sr = torchaudio.load("path-to-audio-file.wav")
melody_waveform = melody_waveform.unsqueeze(0).repeat(2, 1, 1)
output = model.generate_with_chroma(
descriptinotallow=['Add heavy drums'], melody_wavs=melody_waveform, melody_sample_rate=sr,progress=True)
display_audio(output, sample_rate=32000)

將音頻文件寫入磁盤。

如果想從Colab下載文件,那么需要在磁盤上寫入WAV文件。下面是將WAV文件寫入磁盤的函數(shù)。它將模型輸出作為第一個輸入,文件名作為第二個輸入。

def write_wav(output, file_initials):
 try:
 for idx, one_wav in enumerate(output):
 audio_write(f'{file_initials}_{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
 return True
 except Exception as e:
 print("error while writing the file ", e)
 return None
# this will write a file that starts with bollywood
write_wav(res, "audio-file")

2.全面實(shí)施(Google Colab文件鏈接)

在Colab文件中給出了Meta公司的MusicGen庫的完整實(shí)現(xiàn)。使用它可以自由地探索和創(chuàng)作音樂。

結(jié)論

綜上所述,Audiocraft的MusicGen是一個功能強(qiáng)大且可控的音樂生成模型。展望未來,Audiocraft在人工智能生成音樂方面擁有令人興奮的未來發(fā)展?jié)摿?。無論是音樂家還是人工智能愛好者,Audiocraft的MusicGen都將為他們打開一個充滿創(chuàng)造力的世界。

原文標(biāo)題:Generate Music Using Meta’s MusicGen On Colab,作者:Mittal Patel

責(zé)任編輯:華軒 來源: 51CTO
相關(guān)推薦

2023-08-05 13:56:03

數(shù)據(jù)音樂

2023-10-20 08:00:00

人工智能MusicGen

2023-12-08 08:00:00

人工智能MusicGen音樂模型

2023-06-12 16:04:52

谷歌音樂

2023-08-03 07:24:40

MetaAI 語言模型

2019-01-07 09:50:06

Linuxtarball命令

2019-11-26 16:58:51

Linuxpkgsrc

2021-07-25 10:34:17

FedoraPodmanLinux

2023-01-17 07:40:59

LinuxAppImage應(yīng)用程序

2021-10-02 10:10:47

LinuxBusyBox命令

2020-08-24 12:37:54

Linuxxargs命令

2020-06-15 18:20:37

Fedora電子書開源

2018-12-26 09:00:07

VirtualBoxFreeDOSLinux

2022-06-10 10:01:17

MacDockerLinux

2016-11-03 20:06:53

UbuntuGrafanaDocker

2014-07-14 09:24:51

Debiansystemd

2016-01-15 09:56:44

LinuxUbuntuGlances

2022-08-10 13:12:04

Linuxcat命令

2010-07-27 09:44:16

HTML 5

2018-02-25 09:48:36

LinuxUbuntu文件系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號