Closed MLRadfys closed 3 years ago
Hi, Can you please share more details:
Thanks!
Hi and thanks for the quick reply:
Here is the code I used (As I use grayscale images they are first converted to RGB)
Cheers,
M
from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from torchvision.models import resnet50
import torchvision
model = resnet50(pretrained=True)
target_layer = model.layer4[-1]
#Load image
img = np.squeeze(subject.img_data)
#Convert img to RGB
vol_greyscale = (255*(img - np.min(img))/np.ptp(img)).astype(int)
# Convert volume to RGB
vol_rgb = np.stack([vol_greyscale, vol_greyscale, vol_greyscale], axis=0)
input_tensor = torchvision.transforms.functional.to_tensor(vol_rgb)
# This should be constructed once:
cam = GradCAM(model=model, target_layer=target_layer, use_cuda=True)
# And then cam be used on many images:
grayscale_cam = cam(input_tensor=input_tensor, target_category=None)
visualization = show_cam_on_image(rgb_img, grayscale_cam)´´´
Hi again,
I found the issue. Right before the library import I used:
with torch.no_grad():
Now its working like a charm :-)
Thanks for your help!
Cheers,
M
Hi,
I tried to use grad cam and followed your example. Unfortunately I get the following error:
"element 0 of tensors does not require grad and does not have a grad_fn"
Any suggestions would be appreciated .
Cheers, M