Python 访问最后一个卷积层转移学习

Python 访问最后一个卷积层转移学习,python,tensorflow,keras,heatmap,transfer-learning,Python,Tensorflow,Keras,Heatmap,Transfer Learning,我正试图从计算机视觉模型中获取一些热图,它已经在对图像进行分类,但我发现了一些困难。 这是模型摘要: model.summary() 作为创建热图的标准过程的一部分,我知道我必须访问模型中的最后一个卷积层,在这种情况下,我会说它是Densenet121内部的一个层,但我找不到访问属于Densenet121的所有层的方法 现在,我一直在使用conv2d_4层来运行一些测试,但我觉得这不是正确的方法,因为该层在densenet的所有迁移学习工作之前 另外,我刚刚在Kerr官方文档中查找了功能层,但

我正试图从计算机视觉模型中获取一些热图,它已经在对图像进行分类,但我发现了一些困难。 这是模型摘要:

model.summary()
作为创建热图的标准过程的一部分,我知道我必须访问模型中的最后一个卷积层,在这种情况下,我会说它是Densenet121内部的一个层,但我找不到访问属于Densenet121的所有层的方法

现在,我一直在使用conv2d_4层来运行一些测试,但我觉得这不是正确的方法,因为该层在densenet的所有迁移学习工作之前

另外,我刚刚在Kerr官方文档中查找了功能层,但没有找到,所以我猜它不是一个层,就像嵌入在那里的洞densenet模型,但我找不到访问的方法

顺便说一下,我在这里分享模型构造,因为它可能有助于回答以下问题:

from tensorflow.keras.applications.densenet import DenseNet121

num_classes = 2
input_tensor = Input(shape=(IMG_SIZE,IMG_SIZE,1))
x = Conv2D(3,(3,3), padding='same')(input_tensor)   
x = DenseNet121(include_top=False, classes=2, pooling="avg", weights="imagenet")(x)

x = Dense(100)(x)
x = Dropout(0.45)(x)
predictions = Dense(num_classes, activation='softmax', name="predictions")(x)
model = Model(inputs=input_tensor, outputs=predictions)   
我发现你可以用
.get_layer()
两次访问功能性densenet模型中的层,该模型在“主”模型中被嵌入


在这种情况下,我可以使用
model.get_layer('densenet121').summary()
来检查Embeed模型中的所有thje层,然后将它们与以下代码一起使用:
model.get_layer('densenet121')。get_layer('xxxxx')

您可以使用
get_layer
来检索其名称或索引上的层。要获取
densenet121
层(即在模型中索引为2)的详细信息,您可以尝试使用
model.get_layer(index=2).summary()
从中轻松访问所需的层。谢谢
from tensorflow.keras.applications.densenet import DenseNet121

num_classes = 2
input_tensor = Input(shape=(IMG_SIZE,IMG_SIZE,1))
x = Conv2D(3,(3,3), padding='same')(input_tensor)   
x = DenseNet121(include_top=False, classes=2, pooling="avg", weights="imagenet")(x)

x = Dense(100)(x)
x = Dropout(0.45)(x)
predictions = Dense(num_classes, activation='softmax', name="predictions")(x)
model = Model(inputs=input_tensor, outputs=predictions)