How to take the intermediate Transfer learning output. ? Eg:
JavaScript
x
11
11
1
from keras.models import Sequential
2
from keras.layers import Dense
3
# ... Other Imports..
4
5
from tensorflow.keras.applications.resnet50 import ResNet50
6
model = Sequential()
7
resnet = ResNet50(include_top = False, pooling = 'avg', weights = 'imagenet')
8
model.add(resnet)
9
model.add(Dense(10, activation = 'softmax'))
10
model.layers[0].trainable = False
11
Tried:
JavaScript
1
4
1
layer_output=model.get_layer('resnet').output
2
layer_output=model.get_layer('resnet').output
3
intermediate_model=tf.keras.models.Model(inputs=model.input,outputs=layer_output)
4
Advertisement
Answer
There’s an unresolved issue in Tensorflow on this problem. According to the issue, you need to pass inputs of both outer model and inner model to get the output of inner model.
JavaScript
1
10
10
1
import numpy as np
2
3
layer_output = model.get_layer("resnet50").output
4
intermediate_model = tf.keras.models.Model(inputs=[model.input, resnet.input], outputs=[layer_output])
5
6
input_data = np.random.rand(1, 224, 224, 3)
7
result = intermediate_model.predict([input_data, input_data])
8
9
print(result[0].shape)
10
JavaScript
1
2
1
(7, 7, 2048)
2