How to overlay 3D cam heatmap on the 3D image and visualize it slice by slice? #127
-
Hi, frgfm. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Hello @guyucowboy 👋 My apologies for the late reply!
import torch
import matplotlib.pyplot as plt
# Let's take a random cam
cam_2d = torch.rand((32, 32))
# Matplotlib will automatically select a colormap to render the values
plt.imshow(cam_2d.numpy()); plt.show()
I'll illustrate all three, feel free to pick the option that suits you best! import torch
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
# Let's take a random cam
cam_3d = torch.rand((32, 32, 32))
ax = plt.subplot(111, projection='3d')
# Initialize the color with RGBA
colors = np.empty((*cam_3d.shape, 4))
# Fixed RGB (grey), Alpha as values
colors[..., :3] = .5
# Alpha = 1 means no transparency, so I rescale down the values
colors[..., -1] = 0.5 * cam_3d.numpy()
ax.voxels(np.ones(cam_3d.shape, dtype=bool), facecolors=colors); plt.show()
# Now with alpha fixed
ax = plt.subplot(111, projection='3d')
# Fixed Alpha, RGB as values (using a colormap)
colors[..., -1] = .5
# Pick a colormap here https://matplotlib.org/stable/tutorials/colors/colormaps.html
# I personally use jet for TorchCAM
cmap = get_cmap('jet')
colors[..., :3] = cmap(cam_3d.numpy())[..., :-1]
ax.voxels(np.ones(cam_3d.shape, dtype=bool), facecolors=colors); plt.show()
# Finally with RGBA to display values
ax = plt.subplot(111, projection='3d')
# Combine both previous iterations
colors[..., -1] = 0.5 * cam_3d.numpy()
cmap = get_cmap('jet')
colors[..., :3] = cmap(cam_3d.numpy())[..., :-1]
ax.voxels(np.ones(cam_3d.shape, dtype=bool), facecolors=colors); plt.show() Then the choice is all a matter of taste & use cases! Final option I had in mind: animate along a given axis. import torch
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Let's take a random cam
cam_3d = torch.rand((32, 32, 32))
npy_cam = cam_3d.numpy()
fig, ax = plt.subplots()
img = ax.imshow(npy_cam[0, ...])
# For the animation (imagine a GIF), we have to set the starting point
def reset():
img.set_data(npy_cam[0, ...])
return (img,)
# Now we defined how frame are defined
def animate(idx):
img.set_data(npy_cam[idx, ...])
return (img,)
anim = animation.FuncAnimation(fig, animate, init_func=reset, frames=cam_3d.shape[0], interval=20, blit=True)
plt.show() I hope this helps, let me know if this isn't clear :) |
Beta Was this translation helpful? Give feedback.
-
Hi, frgfm.
When I debug my code, I found activation_map = {list}<class 'list'>: [tensor ([[[nan]]], device = 'cuda:0')]
In addition, after executing the code, weights = self._get_weights(class_idx, scores). I found 'weights' is a list with a single Tensor in my code. Then for loop for weight, activation in zip(weights, self.hook_a): is executed only once. In my code, I found cam became nan after normalized. Then, I set normalized as False in the core.py in line 137. I found activation_map = {list}<class 'list'>: [tensor ([[[4.2340]]], device = 'cuda:0')]. There should be something wrong in the function cam_visualization_3Dcube. But I do not know how to generate 3D cam for 3D CNN. Thanks for your great helps! I am looking forward to your reply. |
Beta Was this translation helpful? Give feedback.
-
Hi, frgfm. PS:
|
Beta Was this translation helpful? Give feedback.
-
Hi frgfm! Thank you for your understanding! Besides, you mentioned uploading the code to GitHub before. I studied it these days and I have successfully uploaded the code to GitHub! The following is a link to my code:https://github.com/Ljiaqii/cam_3d_cube_project/tree/master And, I wrote a detailed readme.md, which contains the output of the activation_map_numpy is ‘nan’, the shape of the activation_map_numpy.:(1, 1, 1) , the error track of the program, the environment, and the packages used. |
Beta Was this translation helpful? Give feedback.
-
In the previous discussion, in order to slice and display the cube, I # added the following two lines of code:
The following is the error track: Exception in Tkinter callback Thanks again! |
Beta Was this translation helpful? Give feedback.
Hello @guyucowboy 👋
My apologies for the late reply!
This is a very interesting question, I'll try to answer as best as I can: