Visualizations for CNN trained for timeseries classification
kalfasyan opened this issue · comments
Yannis Kalfas commented
Would it be possible to handle networks trained for timeseries classification? Example CNN below:
class Cnn1d(nn.Module):
def __init__(self, outputs=2):
super(Cnn1d, self).__init__()
self.outputs = outputs
self.conv1 = nn.Conv1d(1, 16, 3)
self.bn1 = nn.BatchNorm1d(16)
self.pool1 = nn.MaxPool1d(2)
self.conv2 = nn.Conv1d(16, 32, 3)
self.bn2 = nn.BatchNorm1d(32)
self.pool2 = nn.MaxPool1d(2)
self.conv3 = nn.Conv1d(32, 64, 3)
self.bn3 = nn.BatchNorm1d(64)
self.pool3 = nn.MaxPool1d(2)
self.conv4 = nn.Conv1d(64, 128, 3)
self.bn4 = nn.BatchNorm1d(128)
self.pool4 = nn.MaxPool1d(2)
self.dropout = nn.Dropout()
self.avgPool = nn.AvgPool1d(127)
self.fc1 = nn.Linear(256, self.outputs)
def forward(self, x):
x = self.conv1(x)
x = F.relu(self.bn1(x))
x = self.pool1(x)
x = self.conv2(x)
x = F.relu(self.bn2(x))
x = self.pool2(x)
x = self.conv3(x)
x = F.relu(self.bn3(x))
x = self.pool3(x)
x = self.conv4(x)
x = F.relu(self.bn4(x))
x = self.pool4(x)
x = self.dropout(x)
x = self.avgPool(x)
x = x.view(x.shape[0], -1)
x = self.fc1(x)
return x
Kazuto Nakashima commented
If you make grad-cam of class 1 at pool4:
def get_fmaps(module, inputs, outputs):
global fmaps
fmaps = outputs.detach()
def get_grads(module, inputs, outputs):
global grads
grads = outputs[0].detach()
model = Cnn1d().eval()
model.pool4.register_forward_hook(get_fmaps)
model.pool4.register_backward_hook(get_grads)
signals = torch.randn(1, 1, 4096) # batch, channel, length
target_class = 1 # or 0
logit = model(signals)
mask = torch.zeros_like(logit)
mask[:, target_class] = 1.0
logit.backward(gradient=mask)
weights = grads.mean(dim=2, keepdim=True)
gradcam = (fmaps * weights).sum(dim=1, keepdim=True).relu()
Yannis Kalfas commented
@kazuto1011 thanks for the response 👍
How would this work with 1 Cnn1d output unit (e.g. sigmoid where classes are mapped in the range [0,1]) instead of 2 outputs (softmax w/ 2 units)? Maybe I'm missing some theoretical detail on GradCam.
Kazuto Nakashima commented
You can get the class-wise gradients as follows.
For a positive class:
logit = model(signals) # before sigmoid
mask = torch.ones_like(logit)
logit.backward(gradient=mask)
For a negative class:
logit = model(signals)
mask = torch.ones_like(logit)
(-logit).backward(gradient=mask)