迁移学习后TensorFlow ResNet50模型中GradCAM所需卷积层的访问方法
如何在自定义训练的ResNet50模型中访问嵌套的conv5_block3_out层?
我是深度学习领域新手,在将GradCAM应用于自行重新训练的TensorFlow ResNet50模型时遇到问题。我构建模型的代码如下:
IMG_SHAPE = IMG_SIZE + (3,) base_model = tf.keras.applications.ResNet50(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')
global_average_layer = tf.keras.layers.GlobalAveragePooling2D() feature_batch_average = global_average_layer(feature_batch)
prediction_layer = tf.keras.layers.Dense(5, activation='softmax') prediction_batch = prediction_layer(feature_batch_average)
inputs = tf.keras.Input(shape=(224, 224, 3)) x = data_augmentation(inputs) x = preprocess_input(x) x = base_model(x, training=False) x = global_average_layer(x) x = tf.keras.layers.Dropout(0.2)(x) outputs = prediction_layer(x) model = tf.keras.Model(inputs, outputs)
base_learning_rate = 0.0001 model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False), metrics=['accuracy'])
模型摘要:
Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_2 (InputLayer) [(None, 224, 224, 3)] 0 sequential (Sequential) (None, 224, 224, 3) 0 tf.__operators__.getitem (S (None, 224, 224, 3) 0 licingOpLambda) tf.nn.bias_add (TFOpLambda) (None, 224, 224, 3) 0 resnet50 (Functional) (None, 7, 7, 2048) 23587712 global_average_pooling2d (G (None, 2048) 0 lobalAveragePooling2D) dropout (Dropout) (None, 2048) 0 dense (Dense) (None, 5) 10245 ================================================================= Total params: 23,597,957 Trainable params: 19,463,173 Non-trainable params: 4,134,784 _________________________________________________________________
按照GradCAM教程要求,需获取最后一层卷积层输出,尝试用以下代码:
last_conv_layer = model.get_layer("conv5_block3_out") last_conv_layer_model = tf.keras.Model(model.inputs, last_conv_layer.output)
但该卷积层嵌套在模型的"resnet50 (Functional)"子模型中,无法直接访问。仅使用未修改的ResNet50基础模型时才能访问该层,但不符合项目需求。
解决方法
因为conv5_block3_out层是嵌套在resnet50子模型内部的,你需要先获取这个子模型,再从中提取目标层:
- 从整体模型中取出嵌套的ResNet50子模型:
base_model = model.get_layer('resnet50')
- 从子模型中获取conv5_block3_out层:
last_conv_layer = base_model.get_layer('conv5_block3_out')
- 构建GradCAM所需的输出模型:
last_conv_layer_model = tf.keras.Model(model.inputs, last_conv_layer.output)
或者你可以直接用一行代码完成层的获取:
last_conv_layer = model.get_layer('resnet50').get_layer('conv5_block3_out')
这样就能成功访问到嵌套的卷积层,继续完成GradCAM的后续步骤。
内容的提问来源于stack exchange,提问作者Skruff
相关产品推荐
相关产品推荐

