Plot feature maps (images) from intermediate layers

I am trying to plot images from intermediate layers. Suppose I want to plot only nine images. I defined a function like:

def feature_maps(features):

features=features[0:3, 0:3,:,:]
###do something###
###show or save images###

Now, I am calling this function after the required layer, like:

def forward(self, x):

        x = self.conv1(x)
        x=self.R1(x)
        ........
        ........
         
       layer1_var=self.variance(x) ##calculate variance
       feature_maps( layer1_var)

Now, the obvious problem is that it will be called repeatedly depending on no of epochs, batch size and data size. How can I modify it so that it is called only once,say only on first forward pass of first epoch?

Is there any other way to do same?

It’s a Python question, use some dirty trick

def forward(self, x):

        x = self.conv1(x)
        x=self.R1(x)
        ........
        ........
         
       layer1_var=self.variance(x) ##calculate variance
      if self.plot:
             feature_maps( layer1_var)
             self.plot=False

for epoch in range(10):
         model.plot=True
         for data in dataloader:
                  output = model(input)
1 Like

Thanks @chenyuntc :slight_smile:
Is there any pytorch way of doing this?

I’m afraid not. ~~~~~~~