鸣涧_GC96O 发表于 2021-3-28 11:25:06

当GAN生成图像可以卡音效,这个Python包只需几行代码就能...

本帖最后由 sunsili 于 2022-1-24 15:24 编辑

当GAN生成图像可以卡音效,这个Python包只需几行代码就能实现「音画同步」

GAN 生成图像奇幻诡谲,对此我们早已不陌生。如果它们还可以卡音效呢?最近,有人就创建了一个实现类似效果的 Python 包——Lucid Sonic Dreams,只用几行代码就可以实现 AI 生成画作与音乐的同步。

[*]GitHub 地址:https://github.com/mikaelalafriz/lucid-sonic-dreams
[*]Colab 教程地址:https://colab.research.google.co ... tts7RQD?usp=sharing

在该项目提供的 demo 视频中,伴随着 Saje 的歌「Raspberry」,GAN 生成的图像不断变换且呈现出了对应的节奏。

工作原理
生成艺术品通常是由 GAN 网络来完成的。Lucid Sonic Dreams 包默认使用 StyleGAN2-ADA 架构,不过 GAN 架构也可以自定义。将这些模型在特定「风格」的图像数据集上进行训练,使之能够输出与训练图像风格一致的无穷多张图像。此外,Lucid Sonic Dreams 使用 Justin Pinkney 创建库中的 StyleGAN2 预训练模型(地址:https://github.com/justinpinkney/awesome-pretrained-stylegan2) 。
那么这些图像到底是如何生成的呢?
将输入馈送至 StyleGAN2 模型(输入为有 512 个数字的向量),输入决定了模型的输出图像,因此对输入向量进行微小更改也将带来输出图像的微小变化。
现在,有趣的部分到了:如果我们从音乐中获取声波,从声波中提取数值(如振幅)并添加至输入向量,会发生什么?
Lucid Sonic Dreams 对视频每一帧执行这些操作,生成脉冲与变换与音乐一致的图像。
具体而言,利用 Lucid Sonic Dreams 包,音乐控制了 3 个主要视觉组件:脉冲(Pulse)、运动(Motion)和类别(Class):

[*]脉冲指视觉画面随着音乐的敲击性节奏而「跳动」。从数学角度来看,「脉冲」是向输入向量暂时添加声波振幅的结果(即在下一帧中该向量仍是初始向量);
[*]运动指视觉画面变换的速度。从数学上看,它是向输入向量累积添加振幅(即添加的振幅后续不会被清零);
[*]类别指生成图像中物体的标签,例如基于 WikiArt 图像训练的风格中就有 167 个类别(包括梵高、达芬奇、抽象派等)。而这些由音调进行控制,具体而言,12 个音高分别对应 12 个不同类别。这些音高的振幅对传输至第二个输入向量(类别向量)的数字造成影响,而这由模型生成的对象来决定。

项目作者表示,这个想法受到 Matt Siegelman 的 Deep Music Visualizer 项目的启发。目前网上也有一些类似的项目,但 Lucid Sonic Dreams 的独特之处在于以 Python 包形式实现,且允许自定义。
使用 Lucid Sonic Dreams 包,你可以做到这些
Lucid Sonic Dreams 具备极强的易用性和灵活性。用户可以使用 pip 进行安装:
然后只需输入几行 Python 代码即可:
from lucidsonicdreams import LucidSonicDream
L = LucidSonicDream(song = 'chemical_love.mp3', style = 'abstract photos')L.hallucinate(file_name = 'chemical_love.mp4')

改变风格
运行以下代码,我们可以查看默认可用的风格:
from lucidsonicdreams import show_styles
show_styles()

这样就可以得到一组风格名称,这些风格来自 Justin Pinkney 创建的库。你还可以输入自己的 StyleGAN 权重,或者使用其他 GAN 架构。
调整参数
Lucid Sonic Dreams 包的默认设置很好用,但它实际上有很多参数——30 多个,不过这些参数是可以调整的(参数详细信息参见 Colab 教程)。
哪些参数最重要呢?我们来看整个视频生成 pipeline:
首先,对输入向量进行初始化和插值,作为视频的「基础运动」(base motion)。参数 speed_fpm 控制运动的速度,fpm 表示「每分钟帧数」,即每分钟初始化的向量数。对于每个后续帧而言,参数 pulse_react, motion_react, and class_react 控制音频操纵每个对应组件的程度。
模型基于这些向量生成图像后,图像被传输经过一系列特效(也对音乐产生反应)。默认情况下,Lucid Sonic Dreams 包具备「contrast」和「flash」特效,可以与音频的敲击性节奏同步。通过设置 contrast_strength 和 flash_strength 参数,即可进行调整。使用者还可以创建自定义特效。
以下代码展示了调参过程:
<div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">L = LucidSonicDream('pancake_feet.mp3', style = 'modern art')</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            L.hallucinate(file_name = 'pancake_feet.mp4',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            speed_fpm = 0,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            motion_react = 0.8,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            contrast_strength = 0.5,</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">               flash_strength = 0.7)</font></font></font></font></div>

使用自己的 StyleGAN 权重
如果你自己训练过 StyleGAN,或者在网上获得了一些模型权重,你可以选择将文件路径传输至这些权重,作为风格参数的值。
例如,文章开头的视频使用的是 Jeremy Torman 训练的模型。生成视频所用代码如下所示:
<div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">L = LucidSonicDream(song = 'raspberry.mp3', style = 'VisionaryArt.pkl')</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            L.hallucinate(file_name = 'raspberry.mp4',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            pulse_react = 1.2,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            motion_react = 0.7,</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            contrast_strength = 0.5,</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">               flash_strength = 0.5)</font></font></font></font></div>
使用单独的音轨


这个包还可以用作音乐可视化工具,使用者可以上传单独的音轨,进而控制 Pulse、Motion、Class、Contrast 和 Flash。如果你想使这些视觉组件与特定的乐器同步,使用这个包是不错的选择。你还可以利用这些单独的音轨自定义特效。
以下是示例代码:
<div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">L = LucidSonicDream(song = 'lucidsonicdreams_main.mp3',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                   pulse_audio = 'lucidsonicdreams_pulse.mp3',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                   class_audio = 'lucidsonicdreams_class.mp3',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                   style = 'wikiart')</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">L.hallucinate('lucidsonicdreams.mp4',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            pulse_react = 0.25,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             motion_react = 0,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            classes = ,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             dominant_classes_first = True,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             class_shuffle_seconds = 8,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             class_smooth_seconds = 4,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             class_pitch_react = 0.2,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            contrast_strength = 0.3)</font></font></font></font></div>

自定义特效


除了内置的「Contrast」和「Flash」特效外,Lucid Sonic Dreams 包还允许用户自定义创建特效。用户只需创建一个包含至少以下 3 个参数的函数即可:array,表示应用特效的图像;strength,决定对音乐的反应强度;amplitude 表示在任意给定时间点的音量。之后,将该自定义函数传输至 EffectsGenerator 对象。
作者用以下代码进行了试验,其使用的是 scikit-image 的 swirl 特效:
<font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">import numpy as np from skimage.transform import swirlfrom lucidsonicdreams import EffectsGenerator

</font></font></font></font><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">def swirl_func(array, strength, amplitude):</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">      swirled_image = swirl(array,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                       rotation = 0,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                       strength = 100 * strength * amplitude,</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                        radius=650)</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">    return (swirled_image*255).astype(np.uint8)</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">swirl_effect = EffectsGenerator(swirl_func,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                                   audio = 'unfaith.mp3',</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                                   strength = 0.2,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">                                                   percussive = False)</font></font></font></font></div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">L = LucidSonicDream('unfaith.mp3',                  style = 'textures')
</font></font></font></font><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             L.hallucinate('unfaith.mp4',
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             motion_react = 0.15,</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             speed_fpm = 2,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             pulse_react = 1.5,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            contrast_strength = 1,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">             flash_strength = 1,
</font></font></font></font></div><div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">            custom_effects = )</font></font></font></font></div><font color="rgb(51, 51, 51)"><font style="background-color:rgb(255, 255, 255)"><font "="" face="-apple-system, BlinkMacSystemFont, "><font style="font-size: 17px">files.download("unfaith.mp4")</font></font></font></font>

鸣涧_GC96O 发表于 2021-3-28 11:27:55

使用其他 GAN 架构

你还可以使用其他 GAN 架构。只需定义一个函数,该函数以一组噪声向量和类别向量(NumPy 数组)作为输入,输出一组 Pillow 图像。事实上,该函数甚至不需要使用 GAN,它可以是能够将输入向量转换成图像的任意函数。

下列代码使用 BigGAN 的 PyTorch 实现复现了 Deep Music Visualizer:

from pytorch_pretrained_biggan import BigGAN, convert_to_images
import torch

biggan = BigGAN.from_pretrained('biggan-deep-512')
biggan.to('cuda:0')

def biggan_func(noise_batch, class_batch):
noise_tensor = torch.from_numpy(noise_batch).cuda()
class_tensor = torch.from_numpy(class_batch).cuda()
with torch.no_grad():
    output_tensor = biggan(noise_tensor.float(), class_tensor.float(), truncation = 1)
return convert_to_images(output_tensor.cpu())

L = LucidSonicDream('sea_of_voices_inst.mp3',
                  style = biggan_func,
                  input_shape = 128,
                  num_possible_classes = 1000)

L.hallucinate('sea_of_voices.mp4',
            output_audio = 'sea_of_voices.mp3',
            speed_fpm = 3,
            classes = ,
            class_shuffle_seconds = 10,
            class_shuffle_strength = 0.1,
            class_complexity = 0.5,
            class_smooth_seconds = 4,
            motion_react = 0.35,
            flash_strength = 1,
            contrast_strength = 1)
页: [1]
查看完整版本: 当GAN生成图像可以卡音效,这个Python包只需几行代码就能...