How to get output of layers?

I want to look into the output of the layers of the neural network. What I want to see is the output of specific layers (last and intermediate) as a function of test images.

Can you please help? I am well aware that this question already happened, but I couldn’t find an appropriate answer. I have pretrained neural network, so first of all I am not sure how it is possible with the pretrained or it has to be trained again and what is the procedure of training it in case you want to see the output?
Definition of neural network: cnn_create.py

import torch.nn as nn
import torch.nn.functional as F
import torch
class CNN(nn.Module):
    def __init__(self, num_classes):
        nn.Module.__init__(self)
        self.conv1 = nn.Sequential(
            # Input shape (3, 64, 64)
            nn.Conv2d(
                in_channels=3,
                out_channels=6,
                kernel_size=5,
                stride=1,
                padding=2
            ),
            # Output shape (6, 60, 60)
            nn.ReLU(),
            # Output shape (6, 30, 30)
            nn.MaxPool2d(kernel_size=2)
        )
        self.conv2 = nn.Sequential(
            # Input shape (6, 30, 30)
            nn.Conv2d(
                in_channels=6,
                out_channels=16,
                kernel_size=5,
                stride=1,
                padding=2
            ),
            # Output shape (16, 26, 26)
            nn.ReLU(),
            # Output shape (16, 13, 13)
            nn.MaxPool2d(kernel_size=2)
        )
        self.fc = nn.Sequential(
            nn.Linear(in_features=16 * 16 * 16,
                      out_features=300),
            nn.ReLU(),
            nn.Linear(in_features=300,
                      out_features=84),
            nn.ReLU(),
            nn.Linear(in_features=84,
                      out_features=num_classes)
        )

    def forward(self, x):
        x = self.conv1(x)
        # print("x = self.conv1(x)   ", x.shape, "  ", torch.Size)
        x = self.conv2(x)
        x = x.view(x.size()[0], -1)
        x = self.fc(x)
        return x

Training of the neural network: training.py

import time
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
from cnn_create_densenet import DenseNet3
from dataset_create import TrafficSignDataSet as tsds
import time
import os
from cnn_create import CNN

num_epochs = 5
num_classes = 43
batch_size = 50
learning_rate = 0.001

# Import dataset
train_dataset = tsds(need_train=True, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

# Create the CNN model
net = CNN(num_classes)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)

# Train the model
for epoch in range(num_epochs):
    start_time = time.time()
    print("epoch",epoch)
    for i, data in enumerate(train_loader):
        # Forward pass
        image, label = data['image'].to(device), data['label'].to(device)
        output = net(image)
        loss = criterion(output, label)
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print("time per epoch")
    print(time.time() - start_time, "seconds")
print('Train Finish.')
# Save the model checkpoint
torch.save(net.state_dict(), 'net.ckpt')
print('Net model has been saved.')

Thank you for the help.

1 Like

You could use forward hook to get the layers outputs as described here.

2 Likes

That works!

@ptrblck thank you!.