PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.3k stars 5.62k forks source link

TypeError: Can't constructs a 'paddle.Tensor' with data type <class 'NoneType'>, data type must be scalar|list|tuple|np.ndarray|paddle.Tensor #54266

Open Junxiang-Zeng8443 opened 1 year ago

Junxiang-Zeng8443 commented 1 year ago

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

Junxiang-Zeng8443 commented 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)

Junxiang-Zeng8443 commented 1 year ago

PaddlePaddle 2.0.2 不会报错,本地PaddlePaddle 2.4.2 缺不行,这是为何?

Junxiang-Zeng8443 commented 1 year ago

PaddlePaddle 2.0.2 不会报错,本地PaddlePaddle 2.4.2 缺不行,这是为何?

Junxiang-Zeng8443 commented 1 year ago

image

conv.retain_grad()好像没发挥作用

ZhangHandi commented 1 year ago

打印grad_map看下是否有值呢?如果有值可以先转成numpy array再paddle.to_tensor(),看下是否还报错

Junxiang-Zeng8443 commented 1 year ago

打印grad_map看下是否有值呢?如果有值可以先转成numpy array再paddle.to_tensor(),看下是否还报错

没有value,问题就出在这一块。在AI studio可以Print,到本地就不行了。 image