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?