AI Logic

Normalization

Group Normalization

Updated:

Comparison of the ranges over which four normalization methods pool the mean and variance. In each sample's grid, rows are channels and columns are spatial positions. Top left: batch normalization pools one channel across all samples. Top right: layer normalization pools all channels of one sample. Bottom left: instance normalization pools one channel of one sample. Bottom right: Group Normalization pools a group of 3 channels of one sample. Bottom: the computation flow of Group Normalization

What Is Group Normalization?

Group Normalization is a method that divides the channels within one sample into several groups and normalizes each group using its own mean and variance. Wu and He proposed it in 2018. Because it does not use the other samples in the mini-batch, it achieves almost the same accuracy whether the batch size is 2 or 32.

Batch normalization greatly helped CNN training, but with small batches its estimates of the mean and variance fluctuate and accuracy drops sharply. In tasks with large inputs, such as object detection, segmentation, and video recognition, often only 1–2 images fit on a GPU, so batch normalization could not be used as is. Group Normalization sidesteps this problem by "not using the batch axis".

In the paper, when training ResNet-50 on ImageNet with a batch of 2 per GPU, Group Normalization's error rate was 10.6 points lower than batch normalization's (24.1% vs. 34.7%), and with a batch of 32 the gap to batch normalization was 0.5 points.

The figure at the top of the article, like Figure 2 of the paper, compares the ranges (blue cells) over which four normalization methods pool the mean and variance. In each sample's grid, rows are channels and columns are spatial positions. The bottom-right panel is Group Normalization, which splits 6 channels into 2 groups of 3.

Computation Steps

Let an image tensor be "batch NN × channels CC × height HH × width WW". For one sample, Group Normalization computes the following in order (Section 3.1 of the paper).

  1. Split the channels into GG groups. Each group contains C/GC/G channels, in channel order. GG is a hyperparameter; the paper's default is G=32G = 32.
  2. Compute the mean and standard deviation of each group. All values at every position H×WH \times W of the group's C/GC/G channels (m=CGHWm = \frac{C}{G} H W values) are used together. Si\mathcal{S}_i is the set of values normalized with the same mean and variance as the value xix_i. μi=1m∑k∈Sixk,σi=1m∑k∈Si(xk−μi)2+ϵ\mu_i = \frac{1}{m} \sum_{k \in \mathcal{S}_i} x_k, \qquad \sigma_i = \sqrt{\frac{1}{m} \sum_{k \in \mathcal{S}_i} (x_k - \mu_i)^2 + \epsilon}
  3. Subtract the mean and divide by the standard deviation. x^i=xi−μiσi\hat{x}_i = \frac{x_i - \mu_i}{\sigma_i}
  4. Apply the per-channel learned scale and shift. The mean and variance are per group, but γ\gamma and β\beta are per channel. yi=γx^i+βy_i = \gamma \hat{x}_i + \beta

The paper frames the differences among these normalization methods as differences in how Si\mathcal{S}_i is defined (iN,iCi_N, i_C are the batch and channel indices of xix_i).

MethodSi\mathcal{S}_i (values normalized together)Axes pooled for mean and variance
Batch normalizationSame channel (kC=iCk_C = i_C)N,H,WN, H, W
Layer normalizationSame sample (kN=iNk_N = i_N)C,H,WC, H, W
Instance normalizationSame channel of the same sampleH,WH, W
Group NormalizationSame group of the same sample (⌊kC/CG⌋=⌊iC/CG⌋\lfloor k_C / \frac{C}{G} \rfloor = \lfloor i_C / \frac{C}{G} \rfloor)CG\frac{C}{G} channels and H,WH, W

As the table shows, G=1G = 1 is the same as layer normalization, and G=CG = C (one channel per group) is the same as instance normalization. Group Normalization bridges the two.

The implementation takes only a few lines: reshape the tensor to [N,G,C/G,H,W][N, G, C/G, H, W], compute the mean and variance over the last three axes, reshape back, and apply γ\gamma and β\beta (Figure 3 of the paper).

Why Group Channels Together?

The paper reasons that channels are not independent of one another; there are clusters of channels that behave similarly.

  • Classic image features were normalized in groups. SIFT and HOG concatenate histograms of gradients by orientation, and each histogram is normalized separately.
  • CNN channels can also be expected to form clusters. For example, if the first convolutional layer learns a filter and its horizontally flipped counterpart, the distributions of the two channels' responses to natural images should be similar. The paper notes that even in deeper layers, clusters of channels with similar distributions can form based on frequency, shape, brightness, texture, and so on.

This idea also explains the weaknesses of layer normalization and instance normalization.

  • Layer normalization assumes that all channels in a layer share the same mean and variance. In convolutional networks, this assumption is too strong. Group Normalization allows a different distribution for each group, giving it more representational power.
  • Instance normalization computes the mean and variance from the spatial values of a single channel only, so it cannot exploit relationships between channels.

As an analogy, layer normalization "adjusts everyone's scores in the class by a single standard", instance normalization "adjusts each student using only their own scores", and Group Normalization "adjusts scores together by groups of similar subjects".

Experimental Results

With sufficient batches: validation error rates were compared on ResNet-50, ImageNet, with 32 images per GPU (Table 1 of the paper).

Batch normalizationLayer normalizationInstance normalizationGroup Normalization
Error rate23.6%25.3%28.4%24.1%
Difference from batch normalization—+1.7+4.8+0.5

Group Normalization had a lower training error than batch normalization but a slightly higher validation error. The paper attributes this to Group Normalization lacking the regularization effect that comes from the randomness of mini-batch composition.

With small batches: error rates for different batch sizes per GPU (Table 2 of the paper).

Batch size3216842
Batch normalization23.6%23.7%24.8%27.3%34.7%
Group Normalization24.1%24.2%24.0%24.2%24.1%

Batch normalization degrades sharply at batch sizes of 8 or less, while Group Normalization barely changes. With a batch of 4, Batch Renormalization reached 26.3%, better than batch normalization (27.3%) but short of Group Normalization (24.2%).

How to group: varying the number of groups GG from 1 to 64 gave reasonably good results throughout, with G=32G = 32 the best (24.1%) and G=1G = 1 (the same as layer normalization) the worst (25.3%). When the number of channels per group was fixed instead, 16 channels was best (24.2%), and 1 channel (the same as instance normalization) gave 28.4%. Grouping just 2 channels together already lowered the error to 25.6%, confirming the benefit of grouping channels (Table 3 of the paper).

Object detection and video:

  • On COCO object detection and segmentation (Mask R-CNN, ResNet-50 with FPN, 1 image per GPU), compared with batch normalization with frozen statistics, Group Normalization raised box AP from 38.6 to 40.3 and mask AP from 34.5 to 35.7 (Table 6 of the paper). Even training from scratch without pretraining, ResNet-101 reached a box AP of 41.0 and a mask AP of 36.4.
  • On Kinetics video classification (I3D with ResNet-50, 64 frames, 4 clips per GPU), top-1 accuracy was 74.5% for Group Normalization versus 73.3% for batch normalization (Table 8 of the paper).

Practical Considerations

The number of channels must be divisible by the number of groups. PyTorch's nn.GroupNorm(num_groups, num_channels) raises an error if num_channels is not divisible by num_groups. For layers whose channel count is not divisible by 32 (e.g., 48 channels), choose a divisible number per layer, such as 16 groups.

If large batches are available, batch normalization can be slightly better. In the paper, too, with a batch of 32 the error rate was 0.5 points higher than batch normalization. Group Normalization shines when batches have to be small.

Also keep the following points in mind.

  • The computation is the same during training and inference. Group Normalization computes the mean and variance from the current input each time and keeps no running averages. The output does not change between model.train() and model.eval(), and it can be used as is even when the batch size changes from pretraining to fine-tuning.
  • For fine-tuning, removing weight decay on γ\gamma and β\beta can help. In the paper's object detection experiments, setting the weight decay of γ\gamma and β\beta to 0 was important for good results.
  • Hyperparameters are often tuned for batch normalization. The paper also notes that existing models and training settings were designed around batch normalization and may not be optimal for Group Normalization. After switching, revisit the learning rate and training length (in the paper's object detection, extending training from 180k to 270k iterations improved accuracy further).

Comparison with Other Normalization Methods

AspectGroup NormalizationBatch normalizationLayer normalizationInstance normalization
Range pooled for mean and varianceOne sample, group of channels × spaceOne channel, batch × spaceOne sample, all channels × spaceOne sample, spatial extent of one channel
Effect of batch sizeNot affectedAffected (worse when small)Not affectedNot affected
Training vs. inferenceSame computationComputation differs (running averages)Same computationSame computation
ImageNet error rate (ResNet-50, batch 32)24.1%23.6%25.3%28.4%
Extra hyperparametersNumber of groups GGNone (moving-average coefficient)NoneNone

Rough guidelines for choosing:

  • CNNs where enough images fit on one GPU (roughly 16 or more): batch normalization.
  • CNNs where batches become small, as in object detection, segmentation, and video: Group Normalization (start with G=32G = 32).
  • You don't want the computation to differ between training and inference, or the batch size differs between pretraining and fine-tuning: Group Normalization.
  • Transformers and RNNs: layer normalization or RMSNorm.

Note that Weight Normalization takes a different approach, normalizing filter weights rather than values. In the paper's ImageNet experiment, it reached 28.2%.

PyTorch example

Show code
python
import torch
import torch.nn as nn

# Place after the convolution. num_groups = G, num_channels = C (C must be divisible by G)
block = nn.Sequential(
    nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
    nn.GroupNorm(num_groups=32, num_channels=64),   # 2 channels per group. eps=1e-5
    nn.ReLU(),
)

x = torch.randn(2, 3, 32, 32)            # statistics stay stable even with a batch of 2

# Same computation in training and inference (no running averages)
block.train(); y_train = block(x)
block.eval();  y_eval = block(x)
print(torch.allclose(y_train, y_eval))   # True

# The code from Figure 3 of the paper, written in PyTorch
def group_norm(x, gamma, beta, G, eps=1e-5):
    N, C, H, W = x.shape
    x = x.view(N, G, C // G, H, W)                        # split the channels into G groups
    mean = x.mean(dim=(2, 3, 4), keepdim=True)            # per-group mean
    var = x.var(dim=(2, 3, 4), unbiased=False, keepdim=True)
    x = (x - mean) / torch.sqrt(var + eps)
    x = x.view(N, C, H, W)
    return x * gamma.view(1, C, 1, 1) + beta.view(1, C, 1, 1)   # γ and β are per channel

z = torch.randn(4, 6, 5, 5)
gn = nn.GroupNorm(2, 6)
print(torch.allclose(group_norm(z, gn.weight, gn.bias, G=2), gn(z), atol=1e-5))   # True

# G = 1 gives the same computation as layer normalization (normalizing over C, H, W); G = C gives instance normalization

FAQ

Q. How many groups G should be used?
A.

Starting with the paper's default of 32 is a safe choice. In the ResNet-50 experiments, varying G from 1 to 64 made little difference, and 32 was the best. For layers whose channel count is not divisible by 32, use a number that divides it (such as 16). Another option is to set the number of channels per group; in the paper, 16 channels per group was best.

Q. Does Group Normalization behave differently under model.eval()?
A.

No. It computes the mean and variance from the groups of the current input each time and keeps no running averages. Unlike batch normalization, results will not break down because you forgot to call model.eval() before inference (though any dropout or batch normalization elsewhere in the model will still switch modes).

Q. If batches are small, can't synchronized batch normalization, which pools statistics across GPUs, be used instead?
A.

It can. However, the paper points out that synchronized batch normalization requires more GPUs to gather the number of images batch normalization needs, which merely shifts the problem onto computational resources. Group Normalization works entirely within one GPU, so it is not tied to the number of GPUs or the batch size.

References

  1. Y. Wu and K. He, "Group Normalization," ECCV, 2018.
  2. S. Ioffe and C. Szegedy, "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift," ICML, 2015.
  3. PyTorch Documentation, "torch.nn.GroupNorm."