Search code examples
keraskeras-layer

Using model.pop() changes the model's summary but does not effect the output


I am trying to remove the top layers from a model I have previously trained. This is the code I use:

import os
import h5py
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense

# KERAS_BACKEND=theano python
import keras
keras.backend.set_image_dim_ordering("th")
img_width, img_height = 150, 150
data_dir = '//shared_directory/projects/try_CD/data/validation'

nb_train_samples = 2000
nb_validation_samples = 800
nb_epoch = 50



def make_bottleneck_features(model):
    datagen = ImageDataGenerator(rescale=1./255)
    generator = datagen.flow_from_directory(
            data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode=None,
            shuffle=False)
    bottleneck_features = model.predict_generator(generator, nb_validation_samples)
    return (bottleneck_features)


model=keras.models.load_model('/shared_directory/projects/think_exp/CD_M1.h5')
A = make_bottleneck_features(model)
model.summary()    
for i in range (6):  
    model.pop()    

B = make_bottleneck_features(model)
model.summary() 

Judging comparing the results of the two calls to model.summary(), I can see that indeed the 6 topmost layers were removed.

However, the model's output (saved to A and B) does not change after discarding these layers.

What is the source of that discrepancy? How can I retrieve the output of the desired layer instead of that of the entire model?

Thanks in advance!


Solution

  • You can't drop layers like that, in order for it to have an effect, you need to recompile the model (AKA model.compile).

    But that's not the best way to obtain outputs from intermediate layers, you can just use K.function (where K is keras.backend) to build a function from the input to one of the layers and then call the function. More details are available in this answer.