如何在Colab上使用Meta的MusicGen生成音樂
譯文譯者 | 李睿
審校 | 重樓
在人工智能的廣闊領域,深度學習已經徹底改變了許多領域,其中包括自然語言處理、計算機視覺和語音識別。然而,一個吸引研究人員和音樂愛好者的迷人領域是使用人工智能算法生成音樂。MusicGen是一種先進的可控文本到音樂模型之一,可以無縫地將文本提示轉換為迷人的音樂作品。
什么是MusicGen?
MusicGen是為音樂生成設計的卓越模型,它提供了簡單和可控性。與MusicLM等現有方法不同,MusicGen的突出之處在于消除了對自我監督語義表示的需要。該模型采用單級自回歸Transformer架構,并使用32kHz編碼器標記器進行訓練。值得注意的是,MusicGen可以一次生成所有四個碼本,這與傳統方法有所不同。通過在碼本之間引入輕微的延遲,該模型展示了并行預測它們的能力,從而產生每秒僅50步的音頻自動回歸。這種創新的方法優化了音樂生成過程的效率和速度。
MusicGen接受了2萬小時的授權音樂訓練。開發人員還在1萬個高質量音樂曲目的內部數據集以及ShutterStock和Pond5音樂數據上對它進行了訓練。
先決條件
根據MusicGen GitHub官方的回購:
- Python 3.9
- Pytorch 2.0.0
- 具有至少16 GB內存的GPU
可用的MusicGen型號
預訓練模型有4種,分別是:
- 小型:300M型號,僅限文字轉換音樂
- 中型:1.5B型號,僅限文字轉換音樂
- 旋律:1.5B型號,文字轉換音樂和文字+旋律轉換音樂
- 大型:3.3B型號,僅限文字轉換音樂
實驗
下面是使用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上設置MusicGen
確保正在使用GPU進行更快的推理。使用CPU需要9分鐘才能生成10秒的音頻,而使用GPU(T4)只需要35秒。
在開始之前,需要確保在Colab中安裝了Torch和TorchAudio。
從Facebook安裝AudioCraft庫。
python3 -m pip install –U git+https://github.com/facebookresearch/audiocraft#egg=audiocraft
導入必要的庫。
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')
設置參數(可選):
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.生成音樂延續
要創建音樂延續,需要一個音頻文件。將該文件提供給模型,模型將生成并添加更多的音樂。
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文件寫入磁盤的函數。它將模型輸出作為第一個輸入,文件名作為第二個輸入。
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.全面實施(Google Colab文件鏈接)
在Colab文件中給出了Meta公司的MusicGen庫的完整實現。使用它可以自由地探索和創作音樂。
結論
綜上所述,Audiocraft的MusicGen是一個功能強大且可控的音樂生成模型。展望未來,Audiocraft在人工智能生成音樂方面擁有令人興奮的未來發展潛力。無論是音樂家還是人工智能愛好者,Audiocraft的MusicGen都將為他們打開一個充滿創造力的世界。
原文標題:Generate Music Using Meta’s MusicGen On Colab,作者:Mittal Patel