【问题标题】:Keras/Tensorflow: Get predictions or output of all layers efficientlyKeras/Tensorflow:高效获取所有层的预测或输出
【发布时间】:2018-08-03 17:40:41
【问题描述】:

我能够按照Keras Docs: how-can-i-obtain-the-output-of-an-intermediate-layer 中的建议获得所有层的输出/预测

def get_output_of_all_layers(model, test_input):
    output_of_all_layers = []

    for count, layer in enumerate(model.layers):

        # skip the input layer
        if count == 0:
            continue

        intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer.name).output)
        intermediate_output = intermediate_layer_model.predict(test_input)[0]

        output_of_all_layers.append(intermediate_output)

    return np.array(output_of_all_layers)

但这慢得令人难以置信,需要一分钟以上(时钟为 ~65 秒,在 6700HQGTX1070 中,这高得离谱,推理发生在不到一秒... !) 对于大约 50 层的模型。我猜这是因为它每次都在构建模型,将模型加载到内存中,传递输入并获取输出。显然,如果不从其他层获取结果,就无法获得最后一层的输出,我如何像上面一样保存它们而无需创建冗余模型(或以更快、更有效的方式)?

更新:我还注意到这并没有使用我的 GPU,这意味着所有的卷积层都由 CPU 执行?为什么它不使用我的 GPU 呢?我认为如果它使用我的 GPU,它会花费更少的时间。

如何更有效地做到这一点?

【问题讨论】:

  • 我没有专门使用 keras,但是从一般的 tensorflow 角度来看,计算单个 intermediate_layer_model = Model(inputs=model.input, outputs=[layer.output for layer in model.layers[1:]]) 的输出怎么样?

标签: python tensorflow keras output layer


【解决方案1】:

按照 Ben Usman 的建议,您可以先将模型包装在基本的端到端 Model 中,然后将其层作为输出提供给第二个 Model

import keras.backend as K
from keras.models import Model
from keras.layers import Input, Dense

input_layer = Input((10,))

layer_1 = Dense(10)(input_layer)
layer_2 = Dense(20)(layer_1)
layer_3 = Dense(5)(layer_2)

output_layer = Dense(1)(layer_3)

basic_model = Model(inputs=input_layer, outputs=output_layer)

# some random input
import numpy as np
features = np.random.rand(100,10)

# With a second Model
intermediate_model = Model(inputs=basic_model.layers[0].input, 
                              outputs=[l.output for l in basic_model.layers[1:]])
intermediate_model.predict(features) # outputs a list of 4 arrays

或者,您可以以类似的方式使用 Keras 函数:

# With a Keras function
get_all_layer_outputs = K.function([basic_model.layers[0].input],
                                  [l.output for l in basic_model.layers[1:]])

layer_output = get_all_layer_outputs([features]) # return the same thing

【讨论】:

  • 问题中的代码:65s,中间模型(一个所有层作为输出的模型):1.21s,Keras 函数:0.06s -> 这就是我要找的 :)
  • 不错!有趣的是,Keras 函数的表现如此出色!
  • 它为什么运行得这么快? keras 函数背后有什么魔力吗?
  • 如何进一步将 layer_output 嵌入应用到 keras 模型?
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多