可视化

可视化仓库: utkuozbulak/pytorch-cnn-visualizations

| conference | paper | first author | institute | | - | - | - | - | - | | ECCV 2014 | Visualizing and Understanding Convolutional Networks | Matthew D. Zeiler | Dept. of Computer Science, Courant Institute, New York University | | CVPR 16 | Learning Deep Features for Discriminative Localization | Bolei Zhou | MIT | | ICCV 2017 | Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization | Ramprasaath R. Selvaraju | Georgia Institute of Technology, Atlanta, GA, USA |

CAM

Learning Deep Features for Discriminative Localization

为了在定位上得到好的表现,网络的分类性能很重要,因为它决定了网络是否能准确地分类和定位的边界。

用于两个方面:

  • 弱监督语义分割(分类信息有定位能力)

  • 可视化

本文针对使用GAP的CNN网络提出了一个叫做CAM的通用技术,这个技术可以让做过分类训练的CNN网络学会进行物体定位,不需要进行额外的边界框注解训练。

本文针对使用GAP的CNN网络提出了一个叫做CAM的通用技术,这个技术可以让做过分类训练的CNN网络学会进行物体定位,不需要进行额外的边界框注解训练。CAM可以可视化预测类在任何给定图片上的得分,标出CNN检测到的物体的区别性区域。我们在ILSVRC上评估了我们的方法,进行了弱监督物体定位,证明了我们的全局平均池化层的CNN可以进行准确的物体定位。此外,我们证明了CAM定位技术可以推广到其他视觉识别任务中,也就是说,我们的技术可以生成通用的用于定位的深层特征,可以帮助其他用CNN做任务的研究人员,作为他们理解区别性区域的基础。

Grad-CAM

Vanilla Backpropagation

  1. 将图片转化为可以求梯度的tensor

  2. 将图片输入网络

  3. 反传梯度时将目标类的one hot设为1,其他为0

  4. 将梯度进行0-1的min-max归一化,然后保存为彩色图片

  5. 对三个通道的梯度绝对值相加,然后进行0-1的min-max归一化,然后保存为灰度图片

Guided Backpropagation

Vanilla Backpropagation的基础之上,对ReLU激活层的反向传播进行修正。

  1. 将反传的负的梯度设为0

这里除了彩色图和灰度图之外,也对正的和负的梯度分别展示(和彩色图的展示方法一样)。

代码如下:

    def update_relus(self):
        """
            Updates relu activation functions so that
                1- stores output in forward pass
                2- imputes zero for gradient values that are less than zero
        """
        def relu_backward_hook_function(module, grad_in, grad_out):
            """
            If there is a negative gradient, change it to zero
            """
            # Get last forward output
            corresponding_forward_output = self.forward_relu_outputs[-1]
            corresponding_forward_output[corresponding_forward_output > 0] = 1
            modified_grad_out = corresponding_forward_output * torch.clamp(grad_in[0], min=0.0)
            del self.forward_relu_outputs[-1]  # Remove last forward output
            return (modified_grad_out,)

        def relu_forward_hook_function(module, ten_in, ten_out):
            """
            Store results of forward pass
            """
            self.forward_relu_outputs.append(ten_out)

        # Loop through layers, hook up ReLUs
        for pos, module in self.model.features._modules.items():
            if isinstance(module, ReLU):
                module.register_backward_hook(relu_backward_hook_function)
                module.register_forward_hook(relu_forward_hook_function)

Gradient-weighted Class Activation Map (Grad-CAM)

Score-weighted Class Activation Map (Score-CAM)

Guided Gradient-weighted Class Activation Map (Guided-Grad-CAM)

Last updated

Was this helpful?