Convnet layer visualization


#1

Dear all,

any ideas on how to visualize the intermediate layers of a convolutional network? For example, say I have the following custom “network”


class MyNet(HybridBlock):
    def __init__(self,**kwards):
        HybridBlock.__init__(self,**kwards)

        with self.name_scope():
            self.conv1 = gluon.nn.Conv2D(channels=16,kernel_size=3)
            self.conv2 = gluon.nn.Conv2D(channels=32,kernel_size=3)
            self.conv3 = gluon.nn.Conv2D(channels=32,kernel_size=3)


    def hybrid_forward(self,F,_input):
        x = self.conv1(_input)
        x = self.conv2(x)
        x = self.conv3(x)

        return x

and assume I’ve trained it with a loss function etc, so now the network is initialized and trained (the weights+bias are determined). Is there a way to visualize the self.conv2 or self.conv3 layers directly (by providing a batch of input images/data)? Or do I need to define another function that takes these layers and repeats the process of hybrid_forward up to the layer I wish to visualize?

Thanks


#2

A solution I’ve found (for anyone who is interested) is returning a list of objects in the end of the hybrid_forward function:

class MyNet(HybridBlock):
    def __init__(self,**kwards):
        HybridBlock.__init__(self,**kwards)

        with self.name_scope():
            self.conv1 = gluon.nn.Conv2D(channels=16,kernel_size=3)
            self.conv2 = gluon.nn.Conv2D(channels=32,kernel_size=3)
            self.conv3 = gluon.nn.Conv2D(channels=32,kernel_size=3)


    def hybrid_forward(self,F,_input):
        x1 = self.conv1(_input)
        x2 = self.conv2(x1)
        x3 = self.conv3(x2)

        return x1, x2, x3

then after a single forward call, I get all layers (of interest).