自动化使用GradCAM处理图片(用于ViT和swin的变体)附链接

GradCAM_On_ViT

用于可视化模型结果的 GradCAM 自动脚本

如何在 GradCam 中调整 XXXFormer

请确保您的模型格式正确。

如果您应用的变压器是类似 swin(无ClassToken)或类似 ViT (有ClassToken)

张量的形状可能看起来像[Batch,49,768],那么你应该按照以下步骤处理你的模型,以避免一些可怕的运行时错误


Class XXXFormer(nn.Moudle):
    def __init(self,...):
        super().__init__()
        .....
        self.avgpool = nn.AdaptiveAvgPool1d(1) #this is essential
    def forward(self,x):
        x = self.forward_feartrue(x) # Supose that the out put is [Batch,49,768]
        x = self.avgpool(x.transpose(1,2)) # [Batch,49,768] --> [Batch,768,49] --> [Batch,768,1]
        x = torch.flatten(x,1) # [Batch,768]

获取你的目标层

找到最后一个transformer block并选择 LayerNorm() 属性作为目标层,如果您有多个 LayerNorm() 属性,您可以将它们全部放在列表中或仅选择其中一个

您的目标图层可能如下所示

# choose one LayerNorm() attribute for your target layer
target_Layer1 = [vit.block[-1].norm1]
target_Layer2 = [vit.block[-1].norm2]
# or stack up them all
target_Layer3 = [vit.block[-1].norm1,vit.block.norm2]

为什么我们选择LayerNorm作为目标层?

Reference: On the Expressivity Role of LayerNorm in Transformer’s Attention (ACL 2023).

The reason may be like this as shown in the picture

在这里插入图片描述

  • Automatic_Swim_variant_CAM.py
  • Automatic_ViT_variant_CAM.py

上面显示的两个 .py 文件是您需要运行的主要 Python 脚本
只需设置图像文件并运行这两个脚本即可!

Using EigenCam as an example

在这里插入图片描述

Param you need to Pay attention

parser.add_argument('--path', default='./image', help='the path of image')
parser.add_argument('--method', default='all', help='the method of GradCam can be specific ,default all')
parser.add_argument('--aug_smooth', default=True, choices=[True, False],
                    help='Apply test time augmentation to smooth the CAM')
parser.add_argument('--use_cuda', default=True, choices=[True, False],
                    help='if use GPU to compute')
parser.add_argument(
    '--eigen_smooth',
    default=False, choices=[True, False],
    help='Reduce noise by taking the first principle componenet'
         'of cam_weights*activations')
parser.add_argument('--modelname', default="ViT-B-16", help='Any name you want')

链接:https://github.com/Mahiro2211/GradCAM_Automation

Method
CrossFormer (ICLR 2022)
Vision Transformer (ICLR 2021)

相关推荐

  1. vivim使用

    2023-12-15 02:30:02       61 阅读
  2. RUST笔记: 动态创建使用

    2023-12-15 02:30:02       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 02:30:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 02:30:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 02:30:02       82 阅读
  4. Python语言-面向对象

    2023-12-15 02:30:02       91 阅读

热门阅读

  1. Leetcode.75 颜色分类【荷兰国旗问题】

    2023-12-15 02:30:02       60 阅读
  2. 最长的斐波那契子序列的长度【动态规划解决】

    2023-12-15 02:30:02       61 阅读
  3. UDP网络编程其他相关事项

    2023-12-15 02:30:02       53 阅读
  4. Windows10下MySQL5.7.31解压版安装与卸载

    2023-12-15 02:30:02       66 阅读
  5. not exists用法

    2023-12-15 02:30:02       58 阅读
  6. vue表单输入绑定

    2023-12-15 02:30:02       58 阅读
  7. Scala学习二:访问修饰符/运算符

    2023-12-15 02:30:02       50 阅读
  8. 什么是PHPUnit?如何进行单元测试?

    2023-12-15 02:30:02       61 阅读
  9. Threejs之相机基础

    2023-12-15 02:30:02       73 阅读
  10. sql事务

    sql事务

    2023-12-15 02:30:02      56 阅读
  11. GitHub入门介绍

    2023-12-15 02:30:02       53 阅读
  12. 定时器Timer、多线程下的单例模式

    2023-12-15 02:30:02       58 阅读
  13. k8s-1.24.0版本部署

    2023-12-15 02:30:02       52 阅读