Abdelrahman Hussein
Abdelrahman Hussein

Reputation: 31

Get Each Layer Output in Keras Model for a Single Image

I would like to know how to get the output of each layer of a pre-trained CNN Keras model. What I am working on is to get the intermediate outputs of each layer in the model associated with a specific image I am providing to the model. Here is what I did:

model = load_model('model.h5')
img = Image.open('img.jpg')
img_array = np.array (img)
img_array = img_array/255
img_array = img_array.reshape(-1,512,512,1)
pred = model.predict(img_array)

I am just so confused about what to do next to print the output of each layer in such a case!

Upvotes: 0

Views: 3009

Answers (1)

Li-Pin Juan
Li-Pin Juan

Reputation: 1245

The outputs from layers could be collected by following the steps below:

from keras import backend as K 

model = load_model('model.h5')

inp = model.input                                         # input placeholder
out = [layer.output for layer in model.layers]            # all layer outputs
get_outputs = K.function([inp, K.learning_phase()], out)   

img = load_img('img.jpg')
x = img_to_array(img)
x = x.reshape((1,) + x.shape) 
x /= 255.
layer_outs = get_outputs([x, 1.])                                                
print(layer_outs)

The intermediate representation of the input image img.jpg could be replicated by running the following code snippet:

from tensorflow.keras.preprocessing.image import img_to_array, load_img

model = load_model('model.h5')

# Define a new Model that will take an image as input, and will output 
# intermediate representations for all layers except the first layer.
layer_outputs = [layer.output for layer in model.layers[1:]]
visual_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)

# Read your image
img = load_img('img.jpg')
x = img_to_array(img)
x = x.reshape((1,) + x.shape) # add one extra dimension to the front
x /= 255. # rescale by 1/255.

# run your image through the network; make a prediction
feature_maps = visual_model.predict(x)

# Plotting intermediate representations for your image

# Collect the names of each layer except the first one for plotting
layer_names = [layer.name for layer in model.layers[1:]]

# Plotting intermediate representation images layer by layer
for layer_name, feature_map in zip(layer_names, feature_maps):
    if len(feature_map.shape) == 4: # skip fully connected layers
        # number of features in an individual feature map
        n_features = feature_map.shape[-1]  
        # The feature map is in shape of (1, size, size, n_features)
        size = feature_map.shape[1] 
        # Tile our feature images in matrix `display_grid
        display_grid = np.zeros((size, size * n_features))
        # Fill out the matrix by looping over all the feature images of your image
        for i in range(n_features):
            # Postprocess each feature of the layer to make it pleasible to your eyes
            x = feature_map[0, :, :, i]
            x -= x.mean()
            x /= x.std()
            x *= 64
            x += 128
            x = np.clip(x, 0, 255).astype('uint8')
            # We'll tile each filter into this big horizontal grid
            display_grid[:, i * size : (i + 1) * size] = x
        # Display the grid
        scale = 20. / n_features
        plt.figure(figsize=(scale * n_features, scale))
        plt.title(layer_name)
        plt.grid(False)
        plt.imshow(display_grid, aspect='auto', cmap='viridis')

Upvotes: 1

Related Questions