How do I display output image in the middle of a convolutional neural network, it is (64, 30, 30) in size.
How do I display all 64, 30x30 images that are the outputs of middle convolutional layers?
How do I display output image in the middle of a convolutional neural network, it is (64, 30, 30) in size.
How do I display all 64, 30x30 images that are the outputs of middle convolutional layers?
There are two things to do: get the intermediate features, display them.
First, uou will need to use register_forward_hook on the concerned convolution.
To create it you have to write:
def hook(module, input, output):
# output is the [N, 64, 30, 30] Tensor containing your features
model.concerned_conv.register_forward_hook(hook)
Then depending on your display method, you will have. You can use torchvision and Tensorboard to do so. The hook body would be something like:
def hook(module, input, output):
grid = torchvision.utils.make_grid(output)
writer.add_image('conv_features', grid, 0)
Please find more info on using Tensorboard with PyTorch here .
when should I be removing this hook, is it ok to train neural network, and not remove this hook?
Yes you can save the returned torch.utils.hooks.RemovableHandle
:
hook = model.concerned_conv.register_forward_hook(hook)
and to remove it, simply:
hook.remove()
However, you can safely keep the hook, which is a runtime only mechanism. It won’t be saved or influence the training of your model.