Open Junxiang-Zeng8443 opened 1 year ago
执行代码为: def get_gradcam(model, data, label, class_dim=16): conv = model.model_1(data) predict = model.model_2(conv) label = paddle.reshape(label, [-1]) predict_one_hot = paddle.nn.functional.one_hot(label, class_dim) * predict score = paddle.mean(predict_one_hot) score.backward()
grad_map = conv.grad
grad = paddle.mean(paddle.to_tensor(grad_map), (2, 3), keepdim=True)
gradcam = paddle.sum(grad * conv, axis=1)
gradcam = paddle.maximum(gradcam, paddle.to_tensor(0.))
for j in range(gradcam.shape[0]):
gradcam[j] = gradcam[j] / paddle.max(gradcam[j])
return gradcam
def show_gradcam(model, data, label, class_dim=16, pic_size=224):
heat_maps = []
gradcams = get_gradcam(model, data, label)
for i in range(data.shape[0]):
img = ((data[i].numpy() + 1.) / 2. * 255.).astype('uint8').transpose([1, 2, 0])
heatmap = cv2.resize(gradcams[i].numpy() * 255., (data.shape[2], data.shape[3])).astype('uint8')
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = cv2.addWeighted(heatmap, .3, img, .7, 1.)
heat_maps = np.array(heat_maps)
heat_maps = heat_maps.reshape([-1, 8, pic_size, pic_size, 3])
heat_maps = np.concatenate(tuple(heat_maps), axis=1)
heat_maps = np.concatenate(tuple(heat_maps), axis=1)
cv2.imwrite('./output/pics/gradcam.jpg', heat_maps)
show_gradcam(model, data, label, class_dim=CLASS_DIM, pic_size=PIC_SIZE)
PaddlePaddle 2.0.2 不会报错,本地PaddlePaddle 2.4.2 缺不行,这是为何?
PaddlePaddle 2.0.2 不会报错,本地PaddlePaddle 2.4.2 缺不行,这是为何?
conv.retain_grad()好像没发挥作用
打印grad_map看下是否有值呢?如果有值可以先转成numpy array再paddle.to_tensor(),看下是否还报错
打印grad_map看下是否有值呢?如果有值可以先转成numpy array再paddle.to_tensor(),看下是否还报错
没有value,问题就出在这一块。在AI studio可以Print,到本地就不行了。
bug描述 Describe the Bug
Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\paddle_env\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 42, in
show_gradcam(model, data, label, class_dim=CLASS_DIM, pic_size=PIC_SIZE)
File "", line 25, in show_gradcam
gradcams = get_gradcam(model, data, label)
File "", line 14, in get_gradcam
grad = paddle.mean(paddle.to_tensor(grad_map), (2, 3), keepdim=True)
File "C:\ProgramData\Anaconda3\envs\paddle_env\lib\site-packages\paddle\tensor\creation.py", line 546, in to_tensor return _to_tensor_non_static(data, dtype, place, stop_gradient) File "C:\ProgramData\Anaconda3\envs\paddle_env\lib\site-packages\paddle\tensor\creation.py", line 375, in _to_tensor_non_static raise TypeError( TypeError: Can't constructs a 'paddle.Tensor' with data type <class 'NoneType'>, data type must be scalar|list|tuple|np.ndarray|paddle.Tensor
求解答如何解决该问题!!!真的非常感谢
其他补充信息 Additional Supplementary Information
No response