真没想到,举例视频生成上一轮的集中爆发才过去三个月,没想OpenAI一出手,该领域又直接变天了自打2.16日OpenAI发布sora以来,不但把同时段Google发布的Gemmi Pro 1.5干没了声音,而且网上各个渠道,大量新闻媒体、自媒体(含公号、微博、博客、视频)做了大量的解读,也引发了圈内外的大量关注,很多人因此认为,视频生成领域自此进入了大规模应用前夕,好比NLP领域中GPT3的发布一
但其中有个问题是,因为像素的关系,一张图像有着比较大的维度(比如250 x 250 ),即一张图片上可能有着5万多个元素,如果根据上一张图片的5万多元素去逐一交互下一张图片的5万多个元素,未免工程过于浩大(而且,即便是同一张图片上的5万多个像素点之间两两做self-attention,你都会发现计算复杂度超级高)
故为降低处理的复杂度,可以类似ViT把一张图像划分为九宫格(如下图的左下角),如此,处理9个图像块总比一次性处理250 x 250个像素维度 要好不少吧(ViT的出现直接挑战了此前CNN在视觉领域长达近10年的绝对统治地位,其原理细节详见本文开头提到的此文第4部分 )
当我们理解了一张静态图像的patch表示之后(不管是九宫格,还是16 x 9个格),再来理解所谓的时空Patches就简单多了,无非就是在纵向上加上时间的维度,比如t1 t2 t3 t4 t5 t6
而一个时空patch可能跨3个时间维度,当然,也可能跨5个时间维度
一方面,它类似扩散模型那一套流程,给定输入噪声patches(以及文本提示等调节信息),训练出的模型来预测原始的不带噪声的patches「Sora is a diffusion model, given input noisy patches (and conditioning information like text prompts), it's trained to predict the original "clean" patches 」
类似把视频中的一帧帧画面打上各种马赛克,然后训练一个模型,让它学会去除各种马赛克,且一开始各种失败没关系,反正有原画面作为ground truth,不断缩小与原画面之间的差异即可
而当把一帧帧图片打上全部马赛克之后,可以根据"文本-视频数据集"中对视频的描述/prompt(注,该描述/prompt不仅仅只是通过CLIP去与视频对齐,还经过类似DALLE 3所用的重字幕技术加强 + GPT4对字幕的进一步丰富,下节详述),而有条件的去噪
二方面,它把DPPM中的噪声估计器所用的卷积架构U-Net换成了Transformer架构
总之,总的来说,Sora是一个在不同时长、分辨率和宽高比的视频及图像上训练而成的扩散模型,同时采用了Transformer架构,如sora官博所说,Sora is a diffusion transformer,简称DiT
与DALLE 3类似,研究团队还利用 GPT 将用户简短的prompt 转换为较长的详细字幕,然后发送给视频模型(Similar to DALL·E 3, we also leverage GPT to turn short user prompts into longer detailed captions that are sent to the video model),这使得 Sora 能够生成准确遵循详细字幕或详细prompt 的高质量视频
业界最早是用卷积那一套处理视频,比如时空3D CNN(Learning spatiotemporal features with 3d convolutional networks),由于3D CNN比图像卷积网络需要较多的计算量,许多架构在空间和时间维度上进行卷积的因式分解和/或使用分组卷积,且最近,还通过在后续层中引入自注意力来增强模型,以更好地捕捉长程依赖性
2021年的这两篇论文《Is space-time attention all you need for video understanding?》、《Video transformer network》都是基于transformer做视频理解
而Google于2021年5月提出的ViViT(其对应论文为:ViViT: A Video Vision Transformer)便要尝试在视频中使用ViT模型,且他们充分借鉴了之前3D CNN因式分解等工作,比如考虑到视频作为输入会产生大量的时空token,处理时必须考虑这些长范围token序列的上下文关系,同时要兼顾模型效率问题
第一种,如下图所示,将输入视频划分为token的直接方法是从输入视频剪辑中均匀采样 个帧,使用与ViT 相同的方法独立地嵌入每个2D帧(embed each 2D frame independently using the same method as ViT ),并将所有这些token连接在一起
具体地说,如果从每个帧中提取 个非重叠图像块(就像 ViT 一样),那么总共将有 个token通过transformer编码器进行传递,这个过程可以被看作是简单地构建一个大的2D图像,以便按照ViT的方式进行tokenised(这点和本节开头所提到的21年那篇论文space-time attention for video所用的方式一致)
第二种则是把输入的视频划分成若干个tuplet(类似不重叠的带空间-时间维度的立方体 )
每个tuplet会变成一个token(因这个tublelt的维度就是: t * h * w,故token包含了时间、宽、高 )
经过spatial temperal attention进行空间和时间建模获得有效的视频表征token
x += self.pos_embedding[:, :, :(n + 1)] # 加上位置编码
x = self.dropout(x) # 应用dropout
空间Transformer
重排 x 的维度为 (2,197,768),因为 b×t=1×2=2
空间Transformer处理后,x 的维度变为 (2,197,768)
复制代码
# 将(b, t, n, d)重排为((b t), n, d),为了应用空间变换器
x = rearrange(x, 'b t n d -> (b t) n d')
x = self.space_transformer(x) # 应用空间变换器
x = rearrange(x[:, 0], '(b t) ... -> b t ...', b=b) # 把输出重排回(b, t, ...)
时间Transformer
self.temporal_token 的初始维度为(1,1,768),被复制扩展成 (1,2,768)
cls_temporal_tokens 和 x 在时间维度上拼接后,维度变为(1,3,768)
复制代码
# 在每个时间位置加上cls token
cls_temporal_tokens = repeat(self.temporal_token, '() n d -> b n d', b=b)
x = torch.cat((cls_temporal_tokens, x), dim=1) # 在维度1上进行拼接
x = self.temporal_transformer(x) # 应用时间变换器
池化
如果 self.pool 是 'mean',则对 x 在时间维度上取均值,结果维度变为 (1,768)
如果不是 'mean',则直接取 x 的第一个时间维度的cls token,结果维度同样是 (1,768)
复制代码
# 根据pool参数选择池化方式
x = x.mean(dim=1) if self.pool == 'mean' else x[:, 0]
分类头
self.mlp_head,将 (1,768) 维度的 x 转换为最终的分类结果,其维度取决于类别数num_classes,如果 num_classes 是 10,则最终输出维度为 (1,10)
但它把DPPM中的卷积架构U-Net换成了Transformer架构 (We train latent diffusion models of images, replacing the commonly-used U-Net backbone with a transformer that operates on latent patche,至于什么是U-Net,请参见此文的2.1节)
2.2.2 Diffusion Transformer(DiT)的3个不同的条件策略
如下图所示,便是扩散transformer(DiT)的架构
左侧:我们训练条件潜在DiT模型(conditional latent DiT models), 潜在输入被分解成patch并通过几个DiT blocks处理(The input latent is decomposed into patches and processed by several DiT blocks )
比如当输入是一张256x256x3的图片,对图片做切patch后经过投影得到每个patch的token,得到32x32x4的Noised Latent(在推理时输入直接是32x32x4的噪声),结合当前的Timestep t、Label y作为输入
经过N个Dit Block通过mlp进行输出,得到输出的噪声Noise预测以及对应的协方差矩阵(After the final DiT block, we need to decode our sequence of image tokens into an output noise prediction and an output diagonal covariance prediction),经过T个step采样,得到32x32x4的降噪后的latent
右侧:我们的DiT blocks细节。 我们试验了标准transformer块的变体,这些变体通过自适应层归一化、交叉注意和额外输入token来加入条件(incorporate conditioning via adaptive layer norm, cross-attention and extra input tokens),其中自适应层归一化效果最好
接下来,仔细介绍下上图右侧4个不同的条件策略(说白了,就是怎么加入conditioning)
自适应层归一化 ,即Adaptive layer norm (adaLN) block
鉴于自适应归一化层在GANs和具有U-Net骨干的扩散模型中的广泛使用,故用自适应层归一化(adaLN)替换transformer块中的标准层归一化层
不是直接学习维度方向的缩放和偏移参数 和 ,而是从 和类别标签的嵌入向量之和中回归它们(Rather than directly learn dimension-wise scale and shift parameters γ and β, we regress them from the sum of the embedding vectors of t and c.)
受到基于CNN的U-Net在扩散模型中的成功启发,U-ViT采用了类似的浅层和深层之间的长跳跃连接(Inspired by the success of the CNN-based U-Net in diffusion models [65], U-ViT also employs similar long skip connections between shallow and deep layers )
意思是说,的目标是像素级预测任务,对低级特征敏感。 长跳跃连接为低级特征提供了捷径,因此有助于噪声预测网络的训练 Intuitively,the objective in Eq. (1) is a pixel-level prediction task and is sensitive to low-level features. The long skip connections provide shortcuts for the low-level features and therefore ease the training of the noise prediction network
U-ViT在输出之前可选地添加一个3×3的卷积块。 这旨在防止transformer生成的图像中出现潜在的伪影(potential artifacts in images)
只有当模型学习(或记忆)了世界知识(例如时空关系和物理定律)时,它才能生成与真实世界相对应的视频(Only when a model has learned (or memorized) worldly knowledge (e.g., spatiotemporal relationships and physical laws) can it generate videos corresponding to the real word )。因此,模型容量是视频扩散的关键组成部分
Transformer已被证明具有高度可扩展性,使它们比3D U-Net更适合应对视频生成的挑战。例如,最大的U-Net架构的SD-XL有26亿个参数,而PaLM等Transformer架构的模型则拥有5400亿个参数
该模型在训练过程中采用序列封装的方式处理任意分辨率和纵横比的输入(uses sequence packing during training to process inputs of arbitrary resolutions and aspect ratios)
除了具备灵活性的模型应用外,还展示了通过大规模监督和contrastive image-text pretraining来提高训练效率(we demonstrate improved training efficiency for large-scale supervised and contrastive image-text pretraining)
具体而言
视觉Transformer(ViT)输入图像会被调整为固定的平方纵横比,并分割成固定数量的patch(input images are resized to a fixed square aspect ratio and then split into a fixed number of patches )
这是通过在每个训练步骤中对patch大小进行随机采样和调整算法来实现的,以支持多种初始卷积嵌入尺寸(This is achieved via random sampling of a patch size at each training step and a resizing algorithm to allow the initial convolutional embedding to support multiple patch sizes)
Pix2Struct引入了一种替代方法来保留纵横比(introduced an alternative patching approach which preserves the aspect ratio),在图表和文档理解等任务中尤其有用
Google从而提出了一种替代方法NaViT,将来自不同图像的多个patches打包成一个单一序列------称为Patch n' Pack------从而实现可变分辨率并保持长宽比 (Multiple patches from different images are packed in a single sequence--- termed Patch n' Pack---which enables variable resolution while preserving the aspect ratio)
2.5.2 因果3D卷积层(temporally causal 3D convolution)替换掉3D CNN的价值
值得一提的是,如下图所示
C-ViViT采用了完全空间transformer块与因果时间transformer块的组合(如上图2a所示),不过,其有两个缺点
首先,与CNN不同,位置嵌入使得在训练期间未见过的spatial resolutions难以进行分词。 其次,经验发现,3D CNN比spatial transformer表现更好,并且产生具有更好空间因果关系corresponding patch C-ViViT employs full spatial transformer blocks combined with causal temporal transformer blocks .
First, unlike CNNs, the positional embeddings makes it difficult to tokenize spatial resolutions that were not seen during training.
Second, empirically we found that 3D CNNs perform better than spatial transformer and produce tokens with better spatial causality of the corresponding patch
而时空层spatiotemporal layers通过身份注意力掩码在视频中建模时间动态并在图像中传递(spatial layers capture spatial relations in both images and video, while spatiotemporal layers model temporal dynamics in videos and pass through images via identity attention mask )
且通过空间交叉注意力加入text condition
这种条件化通过沿着「噪声潜在输入」的通道维度的拼接,被整合到模型中 This conditioning is integrated into the model through concatenation along the channel dimension of the noisy latent input
在推断过程中,我们使用标准的无分类器引导,以作为条件信号 During inference, we use standard classifier-free guidance with cfp as the conditioning signal
与通常使用外部交叉注意力网络或潜在混合进行风格化基于扩散的方法相比(In contrast to the diffusion-based approaches that usually use external cross-attention networks or latent blending for styliza-tion),Google的这个方法更加类似于利用LLM进行机器翻译,因为只需将结构和文本作为语言模型的前缀
深红色代表模态不可知部分
蓝色代表文本相关组件,即text tokens (embeddings): the pre-extracted T5 embed-dings for any text.
黄色代表视觉相关组件,即visual tokens: the MAGVIT-v2 tokens representing the images, video subsection, or COMMIT encoded video-to-video task.
绿色代表音频相关组件,即audio tokens: the SoundStream tokens representing au-dio
MAGVIT-v2对8 fps采样率下17帧、2.125秒、128×128分辨率的视频进行分词,产生(5, 16, 16)形状,并扁平化为1280个标记,词汇表大小为2-18 MAGVIT-v2tokenizes17-frame2.125-second128×128 resolution videos sampled at 8 fps to pro-duce a latent shape of (5, 16, 16), which is then flattenedinto 1280 tokens, with a vocabulary size of 2-18
sora的第一个reference为这篇论文《Unsupervised learning of video representations using lstms》,该论文考虑了目标序列的不同选择
一种选择是预测与输入相同的序列,动机类似于自动编码器(比如VAE)------我们希望捕获所有重现输入所需的信息,同时克服模型施加的归纳偏差 The motivation is similar to that of auto encoders --we wish to capture all that is needed to reproducethe input but at the same time go through the inductive bi-ases imposed by the model
2.27,随着对sora的不断深挖,再次新增一节的内容,即2.3 VDT:基于扩散的视频生成中首次使用Transformer(但没带文本条件融合)
且加上sora官方博客中的这句话:Similar to DALL·E 3, we also leverage GPT to turn short user prompts into longer detailed captions that are sent to the video model
是为1.4版