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 and . A second image, also one channel with two pixels, has values and . Together they form a batch of two images. The goal is to standardise each image on its own.
-
Compute the mean of the first image. . Check: and .
-
Compute the variance of the first image. The deviations from the mean are and , their squares are and , and the average is . Check: , so the standard deviation is .
-
Standardise the first image. Subtract the mean and divide by the standard deviation: and . Check: the two outputs are and , so their mean is and their variance is .
-
Repeat for the second image, using only its own numbers. Its mean is , its deviations are , its variance is and its standard deviation is . The outputs are and . Check: these match step 3 exactly, even though the second image was five times more contrasted.
-
Apply the learned scale and shift for the channel. Suppose and . The first image becomes and . Check: the spread is now instead of , and the centre is at .
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
Let be a feature tensor with shape , where is the number of images in the batch, the number of channels (feature maps), and the spatial size. For each image and channel , instance normalization computes
Here is a small constant, such as , that keeps the division safe. The parameters and 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 and variance are computed over the 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 . This is the entire difference from Batch Normalization, which computes one mean and variance per channel over the whole batch and all positions, values.
Check with the worked example: , , so , and .
Step 3 in symbols: standardising
The fraction is the standard score of each pixel: how many standard deviations it sits above or below its image's average. Because is tiny compared with a typical variance, it is close to . For a constant channel, where , the numerator is zero and the output is .
Step 5 in symbols: the learned scale and shift
The factor rescales the standardised values and 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 appears only in the indexing of and , never in an average across images. A batch of size one behaves exactly like a batch of thirty-two.
Worked examples
Use the image with values and , , .
- Mean: .
- Variance: .
- Standard deviation, ignoring : .
- Output for : .
- Output for : .
- 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.
One image with two channels, each . Channel 1 has pixels . Channel 2 has all four pixels equal to . Use , and ignore .
- Channel 1 mean: . Check: the sum is , and .
- Channel 1 deviations: . Squares: , sum .
- Channel 1 variance: , standard deviation .
- Channel 1 outputs: , , , .
- Channel 2 mean is , and every deviation is , so its variance is .
- Channel 2 outputs: for all four pixels.
- 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.
A style-transfer network has one channel of a content image, whose pixels average with standard deviation . A style image's channel averages with standard deviation . Adaptive instance normalization maps the content channel onto the style channel's statistics.
- Standardise a content pixel with value : . Check: , and .
- Scale to the style spread: .
- Shift to the style mean: .
- 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.
- Try it: a content pixel has value , and the content channel's statistics are the same as above (mean , standard deviation ), with the style channel's statistics also the same (mean , standard deviation ). What is its output?
Answer: standardise to , scale to , then shift to .
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 and are different for every image. In fact they are learned once per channel and shared by all images. The per-image part comes from and , 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 , and the deviations are , so the variance is and the standard deviation is . The output for 7 is .
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: . Scale: . Shift: . 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.