I am producing heatmaps for my convolutional neural networks made via Keras, as described here. When I run that algorithm for a vanilla VGG16 net, the heatmap looks fine:
Then I created my own custom model, based on that VGG16 network, but with custom top layers:
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)
However, when I generate the heatmap for the same layer of my own custom network (i.e. the last conv. layer from the VGG16 base, being part of my new network), using the very same code/function, the heatmap does not look right:
The validation/testing accuracy of my custom network is at 97-98%, so I would assume it works fine. How come that the activation/heatmap is so off then? Or do I miss something else here?
PS: For your reference, the heatmap is created via the function listed here. It is called like this:
# 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)

