Search

Search papers and lessons

Feature-Wise Linear Modulation (FiLM)

Level 3 · 13 min read

FiLM lets a second input, such as a question, control a network by predicting a scale and a shift for every feature map, so the same image features can be amplified or suppressed depending on the context.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • Sign in to see your progress

TL;DR

Many networks receive two kinds of input: the data they process, such as an image, and a context that says what to do with it, such as a question about that image. Feature-wise linear modulation, or FiLM, lets the context control the data. A small network reads the context and predicts a scale and a shift for every feature map inside the image network. Each feature map is then multiplied and offset by its numbers, so the same image features can be amplified in one context and suppressed in another.

Where you'll see it: in visual question answering, where one picture must be answered under many different questions, and in style transfer and conditional image generation, where a style or class is supplied as a second input.

Intuition

Start from a familiar situation

A sound engineer has one recording of an orchestra. For a classical broadcast she raises the strings and lowers the drums. For a podcast she pulls the bass down and lifts the voice. The recording is the same. What changes is one fader per track, set according to what the listener needs.

FiLM works the same way inside a network. The image features are the recording. The question sets a fader for each feature map.

Why the context needs to reach inside the network

A simple way to combine a question with an image is to feed both in at the input and let the network sort it out. That works poorly for a long chain of reasoning steps, because the question has to survive every layer and keep telling the image features what to look for. A conditioning signal works better when it acts directly on the internal features, at the layers where the features are being built.

Suppose the question is "is there a red object?" Somewhere inside the network there is a feature map that responds to red. If the question could simply turn that map up, the rest of the network would see a strong red signal and could answer. If the question were "is there a green object?", the same map should be turned down. That is exactly the control FiLM gives.

Turning a context into knobs

Each feature map is one channel of a Convolutional Neural Network: a grid of numbers that is high wherever a particular pattern is present. The question is first turned into a vector of numbers, for example by a Recurrent Neural Network such as a long short-term memory network reading the words one at a time. A linear map then takes that vector and outputs two numbers for every channel: a gain γ\gamma and an offset β\beta.

−4−3−2−101234−505activation of one featuremodulated activation
  • gamma 1, beta 0 (no change)
  • gamma 1.5, beta -1
  • gamma 0.5, beta 1
Each channel gets its own straight line. A gain above 1 amplifies the channel, a gain between 0 and 1 weakens it, and the offset moves it up or down.

The name says it all. The modulation is feature-wise, because each feature map has its own pair of numbers. It is linear, in the sense that it multiplies and adds, which is the same kind of straight-line rule as y=ax+by = ax + b.

One knob per channel, not per pixel

The gain and offset are the same at every position in a channel. A red detector is switched up across the whole image, not just in one corner. This keeps the weight sharing of the convolution: the network still looks for a pattern wherever it appears, and the question only decides how much that pattern matters.

flowchart LR
  Q[Question text] --> E[Encoder, such as an LSTM]
  E --> V[Question vector]
  V --> G[FiLM generator]
  G --> P[Gamma and beta for each channel]
  I[Image] --> C[Convolution]
  C --> BN[Batch normalization]
  BN --> F[FiLM layer]
  P --> F
  F --> R[ReLU and the next block]

The math

Setup

Take one layer of an image network. Its output is a stack of CC feature maps, each of height HH and width WW. Write Fc,i,jF_{c,i,j} for the value of channel cc at row ii and column jj. The context is a vector q∈Rd\mathbf{q} \in \mathbb{R}^{d}, for example the encoding of a question. The FiLM generator is trained together with the rest of the network.

The generator

The generator maps the context to one gain and one offset per channel:

γ=Wγq+bγ,β=Wβq+bβ.\boldsymbol{\gamma} = W_\gamma \mathbf{q} + \mathbf{b}_\gamma, \qquad \boldsymbol{\beta} = W_\beta \mathbf{q} + \mathbf{b}_\beta.

Here WγW_\gamma and WβW_\beta are matrices of size C×dC \times d, so that each one multiplies the vector q\mathbf{q} and returns a vector of length CC. The biases bγ\mathbf{b}_\gamma and bβ\mathbf{b}_\beta are also vectors of length CC. The entry γc\gamma_c is the gain for channel cc and βc\beta_c is its offset. This is a matrix times vector followed by a shift, which is a linear map plus a constant.

Definition: FiLM layer

Given a feature map Fc,i,jF_{c,i,j} and the gain γc\gamma_c and offset βc\beta_c for its channel, the FiLM layer returns

FiLM(Fc,i,j)=γc Fc,i,j+βc.\mathrm{FiLM}(F_{c,i,j}) = \gamma_c \, F_{c,i,j} + \beta_c.

The rule multiplies each value by its channel's gain and then adds its channel's offset. The map x↦γx+βx \mapsto \gamma x + \beta is an affine function: a straight line that need not pass through the origin. The layer has no weights of its own. All of its parameters come from the generator.

Where the layer sits

In the residual blocks used in the original paper, the order is: convolution, then batch normalization, then the FiLM layer, then Rectified Linear Unit (ReLU), and finally the Residual Connection adds the block's input back. Batch normalization has already put each channel on a standard scale. So the gain from the question acts on a predictable range of values, and a gain of 2 always means "double the typical response of this channel".

Starting from no change

If every gain is 1 and every offset is 0, the FiLM layer is the identity and does nothing. Generators are often initialised so that their outputs start close to these values. Training then only has to learn how far to move each channel away from neutral, which makes early training stable.

Cost of the generator

The generator adds parameters, and the cost grows with the context size and the number of channels. For context size d=128d = 128 and C=256C = 256 channels, each of WγW_\gamma and WβW_\beta has 128×256=32,768128 \times 256 = 32{,}768 weights, and each bias vector has 256 entries. One FiLM layer therefore has 2×(32,768+256)=66,0482 \times (32{,}768 + 256) = 66{,}048 parameters. Four such layers add up to 264,192264{,}192. That is small next to the image network, but it grows linearly with the number of blocks that are modulated.

Worked examples

Example
Tiny: one row of three values

One channel of a feature map contains the values 11, −2-2 and 44 at three positions. Use gain γ=2\gamma = 2 and offset β=0.5\beta = 0.5, with the same numbers at every position.

  1. Position 1: 2×1+0.5=2.52 \times 1 + 0.5 = 2.5.
  2. Position 2: 2×(−2)+0.5=−3.52 \times (-2) + 0.5 = -3.5.
  3. Position 3: 2×4+0.5=8.52 \times 4 + 0.5 = 8.5.
  4. Passing the result through Rectified Linear Unit (ReLU) (negative values become 0) gives 2.52.5, 00 and 8.58.5.

Interpretation: the layer doubled every value and moved it up by a half. Because the offset is added after the gain, a value of zero becomes 0.50.5, not zero. The ReLU then removes the negative response at position 2.

Example
Standard: two channels and a two-number context

A feature map has two channels. The context is q=(1,−1)\mathbf{q} = (1, -1). The generator has Wγ=(0.5000.5)W_\gamma = \begin{pmatrix} 0.5 & 0 \\ 0 & 0.5 \end{pmatrix} with bias bγ=(1,1)\mathbf{b}_\gamma = (1, 1), and Wβ=(0110)W_\beta = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} with bias bβ=(0,0)\mathbf{b}_\beta = (0, 0).

Step 1: the gains. γ=(0.5×1+1,  0.5×(−1)+1)=(1.5,  0.5)\boldsymbol{\gamma} = (0.5 \times 1 + 1, \; 0.5 \times (-1) + 1) = (1.5, \; 0.5).

Step 2: the offsets. β=(1×(−1)+0,  1×1+0)=(−1,  1)\boldsymbol{\beta} = (1 \times (-1) + 0, \; 1 \times 1 + 0) = (-1, \; 1).

Step 3: channel 1. Its values are (2402)\begin{pmatrix} 2 & 4 \\ 0 & 2 \end{pmatrix}. Apply 1.5x−11.5 x - 1 to each entry: 2→22 \to 2, 4→54 \to 5, 0→−10 \to -1, 2→22 \to 2. The result is (25−12)\begin{pmatrix} 2 & 5 \\ -1 & 2 \end{pmatrix}.

Step 4: channel 2. Its values are (−2026)\begin{pmatrix} -2 & 0 \\ 2 & 6 \end{pmatrix}. Apply 0.5x+10.5 x + 1: −2→0-2 \to 0, 0→10 \to 1, 2→22 \to 2, 6→46 \to 4. The result is (0124)\begin{pmatrix} 0 & 1 \\ 2 & 4 \end{pmatrix}.

Interpretation: the question amplified channel 1 (gain 1.5) and weakened channel 2 (gain 0.5). The same two feature maps would have come out differently for a different question, because only q\mathbf{q} changed.

Example
Realistic: a red detector across questions

Suppose one channel of a visual reasoning network responds to red colour. On a scene with a red sphere at three positions, its activations are [0.9,  0.1,  0.0][0.9, \; 0.1, \; 0.0]. The values are illustrative, not from a trained model.

  • Question: "is there a red object?" The generator produces gain γ=1.8\gamma = 1.8 and offset β=0\beta = 0. The modulated values are [1.62,  0.18,  0.00][1.62, \; 0.18, \; 0.00]. The red signal is strengthened, and the rest of the network sees a clear response.
  • Question: "is there a green object?" The generator produces gain γ=0.05\gamma = 0.05 for this channel. The modulated values are [0.045,  0.005,  0.000][0.045, \; 0.005, \; 0.000]. The red channel is almost silenced, so it cannot distract from the green answer.

Interpretation: nothing about the image changed between the two questions. Only one number per channel changed, and that was enough to shift the network's attention from one colour to the other. This is why FiLM is useful for question answering on a fixed picture.

Tradeoffs and failure modes

FiLM is cheap and simple, and it works best when the conditioning signal tells the network which features matter as a whole. It is weaker when the answer depends on where things are, because a channel gain cannot say "here but not there".

SituationProblemBetter choice
The answer depends on a location, such as "the object to the left of the cube"A gain is the same at every position, so FiLM cannot select a placeAttention Mechanism over spatial positions, or add coordinate channels to the image
The context is a long sentence with many stepsOne vector may not hold every detail, and a fixed generator may miss some of themA stronger encoder such as a Transformer before the generator
Only a few discrete conditions, such as a class labelA full generator is more than neededA lookup table of one gain and offset per class (the simplest form of conditional normalization)
Very many modulated layers with wide contextsThe generator's parameter count grows with channels times context sizeShare one generator across blocks, or use a low-rank form
A gain drifts towards 0That channel's output and its gradient both vanish, so it stops learningInitialise near identity, and monitor the distribution of the generated gains

Two practical points follow from the math. First, FiLM can only scale and shift what the network has already built. It cannot create a new kind of feature or mix channels together, so the image network must contain the relevant detectors. Second, the gain is multiplicative: a large gain on an already large activation can cause large values and unstable training, which is why normalization such as Batch Normalization or Layer Normalization is usually placed before the FiLM layer.

History

The idea of letting one input set the parameters of a normalization step appeared in several places around 2016 and 2017, each for a different task.

Vincent Dumoulin, Jonathon Shlens and Manjunath Kudlur (2016) trained a network to apply artistic styles. They gave each style its own learned scale and shift inside an instance-normalization step, so the style was a set of channel parameters. This was published as "A Learned Representation for Artistic Style".

Harm de Vries and colleagues (2017) used the same kind of step in a different setting. They used a language representation to set the scale and shift of batch normalization inside a visual network, so that the words of a question could change early visual processing. This was called conditional batch normalization.

Xun Huang and Serge Belongie (2017) introduced adaptive instance normalization for real-time style transfer. Their rule takes the channel means and spreads from a style image and uses them as the new scale and shift for the content image.

Ethan Perez and colleagues (2017, published at AAAI in 2018) gave the general form the name FiLM. They tested it on CLEVR, a synthetic benchmark of 3D shapes with compositional questions, where the questions require counting, comparing and locating objects. Their model reached high accuracy with a simple architecture, and the paper argued that one general layer could replace the separate methods. A 2018 article in Distill by Dumoulin and colleagues later placed these methods in one framework of feature-wise transformations. FiLM caught on because it is a single layer with a clear meaning: the context chooses how much each feature counts.

Recap

FiLM takes a context vector, maps it through a linear generator to a gain and an offset for every channel, and applies γcFc,i,j+βc\gamma_c F_{c,i,j} + \beta_c to each feature map. The gains and offsets are the same at every position, so the convolutional structure stays intact, and the context decides which features are emphasised. The method is cheap and fits well after Batch Normalization in a residual block. Its main limit is spatial: when the answer depends on where something is, it needs help from attention. It is a natural next step after convolutional networks and word embeddings for multimodal models such as vision-language models.

Check yourself

QuestionIn FiLM, what does each feature map receive from the context, and does the same pair of numbers apply to every position in the channel?Show answer

Each feature map receives a gain γc\gamma_c and an offset βc\beta_c, produced by the generator from the context vector. The same pair applies to every position in channel cc. The layer computes γcFc,i,j+βc\gamma_c F_{c,i,j} + \beta_c at each position, so the context changes how strongly each feature is used, not where it is looked for.

QuestionA channel has the values 3, -1 and 0.5. The gain is -2 and the offset is 1. What are the outputs?Show answer

Apply −2x+1-2x + 1 to each value. For 33: −6+1=−5-6 + 1 = -5. For −1-1: 2+1=32 + 1 = 3. For 0.50.5: −1+1=0-1 + 1 = 0. The outputs are −5-5, 33 and 00.

QuestionWhy does FiLM use one gain and one offset per channel, instead of letting the context produce a full new feature map?Show answer

A full map would need one number for every channel, row and column, which is far more parameters and would need the generator to output a large grid. It would also throw away the weight sharing of the convolution. A per-channel gain and offset is small, keeps the spatial structure of the features, and lets the context choose which features matter, which is the decision that most questions need.

QuestionA model must decide whether a road or a river is present at a given location in a satellite image, and the question says which one to look for. Would FiLM be enough on its own?Show answer

FiLM can strengthen the road-detecting channels for a road question and the river-detecting channels for a river question, which is a good start. But the answer is about location, and a channel gain is the same at every position. If the model must say where the road is, or distinguish a road next to a river, it would need spatial attention or position information as well, and the road and river channels would have to be separate enough for the gains to tell them apart.

Sources