从 CNN Keras 中提取迁移学习输出
Extracting Transfer learning output from CNN Keras
如何获取中间迁移学习输出。 ?例如:
from keras.models import Sequential
from keras.layers import Dense
# ... Other Imports..
from tensorflow.keras.applications.resnet50 import ResNet50
model = Sequential()
resnet = ResNet50(include_top = False, pooling = 'avg', weights = 'imagenet')
model.add(resnet)
model.add(Dense(10, activation = 'softmax'))
model.layers[0].trainable = False
尝试过:
layer_output=model.get_layer('resnet').output
layer_output=model.get_layer('resnet').output
intermediate_model=tf.keras.models.Model(inputs=model.input,outputs=layer_output)
Tensorflow 中有一个关于此问题的未解决 issue。根据issue,你需要同时传入outer model和inner model的输入,才能得到inner model的输出。
import numpy as np
layer_output = model.get_layer("resnet50").output
intermediate_model = tf.keras.models.Model(inputs=[model.input, resnet.input], outputs=[layer_output])
input_data = np.random.rand(1, 224, 224, 3)
result = intermediate_model.predict([input_data, input_data])
print(result[0].shape)
(7, 7, 2048)
如何获取中间迁移学习输出。 ?例如:
from keras.models import Sequential
from keras.layers import Dense
# ... Other Imports..
from tensorflow.keras.applications.resnet50 import ResNet50
model = Sequential()
resnet = ResNet50(include_top = False, pooling = 'avg', weights = 'imagenet')
model.add(resnet)
model.add(Dense(10, activation = 'softmax'))
model.layers[0].trainable = False
尝试过:
layer_output=model.get_layer('resnet').output
layer_output=model.get_layer('resnet').output
intermediate_model=tf.keras.models.Model(inputs=model.input,outputs=layer_output)
Tensorflow 中有一个关于此问题的未解决 issue。根据issue,你需要同时传入outer model和inner model的输入,才能得到inner model的输出。
import numpy as np
layer_output = model.get_layer("resnet50").output
intermediate_model = tf.keras.models.Model(inputs=[model.input, resnet.input], outputs=[layer_output])
input_data = np.random.rand(1, 224, 224, 3)
result = intermediate_model.predict([input_data, input_data])
print(result[0].shape)
(7, 7, 2048)