在预测期间从KERAS/TensorFlow获取中间输出 [英] get intermediate output from Keras/Tensorflow during prediction
本文介绍了在预测期间从KERAS/TensorFlow获取中间输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我加载了INSITION,并且我需要在分类之前提取最终的描述符。 因此,给出如下的简单代码:
cnn = InceptionV3(weights='imagenet',
include_top='False',
pooling='avg')
cnn.predict(x, batch_size=32, verbose=0)
如何在预测期间提取最后一层?
推荐答案
查找需要获取结果的层的名称或索引,并根据该层的输出创建新模型。
仅输出该层的模型:
earlyPredictor = Model(cnn.inputs, cnn.layers[theIndexYouWant].outputs)
#alternatively
earlyPredictor = Model(cnn.inputs, cnn.get_layer(theNameYouWant).outputs)
同时输出最终输出和所需层的模型:
fullPredictor = Model(cnn.inputs, [cnn.output, cnn.layers[index].output])
仅当层或模型具有多个输出时,才会显示使用"输出"和"输出"之间的差异。如果CNN输出和特定层输出是多个,则第二个示例需要格外小心地连接结果列表。
这篇关于在预测期间从KERAS/TensorFlow获取中间输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文