Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get intermediate output from Keras/Tensorflow during prediction

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?

like image 327
D.Giunchi Avatar asked Feb 24 '18 18:02

D.Giunchi


1 Answers

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.

like image 98
Daniel Möller Avatar answered Nov 12 '22 00:11

Daniel Möller