CAM cam_example.py

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'.

 

posted @ 2023-10-08 09:21  emanlee  阅读(52)  评论(0编辑  收藏  举报