utkuozbulak / pytorch-cnn-visualizations

Pytorch implementation of convolutional neural network visualization techniques
MIT License
7.81k stars 1.49k forks source link

Visualizations for CNN trained on timeseries classification #109

Closed kalfasyan closed 2 years ago

kalfasyan commented 2 years ago

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
utkuozbulak commented 2 years ago

I haven't worked with time series but worked with some radar data that had a temporal dimension. Some techniques (the ones that propagate all the way back to input: integrated grads, guided backprop) are easily applicable while others (gradcam) are a bit tricky to get it right. You have to play around with the implementation to tune it to your model.