自定义模型迁移学习中的热力图

在尝试为我的自定义模型生成Grad-CAM时,我遇到了一个问题。我正在尝试使用resnet50对图像分类模型进行微调。我的模型是这样定义的:

IMG_SHAPE = (img_height,img_width) + (3,)base_model = tf.keras.applications.ResNet50(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')

然后,

preprocess_input = tf.keras.applications.resnet50.preprocess_input

最后,

input_layer = tf.keras.Input(shape=(img_height, img_width, 3),name="input_layer")x = preprocess_input(input_layer)x = base_model(x, training=False)x = tf.keras.layers.GlobalAveragePooling2D(name="global_average_layer")(x)x = tf.keras.layers.Dropout(0.2,name="dropout_layer")(x)x = tf.keras.layers.Dense(4,name="training_layer")(x)outputs = tf.keras.layers.Dense(4,name="prediction_layer")(x)model = tf.keras.Model(input_layer, outputs)

现在,我按照https://keras.io/examples/vision/grad_cam/上的教程来生成热力图。但是,尽管教程建议使用model.summary()来获取最后的卷积层和分类层,我不确定如何在我的模型中实现。如果我运行model.summary(),我得到的是:

__________________________________________________________________________________________________Layer (type)                    Output Shape         Param #     Connected to                     ==================================================================================================input_layer (InputLayer)        [(None, 224, 224, 3)] 0                                            __________________________________________________________________________________________________tf.operators.getitem_11       (None, 224, 224, 3)  0                             __________________________________________________________________________________________________tf.nn.bias_add_11 (TFOpLambd  [(None, 224, 224, 3)] 0__________________________________________________________________________________________________resnet50 (Functional)          (None, 7, 7, 2048)   23587712__________________________________________________________________________________________________global_average (GlobalAverag    (None, 2048)    0__________________________________________________________________________________________________dropout_layer (Dropout)       (None, 2048)     0__________________________________________________________________________________________________hidden_layer (Dense)         (None, 4)        8196__________________________________________________________________________________________________predict_layer (Dense)         (None, 4)      20==================================================================================================

然而,如果我运行base_model.summary(),我得到的是:

Layer (type)                    Output Shape         Param #     Connected to                     ==================================================================================================input_29 (InputLayer)           [(None, 224, 224, 3) 0                                            __________________________________________________________________________________________________conv1_pad (ZeroPadding2D)       (None, 230, 230, 3)  0           input_29[0][0]                   __________________________________________________________________________________________________conv1_conv (Conv2D)             (None, 112, 112, 64) 9472        conv1_pad[0][0]                  __________________________________________________________________________________________________conv1_bn (BatchNormalization)   (None, 112, 112, 64) 256         conv1_conv[0][0]                 __________________________________________________________________________________________________...   ...   ...           ...                                 __________________________________________________________________________________________________conv5_block3_3_bn (BatchNormali (None, 7, 7, 2048)   8192        conv5_block3_3_conv[0][0]        __________________________________________________________________________________________________conv5_block3_add (Add)          (None, 7, 7, 2048)   0           conv5_block2_out[0][0]                                                                            conv5_block3_3_bn[0][0]          __________________________________________________________________________________________________conv5_block3_out (Activation)   (None, 7, 7, 2048)   0           conv5_block3_add[0][0]           ==================================================================================================

如果我按照教程使用’resnet50’作为最后的卷积层,我会得到以下错误:

图形断开连接:无法获取层“conv1_pad”处张量KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name=’input_29′), name=’input_29′, description=”created by layer ‘input_29′”)的值。之前访问的层没有问题:[]

但是,如果我使用’conv5_block3_out’,程序无法在模型中找到该层。我该如何访问似乎隐藏在resnet50层中的层呢?


回答:

我设法找到了解决这个问题的方法。在定义“make-gradcam_heatmap”时,我添加了以下一行代码

input_layer = model.get_layer('resnet50').get_layer('input_1').input

并将下一行改为

last_conv_layer = model.get_layer(last_conv_layer_name).get_layer("conv5_block3_out")

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注