Visualizing each layer of the Neural Network

I would like to know how this can be performed in Pytorch.

######################### [Convolution] -> [Batch Normalization] -> [ReLU] ##################################################
def Conv_block2d(in_channels,out_channels,*args,**kwargs):
	return nn.Sequential(nn.Conv2d(in_channels,out_channels,*args,**kwargs,bias=False),nn.BatchNorm2d(out_channels,eps=0.001),nn.ReLU(inplace=True))
#############################################################################################################################

############################## Main Model ##########################################################################################
class MainBlock(nn.Module):
    """docstring for MainBlock"nn.Module"""
    def __init__(self):
        super(MainBlock,self).__init__()
        """
        size = [3,15,33,3,3]
        for in_c,out_c,k in zip(size[:2],size[1:3],size[3:]):
            print("in channel",in_c,"out channel",out_c,"kernel size",k)
        
        [Convolution] -> []
        in channel 3 out channel 15 kernel size 3
        (256x256x3) * (3x3x15) = (254x254x15))*(2x2x1) = (127x127x18)
        in channel 15 out channel 33 kernel size 3
        (127x127x15) * (3x3x33) = (127x127x33))*(2x2x1) = (62x62x33)
        """
        self.block1_size = [3,15,33,3,3]
        self.block1_out = [nn.Sequential(*Conv_block2d(in_c,out_c,kernel_size=k),nn.MaxPool2d(2,2)) for in_c,out_c,k in zip(self.block1_size[:2],self.block1_size[1:3],self.block1_size[3:])]
        self.block1_output = nn.Sequential(*self.block1_out)

     def forward(self,x):
    	
        # (62x62x33)
        x = self.block1_output(x)
        return x

After training the Neural Network

How do i detect the and store image patches that mamimizes the units activation in that specific layer as show in images below?

I didn’t get the question properly, I might be able to help if you clarify what you mean by the patch maximizing the activation, possibly elucidate the example even more.

As far as storing the patches is concerned, once you find out which patch, you can create a dummy forward to obtain output tensor from that specific layer and thats actually the image you want. ( If what you want is the best kernel which is giving the max activation, then you can simply access the model parameters to get the kernel values)

PATCH good question.

It is more complicated than what i had though.

Have to read more of this Visualizing and Understanding Convolutional Networks

In the paper, Fig 2, they show the kernel and corresponding output images.

I would have to go through the paper to understand top 9 activations as they mention.

One intuition that is trivial is that out of say 32 kernels you have, after applying the activation say ReLU, you can sum up all the output cell values (of the image passing through a kernel) and decide the max 9 activated ones.