RuntimeError: cannot register a hook on a tensor that doesn't require gradient #146
-
When I tried to use torchcam on my fine-tuned resnet50, it threw an error:
The main codes are as follows: import torch
from torchvision import transforms
from torchcam.methods import SmoothGradCAMpp
from PIL import Image
my_model = torch.load(model_path, map_location=device).eval()
cam_extractor = SmoothGradCAMpp(my_model)
img = Image.open(image_path)
test_transforms = transforms.Compose(
[transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
input_tensor = test_transforms(img).to(device)
out = my_model(input_tensor.unsqueeze(0))
... And my environment verion is:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @RuixiangZhao, Thanks for bringing up that up 🙏
In this case:
import torch
from torchvision import transforms
from torchvision.models import resnet50
from torchcam.methods import SmoothGradCAMpp
from PIL import Image
my_model = resnet50().eval()
cam_extractor = SmoothGradCAMpp(my_model)
img = Image.fromarray((255 * torch.rand((224, 224, 3))).round().to(dtype=torch.uint8).numpy())
test_transforms = transforms.Compose(
[transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
input_tensor = test_transforms(img)
out = my_model(input_tensor.unsqueeze(0)) and I'm unable to reproduce it so far.
Would that be OK with you? :) |
Beta Was this translation helpful? Give feedback.
Hi @RuixiangZhao,
Thanks for bringing up that up 🙏
First, I'd like to point a few things out :
In this case: