yizt / Grad-CAM.pytorch

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

语义分割 #16

Open linhaoqi027 opened 4 years ago

linhaoqi027 commented 4 years ago

对于unet网络,是否有必要在encoder的最后一层使用grad-cam,这样会对分割结果有帮助吗?

yizt commented 4 years ago

@linhaoqi027 您好,这是一个开放问题,需要实验测试下看看效果;但是存在一个问题,从输出层到encoder的最后一层,中间有太多层,loss反向传播到这里以后语义信息应该已经很弱了。

linhaoqi027 commented 4 years ago

那如果我把decoder直接换成8倍或16倍上采样,这个操作是否有意义。另外请问grad-cam是针对于分类问题提出的嘛?

yizt commented 4 years ago

@linhaoqi027 a)一般而言直接修改网络结构,模型精度会有较大下滑;b)grad-cam论文中说了三个应用场景classification, image caption generation and 3D action recognition;共同点是图像级别结果输出,Grad-CAM可以辅助输出:模型结果主要关注了图像的哪个位置。

linhaoqi027 commented 4 years ago

谢谢您

linhaoqi027 commented 4 years ago

Towards Interpretable Semantic Segmentation via Gradient-weighted Class Activation Mapping这篇论文有讲到怎么变化就可以实现语义分割可视化。请问大佬这个要复现难吗?

yizt commented 4 years ago

@linhaoqi027 感谢您的反馈,我看看这篇论文,再给您回复哈

yizt commented 4 years ago

@linhaoqi027 基本不需要修改什么,找一个语义分割网络;把index改为对应类别的index就好了,就像论文中的sky,把所有预测为sky的像素的位置赋值为index就好。

        output = self.net(inputs)  # [1,num_classes]
        if index is None:   # 这两行修改
            index = np.argmax(output.cpu().data.numpy())
        target = output[0][index]
        target.backward()
linhaoqi027 commented 4 years ago

请问这里的index指的是onehot编码下对应类别的index嘛

yizt commented 4 years ago

@linhaoqi027 对应分类是的,对于语义分割index应该是预测为某个类别(比如sky)的所有坐标位置,就是长宽方向坐标位置。

liricky commented 3 years ago

@linhaoqi027 a)一般而言直接修改网络结构,模型精度会有较大下滑;b)grad-cam论文中说了三个应用场景classification, image caption generation and 3D action recognition;共同点是图像级别结果输出,Grad-CAM可以辅助输出:模型结果主要关注了图像的哪个位置。

你好,我想请教一下,对于分割任务的模型(比如unet)需要如何修改才能实现用CAM表现对每个分割类别的关注位置呢?

yizt commented 2 years ago

直接将这个输出为这个类别的神经元的loss反向传播即可,跟分类是一样的,只是分类只有一个神经元需要反向传播,分割有多个。

在 2021年9月7日,上午11:49,liricky @.***> 写道:

@linhaoqi027 https://github.com/linhaoqi027 a)一般而言直接修改网络结构,模型精度会有较大下滑;b)grad-cam论文中说了三个应用场景classification, image caption generation and 3D action recognition;共同点是图像级别结果输出,Grad-CAM可以辅助输出:模型结果主要关注了图像的哪个位置。

你好,我想请教一下,对于分割任务的模型(比如unet)需要如何修改才能实现用CAM表现对每个分割类别的关注位置呢?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/yizt/Grad-CAM.pytorch/issues/16#issuecomment-913968716, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABV2ST4KHDHJXG6IUOND7RTUAWDTVANCNFSM4NL5JSSA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

liricky commented 2 years ago

@yizt 感谢回复🙏。我还想请教一下,是不是不额外添加分类器的情况下,分割网络中(以unet为例),CAM的使用要放在上采样最后一层之后,输出为对应分割类别数的卷积层上(比如VOC数据集,就是上采样之后的从channel64压缩到21的卷积层)。如果是放在最后输出的通道层上,这个CAM的结果和经过softmax可视化的各类别通道上的结果有什么本质的区别吗?这两者的区别在分割任务里我不是很理解,不知道您有什么见解,再次感谢! XD

yizt commented 2 years ago

不是很明白您的意思,CAM放在哪一层可以自己选择,在不同的层结果肯定不一样;一般选择是靠近输出层,语义信息更强

在 2021年9月11日,下午5:39,liricky @.***> 写道:

@yizt https://github.com/yizt 感谢回复🙏。我还想请教一下,是不是不额外添加分类器的情况下,分割网络中(以unet为例),CAM的使用要放在上采样最后一层之后,输出为对应分割类别数的卷积层上(比如VOC数据集,就是上采样之后的从channel64压缩到21的卷积层)。如果是放在最后输出的通道层上,这个CAM的结果和经过softmax可视化的各类别通道上的结果有什么本质的区别吗?这两者的区别在分割任务里我不是很理解,不知道您有什么见解,再次感谢! XD

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yizt/Grad-CAM.pytorch/issues/16#issuecomment-917376488, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABV2ST24ZX4ESZ2UH6IYP2TUBMPTPANCNFSM4NL5JSSA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

liricky commented 2 years ago

@yizt 我是否可以理解为分割任务中的CAM的使用在本质上还是使用分类器进行实现的?相当于要在分割网络的结构之外有一个额外的分类器去预测图像中存在的类别,还是分割任务中实现CAM不用添加任何额外的结构,只需要在本身的某一层结构上添加使用loss和特征图计算即可?因为我对CAM这方面不是很理解,所以可能问得有点模糊。感谢回复!

yizt commented 2 years ago

分割就是分类,分割是像素级分类;无需添加额外结构

在 2021年9月11日,下午11:22,liricky @.***> 写道:

@yizt https://github.com/yizt 我是否可以理解为分割任务中的CAM的使用在本质上还是使用分类器进行实现的?相当于要在分割网络的结构之外有一个额外的分类器去预测图像中存在的类别,还是分割任务中实现CAM不用添加任何额外的结构,只需要在本身的某一层结构上添加使用loss和特征图计算即可?因为我对CAM这方面不是很理解,所以可能问得有点模糊。感谢回复!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yizt/Grad-CAM.pytorch/issues/16#issuecomment-917424503, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABV2STYFXAOIQXGAHTGTQ2DUBNX3JANCNFSM4NL5JSSA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

liricky commented 2 years ago

@yizt 不好意思,我还是没有完全理解您得意思。我能否举一个具体的例子,如果对一个三类别分割的网络,我在最后输出层即将channel压缩到3维的卷积层上使用grad-cam,这样就能得到三幅特征图和对应的权重。此时我将权重和特征图对应相乘是否就是对于分割这三个类别的CAM图结果呢?

yizt commented 2 years ago

不明白名您的问题点; a) 对于您说的三个类别的分割,假设想看第一个类别的CAM,直接对于第一个通道中激活输出超过0.5(假设超过0.5认为是这个类别)神经元反向传播梯度即可,其它神经元不传播梯度。 b) CAM想在哪一层做,就是在程序中修改的层名layer_name就好; c) 对于分类网络使用的最后一个卷积层,因为卷积层后面还有池化层和FC输出层;对于分割肯定不能使用最后一个卷积层,因为最后一个卷积层就是输出层;可以选择倒数第二个,倒数第三个… 卷积层看看

在 2021年9月12日,下午11:13,liricky @.***> 写道:

@yizt https://github.com/yizt 不好意思,我还是没有完全理解您得意思。我能否举一个具体的例子,如果对一个三类别分割的网络,我在最后输出层即将channel压缩到3维的卷积层上使用grad-cam,这样就能得到三幅特征图和对应的权重。此时我将权重和特征图对应相乘是否就是对于分割这三个类别的CAM图结果呢?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yizt/Grad-CAM.pytorch/issues/16#issuecomment-917654960, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABV2ST5F3LB4YWSVUKWTVA3UBS7QDANCNFSM4NL5JSSA. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

liricky commented 2 years ago

@yizt 非常感谢,我理解您的意思了。另外我还有一个针对CAM用在分割上的问题,就是输出层如果不处理成结果类别而是直接对整个输出图像进行归一化,也可以得到一个对于每个像素置信度的结果图,您觉得这个图像和CAM的输出有什么本质上的差异吗?二者都可以解释为对于分割类别归属显著性的描述。在分类问题中由于缺少像素级别的描述信息所以需要借助于CAM图,而分割结果本身具备像素级别的信息,所以分割是否并不一定要完全依赖于分类问题的可视化方式呢?上面是一些我不成熟的想法,如果您有什么见解我希望与您探讨下。非常感谢您的回复!