Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization and refactoring #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

eclique
Copy link

@eclique eclique commented Dec 1, 2017

Hi @jacobgil
Thanks for sharing the code. I've been working with it and here's a version I ended up with.
Please feel free to edit it and correct me if my reasoning if wrong.

Here's a list of major changes:

  • At least in the current version replacing keras.relu with tf.relu is unnecessary
  • There was no need in creating target_category_loss. Loss you were computing (that's y_c in the paper) can be accessed by simple indexing.
  • In guided backprop there were also some strange computations with taking max and then sum. The output here should be just the gradient of conv output w.r.t. the input image.

There are some minor optimizations, such as computing GradCAM with dot product instead of the loop, accessing model's layer directly by it's name.

One difference with the paper that remains is l2-normalization in grad_cam. I believe authors didn't use it, but I kept it, because it helped in my case.

Modules versions: keras (2.0.8), tensorflow (1.3.0)


if save:
jetcam = cv2.applyColorMap(np.uint8(255 * gradcam), cv2.COLORMAP_JET)
jetcam = (np.float32(jetcam) + np.float32(cv2.imread(sys.argv[1]))) / 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the arguments of np.float32( cv2.imread (...) ) loading of the image as np.float32(image.load_img(sys.argv[1], target_size=(224, 224))) instead.

Otherwise, it would break if the image has a different size when being loaded by the load_image function.
In best case scenario, the size of the image needs to be taken out as a separate parameter - e.g. top of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants