-
Hello i have the following model: model = models.resnet50(pretrained=True)
for param in model.parameters():
param.requires_grad = False
fc_inputs = model.fc.in_features
model.fc = nn.Sequential(nn.Linear(fc_inputs, 512),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(256, 2),
nn.LogSoftmax(dim=1))
model.to(device)
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.fc.parameters(), amsgrad=True) i trained it the following way for cat and dog classification epochs = 1
running_loss = 0
test_loss = 0
accuracy = 0
best_acc = 0
for epoch in range(epochs):
start_time = time.time()
for images, labels in trainloader:
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
logps = model(images)
loss = criterion(logps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
model.eval()
with torch.no_grad():
for images, labels in testloader:
images, labels = images.to(device), labels.to(device)
logps = model(images)
loss = criterion(logps, labels)
test_loss += loss.item()
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
print(f'Epoch {epoch+1}/{epochs}.. '
f'Train loss: {running_loss/len(trainloader):.3f}.. '
f'Test loss: {test_loss/len(testloader):.3f}.. '
f'Test accuracy: {accuracy/len(testloader):.3f}')
elapsed_time = time.time() - start_time
print(time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
running_loss = 0
test_loss = 0
accuracy = 0
model.train() but now if i want to draw the heatmap to overlay my imagelike this: from torchvision.io.image import read_image
from torchvision.transforms.functional import normalize, resize, to_pil_image
from torchvision.models import resnet50
from torchcam.methods import SmoothGradCAMpp
import matplotlib.pyplot as plt
from torchcam.utils import overlay_mask
img = read_image("./HandTest/m1.jpg")
input_tensor = normalize(resize(img, (224, 224)) / 255., [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
with SmoothGradCAMpp(model) as cam_extractor:
out = model(input_tensor.unsqueeze(0).to(device))
activation_map = cam_extractor(0, out)
result = overlay_mask(to_pil_image(img), to_pil_image(activation_map[0].squeeze(0), mode='F'), alpha=0.5)
plt.imshow(result); plt.axis('off'); plt.tight_layout(); plt.show() i always get the following error:
what am I doing wrong? I'm really grateful for any help |
Beta Was this translation helpful? Give feedback.
Answered by
frgfm
Jul 3, 2024
Replies: 1 comment 9 replies
-
Hey @kvnae94 👋 It looks like you're trying to use a gradient-based CAM method on a model where you've disabled gradients. So here is a quick fix: from torchvision.io.image import read_image
from torchvision.transforms.functional import normalize, resize, to_pil_image
from torchvision.models import resnet50
from torchcam.methods import SmoothGradCAMpp
import matplotlib.pyplot as plt
from torchcam.utils import overlay_mask
img = read_image("./HandTest/m1.jpg")
input_tensor = normalize(resize(img, (224, 224)) / 255., [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
for param in model.parameters():
param.requires_grad_(True)
with SmoothGradCAMpp(model) as cam_extractor:
out = model(input_tensor.unsqueeze(0).to(device))
activation_map = cam_extractor(0, out)
result = overlay_mask(to_pil_image(img), to_pil_image(activation_map[0].squeeze(0), mode='F'), alpha=0.5)
plt.imshow(result); plt.axis('off'); plt.tight_layout(); plt.show() Let me know if that solves your problem or if you have any other questions! |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alright, now it's clearer thanks!
So to make this minimal:
this snippet works, no need to instantiate GradCAM. Now the reason why this throws an error is:
@torch.no_grad
decorator. And for that method at some point, we forward the input in the model (did this to avoid ex…