get intermediate output from Keras/Tensorflow during prediction
Asked Answered
M

1

5

Let's say I load inception, and I need to extract the final descriptor just before classification. So given a simple code like this:

cnn = InceptionV3(weights='imagenet',
              include_top='False',
              pooling='avg')
cnn.predict(x, batch_size=32, verbose=0)

How can I extract during prediction the last layer?

Mariselamarish answered 24/2, 2018 at 18:33 Comment(0)
J
11

Find out the name or the index of the layer you need to get the results for and make a new model considering the output of that layer.

A model that outputs only that layer:

earlyPredictor = Model(cnn.inputs, cnn.layers[theIndexYouWant].outputs)

#alternatively
earlyPredictor = Model(cnn.inputs, cnn.get_layer(theNameYouWant).outputs)    

A model that outputs both the final output and the desired layer:

fullPredictor = Model(cnn.inputs, [cnn.output, cnn.layers[index].output])  

The difference between using "output" and "outputs" only appear when a layer or model has more than one output. The second example would need extra care to concatenate the result lists if the cnn output and the specific layer output are multiple.

Jerrilyn answered 24/2, 2018 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.