如何在PyTorch上使用GradCAM進行神經網路分類依據視覺化?

Yanwei Liu
4 min readJul 6, 2021

使用pytorch-grad-cam

圖片數量較多張時請耐心等待,運算會較花時間

GitHub Repo

requirements.txt

dataclasses==0.8
dicom-factory==0.0.3
numpy==1.19.5
Pillow==8.1.1
torch==1.7.1
torchvision==0.8.2
typing-extensions==3.7.4.3
ttach
tqdm

安裝

# 正常安裝
pip install grad-cam
# aiForge
pip uninstall -y enum34
pip install grad-cam
pip install ttach

支援的 GradCAM方法

效果

選擇 Target Layer

選擇用來計算CAM的layer

Resnet18 and 50: target_layer = model.layer4[-1]VGG and densenet161: target_layer = model.features[-1]mnasnet1_0: target_layer = model.layers[-1]ViT: target_layer = model.blocks[-1].norm1SwinT: target_layer = model.layers[-1].blocks[-1].norm1# 如果是Custom的網路架構,就必須另外寫。
# 例如
Transfer-Learning-Library的神經網路架構是自定義的,就要寫成:target_layer = model.backbone.layer4[-1]

遇到的BUG

實作上遇到了這個錯誤:AttributeError: ‘tuple’ object has no attribute ‘cpu’,經查詢後,直接修改base_cam.py的第65行

$ nano /opt/conda/lib/python3.6/site-packages/pytorch_grad_cam/base_cam.py#修改前的
if target_category is None:
target_category = np.argmax(output.cpu().data.numpy(), axis=-1)
#修改後的
if target_category is None:
output = output[0]
target_category = np.argmax(output.cpu().data.numpy(), axis=-1)

解決方案參考資料:https://blog.csdn.net/qq_37278761/article/details/117791533

官方範例 (使用多張照片、ResNet50預訓練模型)

https://github.com/jacobgil/pytorch-grad-cam/blob/6f0f161928163ebdcc7b025ac270d46af8a3de9e/batch_test.py

使用自定義的模型進行GradCAM視覺化,以Transfer-Learning-Library為例

Issues如何使用自己的Model?

1. Load your own model2. Define target_layer =, one of the layers in the model.

--

--