首页 > 其他分享 >CAM cam_example.py

CAM cam_example.py

时间:2023-10-08 09:36:33浏览次数:57  
标签:layer set cam -- py WARNING CAM root target

Good :

https://blog.csdn.net/qq_46378251/article/details/128773539

https://blog.csdn.net/m0_59286668/article/details/128768117

https://www.jianshu.com/p/87661307c5b2

https://zhuanlan.zhihu.com/p/371296750

https://blog.csdn.net/m0_59286668/article/details/128768117

 

CAM

https://github.com/jacobgil/pytorch-grad-cam

 

Chosing the Target Layer

You need to choose the target layer to compute CAM for. Some common choices are:

  • FasterRCNN: model.backbone
  • Resnet18 and 50: model.layer4[-1]
  • VGG and densenet161: model.features[-1]
  • mnasnet1_0: model.layers[-1]
  • ViT: model.blocks[-1].norm1
  • SwinT: model.layers[-1].blocks[-1].norm1

If you pass a list with several layers, the CAM will be averaged accross them. This can be useful if you're not sure what layer will perform best.

 

 

!git clone https://github.com/frgfm/torch-cam.git
!pip install -e torch-cam/.

 

pip install torchcam

 

os.chdir("/data/pincer/other/Train_Custom_Dataset-main/")
!python torch-cam/scripts/cam_example.py --help

usage: cam_example.py [-h] [--arch ARCH] [--img IMG] [--class-idx CLASS_IDX]
                      [--device DEVICE] [--savefig SAVEFIG] [--method METHOD]
                      [--target TARGET] [--alpha ALPHA] [--rows ROWS]
                      [--noblock]

Saliency Map comparison

optional arguments:
  -h, --help            show this help message and exit
  --arch ARCH           Name of the architecture (default: resnet18)
  --img IMG             The image to extract CAM from (default:
                        https://www.woopets.fr/assets/races/000/066/big-
                        portrait/border-collie.jpg)
  --class-idx CLASS_IDX
                        Index of the class to inspect (default: 232)
  --device DEVICE       Default device to perform computation on (default:
                        None)
  --savefig SAVEFIG     Path to save figure (default: None)
  --method METHOD       CAM method to use (default: None)
  --target TARGET       the target layer (default: None)
  --alpha ALPHA         Transparency of the heatmap (default: 0.5)
  --rows ROWS           Number of rows for the layout (default: 1)
  --noblock             Disables blocking visualization (default: False)

 

 

# 类别-边牧犬
!python torch-cam/scripts/cam_example.py \
        --img test_img/border-collie.jpg \
        --savefig output/B1_border_collie.jpg \
        --arch resnet18 \
        --class-idx 232 \
        --rows 2

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:209: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  f"The parameter '{pretrained_param}' is deprecated since 0.13 and may be removed in the future, "
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /root/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
100%|██████████████████████████████████████| 44.7M/44.7M [00:02<00:00, 15.6MB/s]
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `fc_layer`, thus set to 'fc'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
Figure(600x400)

 

# 类别-虎斑猫
!python torch-cam/scripts/cam_example.py \
        --img test_img/cat_dog.jpg \
        --savefig output/B2_cat_dog.jpg \
        --arch resnet18 \
        --class-idx 282 \
        --rows 2

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:209: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  f"The parameter '{pretrained_param}' is deprecated since 0.13 and may be removed in the future, "
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `fc_layer`, thus set to 'fc'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.

 

# 类别-边牧犬
!python torch-cam/scripts/cam_example.py \
        --img test_img/cat_dog.jpg \
        --savefig output/B3_cat_dog.jpg \
        --arch resnet18 \
        --class-idx 232 \
        --rows 2

 

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:209: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  f"The parameter '{pretrained_param}' is deprecated since 0.13 and may be removed in the future, "
/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `fc_layer`, thus set to 'fc'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.

 

标签:layer,set,cam,--,py,WARNING,CAM,root,target
From: https://www.cnblogs.com/emanlee/p/17112542.html

相关文章

  • Python基础环境安装
    环境安装记录,便以后直接一套执行一、准备1.Python下载地址:https://www.python.org/downloads/windows/2.卸载①控制面板寻找Python直接卸载;②查看环境变量path中python的变量,找到对应文件夹,将文件夹删除,后在删除环境变量中有关python的变量;③Win+R运行"regedit"打开注......
  • 深挖 Python 元组 pt.1
    哈喽大家好,我是咸鱼好久不见甚是想念,2023年最后一次法定节假日已经结束了,不知道各位小伙伴是不是跟咸鱼一样今天就开始“搬砖”了呢?我们知道元组(tuple)是Python的内置数据类型,tuple是一个不可变的值序列tuple的元素可以是任何类型,一般用在存储异构数据(例如数据库记录)的场景......
  • 配置Python国内pip源
    使用按键win+e打开文件管理器,在上方路径栏输入:%APPDATA%查看此目录下是否有pip目录,如果没有则需要创建,并在pip目录下以文本方式添加pip.ini文件。写入内容为[global]index-url=https://pypi.tuna.tsinghua.edu.cn/simple也可以更改index-url的内容为其他pip源。保存退......
  • 【进阶16】Python多线程实战案例
    一、Python实现多线程的几种方式_thread:模块提供了基本的线程和互斥锁支持;更底层的的线程管理实现模块threading:threading模块则通过封装_thread,提供了更加全面的线程使用方法。_thread案例:#*coding:utf-8*#用_thread启动多个线程完成任务import_threadimportthread......
  • python Excel添加Excel附件
    #"""#插入附件importwin32com.clientif__name__=="__main__":filename=r"23年09月.xlsx"xlApp=win32com.client.Dispatch('Excel.Application')xlApp.Visible=0#0不可见,1可见workbook=xlApp.Wo......
  • 用Python画函数的曲线
    #coding:utf8importmatplotlib.pyplotaspltimportnumpyasnp#先获取一个图表fig=plt.figure()stringabc="test"#设置x,y坐标轴的刻度显示范围plt.xlim(-7,7)plt.ylim(-3,7)#抛物线X1=np.linspace(-5,5,50)#-5~5之间生成50个点Y1=0.25*X1**2plt......
  • Pycharm连接远程服务器并实现远程调试
    Pycharm连接远程服务器并实现远程调试Pycharm连接远程服务器并实现远程调试连接远程服务器同步代码配置远程解释器进行调试连接远程服务器1、点击Tools(工具),点击部署Deployment(部署),点击Configuration(配置)2、新增一个SFTP协议的链接3、给链接命名4、配置服务器信息......
  • Python 元组完全指南2
    更新元组更改元组的值元组是不可更改的,但有一种变通方法。您可以将元组转换为列表,更改列表,然后将列表转换回元组。示例:x=("apple","banana","cherry")y=list(x)y[1]="kiwi"x=tuple(y)print(x)添加项由于元组是不可变的,没有内置的append()方法,但可以使用其他......
  • python · ssh · SQL | python 连接远程 SQL 数据库
    python连接本地SQL的教程存档。如果要连接远程的SQL数据库,需要先开一个ssh连接,在ssh连接里写pymysql的connect代码。代码如下:'''pipinstallpymysqlpipinstallsshtunnel'''importpymysqlimportpandasaspdfromsshtunnelimportSSHTunnelForward......
  • 在Python中,元类是什么?
    内容来自DOChttps://q.houxu6.top/?s=在Python中,元类是什么?什么是元类(metaclasses)?它们用于什么目的?元类是Python中的一种高级概念,它们是创建类的类。在Python中,类也是对象,因此它们也可以被看作是对象的模板。元类允许您控制类的创建过程,例如修改类的属性或方法,或者添加新的......