简体   繁体   中英

How to save the output from a layer of a neural network in keras

I have a InceptionV3 model built in Keras.

cnn_model = InceptionV3(weights='imagenet', include_top=False)


#Adding custom layers to the model (output layer)
x = cnn_model.output
x = Flatten()(x)
x = Dense(units=1024, activation='relu')(x)
x = Dropout(0.25)(x)
x = Dense(2048, activation='relu')(x)
predictions = Dense(units=4, activation='softmax')(x)

#Creating the predictor model
predictor_model = Model(input=cnn_model.input, output=predictions)
return predictor_model

I need to save the output from the final pool layer and convert these output (features) into a sequence to process that sequence in an LSTM layer. Actually i'm working with frames (of a video, of course), but I still don't know how to do this.

So, to confirm, i need to:

  1. Save the output from the final pool layer of the network
  2. Convert this output in a sequence to be processed in an LSTM layer.

Thanks a lot for the support!

I found out the answer!

To extract the output from the final pooling layer in a Keras InceptionV3 module you have just to:

output = cnn_model.get_layer('avg_pool').output

You can check the name of the layers and more module informations using this snippet:

from keras.applications.inception_v3 import InceptionV3
model = InceptionV3()
model.summary()

If you use the InceptionV3 module, you'll get this:

avg_pool (GlobalAveragePooling2 (None, 2048)         0           mixed10[0][0]

Best,

Arthur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM