Search code examples
tensorflowneural-networkkerasextractdescriptor

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?


Solution

  • 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.