question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Visualizing heatmaps of class activation

See original GitHub issue

Based on this deep-learning-with-python-notebooks implementation of CAM I have the following issue:

I have fine-tuned ResNet50 on my 9 new categories and have saved my model in .h5 format.

Then I am loading the saved model and find out the correct entry in the prediction vector without problems. My code then is the following:

# The is the output feature map of the `block5_conv3` layer,
# the last convolutional layer in ResNet50
last_conv_layer = model.get_layer('conv2d_14')

# This is the gradient of the "my_label" class with regard to
# the output feature map of `block5_conv3`
grads = K.gradients(my_label_output, last_conv_layer.output)[0]

where my_label_output = model.output[:, 1]

after that

# This is a vector of shape (60,), where each entry
# is the mean intensity of the gradient over a specific feature map channel
pooled_grads = K.mean(grads, axis=(0, 1, 2))

print initial_model.input.shape
print pooled_grads.shape
print last_conv_layer.output[0].shape

# This function allows us to access the values of the quantities we just defined:
# `pooled_grads` and the output feature map of `conv2d_14`,
# given a sample image
iterate = K.function([initial_model.input], [pooled_grads, last_conv_layer.output[0]])

# These are the values of these two quantities, as Numpy arrays,
# given our sample image of two elephants
pooled_grads_value, conv_layer_output_value = iterate([x])

but then I get the following error:

Invalid argument: Shape [-1,7,7,2048] has negative dimensions

Here is the summary of my loaded model

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         (None, 7, 7, 2048)        0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 7, 7, 30)          61470     
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 7, 7, 60)          1860      
_________________________________________________________________
average_pooling2d_7 (Average (None, 1, 1, 60)          0         
_________________________________________________________________
flatten_7 (Flatten)          (None, 60)                0         
_________________________________________________________________
dense_7 (Dense)              (None, 9)                 549       
=================================================================
Total params: 63,879
Trainable params: 63,879
Non-trainable params: 0

Any thoughts on this ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19

github_iconTop GitHub Comments

1reaction
GKalliatakiscommented, Nov 17, 2017

@vianamp Can you give me an example of a fine-tuned model ?

The code is working fine when I use the models from keras applications, but I want to use it on one of my fine-tuned models.

Many thanks

0reactions
JosephPaicommented, Dec 12, 2018

Hi @GKalliatakis I have fine-tuned ResNet50 on a 6 categories task and want a heap-map from it, then got a similar problem. Could you please paste your codes about building and training of the model, it’s better if there is the codes of getting heat-map. Many thanks !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class activation maps in Keras for visualizing where deep ...
Class activation maps are a simple technique to get the discriminative image regions used by a CNN to identify a specific class in...
Read more >
Class activation maps: Visualizing neural network decision ...
In this guide, we'll discuss a few approaches such as activation maximization, occlusion-based heatmaps, and class activation mapping, which can be use to ......
Read more >
Grad-CAM: Visualize class activation maps with Keras ...
Learn how to visualize class activation maps for debugging deep neural networks using Grad-CAM. We'll then implement Grad-CAM using Keras ...
Read more >
CNN Heat Maps: Class Activation Mapping (CAM) - Glass Box
Class Activation Mapping (CAM) is one technique for producing heat maps to highlight class-specific regions of images.
Read more >
Grad-CAM class activation visualization - Keras
Description: How to obtain a class activation heatmap for an image classification model. View in Colab • GitHub source. Adapted from Deep ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found