[英]Intermediate Output of let' s say Resnet50 from Keras Model
import keras
print(keras.__version__)
#2.3.0
from keras.models import Sequential
from keras.layers import Input, Dense,TimeDistributed
from keras.models import Model
model = Sequential()
resnet = ResNet50(include_top = False, pooling = 'avg', weights = 'imagenet')
model.add(resnet)
model.add(Dense(10, activation = 'relu'))
model.add(Dense(6, activation = 'sigmoid'))
model.summary()
// Training // model.fit(.. ) done // 训练 // model.fit(..) 完成
now how to just the output from layer?现在如何从图层中提取 output?
model.layers[0]._name='resnet50'
print(model.layers[0].name) # prints resnet50
layer_output = model.get_layer("resnet50").output
intermediate_model = Model(inputs=[model.input, resnet.input], outputs=[layer_output])
result = intermediate_model.predict([x, x])
print(result.shape)
print(result[0].shape)
Got Error出错了
AttributeError: Layer re.net50 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. AttributeError:层 re.net50 有多个入站节点,因此“层输出”的概念定义不明确。 Use
get_output_at(node_index)
instead.请改用get_output_at(node_index)
。 add Codeadd Markdown添加代码添加 Markdown
Please try again using tf.keras
to import model and layers.请再次尝试使用tf.keras
导入 model 和图层。
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense,TimeDistributed
from tensorflow.keras.models import Model
and then run the same:然后运行相同的:
model.layers[0]._name='resnet50'
print(model.layers[0].name) # prints resnet50
layer_output = model.get_layer("resnet50").output
intermediate_model = Model(inputs=[model.input, resnet.input], outputs=[layer_output])
x = tf.ones((1, 250, 250, 3))
result = intermediate_model.predict([x, x])
print(result.shape)
print(result[0].shape)
Output: Output:
resnet50
(1, 2048)
(2048,)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.