【问题标题】:How can I get the output out of a tensor object in keras?如何从 keras 中的张量对象中获取输出?
【发布时间】:2017-06-16 07:03:37
【问题描述】:

我正在使用 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)

非常感谢任何帮助。谢谢。

【问题讨论】:

标签: tensorflow neural-network theano keras conv-neural-network


【解决方案1】:

您需要评估张量,这可能最好通过将模型配置为在您运行预测时返回它们来完成。

例如

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

【讨论】:

  • 非常感谢您的回答 - 我只需将 input=Input(shape=(224,224,3)) 更改为 input=model.input 否则会引发错误。希望你不介意我在你的回答中也改变它。再次,谢谢你:)
猜你喜欢
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 2020-08-14
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
相关资源
最近更新 更多