Search

Search papers and lessons

Instance Normalization

Level 4 · 11 min read

Instance normalization standardises each channel of each image on its own, removing that image's average brightness and contrast, so a network can treat a picture's style separately from its content.

On this page

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

TL;DR

Instance normalization rescales each channel of each image so that its values have mean 0 and variance 1, using only that one image's numbers. Because it ignores the rest of the batch, it removes an image's overall brightness and contrast and keeps its layout. Image-generation networks use it so that the look of an image can be controlled separately from its content.

Builds on: Batch Normalization, Convolutional Neural Network · Leads to: FiLM, Group Normalization

Where it's used

Fast artistic style transfer. In 2016 Ulyanov, Vedaldi and Lempitsky trained feed-forward networks that repaint a photo in the style of a painting in a single pass. They found that replacing batch normalization with instance normalization in the generator gave clearly better stylised images, because each photo's contrast was no longer carried into the next one through the batch. (The generator is a Convolutional Neural Network that maps one image to another.)

Learned styles. Dumoulin and colleagues (2016) gave a network one learned scale and shift per style at each instance-normalization layer. A single network could then produce many styles, one for each set of parameters. Huang and Belongie (2017), in adaptive instance normalization, went further: they took the channel means and spreads of a style image and used them as the scale and shift for a content image, which allows styles the network never saw during training.

Conditioned generation. Image generators and conditional models use the same idea to let a second input steer the features. Feature-wise modulation, which predicts a scale and shift per feature map from a question or a label, is built on the same per-channel rescaling.

How it works

Take one grey image with a single channel and only two pixels, in a row: the values are 22 and 66. A second image, also one channel with two pixels, has values 1010 and 3030. Together they form a batch of two images. The goal is to standardise each image on its own.

  1. Compute the mean of the first image. (2+6)/2=4(2 + 6) / 2 = 4. Check: 2+6=82 + 6 = 8 and 8/2=48 / 2 = 4.

  2. Compute the variance of the first image. The deviations from the mean are −2-2 and +2+2, their squares are 44 and 44, and the average is 44. Check: (4+4)/2=4(4 + 4) / 2 = 4, so the standard deviation is 22.

  3. Standardise the first image. Subtract the mean and divide by the standard deviation: (2−4)/2=−1(2 - 4) / 2 = -1 and (6−4)/2=1(6 - 4) / 2 = 1. Check: the two outputs are −1-1 and 11, so their mean is 00 and their variance is 11.

  4. Repeat for the second image, using only its own numbers. Its mean is 2020, its deviations are ±10\pm 10, its variance is 100100 and its standard deviation is 1010. The outputs are (10−20)/10=−1(10 - 20) / 10 = -1 and (30−20)/10=1(30 - 20) / 10 = 1. Check: these match step 3 exactly, even though the second image was five times more contrasted.

  5. Apply the learned scale γ\gamma and shift β\beta for the channel. Suppose γ=0.5\gamma = 0.5 and β=3\beta = 3. The first image becomes 0.5×(−1)+3=2.50.5 \times (-1) + 3 = 2.5 and 0.5×1+3=3.50.5 \times 1 + 3 = 3.5. Check: the spread is now 11 instead of 22, and the centre is at 33.

flowchart LR
  A["Input: one image, one channel"] --> B["Mean and variance over this image's pixels only"]
  B --> C["Standardise each pixel"]
  C --> D["Scale by gamma and shift by beta, per channel"]
  D --> E["Output: same layout, normalised contrast"]

The math

Definition: Instance normalization

Let xx be a feature tensor with shape N×C×H×WN \times C \times H \times W, where NN is the number of images in the batch, CC the number of channels (feature maps), and H×WH \times W the spatial size. For each image nn and channel cc, instance normalization computes

μnc=1HW∑h=1H∑w=1Wxnchwσnc2=1HW∑h=1H∑w=1W(xnchw−μnc)2ynchw=γc xnchw−μncσnc2+ϵ+βc\begin{aligned} \mu_{nc} &= \frac{1}{HW} \sum_{h=1}^{H} \sum_{w=1}^{W} x_{nchw} \\ \sigma_{nc}^2 &= \frac{1}{HW} \sum_{h=1}^{H} \sum_{w=1}^{W} (x_{nchw} - \mu_{nc})^2 \\ y_{nchw} &= \gamma_c \, \frac{x_{nchw} - \mu_{nc}}{\sqrt{\sigma_{nc}^2 + \epsilon}} + \beta_c \end{aligned}

Here ϵ\epsilon is a small constant, such as 10−510^{-5}, that keeps the division safe. The parameters γc\gamma_c and βc\beta_c are one scale and one shift per channel, shared by all images in the batch.

Steps 1 and 2 in symbols: the statistics

The mean μnc\mu_{nc} and variance σnc2\sigma_{nc}^2 are computed over the HWHW pixels of one channel of one image. Mean and Variance explains these two quantities. The average runs over space only, and never over the batch index nn. This is the entire difference from Batch Normalization, which computes one mean and variance per channel over the whole batch and all positions, N×H×WN \times H \times W values.

Check with the worked example: H=1H = 1, W=2W = 2, so HW=2HW = 2, and μ11=(2+6)/2=4\mu_{11} = (2 + 6) / 2 = 4.

Step 3 in symbols: standardising

The fraction (x−μ)/σ2+ϵ(x - \mu) / \sqrt{\sigma^2 + \epsilon} is the standard score of each pixel: how many standard deviations it sits above or below its image's average. Because ϵ\epsilon is tiny compared with a typical variance, it is close to (x−μ)/σ(x - \mu) / \sigma. For a constant channel, where σ=0\sigma = 0, the numerator is zero and the output is 00.

Step 5 in symbols: the learned scale and shift

The factor γc\gamma_c rescales the standardised values and βc\beta_c moves their centre. Without them, the layer could only output values with mean 0 and variance 1, which may not suit the next layer. With them, the network can learn to keep or undo the normalisation. The parameters are learned by Backpropagation along with the Convolution weights.

Notice what is missing from the formula: the batch index nn appears only in the indexing of μ\mu and σ\sigma, never in an average across images. A batch of size one behaves exactly like a batch of thirty-two.

Worked examples

Example
1. The two-pixel image

Use the image with values 22 and 66, γ=1\gamma = 1, β=0\beta = 0.

  1. Mean: (2+6)/2=4(2 + 6) / 2 = 4.
  2. Variance: ((2−4)2+(6−4)2)/2=(4+4)/2=4((2 - 4)^2 + (6 - 4)^2) / 2 = (4 + 4) / 2 = 4.
  3. Standard deviation, ignoring ϵ\epsilon: 4=2\sqrt{4} = 2.
  4. Output for 22: (2−4)/2=−1(2 - 4) / 2 = -1.
  5. Output for 66: (6−4)/2=1(6 - 4) / 2 = 1.
  6. Interpretation: the image is now centred on zero, with its contrast set to a fixed size. Both pixels keep their order: the darker one is still negative.
Example
2. Two channels, one constant

One image with two channels, each 2×22 \times 2. Channel 1 has pixels 1,3,5,71, 3, 5, 7. Channel 2 has all four pixels equal to 44. Use γ=1\gamma = 1, β=0\beta = 0 and ignore ϵ\epsilon.

  1. Channel 1 mean: (1+3+5+7)/4=4(1 + 3 + 5 + 7) / 4 = 4. Check: the sum is 1616, and 16/4=416 / 4 = 4.
  2. Channel 1 deviations: −3,−1,1,3-3, -1, 1, 3. Squares: 9,1,1,99, 1, 1, 9, sum 2020.
  3. Channel 1 variance: 20/4=520 / 4 = 5, standard deviation 5≈2.236\sqrt{5} \approx 2.236.
  4. Channel 1 outputs: −3/2.236≈−1.342-3 / 2.236 \approx -1.342, −1/2.236≈−0.447-1 / 2.236 \approx -0.447, 1/2.236≈0.4471 / 2.236 \approx 0.447, 3/2.236≈1.3423 / 2.236 \approx 1.342.
  5. Channel 2 mean is 44, and every deviation is 00, so its variance is 00.
  6. Channel 2 outputs: 0/0+ϵ=00 / \sqrt{0 + \epsilon} = 0 for all four pixels.
  7. Interpretation: the channel with variation is spread to a standard size, and the flat channel becomes all zeros. Each channel is treated on its own, so one channel's contrast does not leak into another's.
Example
3. Restyling a region's contrast, with a try-it

A style-transfer network has one channel of a content image, whose pixels average 120120 with standard deviation 4040. A style image's channel averages 9090 with standard deviation 2525. Adaptive instance normalization maps the content channel onto the style channel's statistics.

  1. Standardise a content pixel with value 160160: (160−120)/40=1(160 - 120) / 40 = 1. Check: 160−120=40160 - 120 = 40, and 40/40=140 / 40 = 1.
  2. Scale to the style spread: 1×25=251 \times 25 = 25.
  3. Shift to the style mean: 25+90=11525 + 90 = 115.
  4. Interpretation: a pixel one standard deviation above the content average becomes one standard deviation above the style average. The content's layout is kept, but its brightness and spread now follow the style.
  5. Try it: a content pixel has value 100100, and the content channel's statistics are the same as above (mean 120120, standard deviation 4040), with the style channel's statistics also the same (mean 9090, standard deviation 2525). What is its output?

Answer: standardise to (100−120)/40=−0.5(100 - 120) / 40 = -0.5, scale to −0.5×25=−12.5-0.5 \times 25 = -12.5, then shift to −12.5+90=77.5-12.5 + 90 = 77.5.

Common mistakes and tradeoffs

It is tempting to think instance normalization is batch normalization applied to one image. In fact the statistics run over pixels within one channel of one image, and they are never shared across images. That is why it works with batch size one, and why it does not need running averages at test time.

It is tempting to think instance normalization is always better for images. In fact it removes each image's average contrast and brightness, and that information is sometimes what a task needs. Classification networks, which decide by the presence of patterns rather than their intensity, usually do well with batch normalization, and removing contrast can hurt them. Instance normalization fits generation and style tasks, where overall look is the thing to control.

It is tempting to think γ\gamma and β\beta are different for every image. In fact they are learned once per channel and shared by all images. The per-image part comes from μ\mu and σ\sigma, which are recomputed from each image's own pixels.

When to use it: in image-to-image generation and style transfer, where the statistics of each feature map carry the style and should be replaced. When not to: for classification, detection, or any setting where the absolute brightness or contrast of an image is informative. Alternatives include Layer Normalization, which normalises across all channels of one example, and Group Normalization, which normalises over groups of channels and is a common choice when batches are small.

History

Before 2016, the standard recipe for fast image-to-image generation used batch normalization, which had been introduced to speed up training of classifiers. Its batch statistics mix images together, and the outputs for one image depended on the others it was processed with.

In 2016, Ulyanov, Vedaldi and Lempitsky showed that a feed-forward style-transfer network improved markedly when the batch statistics were replaced by per-image ones. The same year, Dumoulin, Shlens and Kudlur used per-style learned scales and shifts inside the normalisation step, so one network could produce many styles.

In 2017, Huang and Belongie introduced adaptive instance normalization, which takes the style image's channel statistics as the scale and shift and allows arbitrary styles at test time. Wu and He's group normalization (2018) then gave a version that does not depend on the batch and works well with small batches.

Recap

  • Instance normalization standardises each channel of each image using only that image's pixels.
  • The formula is the batch normalization formula with the average taken over space only, per image and per channel.
  • A learned scale and shift per channel restore the flexibility the network needs.
  • It removes per-image contrast and brightness, which suits style and generation tasks and can hurt classification.
  • Read next: FiLM, which predicts the scale and shift from a second input, and Group Normalization.

Check yourself

QuestionInstance normalization and batch normalization both subtract a mean and divide by a standard deviation. What is the key difference in which values are averaged?Show answer

Batch normalization averages each channel over every image in the batch and every pixel, while instance normalization averages each channel over the pixels of a single image only. So instance normalization's output for one image does not change when other images are added to the batch.

QuestionA channel of one image has pixels 3 and 7, and the scale and shift are 1 and 0. What does instance normalization output for the pixel 7? Ignore epsilon.Show answer

The mean is (3+7)/2=5(3 + 7) / 2 = 5, and the deviations are ±2\pm 2, so the variance is (4+4)/2=4(4 + 4) / 2 = 4 and the standard deviation is 22. The output for 7 is (7−5)/2=1(7 - 5) / 2 = 1.

QuestionWhy is instance normalization a poor choice for a classifier that must tell a bright cat from a dim cat?Show answer

Because it removes each image's average brightness and contrast, so the bright and dim versions of the same pattern look the same to the next layer. The classifier needs that information, so the normalisation throws away useful signal.

QuestionIn a style-transfer network, the content channel has mean 50 and standard deviation 10, and the style channel has mean 80 and standard deviation 5. What is the output for a content pixel of value 60?Show answer

Standardise: (60−50)/10=1(60 - 50) / 10 = 1. Scale: 1×5=51 \times 5 = 5. Shift: 5+80=855 + 80 = 85. The output is 85.

QuestionA researcher runs the same generator on a batch of 1 image and on a batch of 16 images, and the outputs for that image differ. The layer is instance normalization. What is wrong?Show answer

Nothing should differ, because instance normalization computes its statistics per image and per channel. A difference would suggest that another part of the network, such as a batch-dependent layer, is still in use, or that the test-time mode differs from the training mode. Instance normalization by itself gives the same output for an image whatever the batch size.

Sources