Search

Search papers and lessons

Global Average Pooling

Level 2 · 9 min read

Global average pooling replaces each whole feature map with its mean, turning a stack of maps into a short vector that a classifier can read directly.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Architectures
  • Probability
  • ReadReady to readNot yet

TL;DR

Global average pooling takes each channel of a Convolutional Neural Network output, which is a grid of numbers called a feature map, and replaces the whole grid with one number: its average. If the network ends with ten maps, the result is a list of ten numbers. That list can go straight into a classifier with no large layer of learned weights in between. Builds on: Convolutional Neural Network · Leads to: Feedforward Neural Network

Where it's used

Image classifiers at the end of a convolutional network. Many image networks finish their convolution layers with a stack of maps, one per class or one per learned feature. Global average pooling turns that stack into one score per class, so the final layer needs no extra weights to flatten the grid. Compared with flattening the maps into a long vector and feeding it to large dense layers, this design needs far fewer learned weights, because the pooling step itself has none.

Networks that must accept any image size. A dense layer expects a fixed number of inputs. If the final map grows when the input image is larger, a flattened vector changes length and the dense layer breaks. Global average pooling always returns one number per channel, whatever the height and width, so the same network runs on a 224 by 224 image or a 512 by 512 one.

Class activation maps. Each class score is a weighted sum of the channel averages. Because of that, a researcher can weight each un-pooled map by its contribution to one class and look at where the high values sit, which shows which image regions drove the decision.

How it works

Take a tiny instance: two feature maps, each 2 by 2, from the last convolution layer of a network with two classes.

  1. Start with the maps. Channel 1 holds a grid with rows (1, 2) and (3, 4). Channel 2 holds a grid with rows (0, 0) and (0, 4). Check: each channel has four numbers, one per position.
  2. Add up each channel's grid. Channel 1 sums to 1 + 2 + 3 + 4 = 10. Channel 2 sums to 0 + 0 + 0 + 4 = 4. Check: the grids have four positions each, so the sums are over four numbers.
  3. Divide by the number of positions. There are 2 times 2 = 4 positions. Channel 1 gives 10 / 4 = 2.5 and channel 2 gives 4 / 4 = 1. Check: the result is the average, so it lies between the smallest and largest value in its grid (1 to 4 for channel 1, 0 to 4 for channel 2).
  4. Collect the averages into a vector. The output is [2.5, 1]. One number per channel, regardless of the 2 by 2 size.
  5. Pass the vector to the classifier. The classifier layer turns the vector into one score per class, and a Softmax turns those scores into class probabilities. (The worked examples below give the classifier explicit weights.) Check: the vector has length 2, which equals the number of channels, so the classifier's input size is fixed by the design of the network, not by the image.
flowchart LR
  A["feature maps: H x W x C"] --> B["average each H x W grid"]
  B --> C["vector of length C"]
  C --> D["softmax classifier"]

The math

Definition: Global average pooling

Let ak,i,ja_{k,i,j} be the value at row ii, column jj of feature map kk, where the map has HH rows and WW columns. Global average pooling produces one number per map:

gk=1H W∑i=1H∑j=1Wak,i,jg_k = \frac{1}{H\,W} \sum_{i=1}^{H} \sum_{j=1}^{W} a_{k,i,j}

The output is the vector g=(g1,…,gC)\mathbf{g} = (g_1, \dots, g_C) with one entry for each of the CC maps.

Step 2 in symbols: the sum

The double sum adds every position of map kk. For the example, map 1 has H=W=2H = W = 2 and values a1,1,1=1a_{1,1,1} = 1, a1,1,2=2a_{1,1,2} = 2, a1,2,1=3a_{1,2,1} = 3, a1,2,2=4a_{1,2,2} = 4. The sum is 1+2+3+4=101 + 2 + 3 + 4 = 10.

Step 3 in symbols: the division

The factor 1HW\frac{1}{H W} turns the sum into a mean. With HW=4H W = 4, g1=104=2.5g_1 = \frac{10}{4} = 2.5. The division is what keeps the output size-independent: a 512 by 512 map has a mean on the same scale as a 2 by 2 map.

Step 5 in symbols: the classifier

The scores are s=Wg+bs = \mathbf{W}\mathbf{g} + \mathbf{b}, where W\mathbf{W} is a weight matrix with one row per class and one column per channel. Pooling has no weights of its own, so the only learned parameters here are W\mathbf{W} and b\mathbf{b}.

Worked examples

Example
Tiny: one map, by hand

Take a single 2 by 2 map whose rows are (2, 2) and (2, 2).

  1. Sum the four values: 2+2+2+2=82 + 2 + 2 + 2 = 8.
  2. Divide by HW=4H W = 4: 8/4=28 / 4 = 2.
  3. The output is g1=2g_1 = 2.

Interpretation: a constant map returns its constant. Averaging does not change a uniform grid.

Example
Standard: two maps and a classifier

Use the maps from the walkthrough, g=(2.5,1)\mathbf{g} = (2.5, 1), and a classifier with weights W=(1002)\mathbf{W} = \begin{pmatrix} 1 & 0 \\ 0 & 2 \end{pmatrix} and biases b=(0,0)\mathbf{b} = (0, 0).

  1. Score for class 1: s1=1×2.5+0×1=2.5s_1 = 1 \times 2.5 + 0 \times 1 = 2.5.
  2. Score for class 2: s2=0×2.5+2×1=2s_2 = 0 \times 2.5 + 2 \times 1 = 2.
  3. The scores (2.5,2)(2.5, 2) go to the softmax. The first class has a higher score, so it has the higher probability.

Interpretation: each class reads only its own map. Here class 1 is driven by the average of map 1, and class 2 by the average of map 2.

Example
Realistic: a ten-class network on 7 by 7 maps

A ten-class image network ends with ten feature maps, each 7 by 7. Suppose the ten maps have been computed for one image. Only the sums of the grids are needed here.

  1. The grid for class 3 has 7×7=497 \times 7 = 49 positions.
  2. Suppose its values sum to 147147. Then g3=147/49=3g_3 = 147 / 49 = 3.
  3. Doing this for all ten maps gives a vector of length 10, one entry per class.
  4. The classifier computes ten scores from this vector, one of which is the class-3 score, and the softmax picks the most likely class.

Try it: a different image gives the class-3 map a sum of 9898 over its 49 positions. What is g3g_3 for that image, and is it higher or lower than 3? Answer: 98/49=298 / 49 = 2, which is lower than 3.

Common mistakes and tradeoffs

It is tempting to think global average pooling is the same as flattening the maps. In fact flattening keeps every position as a separate input, so a 7 by 7 map gives 49 inputs and the next layer must learn a weight for each one. Global average pooling keeps one number per map, so the next layer learns far fewer weights, and the output does not depend on where in the map the activity sits. That last property is a feature for classification and a limit for tasks that need location, such as segmentation or detection, where you want the spatial grid.

It is also tempting to think averaging always beats taking the maximum. In fact Max Pooling keeps the strongest response in a region, which suits detecting a feature that is either present or absent. Averaging dilutes a single strong response across the whole map, so a small but important feature can be hidden by many weak positions. Which one works better depends on the task, and the choice is usually tested.

Use global average pooling at the end of a classifier when the input size can vary, when you want few parameters, or when you want class activation maps. Do not use it when the output needs to keep a spatial layout, such as a per-pixel label in segmentation or the locations needed by detection. For those tasks, a Fully Convolutional Network keeps the grid. Other alternatives are flattening followed by dense layers (more parameters, fixed input size) and global max pooling (keeps only the strongest response per map).

History

Before 2013 most image classifiers ended with large fully connected layers, which held most of the network's weights and were prone to Overfitting. Min Lin, Qiang Chen and Shuicheng Yan proposed replacing them with averaging over the last feature maps in the paper "Network In Network", posted to arXiv in 2013. The idea caught on because it removed the large dense layers and let the same network run on different image sizes. Later image networks adopted it as the standard finish before the classifier.

Recap

  • A convolution layer outputs a stack of feature maps, one per channel.
  • Global average pooling replaces each map with its mean, giving one number per channel.
  • The result is a vector whose length is fixed by the number of channels, not by the image size.
  • The output has no weights of its own, so the classifier after it has few parameters.
  • Averaging smooths away position, which helps classification and hurts tasks that need a spatial grid.

Read next: Feedforward Neural Network for the classifier that usually follows, and Receptive Field for how far each value in a map can see into the image.

Check yourself

QuestionA feature map is a 4 by 4 grid whose values sum to 40. What does global average pooling output for this map?Show answer

The output is the mean: 40/(4×4)=40/16=2.540 / (4 \times 4) = 40 / 16 = 2.5.

QuestionWhy does a network that ends with global average pooling accept images of different sizes, while one that flattens its maps does not?Show answer

Global average pooling always gives one number per channel, so the vector length depends only on the number of channels. Flattening gives a vector whose length is channels times height times width, which changes when the image size changes, and the dense layer after it expects one fixed length.

QuestionTwo 2 by 2 maps are the grid with rows (1, 1) and (1, 1), and the grid with rows (0, 0) and (0, 8). Compute the pooled vector.Show answer

Map 1 averages to 4/4=14 / 4 = 1. Map 2 sums to 88, so it averages to 8/4=28 / 4 = 2. The pooled vector is (1,2)(1, 2).

QuestionA segmentation model must label each pixel of a photo as road or not road. Is global average pooling a good final step, and why?Show answer

No. Segmentation needs an output that keeps the spatial layout, one label per pixel. Global average pooling removes all position information by returning one number per channel, so it cannot say where the road is.

Sources