frgfm / torch-cam

Class activation maps for your PyTorch models (CAM, Grad-CAM, Grad-CAM++, Smooth Grad-CAM++, Score-CAM, SS-CAM, IS-CAM, XGrad-CAM, Layer-CAM)
https://frgfm.github.io/torch-cam/
Apache License 2.0
1.98k stars 205 forks source link

target_layer path error #205

Closed ReIDkj closed 1 year ago

ReIDkj commented 1 year ago

Bug description

My network structure is a little complex,when run:

cam_extractor = CAM(model, 'model.backbone.shared_module_bh.model_sh_fr.layer4[-1]') 

the error occurred:

ValueError: Unable to find all submodules ['model.backbone.shared_module_bh.model_sh_fr.layer4[-1]'] in the model

but print(model.backbone.shared_module_bh.model_sh_fr.layer4[-1]) is executable and correct Is there any solution? thanks

Code snippet to reproduce the bug

cam_extractor = CAM(model, 'model.backbone.shared_module_bh.model_sh_fr.layer4[-1]')

Error traceback

Current run is terminating due to exception: Unable to find all submodules ['model.backbone.shared_module_bh.model_sh_fr.layer4[-1]'] in the model
Engine run is terminating due to exception: Unable to find all submodules ['model.backbone.shared_module_bh.model_sh_fr.layer4[-1]'] in the model
Engine run is terminating due to exception: Unable to find all submodules ['model.backbone.shared_module_bh.model_sh_fr.layer4[-1]'] in the model
Traceback (most recent call last):
  File "train.py", line 256, in <module>
    train(cfg)
  File "train.py", line 208, in train
    engine.run(train_loader, max_epochs=cfg.num_epoch)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 326, in run
    self._handle_exception(e)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 291, in _handle_exception
    raise e
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 319, in run
    self._fire_event(Events.COMPLETED)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 226, in _fire_event
    func(self, *(event_args + args), **kwargs)
  File "/home/zhang/E/RKJ/MAPnet/MPANet-dct/engine/__init__.py", line 67, in train_completed
    evaluator.run(query_loader)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 326, in run
    self._handle_exception(e)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 291, in _handle_exception
    raise e
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 313, in run
    hours, mins, secs = self._run_once_on_dataset()
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 280, in _run_once_on_dataset
    self._handle_exception(e)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 291, in _handle_exception
    raise e
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/ignite/engine/engine.py", line 272, in _run_once_on_dataset
    self.state.output = self._process_function(self, batch)
  File "/home/zhang/E/RKJ/MAPnet/MPANet-dct/engine/engine.py", line 61, in _process_func
    cam_extractor = CAM(model, 'model.backbone.shared_module_bh.model_sh_fr.layer4[-1]')#layer4[2].conv3
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/torchcam/methods/activation.py", line 61, in __init__
    super().__init__(model, target_layer, input_shape, **kwargs)
  File "/home/zhang/anaconda3/envs/RKJ/lib/python3.7/site-packages/torchcam/methods/core.py", line 64, in __init__
    raise ValueError(f"Unable to find all submodules {target_names} in the model")
ValueError: Unable to find all submodules ['model.backbone.shared_module_bh.model_sh_fr.layer4[-1]'] in the model

Environment

Collecting environment information...
TorchCAM version: 0.3.2.dev0
PyTorch version: 1.10.1

OS: Ubuntu 16.04.6 LTS

Python version: 3.7.13
Is CUDA available: Yes
CUDA runtime version: 10.0.130
GPU models and configuration: 
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti
GPU 2: GeForce RTX 2080 Ti
GPU 3: GeForce RTX 2080 Ti

Nvidia driver version: 440.36
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.4
frgfm commented 1 year ago

Hello @ReIDkj :wave:

Yes there is! Here is the explanation:

So you can either use the module itself (recommended):

cam_extractor = CAM(model, model.backbone.shared_module_bh.model_sh_fr.layer4[-1]) 

or use the string argument (below I supposed that the last item was at index 3):

cam_extractor = CAM(model, 'model.backbone.shared_module_bh.model_sh_fr.layer4.3') 

Hope this helps, let me know if some aspects are unclear :) Cheers :v:

ReIDkj commented 1 year ago

Thank you! code: cam_extractor = CAM(model, target_layer=model.backbone.shared_module_bh.model_sh_fr.layer4[-1].conv3, fc_layer=model.classifier) can work. The network structure should not add ' ' .

frgfm commented 1 year ago

Happy to help! I'll close the issue then, feel free to reopen if some details are unclear :)