z1069614715 / pytorch-classifier

image classifier implement in pytoch.
MIT License
102 stars 17 forks source link

如何在pytorch_grad_cam库实现热力图可视化那部分切换device为CPU #3

Closed YiDanCheung closed 1 year ago

YiDanCheung commented 1 year ago

魔先生你好,pytorch_grad_cam库实现热力图可视化那部分python predict.py --source dataset/test/00 --save_path runs/ghostnet_flower --cam_visual --cam_type GradCAMPlusPlus,报异常

Traceback (most recent call last):
  File "predict.py", line 67, in <module>
    opt, DEVICE, model, test_transform, label = parse_opt()
  File "predict.py", line 42, in parse_opt
    raise Exception('cam visual only supported FP32.')
Exception: cam visual only supported FP32.

predict.py代码要求device必须是CPU,

if (opt.device != 'cpu') and opt.cam_visual:
        raise Exception('cam visual only supported FP32.') 

请问怎么在您的代码里改啊? :face_with_spiral_eyes: (我是云GPU环境)

z1069614715 commented 1 year ago

你这个应该是开启了fp16吧 不要启用half 然后device参数填写cpu

YiDanCheung commented 1 year ago

谢谢魔老师:shipit:

YiDanCheung commented 1 year ago

我使用python predict.py --device cpu --source dataset/test/00 --save_path runs/ghostnet_flower --cam_visual --cam_type GradCAMPlusPlus命令,得到以下报错信息:

Image-Classifier Python-3.8.10 Torch-1.12.0+cu113 CPU model fuse... 301 layers to 209 layers found checkpoint from runs/ghostnet_flower, model type:ghostnet +--------------------------------------------------------------------------------+ | Best Metrice | +------------+-----------+-----------+----------+----------------+---------------+ | train_loss | test_loss | train_acc | test_acc | train_mean_acc | test_mean_acc | +------------+-----------+-----------+----------+----------------+---------------+ | 0.58322 | 0.52268 | 0.98062 | 1.00000 | 0.98051 | 1.00000 | +------------+-----------+-----------+----------+----------------+---------------+ Traceback (most recent call last): File "predict.py", line 70, in <module> cam_model = cam_visual(model, test_transform, DEVICE, opt) File "/root/pytorch-classifier/utils/utils.py", line 664, in __init__ self.cam_model = eval(opt.cam_type)(model=model, target_layers=model.cam_layer(), use_cuda=(DEVICE.type != 'cpu')) File "/root/miniconda3/lib/python3.8/site-packages/pytorch_grad_cam/grad_cam_plusplus.py", line 10, in __init__ super(GradCAMPlusPlus, self).__init__(model, target_layers, use_cuda, File "/root/miniconda3/lib/python3.8/site-packages/pytorch_grad_cam/base_cam.py", line 27, in __init__ self.activations_and_grads = ActivationsAndGradients( File "/root/miniconda3/lib/python3.8/site-packages/pytorch_grad_cam/activations_and_gradients.py", line 11, in __init__ for target_layer in target_layers: TypeError: 'GhostBottleneck' object is not iterable Exception ignored in: <function BaseCAM.__del__ at 0x7fe36da68160> Traceback (most recent call last): File "/root/miniconda3/lib/python3.8/site-packages/pytorch_grad_cam/base_cam.py", line 192, in __del__ self.activations_and_grads.release() AttributeError: 'GradCAMPlusPlus' object has no attribute 'activations_and_grads'

很抱歉再次打扰您,但是还是很希望得到您的解答

YiDanCheung commented 1 year ago

根据该issue:pytorch-grad-cam issues,应该将‘要分析的层’变为列表,即,把utils.py的664行变为

self.cam_model = eval(opt.cam_type)(model=model, target_layers=[model.cam_layer()], use_cuda=(DEVICE.type != 'cpu'))

:sunglasses: