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

译文
人工智能
本文介绍如何在Colab上设置MusicGen。作为一个先进的文本到音乐的模型,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

责任编辑:华轩 来源: 51CTO
相关推荐

2023-10-20 08:00:00

人工智能MusicGen

2023-08-05 13:56:03

数据音乐

2023-12-08 08:00:00

人工智能MusicGen音乐模型

2023-06-12 16:04:52

谷歌音乐

2023-08-03 07:24:40

MetaAI 语言模型

2021-10-02 10:10:47

LinuxBusyBox命令

2021-07-25 10:34:17

FedoraPodmanLinux

2019-01-07 09:50:06

Linuxtarball命令

2019-11-26 16:58:51

Linuxpkgsrc

2023-01-17 07:40:59

LinuxAppImage应用程序

2020-06-15 18:20:37

Fedora电子书开源

2020-08-24 12:37:54

Linuxxargs命令

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

2016-11-03 20:06:53

UbuntuGrafanaDocker

2022-06-10 10:01:17

MacDockerLinux

2018-12-26 09:00:07

VirtualBoxFreeDOSLinux

2023-06-12 14:15:38

AI开源
点赞
收藏

51CTO技术栈公众号