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

Add GradCAM analyzer #155

Merged
merged 12 commits into from
Feb 8, 2024
Merged

Add GradCAM analyzer #155

merged 12 commits into from
Feb 8, 2024

Conversation

JeanAnNess
Copy link
Contributor

@JeanAnNess JeanAnNess commented Jan 8, 2024

Add GradCAM analyzer

The following has been changed:

  • Added struct GradCAM and function analyzer for the type in gradient.jl
  • Added export GradCAM to ExplainableAI.jl
  • Added Dictionary entry for GradCAM in heatmap.jl (Couldn't find the file in the repository anymore - Where is it?)

Papers / Blogs as reference

To-Do

  • Potentially make the PR able to be automatically mergeable (?)
  • Expand on visualisation function
    • Upscaling the 7x7 output and layering it over the input image (improve interpolation in example below)
    • Adding a function for it somewhere (TBD)
  • Validate the choosen colorscheme

Example

# Imports
using ExplainableAI
using Flux
using Metalhead                         # pre-trained vision models
using HTTP, FileIO, ImageMagick         # load image from URL
using ImageInTerminal                   # show heatmap in terminal
using ImageTransformations: imresize    # resize of heatmap

# Load model
model = VGG(16, pretrain=true).layers
# Load input
url = HTTP.URI("https://raw.githubusercontent.com/Julia-XAI/ExplainableAI.jl/gh-pages/assets/heatmaps/castle.jpg")
img = load(url)
input = preprocess_imagenet(img)
input = reshape(input, 224, 224, 3, :)  # reshape to WHCN format

# Run XAI method
analyzer = GradCAM(model.layers[1], model.layers[2])    
expl = analyze(input, analyzer)         

# Show heatmap
output = heatmap(expl)

# Show overlayed heatmap with input
alpha = 0.6
hmap_scaled = imresize(output, size(img))
combined = img*(1-alpha) + hmap_scaled * alpha
Input 7x7 GradCAM output Upscaled output Upscaled and overlayed
input image 7x7 GradCAM result Upscaled GradCAM output Upscaled and overlayed

@adrhill
Copy link
Member

adrhill commented Jan 8, 2024

Awesome, thanks a lot!

I'm currently restructuring the package for a 1.0 release, moving the interface into XAIBase.jl, heat mapping into VisionHeatmaps.jl and LRP into RelevancePropagation.jl, so that's why src/heatmap.jl disappeared.

I'll take care of the rest!

Copy link

codecov bot commented Jan 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (75954c8) 95.89% compared to head (3613341) 96.29%.
Report is 1 commits behind head on master.

❗ Current head 3613341 differs from pull request most recent head 26ea235. Consider uploading reports for the commit 26ea235 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #155      +/-   ##
==========================================
+ Coverage   95.89%   96.29%   +0.40%     
==========================================
  Files           4        5       +1     
  Lines          73       81       +8     
==========================================
+ Hits           70       78       +8     
  Misses          3        3              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/gradcam.jl Outdated Show resolved Hide resolved
src/gradcam.jl Outdated Show resolved Hide resolved
src/gradcam.jl Outdated Show resolved Hide resolved
@adrhill
Copy link
Member

adrhill commented Feb 8, 2024

Awesome, looks great! 👍

I'm working on heatmap overlays in VisionHeatmaps, which should be merged soon (Julia-XAI/VisionHeatmaps.jl#4), so for now, calling heatmap on GradCAM will return the "7x7 GradCAM output" from your table above.

@adrhill adrhill merged commit 974a934 into Julia-XAI:master Feb 8, 2024
5 checks passed
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