-
Hello, I am trying to use
The relevant code (using a densnet121 model) is as follows: class BackboneModel(pl.LightningModule):
# https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html
def __init__(self, conf):
super().__init__()
self.model = monai.networks.nets.densenet.densenet121(
spatial_dims=2,
in_channels=1,
out_channels=2
)
self.gradcam = monai.visualize.GradCAM(
nn_module=self.model,
target_layers="class_layers.relu"
)
def forward(self, x):
return self.model(x)
def train_step(self, batch, batch_idx, prefix):
x = batch["image"]
y = batch["target"]
logits = self(x)
return {"target": y, "logits": logits}
def validation_step(self, batch, batch_idx, prefix):
x = batch["image"]
y = batch["target"]
logits = self(x)
return {"target": y, "logits": logits}
def test_step(self, batch, batch_idx, prefix):
x = batch["image"]
y = batch["target"]
logits = self(x)
raw_image = x[0]
gradcam_img = self.gradcam(x=raw_image[None], class_idx=None) Any idea how to properly incorporate GradCAM into this setup? Note: x[0].requires_grad returns |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
I suspect that GradCAM is returning a numpy array, whereas a torch.Tensor is required for lightning. What happens if you modify this: gradcam_img = self.gradcam(x=raw_image[None], class_idx=None) to this: gradcam_img = torch.Tensor(self.gradcam(x=raw_image[None], class_idx=None)) |
Beta Was this translation helpful? Give feedback.
-
I'm not familiar with pytorch lightning but it's fine if x[0].requires_grad returns False. please ensure |
Beta Was this translation helpful? Give feedback.
-
I have created a minimal working example in a kaggle notebook using pytorch lightning's bug report model. @wyli you were right: calling GradCAM inside But does it really make sense to run a class activation map on training examples? My understanding is that this should more meaningful and possible from inside |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
I have created a minimal working example in a kaggle notebook using pytorch lightning's bug report model.
@wyli you were right: calling GradCAM inside
train_step
works. So I assume the error is related to the model state inside thetest_step
.But does it really make sense to run a class activation map on training examples? My understanding is that this should more meaningful and possible from inside
test_step
...