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

Visualization #199

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
50458e2
create 3D-Guided-GradCAM
Aakanksha-Rana Dec 17, 2021
f854256
Rename 3D-Guided-GradCAM.py to Guided-GradCAM.py
Aakanksha-Rana Dec 17, 2021
2c20abc
Merge branch 'neuronets:master' into visualization
Aakanksha-Rana Jan 3, 2022
3f66f00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 3, 2022
8163ba5
Update Guided-GradCAM.py
Aakanksha-Rana Jan 13, 2022
052a9cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 13, 2022
51f0106
Merge branch 'master' into visualization
Aakanksha-Rana Jan 13, 2022
71f3e5a
Update Guided-GradCAM.py
Aakanksha-Rana Jan 13, 2022
a2d5346
Merge branch 'master' into visualization
Aakanksha-Rana Jan 18, 2022
c1e9127
Merge branch 'master' into visualization
Aakanksha-Rana Jan 24, 2022
24d490c
Merge branch 'master' into visualization
Aakanksha-Rana Jan 24, 2022
d872166
Merge branch 'master' into visualization
satra Feb 28, 2022
061c32d
Update Guided-GradCAM.py
satra Feb 28, 2022
2e0239c
Merge branch 'neuronets:master' into visualization
Aakanksha-Rana Mar 13, 2022
7a35712
Create visualization.ipynb
Aakanksha-Rana Mar 13, 2022
8f673e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 13, 2022
05c7ec4
Merge branch 'master' into visualization
Aakanksha-Rana May 10, 2022
5f72e4e
Merge branch 'master' into visualization
satra May 11, 2022
9f17620
Merge branch 'master' into visualization
Aakanksha-Rana Jul 25, 2022
a6f8c58
Merge branch 'master' into visualization
satra Sep 17, 2022
57a3a44
Merge branch 'master' into visualization
satra Oct 18, 2022
3af0abb
Update nobrainer/visualization/Guided-GradCAM.py
satra Oct 18, 2022
349e5ce
Update nobrainer/visualization/Guided-GradCAM.py
satra Oct 18, 2022
cbbb53c
Merge branch 'neuronets:master' into visualization
Aakanksha-Rana Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added guide/visualization.ipynb
Empty file.
31 changes: 31 additions & 0 deletions nobrainer/visualization/Guided-GradCAM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
from skimage.transform import resize
import tensorflow as tf


def Guided_GradCAM_3D(grad_model, ct_io, class_index):
# First outputs target convolution and output
input_ct_io = tf.expand_dims(ct_io, axis=-1)
input_ct_io = tf.expand_dims(input_ct_io, axis=0)
with tf.GradientTape() as tape:
conv_outputs, predictions = grad_model(input_ct_io)
loss = predictions[:, class_index]
# Extract filters and gradients
output = conv_outputs[0]
grads = tape.gradient(loss, conv_outputs)[0]
guided_grads = (
tf.cast(output > 0, "float32") * tf.cast(grads > 0, "float32") * grads
)
satra marked this conversation as resolved.
Show resolved Hide resolved

# Average gradients spatially
weights = tf.reduce_mean(guided_grads, axis=(0, 1, 2))
# Build a ponderated map of filters according to gradients importance
cam = np.ones(output.shape[0:3], dtype=np.float32)
for index, w in enumerate(weights):
cam += w * output[:, :, :, index]

capi = resize(cam, (ct_io.shape))
print(capi.shape)
satra marked this conversation as resolved.
Show resolved Hide resolved
capi = np.maximum(capi, 0)
heatmap = (capi - capi.min()) / (capi.max() - capi.min())
return heatmap