Search code examples
keraskeras-layer

How to get keras layer's output in a multiple-input model?


When a Keras model accept multiple inputs, its layers behave like there is just one input. It might be a bug.

model = vgg19.VGG19(weights='imagenet', include_top=False, pooling='avg')
model(image1)
model(image2)

model.get_output_at(0)
model.get_output_at(1)
#no error here

outputs_0 = [layer.get_output_at(0) for layer in model.layers]
#no error here

outputs_1 = [layer.get_output_at(1) for layer in model.layers]
#error "Asked to get output at node 1, but the layer has only 1 inbound nodes."

I'm really not sure about what is outputs_0, since model have two inputs, image1 and image2, and when a layer return its output, what is its corresponding input?


Solution

  • Regardless of the model's inputs and outputs, there is no rule about how the layers behave inside a model. A model may have many internal branches and reuse (or not) the same layer with different inputs, yielding thus different outputs. A layer will only have "output at 1 (or more)" if that layer was used more than once.

    The only certain things are:

    • the input layers will match the model's input (see 1),
    • and the output layers will match the model's output (see 1).
    • But anything is possible in between (see 2).

    (1) - But, a model that has many inputs/outputs actually has many "input/output layers". Each output layer has a single output. If you check the "model" outputs, you have many, but if you check the "layers" outputs, then there are several output layers, each yealding a single output (output at 0 only). The same is valid for model's inputs vs input layers.

    (2) - Even though, the most common option is to have layers being used only once, and thus having only "output at 0", without additional outputs.