{'fraction_non_alphanumeric': 0.03438215246862346, 'fraction_numerical': 0.01826001660001509, 'mean_word_length': 5.0850933578206305, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Dual Contrastive Loss and Attention for GANs

Generative Adversarial Networks (GANs) produce impressive results on unconditional image generation when powered with large-scale image datasets. Yet generated images are still easy to spot especially on datasets with high variance (e.g. bedroom, church). In this paper, we propose various improvements to further push the boundaries in image generation. Specifically, we propose a novel dual contrastive loss and show that, with this loss, discriminator learns more generalized and distinguishable representations to incentivize generation. In addition, we revisit attention and extensively experiment with different attention blocks in the generator. We find attention to be still an important module for successful image generation even though it was not used in the recent state-of-the-art models. Lastly, we study different attention architectures in the discriminator, and propose a reference attention mechanism. By combining the strengths of these remedies, we improve the compelling state-of-the-art Fr\'{e}chet Inception Distance (FID) by at least 17.5% on several benchmark datasets. We obtain even more significant improvements on compositional synthetic scenes (up to 47.5% in FID). Code and models are available at https://github.com/ningyu1991/AttentionDualContrastGAN .

We investigate methods to improve GANs in two dimensions. In the first dimension, we work on the loss function.
As the discriminator aims to model the intractable real data distribution via a workaround of real/fake binary classification, a more effective discriminator can back-propagate more meaningful signals for the generator to compete against. However, the feature representations of discriminators are often not generalized enough to incentivize the adversarially evolving generator and are prone to forgetting previous tasks [11] or previous data modes [72,49]. This often leads to the generated samples with discontinued semantic structures [51,98] or the generated distribution with mode collapse [72,96]. To mitigate this issue, we propose to synergize generative modeling with the advancements in contrastive learning [61,8]. In this direction, for the first time, we replace the logistic loss of StyleGAN2 with a newly designed dual contrastive loss.
In the second dimension, we revisit the architecture of both generator and discriminator networks. Specifically, many GAN-based image generators rely on convolutional layers to encode features. In such design, long-range dependencies across pixels (e.g., large-size semantically correlated layouts) can only be formulated with a deep stack of convolutional layers. This, however, does not favor the stability of GAN training because of the challenge to coordinate multiple layers desirably. The minimax formulation and the alternating gradient ascent-descent in the GAN framework further exacerbate such instability. To circumvent this issue, attention mechanisms that support long-range modeling across image regions are incorporated into GAN models [98,5]. After that, however, StyleGAN2 claimed the state of the art with a novel architectural design without any attention mechanisms. Therefore, it turns not clear whether attention still improves results, which of the popular attention mechanisms [37,85,83,103] improves the most, and in return of how many additional parameters. To answer these questions, we extensively study the role of attention in the current state-of-the-art generator, and during this study improve the results significantly.
In the discriminator, we again explore the role of attention as shown in Fig. 1. We design a novel reference attention mechanism in the discriminator where we allow two irrelevant images as the inputs at the same time: one input is sampled from real data as a reference, and the other input is switched between a real sample and a generated sample. The two inputs are encoded through two Siamese branches [6,15,73,97] and fused by a reference-attention module. In this way, we achieve to guide real/fake classification under the attention of the real world. Contributions are summarized as follow: • We propose a novel dual contrastive loss in adversarial training that generalizes representation to more effectively distinguish between real and fake, and further incentivize the image generation quality. • We investigate variants of the attention mechanism in GAN architecture to mitigate the local and stationary issues of convolutions. • We design a novel reference-attention discriminator architecture that substantially benefits limited-scale datasets. • We conduct extensive experiments on large-scale datasets and their smaller subsets. We show that our improvements on the loss function and on the generator hold in both scenarios. On the other hand, we find discriminator to behave differently based on the number of available images, and the reference-attention-based discriminator to be only improving on limited-scale datasets. • We redefine the state of the art by improving FID scores by at least 17.5% on several large-scale benchmark datasets. We also achieve more realistic generation on the CLEVR dataset [38] which poses different challenges from the other datasets: compositional scenes with occlusions, shadows, reflections, and mirror surfaces. It comes with 47.5% FID improvement.
Contrastive learning. Contrastive learning targets a transformation of inputs into an embedding where associated signals are brought together, and they are distanced from the other samples in the dataset [26,76,8,9]. The same intuition behind contrastive learning has also been the base of Siamese networks [6,15,73,97]. Contrastive learning is shown to be an effective tool for unsupervised learning [61,27,87], conditional image synthesis [63,40,106], and domain adaptation [23]. In this work, we study its effectiveness when it is closely coupled with the adversarial training framework and replaces the conventional adversarial loss for unconditional image generation. It is orthogonal to [40,106,36,47] where their contrastive losses serve only as an incremental auxiliary to the conventional adversarial loss, apply to the generator rather than the discriminator, and/or require expensive class annotations or augmentation for generation.
Attention models. Attention models have dominated the language modeling [78,86,17,19,89], and became popular among various computer vision problems from image recognition [16,79,31,32,104,109,30,85] to image captioning [88,90,7] to video prediction [37,83]. They are proposed in various forms: spatial attention that reweights the convolution activations [98,83,12], in different channels [79,31,32], or a combination of them [7,84,22]. Attention models with their reweighting mechanisms provide a possibility for long-range modeling across distant image regions. As attention models outperform others in various computer vision tasks, researchers were quick to incorporate them into unconditional image generation [10,98,65,5], semantic-based image generation [56,75], and text-guided image manipulation models [48,66]. Even though attention models have already benefited the image generation tasks, we believe the results can be further improved by empowering the state-of-the-art image synthesis models [43] (attention not involved) with the most recent achievements in the attention modules [103]. In addition, we design a novel reference-attention architecture for the discriminator and show a further boost on limited-scale datasets.

Approach
Our improvements for GANs include a novel dual contrastive loss and variants of the attention mechanisms. For each improvement, we organize the context in a combination between method formulation and experimental investigation. After validating our optimal configuration, we compare it to the state of the art in Section 4.

Dual contrastive loss
Adversarial training relies on the discriminator's ability on real vs. fake classification. As in other classification tasks, discriminators are also prone to overfitting when the dataset size is limited [2]. On larger datasets, on the other hand, there is no study showing that disciminators overfit but we hypothesize that adversarial training can still benefit from novel loss functions which encourage the distinguishability power of the discriminator representations for their real vs. fake classification task.
We put another lens on the representation power of the discriminator by incentivizing generation via contrastive learning. Contrastive learning associates data points and their positive examples and disassociates the other points within the dataset which are referred to as negative examples. It is recently re-popularized by various unsupervised learning works [26,61,76,8,9] and generation works [63,40,106]. Among these works, contrastive learning is used as an auxiliary task. For example in image to image translation task, a translator learns to output a zebra image given a horse image via adversarial loss and in addition learns to align the input horse image and the generated zebra image via contrastive loss function [63]. Contrastive loss in that work is utilized such that given a patch showing the legs of an output zebra should be strongly associated with the corresponding legs of the input horse, more so than the other patches randomly extracted from the horse image.
In this work, different from the previous ones, we do not use contrastive learning as an auxiliary task but directly couple it in the main adversarial training by a novel loss function formulation. We, to the best of our knowledge, for the first time train an unconditional GAN by solely relying on contrastive learning. As shown in Fig. 2 Right Case I, our contrastive loss function aims at teaching the discriminator to disassociate a single real image against a batch of generated images. Dually in Case II, the discriminator learns to disassociate a single generated image against a batch of real images. The generator adversarially learns to minimize such dual contrasts. Mathematically, we derive this loss function by extending the binary classification used in [24,43] to a noise contrastive estimation framework [61], a one-against-abatch classification in the softmax cross-entropy formulation. The novel formulation is as follows: In Case I: In Case II: Comparing between Eq. 1 and 2, the duality is formulated by switching the order of real/fake sampling while keeping the other calculation unchanged. Comparing to the logistic loss [24,43], contrastive loss enriches the softplus formulation log(1 + e D(·) ) with a batch of inner terms and using discriminator logit contrasts between real and fake samples. Finally, our adversarial objective is: Investigation on loss designs. We extensively validate the effectiveness of dual contrastive loss compared to other Figure 2. Comparisons between the diagram of conventional GAN loss and diagram of our dual contrastive loss. Our contrastive loss in Case I aims at teaching the discriminator to disassociate a single real image (R) against a batch of generated images (F). Dually in Case II, the discriminator learns to disassociate a single generated image against a batch of real images.  Table 2. Comparisons in FDDF between StyleGAN2 default loss and our loss. A larger value is more desirable, indicating the learned discriminator features are more distinguishable between real and fake. Figure 3. The tSNE plots for the distributions of discriminator features. The distinguishability of features based on our contrastive loss is much more significant than that based on the default nonsaturating loss in StyleGAN2 baseline. Our loss learns to associate fake features to a "core" clique (green) while pushing real features in the wild outwards as "satellites" (black). The baseline loss fails to differentiate features from the two sources (red v.s. blue) with a clear margin.
loss functions as presented in Table 1. We replace the loss used in StyleGAN2 [43], non-saturating default loss, with other popular GAN losses while keeping all the other parameters the same. As shown in Table 1, dual contrastive loss is the only loss that significantly improves upon the default loss of StyleGAN2 consistently on all the five datasets. Wasserstein loss is better than ours on CLEVR dataset, but is the worst among all the loss functions on the other datasets.
We reason the success of the dual loss to its formulation that explicitly learns an unbiased representation between real and generated distributions.
The distinguishability of contrastive representation. Motivated by the consistent improvement from our dual contrastive loss, we delve deeper to investigate if and by how much our contrastive representation is more distinguishable than the original discriminator representation. We measure the representation distinguishability by the Fréchet distance of the discriminator features in the last layer (FDDF) between 50K real and generated samples. A larger value indicates more distinguishable features between real and fake. We find our dual contrastive features to be consistently more distinguishable than the original discriminator features as shown in Table 2 and Fig. 3, which back-propagates more effective gradients to incentivize our generator.

Self-attention in the generator
The majority of the GAN-based image generators rely solely on convolutional layers to extract features [67,3,25,60,41,42,43], even though the local and stationary convolution primitive in the generator can not model the longrange dependencies in an image. Among recent GAN-based models, SAGAN [98] uses the self-attention block [83] and demonstrates improved results. BigGAN [5] also follows this choice and uses a similar attention module for better performance. After that, however, StyleGAN [42] and Style-GAN2 [43] redefine the state of the art with various modifications in the generator architecture which do not include any attention mechanisms. StyleGAN2 also shows that generation results can be improved by larger networks with an increased number of convolution filters. Therefore, it is now  Figure 4. The diagram of self-attention and reference-attention schemes. The attention module is instantiated by SAN [103] but is agnostic to network backbone. It can flexibly switch to other options and be plug-and-play. We switch between the sources that are used to calculate the Key and Query tensors, so as to implement self-attention and reference-attention respectively.
not clear what the role of attention is in the state-of-the-art image generation models. Does attention still improve the network performance? Which attention mechanism benefits the most and in the trade of how many additional parameters?
To answer these questions, we experiment with previously proposed self-attention modules: Dynamic Filter Networks (DFN) [37], Visual Transformers (VT) [85], Self-Attention GANs (SAGAN) [98], as well as the state-of-the-art patchbased spatially-adaptive self-attention module, SAN [103]. All the above self-attention modules are benefited from their adaptive data-dependent parameter space while they have their own hand-crafted architecture designs and interpretability. DFN [37] keeps the convolution primitive but makes the convolutional filter condition to its input tensor. VT [85] compresses input tensor to a set of 1D feature vectors, interprets them as semantic tokens, and leverages language transformer [78] for tensor propagation. SAN [103] generalizes the self-attention block [83] (as used in SAGAN [98]) by replacing the point-wise softmax attention with a patch-wise fully-connected transformation.
We show the diagram of self-attention in Figure 4, with a specific instantiation from SAN [103] due to its generalized and state-of-the-art design. Note that the attention module is agnostic to network backbone and can be switched to other options for fair comparisons. For conceptual and technical completeness, we formulate our SAN-based self-attention below.
In details, let T ∈ R h×w×c be the input tensor to a convolutional layer in the original architecture. Following the mainstream protocol of self-attention calculation [83,98,65], we obtain the corresponding key, query, and value tensors K(T), Q(T), V(T) ∈ R h×w×c separately using 1 × 1 convolutional kernel followed by bias and leaky ReLU. For each location (i, j) within the tensor spatial dimensions, we extract a large patch with size s from K centered at (i, j), denoted as k ∈ R s×s×c . We then flatten the patch and concatenate it along the channel dimension with q ∈ R 1×1×c , the query vector at (i, j), to obtain p ∈ R 1×1×(s 2 c+c) : In order to cooperate between the key and query, we feed p through two fully-connected layers followed by bias and leaky ReLU and obtain a vector with sizew ∈ R 1×1×s 2 c : M w1 ∈ R (s 2 c+c)×s 2 c , M w2 ∈ R s 2 c×s 2 c , and b w1 , b w2 ∈ R 1×1×s 2 c are the learnable parameters in the fully connected layers and biases. On one hand we reshapew back to the patch size w ∈ R s×s×c ; on the other hand we extract a patch with the same size from V centered at (i, j), denoted as v ∈ R s×s×c . Next, we aggregate v over spatial dimensions with the correponding weights from w to derive an output vector o ∈ R 1×1×c : We loop over all the (i, j) to constitute an output ten-sorŌ self ∈ R h×w×c and define it as the self-attention output. Finally, we replace the original convolution output with It is worth noting that w plays a conceptually equivalent role as the softmax attention map of the traditional keyquery aggregation [83,98,65], except it is not identical across channels anymore but rather generalized to optimize for each channel. w also aligns in spirit with the concept of DFN [37], except the spatial size s×s is empirically set much larger than 3×3, and more importantly, w is not "sliding" anymore but rather generalized to optimize at each location.
Investigations on self-attention modules. In Table 3 we extensively compare among a variety of self-attention modules by replacing the default convolution in the 32×32resolution layer in StyleGAN2 [43] config E backbone with one of them. We justify that SAN [103] significantly improves over the StyleGAN2 baseline and outperforms the other attention variants on several datasets. DFN [37] is better than ours on CelebA dataset, but is the worst on most other datasets. We provide additional ablation studies on network architectures in the supplementary material. We visualize the attention map examples of the best performing generator (StyleGAN2 + SAN) in Fig. 5. We find attention maps to strongly correspond to the semantic layout and structures of the generated images.
Complexity of self-attention modules. We also compare in Table 4 the time and space complexity of these selfattention modules. We observe that DFN [37] and VT [85] moderately improve the generation quality yet in the trade of undesirable > 3.6× complexity. On the contrary, the improvements from SAGAN [98] or SAN [103] are not at the cost of complexity, but rather benefited from the more representative attention designs. They use a fewer number of convolution channels and the multi-head trick [83] to control their complexity. These results show that the improved performance does not come from any additional parameters but rather the attention structure itself.

Reference-attention in the discriminator
First, we apply SAN [103], the best attention mechanism we validated in the generator, to the discriminator. However, we do not see a benefit of such design as shown in Table  5. Then, we explore an advanced attention scheme given that two classes of input (real vs. fake) are fed to the discriminator. We allow the discriminator to take two image inputs at the same time: the reference image and the primary image where we set the reference image to always be a real sample while the primary image to be either a real or generated sample. The reference image is encoded to represent one part of the attention components. These components are learned to guide the other part of the attention components, which are encoded from the primary image. There are three insights in this advancement. (1) An effective discriminator encodes real images and generated images differently, so that reference-attention is capable of learning positive feedback given both images from the real class and negative feedback given two images from different classes. Such a scheme amplifies the representation difference between real and fake, and in turn potentially strengthens the power of the discriminator. (2) Reference-attention enables distribution estimation Figure 6. Comparisons in FID between StyleGAN2 config E baseline (blue) and that with our reference-attention in the discriminator (orange). Our method consistently improves the baseline when dataset size varies between 1k and 30k images. For computationally efficient comparisons, we use each dataset at 128×128 resolution. See the supplementary material for the values in these plots.
in the discriminator feature level beyond the discriminator logit level in the original GAN framework, which guides generation more strictly towards the real distribution. (3) Reference-attention learns to cooperate real and generated images explicitly in one round of back-propagation, instead of individually classifying them and trivially averaging the gradients over one batch. Arbitrarily pairing up images mitigates discriminator from overfitting, similar to the spirit of random data augmentation, but we instead conduct random feature augmentation using attention.
In detail, we first encode the reference image and the primary image through the original discriminator layers prior to the convolution at a certain resolution. To align feature embeddings, we apply the Siamese architecture [6,15] to share layer parameters as shown in Fig. 1. We then apply the same attention scheme as used in the generator, except we use the tensor T ref ∈ R h×w×c from the reference branch to calculate the key and query tensors, and use the tensor T pri ∈ R h×w×c from the primary branch to calculate the value tensor and the residual shortcut. Finally, we replace the original convolution output with our reference-attention output: After the reference-attention layer, the two Siamese branches fuse into one and are followed by the remaining discriminator layers to obtain the classification logit. We show in Fig. 4 the diagram of reference-attention. Eq. 8 provides the flexibility how to cooperate between reference and primary images. We empirically explore the other compositions of sources to the key, query, and value components of reference-attention in the supplementary material as well as additional ablation studies on network architectures.
From Table 5 we validate reference-attention mechanism (ref attn) to improve the results whereas self-attention to be barely benefiting for the discriminator. Encouraged with these findings, we run the proposed reference-attention on full-scale datasets but do not see any improvements. Therefore, we dive deep into reference-attentions behavior in the discriminator with respect to the dataset size as given in Fig. 6. We find that the reference-attention in the discriminator consistently improves the performance when dataset size varies between 1k and 30k images, and on contrary slightly deteriorates the performance when dataset sizes increase further. We reason that the arbitrary pair-up of the reference and primary image inputs to prevent overfitting when data size is small but causing underfitting with the increase of data size Even though in this paper our main scope is GANs on large-scale datasets, we believe these findings to be very interesting for researchers to design their networks for limited-scale datasets. We summarize our comparisons on limited-scale datasets in the supplementary material.

Comparisons to the state of the art
Implementation details. All our models are built upon the most recent state-of-the-art unconditional Style-GAN2 [43] config E for its high performance and reasonable speed. We leverage the plug-and-play advantages of all our improvement proposals to strictly follow StyleGAN2 official setup and training protocol, which facilitates reproducibility and fair comparisons. For dual contrastive loss, we first warm up training with the default non-saturating loss for about 20 epochs, and then switch to train with our loss.
Datasets. We use several benchmark datasets, 70K FFHQ face dataset [42], 3M LSUN Bedroom dataset [91], 120K LSUN Church dataset [91], 2M LSUN Horse dataset [91], CelebA face dataset [57] and Animal Face Figure 7. Uncurated generated samples. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Artifacts from StyleGAN2 are highlighted with red boxes. Zoom in for details. In particular, our generation significantly outperforms the baselines on CLEVR images which strongly rely on long-range dependencies (occlusions, shadows, reflections, etc) and consistency (consistent shadow directions, consistent specularity, regular shapes, uniform colors, etc). See more samples in the supplementary material. dataset [55], and 70K CLEVR [38] dataset which contains rendered images with random compositions of 3D shapes, uniform materials, uniform colors, point lighting, and a plain background. It poses different challenges from the other common datasets: compositional scenes with occlusions, shadows, reflections, and mirror surfaces. We use 256×256 resolution images for each of these datasets except the CelebA and Animal Face datasets which are used in 128×128 resolutions. We do not experiment with 1024×1024 resolution of FFHQ as it takes 9 days to train StyleGAN2 base model. Instead, we run extensive experiments on the mentioned various datasets. If not otherwise noted, we use the whole dataset.
Evaluation. FID [29] is regarded as the golden standard to quantitatively evaluate generation quality. We follow the protocol in StyleGAN2 [43] to report the FID between 50K generated images and 50K real testing images. The smaller the more desirable. In the supplementary material, we report various other metrics that are proposed in StyleGAN [42] or StyleGAN2 [43] but are less benchmarked in other literature, Perceptual Path Length, Precision, Recall, and Separability.
Comparisons. Besides StyleGAN2 [43], we also compare to a parallel state-of-the-art study, U-Net GAN [69], which was build upon and improved on BigGAN [5]. We train U-Net by adapting it to the better backbone of Style-GAN2 [43] for fair comparison, and obtain better results than their official release on non-FFHQ datasets. As shown in Table 6, our self-attention generator improves on four out of five large-scale datasets, up to 13.3% relative improvement on Bedroom dataset. This highlights the benefits of attention to details and to long-range dependencies on complex scenes. However, self-attention does not improve on the extensively studied FFHQ dataset. We reason that the image pre-processing of facial landmark alignment compensates for the lack of attention schemes, which makes previous works also overlook them on other datasets.
Our dual contrastive loss improves effectively on all the datasets, up to 37% improvement on CLEVR dataset. This highlights the benefits of contrastive learning on generalized representation, especially on aligned datasets, e.g. FFHQ  Table 6. Comparisons in FID to the state-of-the-art GANs on the large-scale datasets. We highlight the best in bold and second best with underline. "w/ attn" indicates using the self-attention in the generator. "Contr" indicates using our dual contrastive loss instead of conventional GAN loss. and CLEVR, that can easily make a traditional discriminator overfit. The synergy effective between self-attention and contrastive learning is significant and consistent, resulting in at least 17.5% and up to 47.5% relative improvement on CLEVR. Especially for CLEVR, our generator handles more realistically for occlusions, shadows, reflections, and mirror surfaces. As shown in Fig. 7, our method suppresses artifacts that were previously visible in StyleGAN2 baseline outputs, with red boxes, e.g., the artifacts on the wall in Bedroom images, discontinuities in the structure in Church images, as well as color leakage between objects in CLEVR images.

Conclusion
The advancements in attention schemes and contrastive learning generate opportunities for new designs of GANs. Our attention schemes serve as a beneficial replacement for local and stationary convolutions, so as to equip generation and discriminator representation with long-range adaptive dependencies. In particular, our reference-attention discriminator cooperates between real reference images and primary images, mitigates discriminator overfitting, and leads to further boost on limited-scale datasets. Additionally, our novel contrastive loss generalizes discriminator representations, makes them more distinguishable between real and fake, and in turn incentivizes better generation quality.

Supplementary material A. Different GAN backbones for dual contrastive loss
In Table 7 we show the consistent and significant advantages of our dual contrastive loss on two other GAN backbones: SNGAN [60] and StyleGAN [42].

B. Self-attention at different generator resolutions
It is empirically acknowledged that the optimal resolution to replace convolution with self-attention in the generator is specific to dataset and image resolution [98]. For the stateof-the-art attention module SAN [103] in Table 3 in the main paper, we find that it achieves the optimal performance at 32×32 generator resolution consistently over all the limitedscale 128×128 datasets, and therefore we report these FIDs.
For the large-scale datasets with varying resolutions in Table 6 in the main paper, we conduct an analysis study on their optimal resolutions as shown in Table 8.
We find there is a specific optimal resolution for each dataset, and the FID turns monotonically deteriorated when introducing self-attention one resolution up or down. We reason that each dataset has its own spatial scale and complexity. If longer-range dependency or consistency counts more than local details in one dataset, e.g., CLEVR, it is more favorable to use self-attention in an earlier layer, thus at a lower resolution. We stick to the optimal resolution and report the corresponding FID for each dataset in Table 6 in the main paper.

C. Different reference-attention configurations
Eq. 8 in the main paper provides the flexibility of how to cooperate between reference and primary images. We empirically explore the other configurations of sources to the key, query, and value components in the reference-attention. The following two equations, Eq. 9 and Eq. 10, correspond to the two configuration variants we compare to. (10) From Table 9, we validate that Eq. 8 in the main paper is the best setting. We reason that the value embedding is relatively independent of the key and query embeddings. Hence we should encode value from one source, and key and query from the other source. Also, because the value and residual shortcut contribute more directly to the discriminator output, we should feed them with the primary image, and feed the key and query with the reference image to formulate the spatially adaptive kernel.

D. Reference-attention at different discriminator resolutions
In Table 10, we analyze the relationship between generation quality and the resolution to replace convolution with reference-attention in the discriminator. We stop investigation to higher resolutions because the training turns easily diverging. We conclude introducing reference-attention at the lowest possible resolution is most beneficial. We reason that the deepest features are the most representative for cooperating between reference and primary images. Also because the primary and reference images are not pre-aligned, the lowest resolution covers the largest receptive field and therefore leads to the largest overlap between the two images that should be corresponded. We stick to the 8×8 resolution for all the experiments involving reference-attention. Table 11. Comparisons in FID between StyleGAN2 config E baseline and that with our reference-attention in the discriminator. Our method consistently improves the baseline when dataset size varies between 1k and 30k images. For computationally efficient comparisons, we use each dataset at 128×128 resolution. See Fig. 6 in the main paper for the corresponding plots.   Table 13. Comparisons in FID to StyleGAN2 config E baseline on the limited-scale datasets. Our configurations consistently improve the baseline, the relative improvements of which are even more significant than those on the large-scale datasets. We use the 30k subset of each dataset at 128×128 resolution.
E. FID w.r.t. data size for reference-attention We report in Table 11 the detailed values from Fig. 6 in the main paper. Our method consistently improves the baseline when dataset size varies between 1k and 30k images.

F. Comparisons to the state of the art in various metrics
We extend Table 6 in the main paper with additional evaluation metrics for GANs, which are proposed and used in StyleGAN [42] and/or StyleGAN2 [43]: Perceptual Path Length (PPL), Precision (P), Recall (R), and Separability (Sep). See Table 12.
Consistent with FID rankings, our attention modules and dual contrastive loss also improve from StyleGAN2 baseline for Precision, Recall, and Separability in most cases. It is worth noting that the rankings of PPL are negatively correlated to all the other metrics, which disqualifies it as an effective evaluation metric in our experiments. E.g., U-Net GAN has the best PPL in most cases but in fact it contradicts against its worst FID and worst visual quality in Fig. 8, 9, 10, 11, and 12.

G. Comparisons on the limited-scale datasets
Besides comparisons on the large-scale datasets, we also compare to StyleGAN2 [43] baseline on the limited-scale datasets in Table 13. We use the 30k subset of each dataset at 128×128 resolution. We find: (1) Comparing across the first, second, and third rows, self-attention generator, dual contrastive loss, and their synergy significantly and consistently improve on all the limitedscale datasets, more than what they improve on the largescale datasets: from 18.1% to 23.3% on CelebA [57] and Animal Face [55], from 17.5% to 43.2% on LSUN Bedroom [91], and from 25.2% to 26.4% on LSUN Church [91]. It indicates the limited-scale setting is more challenging and leaves more space for our improvements.
(2) Comparing between the first and fourth rows, the reference-attention discriminator improves significantly and consistently on all the datasets up to 57.0% on LSUN Bedroom. We reason that the arbitrary pair-up between reference and primary images results in a beneficial effect similar in spirit to data augmentation, and consequently generalizes the discriminator representation and mitigates its overfitting.
(3) However, according to the fifth row, referenceattention discriminator is sometimes not compatible with contrastive learning because they may together overly augment the classification task: contrastive learning for one pair of primary and reference input against a batch of other pairs makes adversarial training unstable. This observation differs from that of pairwise contrastive learning in the unsupervised learning scenario [26,76,8,9] or GAN applications with reconstructive regularization [63].
Even though in this paper our main scope is GANs on large-scale datasets, we believe these findings to be very interesting for researchers to design their networks for limitedscale datasets.

H. Uncurated generated samples
For comparisons to the state of the art, we show more uncurated generated samples in Figure 8, 9, 10, 11 and 12. Our generation significantly outperforms the baselines U-Net GAN [69] and StyleGAN2 [43] in terms of quality, long-range dependencies, and spatial consistency.

I. Self-attention maps
For self-attention maps in the generator, we show more results in Figure 13, 14, 15, and 16. The attention maps strongly align to the semantic layout and structures of the generated images, which enable long-range dependencies across objects. Figure 8. Uncurated generated samples at 256×256 for FFHQ dataset [42]. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Our generation significantly outperforms the baselines in terms of quality, long-range dependencies, and spatial consistency. Figure 9. Uncurated generated samples at 256×256 for LSUN Bedroom dataset [91]. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Our generation significantly outperforms the baselines in terms of quality, long-range dependencies, and spatial consistency. Figure 10. Uncurated generated samples at 256×256 for LSUN Church dataset [91]. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Our generation significantly outperforms the baselines in terms of quality, long-range dependencies, and spatial consistency. Figure 11. Uncurated generated samples at 256×256 for LSUN Horse dataset [91]. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Our generation significantly outperforms the baselines in terms of quality, long-range dependencies, and spatial consistency. Figure 12. Uncurated generated samples at 256×256 for CLEVR dataset [38]. To align the comparisons, we use the same real query images for pre-trained generators to reconstruct. Our generation significantly outperforms the baselines in terms of quality, long-range dependencies, and spatial consistency. Figure 13. StyleGAN2 + SAN generated LSUN Bedroom [91] samples at 256×256 and their self-attention maps at 32×32 in the generator for the corresponding dot positions. Considering there is an attention weight kernel w ∈ R s×s×c for each position, we visualize the norm for each spatial position of w. The attention maps strongly align to the semantic layout and structures of the generated images, which enable long-range dependencies across objects. Figure 14. StyleGAN2 + SAN generated LSUN Church [91] samples at 256×256 and their self-attention maps at 32×32 in the generator for the corresponding dot positions. Considering there is an attention weight kernel w ∈ R s×s×c for each position, we visualize the norm for each spatial position of w. The attention maps strongly align to the semantic layout and structures of the generated images, which enable long-range dependencies across objects. Figure 15. StyleGAN2 + SAN generated LSUN Horse [91] samples at 256×256 and their self-attention maps at 16×16 in the generator for the corresponding dot positions. Considering there is an attention weight kernel w ∈ R s×s×c for each position, we visualize the norm for each spatial position of w. The attention maps strongly align to the semantic layout and structures of the generated images, which enable long-range dependencies across objects. Figure 16. StyleGAN2 + SAN generated CLEVR [38] samples at 256×256 and their self-attention maps at 16×16 in the generator for the corresponding dot positions. Considering there is an attention weight kernel w ∈ R s×s×c for each position, we visualize the norm for each spatial position of w. The attention maps strongly align to the semantic layout and structures of the generated images, which enable long-range dependencies across objects.