Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn #2

Open
P-HL opened this issue Sep 11, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@P-HL
Copy link

P-HL commented Sep 11, 2022

你好,在使用gradcam-yolov3时报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

使用的模型和权重是自己在mmdetection中训练的,可用在gradcam-yolov3中正确画出框并输出结果。但是在使用GradCAM_YOLOV3函数时出现梯度的错误。在该文件定义时使用到self.net.eval(),去掉后依旧没有梯度,请问可以解决嘛?

@P-HL
Copy link
Author

P-HL commented Sep 11, 2022

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_1145163/286742431.py in <module>
      1 ## First is the data, second is the index of the predicted bbox
----> 2 mask, box, class_id = grad_cam(data, 0)

/tmp/ipykernel_1145163/2352451777.py in __call__(self, data, index)
     54         score = res[0][0][index]
     55 
---> 56         score.backward()
     57 
     58         gradient = self.gradient.cpu().data.numpy()  # [C,H,W]

~/anaconda3/envs/open-mmlab3/lib/python3.7/site-packages/torch/_tensor.py in backward(self, gradient, retain_graph, create_graph, inputs)
    394                 create_graph=create_graph,
    395                 inputs=inputs)
--> 396         torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
    397 
    398     def register_hook(self, hook):

~/anaconda3/envs/open-mmlab3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
    173     Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
    174         tensors, grad_tensors_, retain_graph, create_graph, inputs,
--> 175         allow_unreachable=True, accumulate_grad=True)  # Calls into the C++ engine to run the backward pass
    176 
    177 def grad(

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

@RuoyuChen10
Copy link
Owner

你好,在使用gradcam-yolov3时报错:RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

使用的模型和权重是自己在mmdetection中训练的,可用在gradcam-yolov3中正确画出框并输出结果。但是在使用GradCAM_YOLOV3函数时出现梯度的错误。在该文件定义时使用到self.net.eval(),去掉后依旧没有梯度,请问可以解决嘛?

你好,你可以先试试ipynb版本么:gradcam-yolov3.ipynb,另外注意一下你的模型,我测试使用的yolo v3文件好像是yolov3_d53_mstrain-608_273e_coco,你可以参考一下。

此外我认为出现这个问题还有一个可能就是:

grad_cam = GradCAM_YOLOV3(model, 'backbone.conv_res_block4.conv.conv')

指定的层可能与你自己训练的那个文件的层不一样,你的文件下指定该层可能就没有梯度传递了,需要重新指定适合你的层的名称了。

@RuoyuChen10 RuoyuChen10 added the bug Something isn't working label Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants