# AttnGAN: Fine-Grained Text to Image Generation with Attentional Generative Adversarial Networks

Attention lets a GAN match each image region to the right words

Tao Xu, Pengchuan Zhang, Qiuyuan Huang, Han Zhang, Zhe Gan, Xiaolei Huang and 1 more · arXiv · 2017-11-28

- **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](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1711.10485v1) · [PDF ↗](https://arxiv.org/pdf/1711.10485v1)

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}
}
```

## Other paper commentary

- [Stochastic Variational Inference](https://refivu.com/en/papers/36c9d62c-308d-476e-8657-7d699cf4658e-stochastic-variational-inference.md)
- [Transferability in Machine Learning: from Phenomena to Black-Box Attacks using Adversarial Samples](https://refivu.com/en/papers/0070f617-bee7-4459-b94e-a20f7c1d2de5-transferability-in-machine-learning-from-phenomena-to-black.md)
- [Conditional Adversarial Domain Adaptation](https://refivu.com/en/papers/a233380e-bb76-4fca-bc73-df4e86a220e3-conditional-adversarial-domain-adaptation.md)
- [RePaint: Inpainting using Denoising Diffusion Probabilistic Models](https://refivu.com/en/papers/3c0a622f-efc1-4d6c-bb30-bd253079ac9a-repaint-inpainting-using-denoising-diffusion-probabilistic.md)
- [Imagen Video: High Definition Video Generation with Diffusion Models](https://refivu.com/en/papers/79ba3745-980d-44fe-a5b0-70a6fbd0bc66-imagen-video-high-definition-video-generation-with-diffusion.md)
- [NIPS 2016 Tutorial: Generative Adversarial Networks](https://refivu.com/en/papers/3bddac01-a39c-4985-8cda-7d40f3279d07-nips-2016-tutorial-generative-adversarial-networks.md)
- [Semantic Understanding of Scenes through the ADE20K Dataset](https://refivu.com/en/papers/3862d7db-bf28-4590-ab09-839bcbf7ccaa-semantic-understanding-of-scenes-through-the-ade20k-dataset.md)
- [The CMA Evolution Strategy: A Tutorial](https://refivu.com/en/papers/2386515a-206e-4d75-9423-936049ac71e6-the-cma-evolution-strategy-a-tutorial.md)
- [LoFTR: Detector-Free Local Feature Matching with Transformers](https://refivu.com/en/papers/7c0476a6-4dcb-4eab-beac-aaa3eb492203-loftr-detector-free-local-feature-matching-with-transformers.md)
- [Prompt-to-Prompt Image Editing with Cross Attention Control](https://refivu.com/en/papers/a18cb3bd-2a1b-4adf-9a2a-f866c7f83edf-prompt-to-prompt-image-editing-with-cross-attention.md)
- [Named Entity Recognition with Bidirectional LSTM-CNNs](https://refivu.com/en/papers/454922d0-d836-4e69-a3cd-d40cf940d456-named-entity-recognition-with-bidirectional-lstm-cnns.md)

## Terms in this paper

- [COCO](https://refivu.com/en/terms/41f3215c4f4a-coco)
- [Generative adversarial networks](https://refivu.com/en/terms/889f2a50b737-generative-adversarial-networks)
- [attention mechanism](https://refivu.com/en/terms/977ee0201459-attention-mechanism)
- [inception score](https://refivu.com/en/terms/25dbfbd4fd96-inception-score)
- [CUB](https://refivu.com/en/terms/1db8c8020177-cub)
- [text-to-image synthesis](https://refivu.com/en/terms/22c28d1bf493-text-to-image-synthesis)
- [Conditioning Augmentation](https://refivu.com/en/terms/46cda14a8fbb-conditioning-augmentation)
- [StackGAN](https://refivu.com/en/terms/5cb41bfec631-stackgan)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/a0260608-3d36-436c-af24-a535c7e4e5ef-attngan-fine-grained-text-to-image-generation-with)
