Developing custom_nodes issues : GradCAM: cannot register a hook on a tensor that doesn't require gradient
Your question
I am developing a custom_nodes plugin.
problem: cannot register a hook on a tensor that doesn't require gradient
content: I've been trying for an entire night. This is part of my code.
def get_attention_map(image):
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
if image.mode != 'RGB':
image = image.convert('RGB')
input_tensor = preprocess(image).unsqueeze(0).to(device)
output = attention_model(input_tensor)
attention_map = cam_extractor(output.argmax().item(), output)
attention_map = attention_map[0].squeeze().detach().cpu().numpy()
attention_map = cv2.resize(attention_map, (image.width, image.height))
return attention_map
Specifically, I used:
self.attention_model = resnet50(pretrained=True).eval().to(self.device)
self.cam_extractor = GradCAM(self.attention_model)
to perform recognition. However, no matter how I modify it, the same error keeps occurring. Strangely, it runs normally in non-ComfyUI environments. I've already tried using torch.inference_mode, but it caused more problems. Does anyone have a solution?
Logs
No response
Other
No response
I have done something similar, but @torch.inference_mode(False) solved the problem.
This issue is being marked stale because it has not had any activity for 30 days. Reply below within 7 days if your issue still isn't solved, and it will be left open. Otherwise, the issue will be closed automatically.