MECLabTUDA / M3d-Cam

MIT License
306 stars 40 forks source link

M3d-Cam with 3DCNN-RNN #17

Open nasir3843 opened 2 years ago

nasir3843 commented 2 years ago

Hi, My designed network is 3D CNN followed by RNN.

I am trying to use M3D-Cam to get attention maps from the last convolution layer of the proposed network however I get the following error. Kindly have a look and guide me how can I achieve it

model = 3DCNN_RNN().to(device) weights_pth = 'trained_weights' model.load_state_dict(torch.load(weights_pth)) save_pth = 'model_att_maps/'

model_c = medcam.inject(model, backend='gcam', replace = True, label = 1 ,layer = 'conv4') modelc.eval() for (img1, , label) in train_dataloader:
img_vol = img1.unsqueeze(1)
img_cuda = img_vol.float().to(device) output = model_c(img_cuda)
break

I get the following error

<< ======================================================>>

RuntimeError Traceback (most recent call last)

in 54 img_vol = img1.unsqueeze(1) 55 img_cuda = img_vol.float().to(device) ---> 56 output = model_c(img_cuda) 57 58 break ~/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 725 result = self._slow_forward(*input, **kwargs) 726 else: --> 727 result = self.forward(*input, **kwargs) 728 for hook in itertools.chain( 729 _global_forward_hooks.values(), ~/miniconda3/lib/python3.8/site-packages/medcam/medcam_inject.py in forward(self, batch, label, mask, raw_input) 201 if self.medcam_dict['enabled']: 202 with torch.enable_grad(): --> 203 self.test_run(batch, internal=True) 204 if self.medcam_dict['layer'] == 'full' and not self.medcam_dict['tested']: 205 raise ValueError("Layer mode 'full' requires a test run either during injection or by calling test_run() afterwards") ~/miniconda3/lib/python3.8/site-packages/medcam/medcam_inject.py in test_run(self, batch, internal) 230 if batch is not None and not self.medcam_dict['tested']: 231 with torch.enable_grad(): --> 232 _ = self.medcam_dict['model_backend'].generate_attention_map(batch, None) 233 registered_hooks = self.medcam_dict['model_backend'].get_registered_hooks() 234 self.medcam_dict['tested'] = True ~/miniconda3/lib/python3.8/site-packages/medcam/backends/base.py in generate_attention_map(self, batch, label) 20 """Handles the generation of the attention map from start to finish.""" 21 output = self.forward(batch) ---> 22 self.backward(label=label) 23 attention_map = self.generate() 24 return output, attention_map, self.output_batch_size, self.output_channels, self.output_shape ~/miniconda3/lib/python3.8/site-packages/medcam/backends/base.py in backward(self, label) 40 41 self.mask = self._isolate_class(self.logits, label) ---> 42 self.logits.backward(gradient=self.mask, retain_graph=self.retain_graph) 43 self.remove_hook(forward=True, backward=True) 44 ~/miniconda3/lib/python3.8/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph) 219 retain_graph=retain_graph, 220 create_graph=create_graph) --> 221 torch.autograd.backward(self, gradient, retain_graph, create_graph) 222 223 def register_hook(self, hook): ~/miniconda3/lib/python3.8/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables) 128 retain_graph = create_graph 129 --> 130 Variable._execution_engine.run_backward( 131 tensors, grad_tensors_, retain_graph, create_graph, 132 allow_unreachable=True) # allow_unreachable flag RuntimeError: cudnn RNN backward can only be called in training mode