matchifang
matchifang

Reputation: 5450

How to change 1D numpy array from keras layer output to image (3D numpy array)

I have the output or feature maps of a keras layer, but how can I convert that into images (3D numpy array) that I can display.

model = VGG16(weights='imagenet', include_top=True)
layer_outputs = [layer.output for layer in model.layers[1:]]
print layer_outputs
viz_model = Model(input=model.input,
                  output=layer_outputs)
features = viz_model.predict(x)

output = features[0] #has shape (1,224,224,64)

Any comment or suggestion is greatly appreciated. Thank you.

Upvotes: 3

Views: 969

Answers (1)

Simon
Simon

Reputation: 10150

You could add each feature map as a subplot while iterating over each one:

import numpy as np
import matplotlib.pyplot as plt
from pylab import cm

m = np.random.rand(1,224,224,64)

fig = plt.figure()
fig.suptitle("Feature Maps")

for j in range(m.shape[3]):
    ax = fig.add_subplot(8, 8, j+1)
    ax.matshow(m[0,:,:,j], cmap=cm.gray)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))

plt.show()

Which will give you something that looks like this (just noise in my case):

enter image description here

Upvotes: 3

Related Questions