'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
now how to just the output from layer ?
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 resnet50 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use
get_output_at(node_index)
instead. add Codeadd Markdown
Solution 1:[1]
Please try again using tf.keras
to import model and layers.
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:
resnet50
(1, 2048)
(2048,)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TFer2 |