PaddlePaddle / InterpretDL

InterpretDL: Interpretation of Deep Learning Models,基于『飞桨』的模型可解释性算法库。
https://interpretdl.readthedocs.io
Apache License 2.0
241 stars 38 forks source link

示例GradCAM bug #73

Open jiqibuaixuexi opened 1 month ago

jiqibuaixuexi commented 1 month ago

(optional) Briefly introduce yourself.

---我按照gradcam的example代码运行如下

from PIL import Image
import paddle
import interpretdl as it
from interpretdl.data_processor.readers import read_image
# load from paddle.vision

from paddle.vision.models import resnet50
paddle_model = resnet50(pretrained=True)
gradcam = it.GradCAMInterpreter(paddle_model, device='cpu')
img_path = 'dog_and_cat.png'
x = Image.fromarray(read_image(img_path)[0])
x
layers_name = []
for n, v in paddle_model.named_sublayers():
    layers_name.append(n)
print(layers_name)

heatmap = gradcam.interpret(
        img_path,
        'layer4.2.conv3',
        label=None,
        visual=True,
        save_path=None)

随后,出现了报错

🐛 Bug

AxisError Traceback (most recent call last) Cell In[6], line 1 ----> 1 heatmap = gradcam.interpret( 2 img_path, 3 'layer4.2.conv3', 4 label=None, 5 visual=True, 6 save_path=None)

File d:\Anaconda\envs\pyside_app\lib\site-packages\interpretdl\interpreter\gradient_cam.py:101, in GradCAMInterpreter.interpret(self, inputs, target_layer_name, label, resize_to, crop_to, visual, save_path) 96 g = gradients 98 # print(f.shape, g.shape) # [bsz, channels, w, h] 99 100 # the core algorithm --> 101 cam_weights = np.mean(g, (2, 3), keepdims=True) 102 heatmap = cam_weights * f 103 heatmap = heatmap.mean(1)

File <__array_function__ internals>:200, in mean(*args, **kwargs)

File d:\Anaconda\envs\pyside_app\lib\site-packages\numpy\core\fromnumeric.py:3464, in mean(a, axis, dtype, out, keepdims, where) 3461 else: 3462 return mean(axis=axis, dtype=dtype, out=out, **kwargs) -> 3464 return _methods._mean(a, axis=axis, dtype=dtype, ... 81 # axis and full sum is more excessive than needed. 82 83 # guarded to protect circular imports

AxisError: axis 2 is out of bounds for array of dimension 0 Briefly describe the bug.

To Reproduce

Steps to reproduce the behavior:

1. 2. 3.

If you were running a command, please post the exact command that you were running.

If you have a code sample, error messages, stack traces, please provide it here as well.

(optional) Enviroment

If you know well the environment of your machine, please put the information below:

holyseven commented 1 month ago

是用的哪个示例代码?

jiqibuaixuexi commented 3 weeks ago

image

用的这个示例代码

holyseven commented 3 weeks ago

请尝试以下方法:

  1. 尝试加上这一行:
from paddle.vision.models import resnet50
paddle_model = resnet50(pretrained=True)
paddle_model.to("cpu")  # add this line
  1. 检查一下paddle是否安装正确,是否能正常跑一个图像预测。

  2. 如果还是不行,请提供一下paddle的版本号。(我这边使用 '2.5.2' 和 cpu 版本是能跑通的。)