我训练了模型并保存了它:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
new_model=tf.keras.models.load_model('the_model.h5')
new_model.summary()
img = load_img('e.jpg',target_size=(227,227))
img=img_to_array(img)
img = np.expand_dims(img,axis=0)
img=img/255.
print(img.shape)
#prints out (1,227,227,3) the expected shapes
所以我的模型的架构是以下架构,我正在使用预训练的resnet50
backbone = ResNet50(input_shape=(227,227,3),weights='imagenet', include_top=False)
model = Sequential()
model.add(backbone)
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.5))
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
我尝试可视化隐藏层的输出,但是使用keras或keract时,我无法获得输出
与喀拉拉邦:
layer_outputs=[]
for layer in new_model.layers:
if layer.name=='resnet50':
temp = [l.output for l in layer.layers]
layer_outputs=temp
else:
layer_outputs.append(layer.output)
activation_model = Model(inputs=new_model.input, outputs=layer_outputs)
最后一行引起的错误:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 227, 227, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []
我觉得我的模型输入与 layer_outputs 匹配,所以我不太了解错误,确实是在检查时:
print(new_model.layers[0].input)
#prints out :Tensor("input_1:0", shape=(None, 227, 227, 3), dtype=float32)
print(layer_outputs[0])
#prints out : Tensor("input_1:0", shape=(None, 227, 227, 3), dtype=float32)
使用keract时:
a = keract.get_activations(new_model, img) # with just one sample.
keract.display_activations(a, directory='f', save=True)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,227,227,3]
关于如何解决它的任何想法,或者使用预先训练的模型从隐藏层获取输出的另一种可行解决方案?
谢谢
答案 0 :(得分:1)
Okey,我找到了解决问题的简便方法。
实际上,我认为出现此问题是因为我的顺序模型本身是由另一个模型(resnet)组成的。
由于我没有在预训练的resnet模型上添加很多图层,所以我决定从resnet模型中可视化特征图
img = load_img('e.jpg',target_size=(227,227))
img=img_to_array(img)
img = np.expand_dims(img,axis=0)
img=img/255.
print(img.shape)
loaded=tf.keras.models.load_model('age_gender_train.h5')
layer_outputs=[ layer.output for layer in loaded.layers[0].layers]
res = loaded.layers[0]
activation_model = Model(inputs=res.input, outputs=layer_outputs)
activations=activation_model.predict(img)
img = np.squeeze(img,axis=0)
然后,您可以使用 activations 变量轻松显示要素地图。
请注意,由于您具有resnet模型的输出,因此可以通过重复此过程从顶层的图层中获取要素图。使用resnet的输出作为输入,并从 layer_outputs 中删除resnet模型。(我没有尝试过,无法使用)
希望它可以帮助某人