I am able to get the output/predictions of all layers as suggested in 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)
But this is incredible slower and takes more than a minute (clocked at ~65s, in 6700HQ
with GTX1070
, that's ridiculously high, inference happens in less than a second...!) for models with about 50 layers. I guess this is because it is building a model every single time, load the model into the memory, pass inputs and get outputs. Clearly, you can't get the output of the last layer without getting results from the other layers, how do I save them all like above without having to create redundant models (or in a faster, more efficient way)?
Update: I also noticed that this does NOT utilize my GPU, this means all the conv layers are being executed by the CPU? Why wouldn't it use my GPU for this? I reckon it's gonna take way less if it used my GPU.
How do I do it more efficiently?