我正在使用Keras的预训练VGG16模型,我想要可视化每层的输出。但是,layer.output返回一个张量对象 - 如何将其转换为允许我获取图像输出的东西?
model = VGG16(weights='imagenet', include_top=True)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
layer1 = model.layers[1] #I want the output of the second layer
layer1.output #returns a tensor object
此外,当我尝试访问特定节点的输出时,它返回一个张量:
layer1.get_output_at(0)
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
您需要评估张量,这可能是最好的方法,通过配置模型以在运行预测时返回它们。
e.g。
layer_outputs = [layer.output for layer in model.layers]
viz_model = Model(input=model.input, output=layer_outputs)
...
features = viz_model.predict(x)
for feature_map in features:
...
另请查看此博客文章,其中介绍了与您尝试的内容类似的练习:https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html