Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

示例GradCAM bug #73

Open
jiqibuaixuexi opened this issue Oct 8, 2024 · 3 comments
Open

示例GradCAM bug #73

jiqibuaixuexi opened this issue Oct 8, 2024 · 3 comments

Comments

@jiqibuaixuexi
Copy link

(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:

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:

  • PaddlePaddle version (eg. 2.1):
  • CPU:
  • GPU: including CUDA/CUDNN version
  • OS Platform (eg. Mac OS 10.14):
  • Graphics card storage:
  • Other imformation:
@holyseven
Copy link
Collaborator

是用的哪个示例代码?

@jiqibuaixuexi
Copy link
Author

image

用的这个示例代码

@holyseven
Copy link
Collaborator

请尝试以下方法:

  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 版本是能跑通的。)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants