yizt / Grad-CAM.pytorch

pytorch实现Grad-CAM和Grad-CAM++,可以可视化任意分类网络的Class Activation Map (CAM)图,包括自定义的网络;同时也实现了目标检测faster r-cnn和retinanet两个网络的CAM图;欢迎试用、关注并反馈问题...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

移植您的方法到YOLOv3遇到点问题,请您指点一二

withtimesgo1115 opened this issue · comments

感谢您的开源分享,我尝试将您的工作移植到yolo v3网络中,目前基本框架已经实现了,但是在用hook求中间梯度的时候得到的值全是0,这样的话导致我的cam没法计算准确,最后的目标bbx的热力图就是深蓝图。我注意到您的代码中梯度Hook函数是直接传入input_grad 和 output_grad 然后直接赋值即可。我这里实在是不太明白,这两个变量是什么意思?我这里打印出来就是全是0,虽然格式是对的。获取特征的hook函数可以给我想要的结果。如果您有时间的话,请您指点一下,谢谢了。

  1. 存在某一层梯度值全小于零的情况,这样拿出来后经过ReLU后就全为零,但是梯度本身不会全为零。 2. register_forward_hook,register_backward_hook 请查看官网这两个函数的作用,自然就明白了;我们定义网络的时候的都是定义了层(全连接或或者卷积等),我们能够获取的是网络的输出结果,但是没有办法获取网络的中间层的值;这两个函数通过hook方式分别在前向和反向时帮我们获取指定层的特征值和梯度值。output_grad是当前层的梯度;input_grad是计算当前层梯度所需要的信息,包括上一层梯度,当前层权重值。

    在 2021年3月23日,下午9:50,Channing @.***> 写道: 感谢您的开源分享,我尝试将您的工作移植到yolo v3网络中,目前基本框架已经实现了,但是在用hook求中间梯度的时候得到的值全是0,这样的话导致我的cam没法计算准确,最后的目标bbx的热力图就是深蓝图。我注意到您的代码中梯度Hook函数是直接传入input_grad 和 output_grad 然后直接赋值即可。我这里实在是不太明白,这两个变量是什么意思?我这里打印出来就是全是0,虽然格式是对的。获取特征的hook函数可以给我想要的结果。如果您有时间的话,请您指点一下,谢谢了。

我昨天已经成功在yolo上实现了,当时留言的时候还不清楚hook函数的用法,学习了之后弄明白了。指定层的类型是Container,我在pytorch论坛里发现有大佬提到register_forward_hook和register_backward_hook存在一些问题,不适用于Container模块,建议直接针对张量使用register_hook,我修改了代码,成功跑出热度图,有了热度图就好办了。但目前还存在一个小问题,热度图的像素位置和我的输入图片目标之间存在一定的整体偏移,感觉应该是权重*特征那里匹配的不太对。因为我的模型输出的这两个量的维度不一样,导致没法直接相乘。还需要再看一下,如果有问题还请你能指点一二,再次感谢您的回复和您开源的工作,非常感谢!如果其他朋友遇到类似问题,可以借鉴此提问,也可以继续讨论,谢谢。

I successfully implemented it on yolo yesterday. At that time, I didn't know how to use the hook function when I left a message, but I figured it out after learning it. The type of specified layer is Container. I found in pytorch forum that there are some problems in register_forward_hook and register_backward_hook, which are not suitable for Container module. It is suggested to use register_hook directly for tensor. I modified the code and ran out the heat map successfully. It is easy to have the heat map. However, there is still a small problem at present. There is a certain overall deviation between the pixel position of the heat map and the target of my input picture, and the feeling should be that the matching of weight * features is not quite right. Because the dimensions of these two quantities output by my model are different, it is impossible to multiply them directly. You need to look again. If you have any questions, please give me some advice. Thank you again for your reply and your open source work. Thank you very much! If other friends encounter similar problems, they can learn from this question or continue to discuss it. Thank you.