wikillm

Paper · 2021-03-01 · cs.CV, cs.AI, cs.CL, cs.LG

Generative Adversarial Transformers

GANformer builds a GAN generator from bipartite attention: k latent variables and image features attend to each other, and the result scales and shifts features region by region, not via one global style vector as in StyleGAN2. Author-reported at 256 px, equal steps: the duplex variant gets FID 9.17 vs 16.05 (StyleGAN2) on CLEVR, 6.51 vs 11.53 on LSUN-Bedrooms.

Generative adversarial network (GAN)Attention mechanismCross-attentionAdaptive instance normalizationgenerative-modelsganattentionimage-synthesis

arXivPDFCodeMarkdown14 min read

AI-compiled by claude-haiku-5-5 on 2026-11-08, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

A GAN image generator usually starts from one random vector and turns it into a picture in stages. StyleGAN, the strong recent design, uses that vector to set the look of the whole picture at once, like choosing a single colour scheme and lighting for an entire room. That makes it good at global style, but it has little direct control over one spot in the image.

GANformer changes the generator so that the random input is split into several separate pieces, each acting like a small "helper" that is responsible for part of the scene. At each stage, the helpers and the growing image look at each other through attention, so each helper can pick out the region it should control and style that region on its own. Information flows from the helpers into the image and back, so the two sides refine each other. The attention is built so that its cost grows in proportion to the image size rather than with the square of it, which is what makes this affordable for larger pictures.

The paper reports that the duplex version gets a lower FID (a common image-quality score) than StyleGAN2 on each of the four datasets it tests, and that the helpers tend to line up with recognisable parts of a scene. The results come from the authors' own experiments and training budget.

TL;DR

  • GANformer is a GAN whose generator uses a bipartite transformer: k latent variables and image features attend to each other, with multiplicative (scale and shift) control of the features §3.1 §3.2.
  • Attention runs in one direction (latents to image, "simplex") or in both (adding a centroid step, "duplex") §3.1.1 §3.1.2.
  • Cost is linear in the number of image positions for a fixed number of latents, versus quadratic for full self-attention §3.1.3.
  • Author-reported FID at 256 px, equal training steps: CLEVR 9.17 (duplex) vs 16.05 (StyleGAN2); LSUN-Bedrooms 6.51 vs 11.53 tab. 1.
  • Several design details in the paper differ from the released TensorFlow code's defaults (duplex centroids, integration form, latent counts, learning rate); see Paper vs code and Pitfalls.

Problem

Generative adversarial networks (GANs) are usually built from convolutions, which have a local receptive field. The paper argues that this makes long-range relations and global shape hard to model §1. Transformers model long-range relations, but full self-attention over all pixels costs O(n²) in the number of positions, which the authors say blocks high-resolution use §1 §3.1.3.

StyleGAN2 models global style well through a single latent vector that modulates each convolution layer, but it offers no direct means to control the style of a local region §3.2. The paper's aim is an architecture that keeps long-range interaction, supports region-level modulation, and stays linear in cost §1 §3.2.

Assumptions: experiments use 256×256 images with the StyleGAN2 training recipe and a fixed training length §4 app. A.

Method

The generator stays a StyleGAN-style design, with the convolutional blocks replaced in part by bipartite attention layers. The discriminator has no attention in the paper's final version §3.2 §3.2 footnote.

  1. Latent split. The input vector z is cut into k components. Each component goes through a shared mapping network, giving k intermediate latent variables Y = [y_1, ..., y_k] §3.2. The paper uses k between 8 and 32 §4.
  2. Generator stack. The stack starts from a 4×4 grid. It alternates bipartite attention, a convolution with kernel size 3 and LeakyReLU, and up-sampling, until the target resolution. Sinusoidal positional encodings are added to image positions; trained embeddings are used for latents §3.1.3.
  3. Simplex update. Image features X attend to the latents Y. The attended summary sets a per-element scale and bias for the normalised features §3.1.1. Layer normalisation was reported to work better than instance normalisation §3.1.1 footnote.
  4. Duplex update. Each latent first gets a centroid key K, which is an attention-weighted average over image positions. The image then attends to these centroids, with values from the latents, and the result sets scale and bias as in step 3 §3.1.2.
  5. Bidirectional loop. Duplex attention alternates a latent update from the image (bottom-up) and an image update from the latents (top-down) §3.1.2.
  6. Training. The paper adopts the StyleGAN2 recipe: style mixing, stochastic variation, weight moving average, non-saturating logistic loss and lazy R1 regularisation §3.2. Optimiser and batch details are in app. A.

Equations are unnumbered in the paper; they are cited by section.

Attention and modulation, as stated in the paper §3.1.1 §3.1.2:

a(X, Y) = \text{Attention}(q(X), k(Y), v(Y)) \quad [\S 3.1.1]

\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{Q K^{T}}{\sqrt{d}}\right) V \quad [\S 3.1]

u^{s}(X, Y) = \gamma\big(a(X, Y)\big) \odot \omega(X) + \beta\big(a(X, Y)\big) \quad [\S 3.1.1]

\omega(X) = \frac{X - \mu(X)}{\sigma(X)} \quad [\S 3.1.1]

K = a(Y, X), \qquad u^{d}(X, Y) = \gamma\big(A(Q, K, V)\big) \odot \omega(X) + \beta\big(A(Q, K, V)\big), \quad Q = q(X) \quad [\S 3.1.2]

In these formulas, d is the feature dimension and γ, β are mappings to dimension d. The paper presents one head and says that multiple heads are used in practice §3.1.

Pseudocode
# Bipartite update of a target group from a source group  [§3.1.1] [§3.1.2]
# Input:  target (B, n, d), source (B, m, d); n = H*W for images, m = k latents
# Output: updated target (B, n, d)

def simplex_update(target, source):
    # Queries come from the target, keys and values from the source  [§3.1]
    queries = to_queries(target)          # (B, n, d)
    keys = to_keys(source)                # (B, m, d)
    values = to_values(source)            # (B, m, d)
    # Attention over the source elements, scaled by sqrt(d)  [§3.1]
    attended = softmax(queries @ keys^T / sqrt(d)) @ values  # (B, n, d)
    # Normalise the target, then scale and shift it  [§3.1.1]
    normalized = layer_norm(target)       # (B, n, d)
    scale = gamma(attended)               # (B, n, d)
    shift = beta(attended)                # (B, n, d)
    return scale * normalized + shift


# Duplex update: image features read centroids of the latents  [§3.1.2]
def duplex_update(features, latents):
    # Centroid keys: each latent averages the image features it attends to,
    # K = a(Y, X)  [§3.1.2]
    centroid_keys = attend_over_positions(latents, features)  # (B, k, d)
    # Image queries attend to the centroids; values come from the latents
    # [§3.1.2]
    queries = to_queries(features)                            # (B, n, d)
    attended = softmax(queries @ centroid_keys^T / sqrt(d)) @ to_values(latents)
    normalized = layer_norm(features)                         # (B, n, d)
    return gamma(attended) * normalized + beta(attended)


# One duplex step in a generator layer  [§3.1.2]
def duplex_layer(features, latents):
    # Bottom-up: latents read the image, Y := u^a(Y, X)  [§3.1.2]
    latents = simplex_update(latents, features)
    # Top-down: image reads the latents, X := u^d(X, Y)  [§3.1.2]
    features = duplex_update(features, latents)
    return features, latents

Note on the pseudocode: attend_over_positions is the paper's a(Y, X). The paper describes K as weighted averages over the image positions, using the attention distribution from comparing them with the latents §3.1.2. This card reads that as a softmax over image positions for each latent; the paper's text does not name the axis explicitly.

Hyperparameters

Values per dataset are from the paper's hyperparameter table tab. 4. Training settings are from app. A. The code column gives the default in the released TensorFlow repository, where it differs or where the paper is silent.

NameValueWhereNotes
latent variables k, FFHQ / CLEVR / Cityscapes / Bedroom8 / 16 / 16 / 16tab. 4chosen from {8, 16, 32, 64}; code preset is 16 for FFHQ, Cityscapes, Bedroom and 8 for CLEVR run_network.py
latent dimension per variable16 / 32 / 32 / 32tab. 4code preset does not match: see Paper vs code
overall latent dimension128 / 512 / 512 / 512tab. 4chosen from {128, 256, 512}, shared with baselines; code preset is 512 for FFHQ, Cityscapes, Bedroom and 128 for CLEVR run_network.py
R1 weight γ, FFHQ / CLEVR / Cityscapes / Bedroom10 / 40 / 20 / 100tab. 4chosen from {1, 10, 20, 40, 80, 100}; code preset matches run_network.py
batch size32 (4 × 8 with gradient accumulation)app. Acode default 32, batch per GPU 4 run_network.py
optimiserAdam, β1 = 0.0, β2 = 0.99app. Amatches run_network.py; code also sets ε = 1e-8, not stated in paper
learning rateequalised LR 0.001app. Acode default is 0.002 for both G and D run_network.py; see Paper vs code
mapping network layers8app. Acode default 8 run_network.py
mapping learning-rate multipliernot statedcode default 0.01 run_network.py
activationLeakyReLU, α = 0.2app. A
resampling filterbilinear in all up/down-sampling layersapp. A
minibatch std layerat the end of the discriminatorapp. A
ResNet connectionsmapping, synthesis and discriminatorapp. A
training resolution256 × 256, padded as neededapp. A
mirror augmentationFFHQ and Cityscapes onlyapp. Acode preset matches run_network.py
dataset sizesCLEVR 100k, FFHQ 70k, Cityscapes ~25k, LSUN-Bedrooms 3Mapp. A
training lengthsame steps for all models, about one week on 2 V100 GPUs per model (3–4 days on 4 GPUs)§4 app. Astep count not given as one number
lazy regularisation intervalsnot statedcode uses G every 4 steps and D every 16 run_network.py
style-mixing probabilitynot statedcode default 0.9 run_network.py
attention headsone head shown; "multi-head in practice"§3.1code default 1 run_network.py; head count for the paper's runs not stated
attention dropoutnot statedcode default 0.12 network.py
attention scale1/√d per head§3.1matches network.py
positional encodingsinusoidal for image positions; trained for latents§3.1.3code default sinusoidal run_network.py
generator / discriminator attentiongenerator only§3.2 footnotecode default --d-transformer off run_network.py

Model size tab. 5: generator 34–38 M parameters across baselines and GANformer variants (36 M for both GANformer variants), discriminator 29 M, with 16 latent variables and latent dimension 512.

Results

Author-reported. Setup: 256×256 images, the same number of training steps for all models (about one week on 2 NVIDIA V100 per model) §4. Baselines are the paper's own GAN, k-GAN, SAGAN and StyleGAN2, all run inside the authors' TensorFlow codebase §4. FID, Inception Score, precision and recall are computed over 50k samples tab. 1. The paper also reports a 1k-sample study for CLEVR tab. 2 and disentanglement metrics over 1k CLEVR samples tab. 3.

Table 1, headline comparison tab. 1. ↓ lower is better, ↑ higher is better.

DatasetModelFID ↓IS ↑Precision ↑Recall ↑
CLEVRGAN25.022.1721.7716.76
CLEVRk-GAN28.292.2122.9318.43
CLEVRSAGAN26.042.1730.0915.16
CLEVRStyleGAN216.052.1528.4123.22
CLEVRGANformer (simplex)10.262.4638.4737.76
CLEVRGANformer (duplex)9.172.3647.5566.63
LSUN-BedroomsGAN12.162.6652.1713.63
LSUN-Bedroomsk-GAN69.902.4128.713.45
LSUN-BedroomsSAGAN14.062.7054.827.26
LSUN-BedroomsStyleGAN211.532.7951.6919.42
LSUN-BedroomsGANformer (simplex)8.562.6955.5222.89
LSUN-BedroomsGANformer (duplex)6.512.6757.4129.71
FFHQGAN13.184.3067.1517.64
FFHQk-GAN61.144.0050.510.49
FFHQSAGAN16.214.2664.8412.26
FFHQStyleGAN29.244.3368.6125.45
FFHQGANformer (simplex)8.124.4668.9410.14
FFHQGANformer (duplex)7.424.4168.775.76
CityscapesGAN11.571.6361.0915.30
Cityscapesk-GAN51.081.6618.801.73
CityscapesSAGAN12.811.6843.487.97
CityscapesStyleGAN28.351.7059.3527.82
CityscapesGANformer (simplex)14.231.6764.122.03
CityscapesGANformer (duplex)5.761.6948.0633.65

Other author-reported results:

  • Training efficiency: on CLEVR the model reaches high-quality images in about a third of the steps of the second-best method, and it stays data-efficient down to as few as 1k training images §4.1.
  • Attention maps: latent variables attend to coherent regions that match semantic classes such as windows, pillows, sidewalks and cars, measured with a pretrained segmenter over 1k samples §4.2 app. B.

Table 2, CLEVR chi-square statistics over 1k samples. The paper does not spell out the direction in words; the table bolds the largest value per row, and the text calls the GANformer's scores "better" tab. 2 §4.4:

PropertyGANStyleGANGANformer (simplex)GANformer (duplex)
Object Area0.0380.0350.0450.068
Object Number2.3781.6222.1422.825
Co-occurrence13.5329.1779.50613.020
Shape1.3340.6431.8562.815
Size0.2560.0660.3930.427
Material0.1080.3221.5732.887
Color1.0111.4021.5193.189
Class6.4354.5715.31516.742

Table 3, disentanglement metrics (DCI and modularity) over 1k CLEVR samples tab. 3:

MetricGANStyleGANGANformer (simplex)GANformer (duplex)
Disentanglement0.1260.2080.5560.768
Modularity0.6310.7030.8910.952
Completeness0.0710.1240.1950.270
Informativeness0.5830.6850.8990.972
Informativeness'0.4340.3320.8480.963

Paper vs code

Code: official repository github.com/dorarad/gansformer (linked from the abstract; owner is the first author), commit 3a9efa45, MIT licence. The paper's stated framework is TensorFlow app. A; the TensorFlow tree (training/) was compared. The repository also has a PyTorch tree (pytorch_version/), which was not compared.

Confirmed in code:

Differences:

  • Latent counts and dimensions. The paper's table gives FFHQ 8 variables of dimension 16 (overall 128) and CLEVR 16 of dimension 32 (overall 512) tab. 4. The preset gives 16 variables and overall 512 for FFHQ, Cityscapes and Bedroom, and 8 variables and overall 128 for CLEVR run_network.py. The preset therefore swaps the paper's FFHQ and CLEVR settings. The code's per-variable dimension is derived from these two values, so it differs as well.
  • Integration form. The paper's update has a scale γ on the normalised features and a bias β §3.1.1. With the preset's integration = "mul" run_network.py, the code multiplies by (1 + γ) and does not add β network.py.
  • Duplex centroids are learned by default. The paper defines K = a(Y, X), computed from the image features §3.1.2. In the code, iterative defaults to False network.py, so parametric is True network.py. Then the centroid tensor is a learned variable (toasgn_init) that overwrites the assignment-based centroids network.py. Assignments from the same layer enter only in a second k-means iteration, which needs kmeans_iters > 1 (default 1) network.py run_network.py.
  • No bottom-up latent update in the default generator. The paper's duplex alternates a latent update from the image with an image update from the latents §3.1.2. The code's bottom-up step runs only with --g-img2ltnt, which the preset does not set network.py run_network.py. Without it, each layer takes its latent from the mapping output and the latents are not refined through the stack network.py.
  • Heads. The paper says multi-head attention is used in practice §3.1. The preset leaves num_heads at 1 run_network.py.
  • Learning rate. The paper states an equalised learning rate of 0.001 app. A. The code defaults to 0.002 for both networks run_network.py, and the preset does not change it.
  • Duplex in the code. The only switch the code calls duplex is --kmeans. The repository does not hold the run commands for the simplex and duplex rows of Table 1, so which switches produced those rows is not verifiable from the repo.

Not compared: the discriminator code, the PyTorch tree, and any run scripts beyond the preset.

Pitfalls

  • Latent settings. Take the per-dataset values from tab. 4, not from the code preset. The preset's FFHQ and CLEVR latent sizes differ from the paper (see Paper vs code). An agent copying the preset will not reproduce the paper's setting.
  • Modulation form. Implement the paper's γ ⊙ ω(X) + β with layer normalisation §3.1.1. If you follow the code preset, the form is (1 + γ) ⊙ ω(X) with no β network.py.
  • Duplex is not the paper's duplex by default. A duplex implementation that follows §3.1.2 needs the bottom-up latent update and same-layer centroids. The default code path has learned centroids and no bottom-up update (see Paper vs code).
  • Learning rate conflict. The paper says 0.001 and the code says 0.002. The paper does not say which was used for its results. Treat it as unresolved and sweep if reproducing.
  • Unstated values. Not stated in the paper: lazy-regularisation intervals, style-mixing probability, attention dropout, number of heads used in the experiments, and the exact training length in steps. The code defaults above are the only concrete values available.
  • Results are only partly comparable. All models here were trained for the same number of steps, which is much shorter than the training behind the StyleGAN2 paper's own FID values. The paper's footnote attributes those lower published FFHQ and Bedroom FIDs to 5 to 7 times longer training §4 footnote. Do not compare these numbers to those published in the StyleGAN2 paper.
  • Text vs table. The text says the model "matches or outperforms" prior work §4. On Cityscapes, the simplex variant has FID 14.23 against StyleGAN2's 8.35, and its recall is far lower (2.03 vs 27.82) tab. 1. The text does not discuss this.
  • Table 2 wording. The text says the model is better on "almost all" properties §4.4. In tab. 2, GAN has the highest co-occurrence score (13.532 vs 13.020 for the duplex variant), and k-GAN is not in the table.
  • Variance. No error bars or seed variance appear in the compiled text tab. 1. A commented-out sentence in the TeX source mentions averaging over repeated runs, but it is not in the published text. Assume single-run numbers.
  • Memory. Attention maps are n × k per head per image. For a 256 × 256 final layer, n = 65,536 positions and k = 16 gives about 1.05 M entries per image per head. This is arithmetic from the stated sizes §3.1.3 app. A, not a figure the paper reports. The paper claims linear cost in n for fixed k §3.1.3.
  • Compute. The paper gives a rough budget of one week on two V100 GPUs per model §4. The code's default training length is 25,000 kimg run_network.py. The comparison figures in the supplement state 5k to 15k kimg app. Supplementary Material. The run length behind the reported numbers is not stated as one value.
  • Figures and table numbers. Table numbers were counted in TeX source order, which should match the compiled paper. Figures are cited by section because no compiled PDF was checked.
  • Attention resolution range. The code's default range for generator attention is 0 to 8 in logarithmic resolution run_network.py. The paper says the stack starts at a 4×4 grid §3.1.3 but does not give the range of layers that carry attention. Check the code's range before reusing it.

Lineage

  • Builds on: StyleGAN and StyleGAN2, for global style modulation and the training recipe §3.2 app. A; the Transformer, for attention §3.1; key-value memory networks, for the latent keys and values §3.1.2; k-means and slot attention, for the centroid view of duplex attention §3.1.2. The paper gives these as names only; no arXiv ids appear in the source.
  • Compared against: a baseline GAN, k-GAN, SAGAN and StyleGAN2 §4. VQGAN appears in the supplementary sample images only app. Supplementary Material.
  • Superseded by: not known from content in this wiki.

Source and license

Adapted from “Generative Adversarial Transformers” by Drew A. Hudson, C. Lawrence Zitnick (arXiv:2103.01209), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/dorarad/gansformer at commit 3a9efa4545be (MIT). Any quoted code is shown under that license.