【发布时间】:2019-10-21 23:56:43
【问题描述】:
我正在为我通过 Keras 制作的卷积神经网络制作热图,as described here。当我为 vanilla VGG16 网络运行该算法时,热图看起来不错:
然后我创建了自己的自定义模型,基于 VGG16 网络,但具有自定义顶层:
input_layer = layers.Input(shape=(img_size, img_size, 3), name="model_input")
vgg16_base = VGG16(weights="imagenet", include_top=False, input_tensor=input_layer)
temp_model = vgg16_base.output
temp_model = layers.Flatten()(temp_model)
temp_model = layers.Dense(256, activation="relu")(temp_model)
temp_model = layers.Dense(1, activation="sigmoid")(temp_model)
custom = models.Model(inputs=input_layer, outputs=temp_model)
但是,当我为我自己的自定义网络的同一层(即来自VGG16 基础的最后一个转换层,是我的新网络的一部分)生成热图时,使用完全相同的代码/函数,热图看起来不对:
我的自定义网络的验证/测试准确度为 97-98%,所以我认为它工作正常。那么激活/热图怎么会如此关闭呢?还是我在这里错过了其他东西?
PS:供您参考,热图是通过function listed here 创建的。它是这样调用的:
# Load the image from disk and preprocess it via Keras tools
img_path = "/path/to/image.jpg"
img = image.load_img(img_path, target_size=(224, 224))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor = preprocess_input(img_tensor)
# At this point I either load the VGG16 model directly (heatmapü works),
# or I create my own custom VGG16-based model (heatmap does not work)
# The model itself is then stored into the variable "model"
preds = model.predict(img_tensor)
model_prediction = model.output[:, np.argmax(preds[0])]
# Then I call the custom function referred to above
input_layer = model.get_layer("model_input")
conv_layer = model.get_layer("block5_conv3")
plot_conv_heat_map(model_prediction, input_layer, conv_layer, img_tensor, img_path)
【问题讨论】:
-
你能分享你所有的代码吗?
-
@Priyatham 当然,我添加了更多代码,包括。一个指向我用来生成热图的函数的链接到我的帖子底部。
-
在您的自定义模型中,您似乎在进行二进制分类,对吧?如果是这样,那么你认为猫的真正标签是0还是1?
-
@today 确实如此,如上所述,我已经在 VGG16 基础之上添加了
Dense层; CAT 导致 0,DOG 导致 1。 -
好吧,既然是猫图片,那你可以试试
1 - model_prediction,即plot_conv_heat_map(1 - model_prediction, ...)吗?
标签: python machine-learning keras conv-neural-network heatmap