def visualizeLayer(model, img, input_image, layerIndex):
layer = model.layers[layerIndex]
get_activations = K.function([model.layers[0].input, K.learning_phase()], [layer.output,])
activations = get_activations([input_image, 0])[0]
output_image = activations
## If 4 dimensional then take the last dimension value as it would be no of filters
if output_image.ndim == 4:
# Rearrange dimension so we can plot the result
o1 = np.rollaxis(output_image, 3, 1)
output_image = np.rollaxis(o1, 3, 1)
print "Dumping filter data of layer{} - {}".format(layerIndex,layer.__class__.__name__)
filters = len(output_image[0,0,0,:])
fig=plt.figure(figsize=(8,8))
# This loop will plot the 32 filter data for the input image
for i in range(filters):
ax = fig.add_subplot(6, 6, i+1)
#ax.imshow(output_image[img,:,:,i],interpolation='none' ) #to see the first filter
ax.imshow(output_image[0,:,:,i],'gray')
#ax.set_title("Feature map of layer#{} \ncalled '{}' \nof type {} ".format(layerIndex,
# layer.name,layer.__class__.__name__))
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout()
#plt.show()
fig.savefig("img_" + str(img) + "_layer" + str(layerIndex)+"_"+layer.__class__.__name__+".png")
#plt.close(fig)
else:
print "Can't dump data of this layer{}- {}".format(layerIndex, layer.__class__.__name__)
评论列表
文章目录