Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M3d-Cam with 3DCNN-RNN #17

Open
nasir3843 opened this issue Sep 16, 2021 · 0 comments
Open

M3d-Cam with 3DCNN-RNN #17

nasir3843 opened this issue Sep 16, 2021 · 0 comments

Comments

@nasir3843
Copy link

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')
model_c.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant