Abstract
Designed to learn long-range interactions on sequential data, transformers continue to show state-of-the-art results on a wide variety of tasks. In contrast to CNNs, they contain no inductive bias that prioritizes local interactions. This makes them expressive, but also computationally infeasible for long sequences, such as high-resolution images. We demonstrate how combining the effectiveness of the inductive bias of CNNs with the expressivity of transformers enables them to model and thereby synthesize high-resolution images. We show how to (i) use CNNs to learn a context-rich vocabulary of image constituents, and in turn (ii) utilize transformers to efficiently model their composition within high-resolution images. Our approach is readily applied to conditional synthesis tasks, where both non-spatial information, such as object classes, and spatial information, such as segmentations, can control the generated image. In particular, we present the first results on semantically-guided synthesis of megapixel images with transformers and obtain the state of the art among autoregressive models on class-conditional ImageNet. Code and pretrained models can be found at \url{https://git.io/JnyvK}.
设计用于学习序列数据的长期交互作用,变压器在各种各样的任务中继续显示最先进的结果。与cnn相比,它们不包含优先考虑本地交互的归纳偏见。这使得它们具有表现力,但对于长序列,如高分辨率图像,计算也不可行。我们演示了如何将cnn的诱导偏差的有效性与变压器的表达能力相结合,使它们能够建模,从而合成高分辨率图像。我们将展示如何(i)使用cnn来学习图像成分的上下文丰富词汇表,然后(ii)利用变压器在高分辨率图像中有效地对其组成进行建模。我们的方法很容易应用于条件合成任务,其中非空间信息(如对象类)和空间信息(如分割)都可以控制生成的图像。特别地,我们提出了使用变压器对百万像素图像进行语义引导合成的第一个结果,并在类条件的ImageNet上获得了自回归模型的最新技术。代码和经过训练的模型可以在\url{https://git.io/JnyvK}中找到。
Introduction
Transformers are on the rise—-they are now the de-facto standard architecture for language tasks \cite{vaswani2017attention,Radford2018ImprovingLU,Radford2019LanguageMA,brown2020language} and are increasingly adapted in other areas such as audio \cite{child2019generating} and vision \cite{chen2020generative, anonymous2021an}. In contrast to the predominant vision architecture, convolutional neural networks (CNNs), the transformer architecture contains no built-in inductive prior on the locality of interactions and is therefore free to learn complex relationships among its inputs. However, this generality also implies that it \emph{has to} learn all relationships, whereas CNNs have been designed to exploit prior knowledge about strong local correlations within images. Thus, the increased expressivity of transformers comes with quadratically increasing computational costs, because all pairwise interactions are taken into account. The resulting energy and time requirements of state-of-the-art transformer models thus pose fundamental problems for scaling them to high-resolution images with millions of pixels.
变压器在上升——他们现在的实际标准架构语言任务\引用{vaswani2017attention、Radford2018ImprovingLU Radford2019LanguageMA, brown2020language}和越来越适应在其他领域如音频\引用{child2019generating}和视觉\引用{chen2020generative, anonymous2021an}。与占主导地位的视觉结构——卷积神经网络(cnn)相比,变压器的结构在交互位置之前不包含内置的归纳,因此可以自由地学习输入之间的复杂关系。然而,这种普遍性也意味着它必须学习所有的关系,而cnn被设计成利用图像中强局部相关性的先验知识。因此,由于考虑了所有成对的相互作用,变压器表达能力的提高伴随着计算成本的二次增加。因此,最先进的变压器模型所产生的能量和时间需求,为将其缩放到数百万像素(1024*1024=1048576)的高分辨率图像带来了根本性的问题。
Observations that transformers tend to learn convolutional structures \cite{anonymous2021an} thus beg the question: Do we have to re-learn everything we know about the local structure and regularity of images from scratch each time we train a vision model, or can we efficiently encode inductive image biases while still retaining the flexibility of transformers? We hypothesize that low-level image structure is well described by a local connectivity, i.e.\ a convolutional architecture, whereas this structural assumption ceases to be effective on higher semantic levels. Moreover, CNNs not only exhibit a strong locality bias, but also a bias towards spatial invariance through the use of shared weights across all positions. This makes them ineffective if a more holistic understanding of the input is required.
变形金刚倾向于学习卷积结构的观察\引用{anonymous2021an}因此提出了这样一个问题:在每次训练视觉模型时,我们是否必须从头开始重新学习我们所知道的关于图像的局部结构和规则性的一切,或者我们能否有效地编码归纳图像偏差,同时仍然保留变形器的灵活性?我们假设低层次的图像结构可以通过局部连接来描述,即卷积结构,而这种结构假设在更高的语义层次上不再有效。此外,cnn不仅表现出强烈的局部性偏差,而且通过在所有位置使用共享权值,还表现出对空间不变性的偏差。如果需要对输入有更全面的理解,这将使它们无效。
关键观点
Transformer有能力模拟卷积操作
低层次的图像结构适合局部连接来描述,但高层次不适合
CNN的局部性和空间不变性(共享权重),不利于全面理解input
Our key insight to obtain an effective and expressive model is that, \emph{taken together, convolutional and transformer architectures can model % % the compositional nature of our visual world \cite{ommer2007learning}}: We use a convolutional approach to efficiently learn a codebook of context-rich visual parts and, subsequently, learn a model of their global compositions. The long-range interactions within these compositions require an expressive transformer architecture to model distributions over their consituent visual parts. Furthermore, we utilize an adversarial approach to ensure that the dictionary of local parts captures perceptually important local structure to alleviate the need for modeling low-level statistics with the transformer architecture. Allowing transformers to concentrate on their unique strength—-modeling long-range relations—-enables them to generate high-resolution images as in Fig.~\ref{fig:frontpage}, a % feat which previously has been out of reach. Our formulation% directly gives control over the generated images by means of conditioning information regarding desired object classes or spatial layouts. Finally, experiments demonstrate that our approach retains the advantages of transformers by outperforming previous codebook-based state-of-the-art approaches based on convolutional architectures.
为了获得一个有效且富有表现力的模型,我们的关键观点是,卷积和变压器架构结合在一起(这是阶段层面上的组合而不是架构上的组合),可以为我们的视觉世界的组成特性建模% %,引用{ommer2007learning}}:我们使用卷积方法来有效地学习上下文丰富的视觉部分的代码本,然后学习它们的全局组成的模型。这些组合中的长期交互需要一个具有表达能力的转换器体系结构来对其组成的可视化部分进行分布建模。此外,我们使用一种对抗的方法来确保局部部件的字典捕获感知上重要的局部结构,以减轻使用变压器体系结构建模低级统计的需要。允许变压器专注于其独特的强度——建模远程关系——使它们能够生成如图所示的高分辨率图像,这是以前无法实现的壮举。我们的公式直接通过有关所需对象类或空间布局的条件信息来控制生成的图像。。最后,实验证明,我们的方法保留了变压器的优势,优于以往基于编码本的最先进的基于卷积体系结构的方法。
\section{Related Work}
\paragraph{The Transformer Family}
The defining characteristic of the transformer architecture \cite{vaswani2017attention} is that it models interactions between its inputs solely through attention \cite{bahdanau2016neural,kim2017structured,parikh2016decomposable} which enables them to faithfully handle interactions between inputs regardless of their relative position to one another. Originally applied to language tasks, inputs to the transformer were given by tokens, but other signals, such as those obtained from audio \cite{li2019neural} or images \cite{chen2020generative}, can be used. Each layer of the transformer then consists of an attention mechanism, which allows for interaction between inputs at different positions, followed by a position-wise fully connected network, which is applied to all positions independently. More specifically, the (self-)attention mechanism can be described by mapping an intermediate representation with three position-wise linear layers into three representations, query $Q \in \RR^{N\times d_k}$, key $K \in \RR^{N\times d_k}$ and value $V \in \RR^{N\times d_v}$, to compute the output as \newcommand{\Attn}{\operatorname{Attn}} \newcommand{\softmax}{\operatorname{softmax}} \begin{equation} \Attn(Q,K,V) = \softmax\Big(\frac{QK^t}{\sqrt{d_k}}\Big)V \in \RR^{N\times d_v}. \end{equation}
变压器结构的标志性特点\引用{vaswani2017attention}是模型之间的交互输入仅仅通过关注\引用{bahdanau2016neural、kim2017structured parikh2016decomposable}这使他们忠实地处理输入不管他们的相对位置之间的相互作用另一个地方。最初应用于语言任务,变压器的输入是通过词条给出的,但其他信号,如从音频{li2019neural}或图像{chen2020generative}获得的信号,也可以被使用。然后,变压器的每一层都由一个注意机制组成,该机制允许不同位置的输入之间进行交互,然后是一个position-wise fully connected network,该网络独立应用于所有位置。
此处仅考虑SA,没有涉及整个Transformer层
三个线性层作为memory bank
维度变化分析 https://blog.csdn.net/hyzhyzhyz12345/article/details/104119375
When performing autoregressive maximum-likelihood learning, non-causal entries of $QK^t$, i.e. all entries below its diagonal, are set to $-\infty$ and the final output of the transformer is given after a linear, point-wise transformation to predict logits of the next sequence element. Since the attention mechanism relies on the computation of inner products between all pairs of elements in the sequence, its computational complexity increases quadratically with the sequence length. While the ability to consider interactions between \emph{all} elements is the reason transformers efficiently learn long-range interactions, it is also the reason transformers quickly become infeasible, especially on images, where the sequence length itself scales quadratically with the resolution. % Different approaches have been proposed to reduce the computational requirements to make transformers feasible for longer sequences. \cite{parmar2018image} and \cite{weissenborn2020scaling} restrict the receptive fields of the attention modules, which reduces the expressivity and, especially for high-resolution images, introduces assumptions on the independence of pixels. \cite{child2019generating} and \cite{ho2019axial} retain the full receptive field but can reduce costs for a sequence of length $\seqlength$ only from $n^2$ to $n \sqrt{n}$, which makes resolutions beyond $64$ pixels still prohibitively expensive.
在进行自回归极大似然学习时,将$QK^t$的非因果项,即其对角线以下的所有项,设置为$-\infty$,并通过逐点线性变换给出变压器的最终输出,以预测下一个序列元素的logits。由于注意机制依赖于序列中所有元素对之间的内积的计算,其计算复杂度随序列长度的增加呈二次型增加。虽然考虑\强调{所有}元素之间的交互作用的能力是变压器有效地学习长程交互作用的原因,但这也是变压器迅速变得不可用的原因,特别是在图像上,其中序列长度本身与分辨率成二次比例。已经提出了不同的方法来减少计算要求,使变压器适用于较长的序列。\cite{parmar2018image}和\cite{weissenborn2020scaling}限制了注意模块的接受域,这降低了表达能力,特别是对于高分辨率的图像,引入了对像素独立性的假设。\cite{child2019generating}和\cite{ho2019axial}保留了完整的接收域,但可以降低长度$ seqlength$的成本,仅从$n^2$到$n sqrt{n}$,这使得超过$64$像素的分辨率仍然非常昂贵。
\paragraph{Convolutional Approaches} The two-dimensional structure of images suggests that local interactions are particularly important.
CNNs exploit this structure by restricting interactions between input variables to a local neighborhood defined by the kernel size of the convolutional kernel. Applying a kernel thus results in costs that scale linearly with the overall sequence length (the number of pixels in the case of images) and quadratically in the kernel size, which, in modern CNN architectures, is often fixed to a small constant such as $3\times3$. This inductive bias towards local interactions thus leads to efficient computations, but the wide range of specialized layers which are introduced into CNNs to handle different synthesis tasks \cite{park2019semantic,zhang2020cross,Siarohin_2019_NeurIPS,zhu2019sean,zhou2017view} suggest that this bias is often too restrictive.
图像的二维结构表明,局部相互作用尤为重要。
cnn利用这个结构,将输入变量之间的交互限制为一个由卷积核的核大小定义的局部邻域。应用一个内核的结果是成本与整个序列长度(图像中的像素数)线性缩放,并在内核大小中平方,在现代CNN架构中,内核大小通常固定为一个小常数,例如$3\乘以3$。这种对局部交互的归纳偏向因此导致了高效的计算,但广泛的特殊化层被引入到cnn中来处理不同的合成任务\引用{park2019semantic,zhang2020cross,Siarohin_2019_NeurIPS,zhu2019sean,周2017view}表明这种偏向通常过于限制性。
Convolutional architectures have been used for autoregressive modeling of images \cite{oord2016pixel,oord2016conditional,chen2018pixelsnail} but, for low-resolution images, previous works \cite{parmar2018image,child2019generating,ho2019axial} demonstrated that transformers consistently outperform their convolutional counterparts. Our approach allows us to efficiently model high-resolution images with transformers while retaining their advantages over state-of-the-art convolutional approaches.
卷积结构已经被用于图像的自回归建模,但是,对于低分辨率的图像,之前的作品,引用{parmar2018image,child2019generating,ho2019axial}证明,变压器始终优于其卷积同行。我们的方法使我们能够高效地用变压器建模高分辨率图像,同时保留了它们相对于最先进的卷积方法的优势。
\paragraph{Two-Stage Approaches} Closest to ours are two-stage approaches which first learn an encoding of data and afterwards learn, in a second stage, a probabilistic model of this encoding. \cite{dai2019diagnosing} demonstrated both theoretical and empirical evidence on the advantages of first learning a data representation with a Variational Autoencoder (VAE) \cite{vae,vae2}, and then again learning its distribution with a VAE. \cite{esser2020invertible,xiao2019generative} demonstrate similar gains when using an unconditional normalizing flow for the second stage, and \cite{rombach2020making,rombach2020network} when using a conditional normalizing flow. To improve training efficiency of Generative Adversarial Networks (GANs), \cite{liu2019acceleration} learns a GAN \cite{gan} on representations of an autoencoder and \cite{han2020notsobiggan} on low-resolution wavelet coefficients which are then decoded to images with a learned generator.
与我们的方法最接近的是两阶段方法,它首先学习数据编码,然后在第二阶段学习这种编码的概率模型。首先用变分自编码器(VAE,vae2)学习数据表示,然后再用VAE学习数据的分布,这两种方法都展示了理论和经验证据的优点。在第二阶段使用无条件归一化流时,\引用{esser2020invertible,xiao2019generative}演示了类似的收益,而在使用条件归一化流时,\引用{rombach2020making,rombach2020network}。为了提高生成式对抗网络(Generative Adversarial Networks, GAN)的训练效率,{liu2019acceleration}在自动编码器表示上学习生成式对抗网络(GAN),在低分辨率小波系数上学习生成式对抗网络(han2020notsobiggan),然后利用学习后的生成器将小波系数解码为图像。
\cite{oord2018neural} presents the Vector Quantised Variational Autoencoder (VQVAE), an approach to learn discrete representations of images, and models their distribution autoregressively with a convolutional architecture. \cite{razavi2019generating} extends this approach to use a hierarchy of learned representations. However, these methods still rely on convolutional density estimation, which makes it difficult to capture long-range interactions in high-resolution images. % \cite{chen2020generative} models images autoregressively with transformers in order to evaluate the suitability of generative pretraining to learn image representations for downstream tasks. Since input resolutions of $32\times 32$ pixels are still quite computationally expensive \cite{chen2020generative}, a VQVAE is used to encode images up to a resolution of $192\times 192$. In an effort to keep the learned discrete representation as spatially invariant as possible with respect to the pixels, a shallow VQVAE with small receptive field is employed. In contrast, we demonstrate that a powerful first stage, which captures as much context as possible in the learned representation, is critical to enable efficient high-resolution image synthesis with transformers.
提出了矢量量化变分自编码器(VQVAE),这是一种学习图像离散表示的方法,并使用卷积结构自回归地对其分布建模。\cite{razavi2019generating}扩展了这种方法,使用了学习过的表示的层次结构。然而,这些方法仍然依赖于卷积密度估计,这使得在高分辨率图像中捕获远程交互变得困难。% \ \引用{chen2020generative}使用变压器自回归地对图像进行建模,以评估生成式预训练对于学习下游任务的图像表示的适用性iGPT用生成任务学习图像表示,if i can not understand it,i can not create it-dalle。由于$32 32$像素的输入分辨率仍然是相当昂贵的计算引用{chen2020generative}, VQVAE被用来将图像编码到$192 192$的分辨率。为了使学习的离散表示尽可能保持相对于像素的空间不变,我们使用了一个具有小接收域的浅VQVAE。相比之下,我们证明了一个强大的第一阶段,即在学习后的表示中捕获尽可能多的上下文,对于使用变压器实现高效的高分辨率图像合成至关重要。

Our approach uses a convolutional \emph{VQGAN} to learn a codebook of context-rich visual parts, whose composition is subsequently modeled with an autoregressive transformer architecture. A discrete codebook provides the interface between these architectures and a patch-based discriminator enables strong compression while retaining high perceptual quality. This method introduces the efficiency of convolutional approaches to transformer based high resolution image synthesis.
我们的方法使用卷积学习上下文丰富的可视部分的代码本,其组成随后用自回归变压器架构建模。离散码本提供了这些体系结构和基于patch的鉴别器之间的接口,能够在保持高感知质量的同时实现强压缩。该方法介绍了卷积方法在变压器高分辨率图像合成中的有效性。
学习此图画法做一个方法图
\section{Approach}
Our goal is to exploit the highly promising learning capabilities of transformer models \cite{vaswani2017attention} and introduce them to high-resolution image synthesis up to the megapixel range. % Previous work \cite{parmar2018image, chen2020generative} which applied transformers to image generation demonstrated promising results for images up to a size of $64 \times 64$ pixels but, due to the quadratically increasing cost in sequence length, cannot simply be scaled to higher resolutions.
我们的目标是开发变压器模型非常有前途的学习能力,并将其引入到高达百万像素范围的高分辨率图像合成中。%以前的工作\引用{parmar2018image, chen2020generative},将变压器应用于图像生成中,证明了高达$64 \乘以64$像素大小的图像有希望的结果,但是,由于序列长度成本的二次增长,不能简单地缩放到更高的分辨率。
High-resolution image synthesis requires a model that understands the global composition of images, enabling it to generate locally realistic as well as globally consistent patterns. % % Therefore, instead of representing an image with pixels, we represent it as a composition of perceptually rich image constituents from a codebook. By learning an effective code, as described in Sec.~\ref{sec:vqgan}, we can significantly reduce the description length of compositions, which allows us to efficiently model their global interrelations within images with a transformer architecture as described in Sec.~\ref{sec:trafo}. This approach, summarized in Fig.~\ref{fig:model}, is able to generate realistic and consistent high resolution images both in an unconditional and a conditional setting.
高分辨率图像合成需要一个能够理解图像整体组成的模型,使其能够生成局部逼真的以及全局一致的模式。因此,我们不再用像素来表示图像,而是将其表示为来自码本的感知丰富的图像成分的组合。通过学习有效的代码,如Sec.~\ref{Sec:vqgan}中所描述的,我们可以显著减少组合的描述长度,这允许我们有效地用Sec.~\ref{Sec:trafo}中所描述的变压器架构对图像中的全局相互关系建模。这种方法,总结在图~\ref{Fig:model},能够在无条件和条件设置下生成真实和一致的高分辨率图像。
生成质量更好(插值)
速度更快
更新已生成部分
利用deepEMD支持分布拟合
\subsection{Learning an Effective Codebook of Image Constituents for Use in Transformers} \label{sec:vqgan} \noindent To utilize the highly expressive transformer architecture for image synthesis, we need to express the constituents of an image in the form of a \emph{sequence}. % Instead of building on individual pixels, complexity necessitates an approach that uses a discrete codebook of learned representations, such that any image $x \in \RR^{H \times W \times 3}$ can be represented by a spatial collection of codebook entries $\quantizedcode \in \RR^{h \times w \times \codebookdim}$, where $\codebookdim$ is the dimensionality of codes. An equivalent representation is a sequence of $h\cdot w$ indices which specify the respective entries in the learned codebook. To effectively learn such a discrete spatial codebook, we propose to directly incorporate the inductive biases of CNNs and incorporate ideas from neural discrete representation learning \cite{oord2018neural}. First, we learn a convolutional model consisting of an encoder $\encoder$ and a decoder $\decoder$, such that taken together, they learn to represent images with codes from a learned, discrete codebook $\codebook = {zk}{k=1}^K \subset \RR^\codebookdim$ (see Fig.~\ref{fig:model} for an overview). More precisely, we approximate a given image $x$ by $\hat{x}=G(\quantizedcode)$. We obtain $\quantizedcode$ using the encoding $ \hat{z} = \encoder(x) \in \RR^{h\times w \times \codebookdim}$ and a subsequent element-wise quantization $\quantize(\cdot)$ of each spatial code $\hat{z}{ij} \in \RR^\codebookdim$ onto its closest codebook entry $z_k$: % \begin{equation} \quantizedcode = \quantize(\hat{z}) \coloneqq \left(\argmin{zk \in \codebook} \Vert \hat{z}{ij} - zk \Vert\right) \in \RR^{h\times w \times \codebookdim}. \end{equation} The reconstruction $\hat{x} \approx x$ is then given by \begin{equation} % \hat{x} = \decoder(\quantizedcode ) = \decoder\left( \quantize(\encoder(x)) \right). \label{eq:vqrec} \end{equation} Backpropagation through the non-differentiable quantization operation in Eq.~\eqref{eq:vqrec} is achieved by a straight-through gradient estimator, which simply copies the gradients from the decoder to the encoder \cite{bengio2013estimating}, such that the model and codebook can be trained end-to-end via the loss function \begin{align} % % \mathcal{L}{\text{VQ}}(\encoder, \decoder, \codebook) = \Vert x - \hat{x} \Vert^2 &+ \Vert \text{sg}[\encoder(x)] - \quantizedcode \Vert2^2 \nonumber \ &+ \Vert \text{sg}[\quantizedcode] - \encoder(x) \Vert_2^2. \label{eq:origvqloss} \end{align} % % Here, $\mathcal{L}{\text{rec}}=\Vert x - \hat{x} \Vert^2$ is a reconstruction loss, $\text{sg}[\cdot]$ denotes the stop-gradient operation, and $\Vert \text{sg}[\quantizedcode] - \encoder(x) \Vert_2^2$ is the so-called ``commitment loss’’ \cite{oord2018neural}.
为了利用具有高度表达力的变压器结构进行图像合成,我们需要以序列的形式表示图像的成分。而不是建立在单个像素,复杂性需要一种方法,它使用一个离散的表示,这样

学习这样的思路与写法,一个图片就是一个多维实数
为了有效地学习这样的离散空间码本,我们建议直接结合cnn的归纳偏差,并结合神经离散表示学习的思想(引用{oord2018neural})。首先,我们学习了一个卷积模型,它由一个编码器$ encoder$和一个解码器$\decoder$组成,这样放在一起,



此段主要回顾VQVAE,说明如何同时优化编码结果和词表条目、以及如何处理没有梯度的问题
\paragraph{Learning a Perceptually Rich Codebook}
Using transformers to represent images as a distribution over latent image constituents requires us to push the limits of compression and learn a rich codebook. To do so, we propose \emph{VQGAN}, a variant of the original VQVAE, and use a discriminator and perceptual loss \cite{larsen2015autoencoding,johnson2016perceptual,lamb2016discriminative,dosovitskiy2016generating,mentzer2020highfidelity} to keep good perceptual quality at increased compression rate. Note that this is in contrast to previous works which applied pixel-based \cite{oord2016conditional,razavi2019generating} and transformer-based autoregressive models \cite{chen2020generative} on top of only a shallow quantization model. More specifically, we replace the $L2$ loss used in \cite{oord2018neural} for $\mathcal{L}{\text{rec}}$ by a perceptual loss and introduce an adversarial training procedure with a patch-based discriminator $\discriminator$ \cite{isola2017image} that aims to differentiate between real and reconstructed images: \begin{align} % \mathcal{L}{\text{GAN}}({\encoder, \decoder, \codebook }, \discriminator) = \left[ \log \discriminator(x) + \log (1 - \discriminator(\hat{x}))\right] \label{eq:ourvqloss} \end{align} The complete objective for finding the optimal compression model $\mathcal{Q}^ = {\encoder^, \decoder^, \codebook^ }$ then reads \begin{align} % \mathcal{Q}^* = \argmin{\encoder, \decoder, \codebook} \max{\discriminator} \mathbb{E}{x\sim p(x)} \Big[ &\mathcal{L}{\text{VQ}}(\encoder, \decoder, \codebook) \nonumber \ + \lambda &\mathcal{L}{\text{GAN}}({\encoder, \decoder, \codebook }, \discriminator) \Big], \end{align} where we compute the adaptive weight $\lambda$ according to \begin{equation} \lambda = \frac{\nabla{\decoder_L}[ \mathcal{L}{\text{rec}} ]}{\nabla{\decoder_L} [\mathcal{L}{\text{GAN}}] + \delta} \end{equation} where $\mathcal{L}{\text{rec}}$ is the perceptual reconstruction loss \cite{zhang2018perceptual}, $\nabla{\decoder_L}[\cdot]$ denotes the gradient of its input w.r.t.\ the last layer $L$ of the decoder, and $\delta = 10^{-6}$ is used for numerical stability. To aggregate context from everywhere, we apply a single attention layer on the lowest resolution. This training procedure significantly reduces the sequence length when unrolling the latent code and thereby enables the application of powerful transformer models.
使用Transformer将图像表示为潜在图像成分的分布,要求我们突破压缩的极限,学习丰富的代码本。为此,我们提出了\emph{VQGAN},一种原始VQVAE的变体,并使用一个鉴别器和感知损失引用{larsen2015autoencoding,johnson2016perceptual, lamb2016discriminant,dosovitskiy2016generating,mentzer2020highfidelity}在增加的压缩率下保持良好的感知质量。请注意,这与之前的工作形成了对比,之前的工作只在一个浅层量化模型上应用基于像素的引用{oord2016conditional,razavi2019generating}和基于变压器的自回归模型引用{chen2020generative}。

此处涉及GAN的核心思想,后期仔细看
E 编码器 G解码器/生成器 Z码本 D 判别器 x 原图(patch) \hat{x} 重构图


为了聚合来自各处的上下文,我们在最低分辨率上应用单一的注意层。这个训练过程在展开潜码时显著地减少了序列长度,从而使强大的变压器模型的应用成为可能。
\subsection{Learning the Composition of Images with Transformers} \label{sec:trafo} % \paragraph{Latent Transformers} With $\encoder$ and $\decoder$ available, we can now represent images in terms of the codebook-indices of their encodings. More precisely, the quantized encoding of an image $x$ is given by $\quantizedcode = \quantize(\encoder(x)) \in \RR^{h \times w \times \codebookdim}$ and is equivalent to % % a sequence $s \in {0, \dots, \vert \codebook \vert - 1 }^{h \times w}$ of indices from the codebook, which is obtained by replacing each code by its index in the codebook $\codebook$: \begin{equation} % % s{ij} = k \text{ such that } \left( \quantizedcode \right){ij} = zk. \label{eq:indexmap} \end{equation} By mapping indices of a sequence $s$ back to their corresponding codebook entries, $\quantizedcode = \left( z{s{ij}} \right)$ is readily recovered and decoded to an image $\hat{x} = \decoder(\quantizedcode)$. % % Thus, after choosing some ordering of the indices in $s$, image-generation can be formulated as autoregressive next-index prediction: Given indices $s{

此处的ordering 应该指向按行或者按列
\paragraph{Conditioned Synthesis} % In many image synthesis tasks a user demands control over the generation process by providing additional information from which an example shall be synthesized. This information, which we will call $c$, could be a single label describing the overall image class or even another image itself. The task is then to learn the likelihood of the sequence given this information $c$: \begin{equation} \label{eq:conddensity} p(s|c) = \prodi p(s_i|s{在许多图像合成任务中,用户需要通过提供额外的信息来控制生成过程。我们将此信息称为$c$,它可以是描述整个图像类或甚至是另一个图像本身的单个标签。接下来的任务是在给定的信息$c$下学习序列的可能性:
核心点就是prepend
\paragraph{Generating High-Resolution Images}

需要找到此处机制在代码中的实现
似乎没有实现
https://github.com/CompVis/taming-transformers#scene-image-synthesis
https://github.com/CompVis/taming-transformers/blob/09298dc8af53cd13801306cb2ffe50d6dc604105/scripts/make_scene_samples.py
此处仅根据bounding box 生成
The attention mechanism of the transformer puts limits on the sequence length $h \cdot w$ of its inputs $s$. While we can adapt the number of downsampling blocks $m$ of our \emph{VQGAN} to reduce images of size $H\times W$ to $h=H/2^m \times w=W/2^m$, we observe degradation of the reconstruction quality beyond a critical value of $m$, which depends on the considered dataset. To generate images in the megapixel regime, we therefore have to work patch-wise and crop images to restrict the length of $s$ to a maximally feasible size during training. To sample images, we then use the transformer in a sliding-window manner as illustrated in Fig.~\ref{fig:attentionslide}. Our \emph{VQGAN} ensures that the available context is still sufficient to faithfully model images, as long as either the statistics of the dataset are approximately spatially invariant or spatial conditioning information is available. In practice, this is not a restrictive requirement, because when it is violated, \ie unconditional image synthesis on aligned data, we can simply condition on image coordinates, similar to \cite{lin2019cocogan}.
变压器的注意机制限制了其输入的序列长度$h \cdot w$。虽然我们可以调整我们的emph{VQGAN}的下采样块的数量$m$来减少图像的大小$H\ W$到$H =H/2^m \ × W =W/2^m$,我们观察到重建质量的下降超过了一个临界值$m$,这取决于所考虑的数据集。为了生成百万像素级的图像,我们必须在训练过程中对图像进行拼接和裁剪,以将$s$的长度限制为最大可行的大小。
为了对图像进行采样,我们以滑动窗口的方式使用变压器,如图~\ref{Fig:attentionslide}所示。我们的\emph{VQGAN}确保可用的上下文仍然足以忠实地对图像建模,只要数据集的统计信息是近似空间不变的或空间条件信息是可用的。在实践中,这不是一个限制性的要求,因为当它被违反时,例如在对齐数据上的无条件图像合成,我们可以简单地以图像坐标为条件,类似于引用{lin2019cocogan}。
