Paper commentary
AttnGAN: Fine-Grained Text to Image Generation with Attentional Generative Adversarial Networks
Attention lets a GAN match each image region to the right words
- Problem Text-to-image GANs squeeze the whole description into one sentence vector, so word-level details are lost. This matters most for complex COCO scenes with several objects.
- Method AttnGAN adds attention in each refinement stage, so image sub-regions pick the relevant words. Its DAMSM loss trains the generator with a word-level image-text matching score.
- Results AttnGAN reaches an inception score of 4.36 on CUB and 25.89 on COCO, using lambda set to 5 and 50.
Why a single sentence vector falls short
Most GAN-based text-to-image methods compress the whole description into one global sentence vector and feed it to the generator as the condition. This design has produced strong images, but a single vector carries no word-level detail, so attributes and parts named in the text are hard to recover. The gap widens for complex scenes such as those in COCO, where each image holds several objects that must each match their own words.
Stacked GANs from Zhang et al. generate images at several sizes, yet every stage is still conditioned on the global sentence vector. Attention had become a standard part of captioning, question answering and machine translation models, but it had not been used inside GANs for text-to-image synthesis. The alignDRAW model used a GAN only as a post-processing step, without attention.
AttnGAN addresses this with two components: an attentional generative network that brings word-level conditions into each refinement stage, and DAMSM, a matching model that adds a word-level image-text loss during training.
How each refinement stage reads the words
The first stage uses only the noise vector z and the conditioning vector obtained from the global sentence vector, which yields the initial hidden state h0 = F0(z, Fca(e)) and a low-resolution image. The first stage therefore sees no word-level information, and the later stages are where word features enter.
Each later stage i updates the hidden state with h_i = F_i(h_{i-1}, F_attn_i(e, h_{i-1})) and produces its image as x̂_i = G_i(h_i), for i = 1, 2, ..., m-1. Before attention is applied, the word features e in R^{D x T} are projected into the image feature space by a learned matrix U, so that e' = Ue.
For sub-region j, the model scores each projected word against that sub-region's hidden feature with s'_{j,i} = h_j^T e'_i, and normalizes the scores over the words with a softmax, so the weights beta_{j,i} sum to one across words for that sub-region. The word-context vector is the weighted sum c_j = sum over i of beta_{j,i} e'_i, and stacking the N sub-region vectors gives the word-context matrix that is combined with the image features for the next stage.
Conditioning Augmentation, the attention models, and the generator and transition networks are all modeled as neural networks, so their parameters are learned during training.
How the generator is trained
The total objective is L = L_G + λ L_DAMSM, where L_G = sum over i from 0 to m-1 of L_{G_i}, and λ sets the balance between the adversarial part and the DAMSM part. The adversarial term is the sum of the per-stage losses, so each generator stage has its own loss that is combined into L_G.
Each discriminator D_i is trained by minimizing a cross-entropy loss that separates real images from generated ones, in both an unconditional and a conditional form.
The DAMSM loss is applied only to the output of the last generator, because the goal is to produce large images. Applying it to images at every generator resolution did not improve performance and increased computational cost. Fine-tuning DAMSM together with the whole network did not improve performance either.
How DAMSM scores an image against a sentence
The text encoder is a bi-directional LSTM whose two hidden states for each word are concatenated into a word feature, and whose last hidden states are concatenated into the global sentence vector. The image encoder is Inception-v3 pretrained on ImageNet, with the input rescaled to 299 x 299 pixels. Parameters of layers built from Inception-v3 are fixed, while the newly added layers are learned jointly with the rest of the network.
The word-to-sub-region similarity matrix is s = e^T v, and each entry is normalized with a softmax over the words for that sub-region. For each word, the region-context vector c_i is the weighted sum of the regional visual vectors v_j, with weights alpha_j given by a softmax of gamma_1 times s_{i,j} over sub-regions, and the word-to-region relevance R(c_i, e_i) is the cosine similarity between c_i and e_i.
The image-sentence score R(Q, D) is a log-sum-exp over the word relevances scaled by gamma_2, so it behaves like a soft maximum and approaches the largest word relevance as gamma_2 grows. For a batch of M image-sentence pairs, the posterior that sentence D_i matches image Q_i is exp(gamma_3 R(Q_i, D_i)) divided by the sum over j of exp(gamma_3 R(Q_i, D_j)).
The word-level losses are L_w1 = -sum over i of log P(D_i|Q_i) and L_w2 = -sum over i of log P(Q_i|D_i), the sentence-level losses L_s1 and L_s2 use the global vectors, and the DAMSM loss is their sum.
The hyperparameters gamma_1 = 5, gamma_2 = 5, gamma_3 = 10 and M = 50 were chosen on a held-out validation set. DAMSM is pretrained on real image-text pairs at 299 x 299, and its text encoder supplies word vectors that are visually discriminative, unlike word vectors trained on text alone.
How generated images are scored
Experiments use CUB and COCO, with CUB preprocessed following the method of Zhang et al. CUB has 8,855 training and 2,933 test samples with 10 captions per image, and COCO has 80k training and 40k test samples with 5 captions per image.
The main measure is the inception score, following Zhang et al. Because the inception score does not show whether an image matches its description, R-precision is added as a second measure, computed by ranking candidate descriptions for each generated image. The DAMSM image and text encoders produce the global vectors used for this ranking, with cosine similarity.
Each model generates 30,000 images from randomly selected unseen descriptions, and each query image has one ground-truth description and 99 randomly selected mismatching descriptions.
For visualization, weights below 1/T are set to zero, so only words that stand out for a sub-region remain. For each word, the N sub-region weights are reshaped to sqrt(N) by sqrt(N), upsampled to the image size, and the top-5 most attended words per attention model are shown.
What the DAMSM weight and the second stage change
On CUB, raising λ from 0.1 to 5 for AttnGAN1 moved the inception score from 4.19 to 4.35 and R-precision from 16.55% to 58.65%.
On COCO, the authors raised λ from 0.1 to 50 and report that AttnGAN1 reaches both high inception score and high R-precision. The final COCO model, AttnGAN2 with λ = 50, reaches 25.89 inception score and an R-precision of 85.47 ± 3.69. λ was chosen by increasing it until the overall inception score began to fall on a held-out validation set, giving λ = 5 for CUB and λ = 50 for COCO.
The authors attribute the larger COCO value to the DAMSM loss being especially important for complex scenes.
Replacing DAMSM with the text encoder used in prior work gives 3.98 inception score and 10.37% R-precision on CUB. The authors report no collapsed nonsensical mode in the generated images and suggest the matching loss helps stabilize training.
Stacking a second attention model raises the output from 128 x 128 to 256 x 256 and yields higher inception scores on both CUB and COCO. On CUB, AttnGAN2 with λ = 5 reaches 4.36 ± .03 inception score.
How the final models compare with prior GAN scores
On CUB, AttnGAN2 with λ = 5 reaches 4.36 inception score, against 3.82 for the best prior score in the comparison. The abstract puts the gain over the best reported CUB score at 14.14%.
COCO is harder than CUB because it contains images with more complex scenarios.
The authors attribute the gains to the attention mechanism, which captures word-level and sub-region-level information. The comparison in Table 3 uses inception scores only, so R-precision is not compared against prior models in that table.
Which words each stage attends to
Figure 4 shows intermediate images at 64 x 64, 128 x 128 and 256 x 256 with the top-5 most attended words for each of the two attention models. The first stage sketches the basic shape and colors of objects but lacks details tied to specific words, such as the beak and eyes of a bird.
Later stages correct defects and add detail from word vectors, and some sub-regions can be inferred from the previous stage, so their attention is spread evenly over all words and appears black in the map. Other sub-regions, such as object attributes, are linked to their most relevant words, which appear as bright regions.
On CUB, the words the, this and bird are attended for locating the object, while colors and bird parts are attended for correcting defects and drawing details. On COCO, different sub-regions attend to different objects' words, such as bananas and kiwi.
The second attention model attends to some words that the first attention model omitted, and the authors read this as recovering objects and attributes missed at earlier stages.
Changing the most attended words in a CUB description changes the generated bird as expected, such as the color of its wings or belly. Descriptions of unlikely scenes, such as a stop sign floating on a lake, are rendered as described.
Where the results stop
COCO images generated by AttnGAN are less photo-realistic than the CUB images. Some generated birds have multiple heads, eyes or tails, and the authors state that global coherent structure is not yet captured well.
Three attention models were not tried because of GPU memory constraints, so the largest model tested is AttnGAN2 with three generators.
The inception score does not show whether a generated image matches its text, and R-precision relies on the DAMSM encoders themselves. Each R-precision value ranks one ground truth against 99 mismatching descriptions across 30,000 generated images. The text does not explain how the ± values in Table 2 were computed.
Paper info
- Authors
- Tao Xu, Pengchuan Zhang, Qiuyuan Huang, Han Zhang, Zhe Gan, Xiaolei Huang, Xiaodong He
- Venue
- arXiv · Preprint
- First published
- 2017-11-28
- Added
- 2026-11-12
- Updated
- 2017-11-28
- Version
- v1
- arXiv
- 1711.10485
- Category
- cs.CV
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Tao Xu, Pengchuan Zhang, Qiuyuan Huang, Han Zhang, Zhe Gan, Xiaolei Huang, Xiaodong He (2017). AttnGAN: Fine-Grained Text to Image Generation with Attentional Generative Adversarial Networks. arXiv:1711.10485v1. https://arxiv.org/abs/1711.10485v1
BibTeX
@misc{xu2017attngan,
title = {{AttnGAN: Fine-Grained Text to Image Generation with Attentional Generative Adversarial Networks}},
author = {Tao Xu and Pengchuan Zhang and Qiuyuan Huang and Han Zhang and Zhe Gan and Xiaolei Huang and Xiaodong He},
year = {2017},
eprint = {1711.10485},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1711.10485v1}
}